Blob


1 /*
2 * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <stddef.h>
18 #include <string.h>
20 #include "log.h"
21 #include "twind.h"
23 struct mimetype {
24 const char *ext;
25 const char *type;
26 };
28 static const struct mimetype mime_collection[] = {
29 { "gmi", "text/gemini" },
30 { "gemini", "text/gemini" },
31 { "jpeg", "image/jpeg" },
32 { "jpg", "image/jpeg" },
33 { "html", "text/html" },
34 { "m4a", "audio/x-m4a" },
35 { "md", "text/markdown" },
36 { "mov", "video/quicktime" },
37 { "mp3", "audio/mpeg" },
38 { "mp4", "video/mp4" },
39 { "mpeg", "video/mpeg" },
40 { "mpg", "video/mpeg" },
41 { "ogg", "audio/ogg" },
42 { "pdf", "application/pdf" },
43 { "png", "image/png" },
44 { "svg", "image/svg+xml" },
45 { "txt", "text/plain" },
46 { "wmv", "video/x-ms-wmv" }
47 };
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 char *
54 get_file_extension(const char *path)
55 {
56 char *p, *ext;
58 if (strlen(path) == 0)
59 return NULL;
61 if ((p = strrchr(path, '.')) == NULL)
62 return NULL;
64 p += 1;
65 ext = xstrdup(p);
67 return ext;
68 }
70 char *
71 get_mime_type(const char *ext)
72 {
73 char *mime = NULL;
74 size_t len;
75 long unsigned int i;
77 if ((len = strlen(ext)) == 0)
78 return NULL;
80 for (i=0; i < nitems(mime_collection); i++)
81 if (strcasecmp(ext, mime_collection[i].ext) == 0)
82 mime = xstrdup(mime_collection[i].type);
84 return mime;
85 }