mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-24 06:06:21 +00:00
Move unique_filename_from_url functions to common
This commit is contained in:
@@ -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) {
|
||||
|
||||
80
src/common.c
80
src/common.c
@@ -33,6 +33,9 @@
|
||||
* source files in the program, then also delete it here.
|
||||
*
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE 1
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
@@ -575,3 +578,80 @@ format_call_external_argv(const char* template, const char* url, const char* fil
|
||||
|
||||
return argv;
|
||||
}
|
||||
|
||||
gchar*
|
||||
_unique_filename(const char* filename)
|
||||
{
|
||||
gchar* unique = g_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;
|
||||
}
|
||||
|
||||
gchar*
|
||||
_basename_from_url(const char* url)
|
||||
{
|
||||
const char* default_name = "index.html";
|
||||
|
||||
GFile* file = g_file_new_for_uri(url);
|
||||
gchar* filename = g_file_get_basename(file);
|
||||
g_object_unref(file);
|
||||
|
||||
if (g_strcmp0(filename, ".") == 0
|
||||
|| g_strcmp0(filename, "..") == 0
|
||||
|| g_strcmp0(filename, G_DIR_SEPARATOR_S) == 0) {
|
||||
g_free(filename);
|
||||
return strdup(default_name);
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
gchar*
|
||||
unique_filename_from_url(const char* url, const 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) {
|
||||
// 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)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
|
||||
basename = _basename_from_url(url);
|
||||
}
|
||||
|
||||
gchar* filename = g_build_filename(directory, basename, NULL);
|
||||
|
||||
gchar* unique_filename = _unique_filename(filename);
|
||||
if (!unique_filename) {
|
||||
g_free(filename);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
g_free(filename);
|
||||
return unique_filename;
|
||||
}
|
||||
|
||||
@@ -107,4 +107,6 @@ char* get_random_string(int length);
|
||||
gboolean call_external(gchar** argv, gchar*** const output_ptr, gchar*** const error_ptr);
|
||||
gchar** format_call_external_argv(const char* template, const char* url, const char* filename);
|
||||
|
||||
gchar* unique_filename_from_url(const char* url, const char* path);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1865,7 +1865,6 @@ _get_group(preference_t pref)
|
||||
return PREF_GROUP_LOGGING;
|
||||
case PREF_AVATAR_CMD:
|
||||
case PREF_URL_OPEN_CMD:
|
||||
return PREF_GROUP_EXECUTABLES;
|
||||
case PREF_URL_SAVE_CMD:
|
||||
return PREF_GROUP_EXECUTABLES;
|
||||
case PREF_AUTOAWAY_CHECK:
|
||||
|
||||
@@ -44,25 +44,6 @@
|
||||
|
||||
#define FALLBACK_MSG ""
|
||||
|
||||
char*
|
||||
http_basename_from_url(const char* url)
|
||||
{
|
||||
const char* default_name = "index.html";
|
||||
|
||||
GFile* file = g_file_new_for_uri(url);
|
||||
char* filename = g_file_get_basename(file);
|
||||
g_object_unref(file);
|
||||
|
||||
if (g_strcmp0(filename, ".") == 0
|
||||
|| g_strcmp0(filename, "..") == 0
|
||||
|| g_strcmp0(filename, G_DIR_SEPARATOR_S) == 0) {
|
||||
g_free(filename);
|
||||
return strdup(default_name);
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
void
|
||||
http_print_transfer_update(ProfWin* window, char* url, const char* fmt, ...)
|
||||
{
|
||||
|
||||
@@ -38,9 +38,7 @@
|
||||
|
||||
#include "ui/window.h"
|
||||
|
||||
char* http_basename_from_url(const char* url);
|
||||
void http_print_transfer(ProfWin* window, char* url, const char* fmt, ...);
|
||||
void http_print_transfer_update(ProfWin* window, char* url, const char* fmt, ...);
|
||||
gchar** http_format_external_argv(const char* cmd, const char* url, const char* filename);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user