summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authorVincent Douillet <vincent@vdouillet.fr>2024-09-05 12:58:22 +0200
committerVincent Douillet <vincent@vdouillet.fr>2024-09-06 12:22:20 +0200
commit35a145fd161fb4d4f19aa9820b50438695b94414 (patch)
tree00e9f341d5b7e0a6874efbe9fafc0500cf13cd2d /file.c
parent9b8d38ab209c6e7e747d6d420f1b105fe858c7eb (diff)
browse: add upload button
Diffstat (limited to 'file.c')
-rw-r--r--file.c74
1 files changed, 56 insertions, 18 deletions
diff --git a/file.c b/file.c
index 1a53b89..239f548 100644
--- a/file.c
+++ b/file.c
@@ -37,6 +37,7 @@
#include "config.h"
#include "file.h"
#include "mime.h"
+#include "str.h"
#include "url.h"
#include "util.h"
@@ -50,10 +51,6 @@ file_free(struct file * file)
free(file->path);
file->path = NULL;
}
- if (file->name != NULL) {
- free(file->name);
- file->name = NULL;
- }
if (file->action_url != NULL) {
free(file->action_url);
file->action_url = NULL;
@@ -61,7 +58,7 @@ file_free(struct file * file)
free(file);
}
-static bool
+static bool
fill_metadata(struct file * f)
{
char *ext, *data_dir;
@@ -77,7 +74,7 @@ fill_metadata(struct file * f)
f->mime = MIME_BIN;
/* check if it is a directory */
- path_len = url_build(path, PATH_MAX, data_dir, f->path, f->name, NULL);
+ path_len = url_build(path, PATH_MAX, data_dir, f->path, NULL);
if (path_len == 0 || path_len >= PATH_MAX)
return false;
@@ -88,8 +85,8 @@ fill_metadata(struct file * f)
f->is_dir = S_ISDIR(sb.st_mode);
/* find mime from file extension */
- ext = strrchr(f->name, '.');
- if (!ext || ext == f->name) {
+ ext = strrchr(f->path, '.');
+ if (!ext || ext == f->path) {
return true;
}
ext++;
@@ -99,27 +96,68 @@ fill_metadata(struct file * f)
}
struct file *
-file_new(char *dir, size_t dir_len, char *name, size_t name_len)
+file_new(const char *path)
{
struct file *file;
+ if (path == NULL)
+ return NULL;
+
file = (struct file *) calloc(1, sizeof(struct file));
if (file == NULL)
return NULL;
- file->path = strndup(dir, dir_len);
- if (file->path == NULL) {
- file_free(file);
- return NULL;
- }
- file->name = strndup(name, name_len);
- if (file->name == NULL) {
- file_free(file);
- return NULL;
+ /* empty path is considered as root */
+ if (strlen(path) == 0) {
+ file->path = malloc(sizeof(char) * 2);
+ if (file->path == NULL) {
+ file_free(file);
+ return NULL;
+ }
+ strlcpy(file->path, "/", 2);
+ } else {
+ file->path = strdup(path);
+ if (file->path == NULL) {
+ file_free(file);
+ return NULL;
+ }
}
+
if (!fill_metadata(file)) {
file_free(file);
return NULL;
}
return file;
}
+
+char *
+file_get_basename(const struct file * f)
+{
+ if (f == NULL || f->path == NULL)
+ return NULL;
+
+ /* is the path a basename itself? */
+ if (strrchr(f->path, '/') == NULL)
+ return f->path;
+
+ /* split for the basename */
+ return str_split(f->path, '/');
+}
+
+size_t
+file_get_data_path(const struct file * f, char *data_path, size_t data_path_len)
+{
+ char *data_dir;
+ size_t path_len;
+
+ data_dir = config_data_dir();
+ if (data_dir == NULL)
+ return 0;
+
+ /* check if it is a directory */
+ path_len = url_build(data_path, data_path_len, data_dir, f->path, NULL);
+ if (path_len == 0 || path_len >= PATH_MAX)
+ return 0;
+
+ return path_len;
+}