snac2/xs_mime.h

86 lines
2 KiB
C
Raw Normal View History

2024-01-04 08:22:03 +00:00
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
2022-09-25 19:02:47 +00:00
2024-09-20 08:47:14 +00:00
#ifndef _XS_MIME_H
2022-09-25 19:02:47 +00:00
2024-09-20 08:47:14 +00:00
#define _XS_MIME_H
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
extern const char *xs_mime_types[];
2022-09-25 19:02:47 +00:00
#ifdef XS_IMPLEMENTATION
/* intentionally brain-dead simple */
2024-12-07 13:52:15 +00:00
/* CAUTION: sorted by extension */
const char *xs_mime_types[] = {
"3gp", "video/3gpp",
"aac", "audio/aac",
2024-12-07 13:52:15 +00:00
"avif", "image/avif",
"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) {
2024-04-14 17:26:49 +00:00
xs *uext = xs_tolower_i(xs_dup(ext + 1));
int b = 0;
int t = xs_countof(xs_mime_types) / 2 - 2;
2024-04-14 17:26:49 +00:00
while (t >= b) {
int n = (b + t) / 2;
const char *p = xs_mime_types[n * 2];
2022-09-25 19:02:47 +00:00
2024-04-14 17:26:49 +00:00
int c = strcmp(uext, p);
if (c < 0)
t = n - 1;
else
if (c > 0)
2024-04-14 17:26:49 +00:00
b = n + 1;
else
return xs_mime_types[(n * 2) + 1];
}
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 */
2024-09-20 08:47:14 +00:00
#endif /* XS_MIME_H */