file.h (3420B)
1 #ifndef FILE_H 2 #define FILE_H 3 4 #include <stdlib.h> 5 6 static const struct { 7 char *extension; 8 char *type; 9 } mime_types[] = { 10 { "aac", "audio/aac" }, 11 { "abw", "application/x-abiword" }, 12 { "acng", "image/apng" }, 13 { "arc", "application/x-freearc" }, 14 { "avif", "image/avif" }, 15 { "avi", "video/x-msvideo" }, 16 { "azw", "application/vnd.amazon.ebook" }, 17 { "bmp", "image/bmp" }, 18 { "bz", "application/x-bzip" }, 19 { "bz2", "application/x-bzip2" }, 20 { "cda", "application/x-cdf" }, 21 { "csh", "application/x-csh" }, 22 { "css", "text/css" }, 23 { "csv", "text/csv" }, 24 { "doc", "application/msword" }, 25 { "docx", "application/vnd.openxmlformats-officedocument." 26 "wordprocessingml.document" }, 27 { "eot", "application/vnd.ms-fontobject" }, 28 { "epub", "application/epub+zip" }, 29 { "gif", "image/gif" }, 30 { "gz", "application/gzip" }, 31 { "html", "text/html" }, 32 { "htm", "text/html" }, 33 { "ico", "image/vnd.microsoft.icon" }, 34 { "ics", "text/calendar" }, 35 { "jar", "application/java-archive" }, 36 { "jpeg", "image/jpeg" }, 37 { "jpg", "image/jpeg" }, 38 { "json", "application/json" }, 39 { "jsonld", "application/ld+json" }, 40 { "js", "application/javascript" }, 41 { "mid", "audio/midi" }, 42 { "midi", "audio/x-midi" }, 43 { "mjs", "text/javascript" }, 44 { "mp3", "audio/mpeg" }, 45 { "mp4", "video/mp4" }, 46 { "mpkg", "application/vnd.apple.installer+xml" }, 47 { "mpeg", "video/mpeg" }, 48 { "odp", "application/vnd.oasis.opendocument.presentation" }, 49 { "ods", "application/vnd.oasis.opendocument.spreadsheet" }, 50 { "odt", "application/vnd.oasis.opendocument.text" }, 51 { "oga", "audio/ogg" }, 52 { "ogv", "video/ogg" }, 53 { "ogx", "application/ogg" }, 54 { "opus", "audio/ogg" }, 55 { "otf", "font/otf" }, 56 { "pdf", "application/pdf" }, 57 { "php", "application/x-httpd-php" }, 58 { "png", "image/png" }, 59 { "ppt", "application/vnd.ms-powerpoint" }, 60 { "pptx", "application/vnd.openxmlformats-officedocument." 61 "presentationml.presentation" }, 62 { "rar", "application/vnd.rar" }, 63 { "rtf", "application/rtf" }, 64 { "sh", "application/x-sh" }, 65 { "svg", "image/svg+xml" }, 66 { "tar", "application/x-tar" }, 67 { "tif", "image/tiff" }, 68 { "tiff", "image/tiff" }, 69 { "txt", "text/plain" }, 70 { "ts", "video/mp2t" }, 71 { "ttf", "font/ttf" }, 72 { "vsd", "application/vnd.visio" }, 73 { "weba", "audio/webm" }, 74 { "webm", "video/webm" }, 75 { "webp", "image/webp" }, 76 { "woff", "font/woff" }, 77 { "woff2", "font/woff2" }, 78 { "xhtml", "application/xhtml+xml" }, 79 { "xls", "application/vnd.ms-excel" }, 80 { "xlsx", "application/vnd.openxmlformats-officedocument." 81 "spreadsheetml.sheet" }, 82 { "xml", "application/xml" }, 83 { "xul", "application/vnd.mozilla.xul+xml" }, 84 { "zip", "application/zip" }, 85 { "3gp", "video/3gpp" }, 86 { "3g2", "video/3gpp2" }, 87 { "7z", "application/x-7z-compressed" }, 88 }; 89 90 struct file_content { 91 char *content; 92 size_t length; 93 }; 94 95 char * 96 get_mime_type_from_extension(char *extension); 97 98 int 99 is_file_readable(char *path); 100 101 char * 102 get_normalised_path(char *path); 103 104 char * 105 get_file_name(char *path); 106 107 char * 108 get_file_extension(char *path); 109 110 /* 111 * It returns the file_content instances of the file at given path. 112 * 113 * Returns 'struct file_content { NULL, 0 }' in case of an error and a 114 * non-empty file_content instance otherwise. 115 */ 116 struct file_content 117 get_file_content(char *path); 118 119 #endif