summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'file.c')
-rw-r--r--file.c41
1 files changed, 12 insertions, 29 deletions
diff --git a/file.c b/file.c
index fba9682..be044ca 100644
--- a/file.c
+++ b/file.c
@@ -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;
}