Blame


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