Skip to content

Commit

Permalink
Added support for MIME types
Browse files Browse the repository at this point in the history
  • Loading branch information
blakemcbride committed Mar 25, 2024
1 parent 3e6593a commit e1c32aa
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/main/core/org/kissweb/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,120 @@ public static void copy(String source, String dest) throws IOException {
dfile.getParentFile().mkdirs();
Files.copy(sfile.toPath(), dfile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

/**
* Returns the MIME type based on the file name's extension.
*
* @param fname the complete file name or the extension
* @return the MIME type corresponding to the file extension, or "application/octet-stream" if not found
*/
public static String getMimeType(String fname) {
final String dflt = "application/octet-stream";
String ext;
if (fname == null || fname.isEmpty())
return dflt;
int idx = fname.lastIndexOf('.');
if (idx == -1)
ext = fname; // only the extension was passed in
else if (idx == fname.length() - 1)
return dflt;
else
ext = fname.substring(idx+1);
switch (ext.toLowerCase()) {
case "aac": return "audio/aac";
case "abw": return "application/x-abiword";
case "arc": return "application/x-freearc";
case "avi": return "video/x-msvideo";
case "azw": return "application/vnd.amazon.ebook";
case "bin": return "application/octet-stream";
case "bmp": return "image/bmp";
case "bz": return "application/x-bzip";
case "bz2": return "application/x-bzip2";
case "cda": return "application/x-cdf";
case "csh": return "application/x-csh";
case "css": return "text/css";
case "csv": return "text/csv";
case "doc": return "application/msword";
case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case "eot": return "application/vnd.ms-fontobject";
case "epub": return "application/epub+zip";
case "gz": return "application/gzip";
case "gif": return "image/gif";
case "htm": return "text/html";
case "html": return "text/html";
case "ico": return "image/vnd.microsoft.icon";
case "ics": return "text/calendar";
case "jar": return "application/java-archive";
case "jpeg": return "image/jpeg";
case "jpg": return "image/jpeg";
case "js": return "text/javascript";
case "json": return "application/json";
case "jsonld": return "application/ld+json";
case "mid": return "audio/x-midi";
case "midi": return "audio/x-midi";
case "mjs": return "text/javascript";
case "mp3": return "audio/mpeg";
case "mp4": return "video/mp4";
case "mpeg": return "video/mpeg";
case "mpg": return "video/mpeg";
case "mpkg": return "application/vnd.apple.installer+xml";
case "odp": return "application/vnd.oasis.opendocument.presentation";
case "ods": return "application/vnd.oasis.opendocument.spreadsheet";
case "odt": return "application/vnd.oasis.opendocument.text";
case "oga": return "audio/ogg";
case "ogv": return "video/ogg";
case "ogx": return "application/ogg";
case "opus": return "audio/opus";
case "otf": return "font/otf";
case "png": return "image/png";
case "pdf": return "application/pdf";
case "php": return "application/x-httpd-php";
case "ppt": return "application/vnd.ms-powerpoint";
case "pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
case "rar": return "application/vnd.rar";
case "rtf": return "application/rtf";
case "sh": return "application/x-sh";
case "svg": return "image/svg+xml";
case "swf": return "application/x-shockwave-flash";
case "tar": return "application/x-tar";
case "tif": return "image/tiff";
case "tiff": return "image/tiff";
case "ts": return "video/mp2t";
case "ttf": return "font/ttf";
case "txt": return "text/plain";
case "vsd": return "application/vnd.visio";
case "wav": return "audio/wav";
case "weba": return "audio/webm";
case "webm": return "video/webm";
case "webp": return "image/webp";
case "woff": return "font/woff";
case "woff2": return "font/woff2";
case "xhtml": return "application/xhtml+xml";
case "xls": return "application/vnd.ms-excel";
case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
case "xml": return "application/xml";
case "xul": return "application/vnd.mozilla.xul+xml";
case "zip": return "application/zip";
case "3gp": return "audio/3gpp";
case "3g2": return "audio/3gpp2";
case "7z": return "application/x-7z-compressed";
default: return dflt;
}
}

/**
* Obtain the file name extension.
* For example "file.pdf" will return "pdf".
* "" is returned if no extension is found.
*
* @param fn the file name
* @return the extension portion of the file name without the "."
*/
public static String getExtension(String fn) {
int idx;
if (fn == null || fn.isEmpty() || (idx=fn.lastIndexOf(".")) == -1)
return "";
return fn.substring(idx+1);
}

}

0 comments on commit e1c32aa

Please sign in to comment.