snac2/xs_mime.h

81 lines
1.8 KiB
C
Raw Permalink Normal View History

2023-07-28 09:34:18 +00:00
/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
2022-09-25 19:02:47 +00:00
#ifndef _XS_MIME
#define _XS_MIME
2023-08-14 16:02:20 +00:00
const char *xs_mime_by_ext(const char *file);
2022-09-25 19:02:47 +00:00
extern const char *xs_mime_types[];
2022-09-25 19:02:47 +00:00
#ifdef XS_IMPLEMENTATION
/* intentionally brain-dead simple */
/* CAUTION: sorted */
const char *xs_mime_types[] = {
"3gp", "video/3gpp",
"aac", "audio/aac",
"css", "text/css",
"flac", "audio/flac",
"flv", "video/flv",
"gif", "image/gif",
"gmi", "text/gemini",
"html", "text/html",
"jpeg", "image/jpeg",
"jpg", "image/jpeg",
"json", "application/json",
"m4a", "audio/aac",
"m4v", "video/mp4",
"md", "text/markdown",
"mov", "video/quicktime",
"mp3", "audio/mp3",
"mp4", "video/mp4",
"mpg4", "video/mp4",
"oga", "audio/ogg",
"ogg", "audio/ogg",
"ogv", "video/ogg",
"opus", "audio/ogg",
"png", "image/png",
2023-09-15 17:51:04 +00:00
"svg", "image/svg+xml",
"svgz", "image/svg+xml",
"txt", "text/plain",
"wav", "audio/wav",
"webm", "video/webm",
"webp", "image/webp",
"wma", "audio/wma",
"xml", "text/xml",
NULL, NULL,
2022-09-25 19:02:47 +00:00
};
2023-08-14 16:02:20 +00:00
const char *xs_mime_by_ext(const char *file)
2022-09-25 19:02:47 +00:00
/* returns the MIME type by file extension */
{
const char *ext = strrchr(file, '.');
if (ext) {
const char **p = xs_mime_types;
xs *uext = xs_tolower_i(xs_dup(ext + 1));
while (*p) {
int c;
2022-09-25 19:02:47 +00:00
if ((c = strcmp(*p, uext)) == 0)
return p[1];
else
if (c > 0)
break;
2022-09-25 19:02:47 +00:00
p += 2;
}
2022-09-25 19:02:47 +00:00
}
2023-08-14 16:02:20 +00:00
return "application/octet-stream";
2022-09-25 19:02:47 +00:00
}
#endif /* XS_IMPLEMENTATION */
#endif /* XS_MIME */