diff options
| author | William Wennerström <william@wstrm.dev> | 2020-12-04 16:13:13 +0100 |
|---|---|---|
| committer | William Wennerström <william@wstrm.dev> | 2020-12-04 16:13:13 +0100 |
| commit | 1d2c0a8836123c3b484826b974f87a0061bc110b (patch) | |
| tree | 21e926e71bd4ed7391f4d2d512a69802bf2ad17c /src/command | |
| parent | 3a6597ee2967f91f49a1b4e17cf0595f37064587 (diff) | |
| download | profani-tty-1d2c0a8836123c3b484826b974f87a0061bc110b.tar.gz | |
Move unique_filename_from_url functions to common
Diffstat (limited to 'src/command')
| -rw-r--r-- | src/command/cmd_funcs.c | 65 |
1 files changed, 2 insertions, 63 deletions
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 88461f1d..147eca4d 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9110,67 +9110,6 @@ _url_external_method(const char* cmd_template, const char* url, const char* file g_strfreev(argv); } -char* -_unique_filename(const char* filename) -{ - char* unique = strdup(filename); - - unsigned int i = 0; - while (g_file_test(unique, G_FILE_TEST_EXISTS)) { - free(unique); - - if (i > 1000) { // Give up after 1000 attempts. - return NULL; - } - - if (asprintf(&unique, "%s.%u", filename, i) < 0) { - return NULL; - } - - i++; - } - - return unique; -} - -char* -_unique_filename_from_url(char* url, char* path) -{ - gchar* directory = NULL; - gchar* basename = NULL; - if (path != NULL) { - directory = g_path_get_dirname(path); - basename = g_path_get_basename(path); - } - - if (directory == NULL) { - // Explicitly use "./" as directory if no directory has been passed. - directory = "./"; - } - - if (!g_file_test(directory, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) { - cons_show_error("Directory '%s' does not exist or is not a directory.", directory); - return NULL; - } - - if (!basename) { - basename = http_basename_from_url(url); - } - - char* filename = NULL; - filename = g_build_filename(directory, basename, NULL); - - char* unique_filename = _unique_filename(filename); - if (!unique_filename) { - cons_show_error("Failed to generate an unique filename from '%s'.", filename); - free(filename); - return NULL; - } - - free(filename); - return unique_filename; -} - gboolean cmd_url_open(ProfWin* window, const char* const command, gchar** args) { @@ -9200,7 +9139,7 @@ cmd_url_open(ProfWin* window, const char* const command, gchar** args) #ifdef HAVE_OMEMO // OMEMO URLs (aesgcm://) must be saved and decrypted before being opened. if (0 == g_strcmp0(scheme, "aesgcm")) { - char* filename = _unique_filename_from_url(url, files_get_data_path(DIR_DOWNLOADS)); + char* filename = unique_filename_from_url(url, files_get_data_path(DIR_DOWNLOADS)); _url_aesgcm_method(window, cmd_template, url, filename); free(filename); @@ -9241,7 +9180,7 @@ cmd_url_save(ProfWin* window, const char* const command, gchar** args) return TRUE; } - char* filename = _unique_filename_from_url(url, path); + char* filename = unique_filename_from_url(url, path); char* cmd_template = prefs_get_string_with_option(PREF_URL_SAVE_CMD, scheme); if (cmd_template == NULL) { |