diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2024-01-23 09:22:00 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2024-02-08 09:27:54 +0100 |
commit | 42f538cc66546997a166dbe67e489c9afabbb908 (patch) | |
tree | 718bb6b7161892c4b964499c1818c6d7bf0bd6a6 /file.c | |
parent | 760031b4a74d70bdeaab4ce9aedfe772acd05bd3 (diff) |
download files
Diffstat (limited to 'file.c')
-rw-r--r-- | file.c | 41 |
1 files changed, 12 insertions, 29 deletions
@@ -36,6 +36,7 @@ #include "config.h" #include "file.h" +#include "mime.h" #include "url.h" #include "util.h" @@ -61,29 +62,17 @@ file_free(struct file * file) } static bool -ext_cmp(char *ext, char **ext_list) -{ - int i; - - for (i = 0; ext_list[i] != NULL; i++) { - if (strcmp(ext, ext_list[i]) == 0) - return true; - } - - return false; -} - -static bool -find_type(struct file * f) +fill_metadata(struct file * f) { char *ext; - char *video_ext[] = {"mp4", "mkv", "avi", NULL}; - char *audio_ext[] = {"mp3", "flac", "aac", NULL}; - char *text_ext[] = {"txt", "doc", "docx", "odt", NULL}; char path[PATH_MAX]; struct stat sb; size_t path_len; + /* default mime */ + f->mime = MIME__MAX; + + /* check if it is a directory */ path_len = url_build(path, PATH_MAX, DATA_DIR, f->path, f->name, NULL); if (path_len == 0 || path_len >= PATH_MAX) return false; @@ -91,22 +80,16 @@ find_type(struct file * f) if (stat(path, &sb) != 0) return false; + f->size = sb.st_size; f->is_dir = S_ISDIR(sb.st_mode); + /* find mime from file extension */ ext = strrchr(f->name, '.'); if (!ext || ext == f->name) { - f->type = OTHER; return true; } ext++; - if (ext_cmp(ext, audio_ext)) - f->type = AUDIO; - else if (ext_cmp(ext, video_ext)) - f->type = VIDEO; - else if (ext_cmp(ext, text_ext)) - f->type = TEXT; - else - f->type = OTHER; + f->mime = mime_from_ext(ext); return true; } @@ -120,17 +103,17 @@ file_new(char *dir, size_t dir_len, char *name, size_t name_len) if (file == NULL) return NULL; - file->path = v_strcpy(dir, dir_len); + file->path = strndup(dir, dir_len); if (file->path == NULL) { file_free(file); return NULL; } - file->name = v_strcpy(name, name_len); + file->name = strndup(name, name_len); if (file->name == NULL) { file_free(file); return NULL; } - if (!find_type(file)) { + if (!fill_metadata(file)) { file_free(file); return NULL; } |