Skip to content

Instantly share code, notes, and snippets.

@felix19350
Last active February 20, 2022 22:32
Show Gist options
  • Save felix19350/d42ca9a9f505532e0292642ae67b1691 to your computer and use it in GitHub Desktop.
Save felix19350/d42ca9a9f505532e0292642ae67b1691 to your computer and use it in GitHub Desktop.
Kotlin simple mimetype/file extension detector
.aac audio/aac
.abw application/x-abiword
.arc application/octet-stream
.avi video/x-msvideo
.azw application/vnd.amazon.ebook
.bin application/octet-stream
.bz application/x-bzip
.bz2 application/x-bzip2
.csh application/x-csh
.css text/css
.csv text/csv
.doc application/msword
.epub application/epub+zip
.gif image/gif
.htm text/html
.html text/html
.ico image/x-icon
.ics text/calendar
.jar application/java-archive
.jpeg image/jpeg
.jpg image/jpeg
.js application/javascript
.json application/json
.midi audio/midi
.mid audio/midi
.mpeg video/mpeg
.mpkg application/vnd.apple.installer+xml
.odp application/vnd.oasis.opendocument.presentation
.ods application/vnd.oasis.opendocument.spreadsheet
.odt application/vnd.oasis.opendocument.text
.oga audio/ogg
.ogv video/ogg
.ogx application/ogg
.pdf application/pdf
.ppt application/vnd.ms-powerpoint
.rar application/x-rar-compressed
.rtf application/rtf
.sh application/x-sh
.svg image/svg+xml
.swf application/x-shockwave-flash
.tar application/x-tar
.tiff image/tiff
.tif image/tiff
.ttf font/ttf
.vsd application/vnd.visio
.wav audio/x-wav
.weba audio/webm
.webm video/webm
.webp image/webp
.woff font/woff
.woff2 font/woff2
.xhtml application/xhtml+xml
.xls application/vnd.ms-excel
.xml application/xml
.xul application/vnd.mozilla.xul+xml
.zip application/zip
.3gp video/3gpp
.3g2 video/3gpp2
.7z application/x-7z-compressed
import org.slf4j.LoggerFactory
class MimeTypeDetector private constructor() {
private val log = LoggerFactory.getLogger(MimeTypeDetector::class.java)
private var extensionToMimeTypeMapping: MutableMap<String, String> = mutableMapOf()
private var mimeTypeToExtensionMapping: MutableMap<String, String> = mutableMapOf()
companion object {
private var detector: MimeTypeDetector? = null
fun getInstance(): MimeTypeDetector {
if (detector == null) {
detector = MimeTypeDetector()
}
return detector!!
}
}
fun configure(extensionMimeTypePairs: Collection<Pair<String, String>>) {
extensionMimeTypePairs.forEach { p ->
extensionToMimeTypeMapping[p.first] = p.second
mimeTypeToExtensionMapping[p.second] = p.first
}
}
fun findExtension(mimeType: String): String? {
if (mimeTypeToExtensionMapping.isEmpty()) {
log.warn("Mime type to extension mapping is empty. Please configure it.")
}
return mimeTypeToExtensionMapping[mimeType]
}
fun findMimeType(extension: String): String? {
if (mimeTypeToExtensionMapping.isEmpty()) {
log.warn("Extension to mime type mapping is empty. Please configure it.")
}
return extensionToMimeTypeMapping[extension]
}
}
import java.io.File
fun main(args : Array<String>) {
val supportedMimeTypes: MutableCollection<Pair<String, String>> = mutableListOf()
val mimeTypeCatalog = File("mime-type-catalog.csv")
assert(mimeTypeCatalog.exists())
mimeTypeCatalog.file.forEachLine { l ->
val tokens = l.split(",")
supportedMimeTypes.add(Pair(tokens.first(), tokens.last()))
}
MimeTypeDetector.getInstance().configure(supportedMimeTypes)
}
@felix19350
Copy link
Author

No strings-attached kotlin file extension to mime-type detector / mime-type to file extension detector. It's no replacement for a solution like apache tika, but for simple use cases works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment