Improve mime-type handling
parent
c51218240e
commit
7efc47724e
@ -0,0 +1,46 @@
|
|||||||
|
package org.koitharu.kotatsu.core.util
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
|
import android.webkit.MimeTypeMap
|
||||||
|
import org.jetbrains.annotations.Blocking
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.MimeType
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.toMimeTypeOrNull
|
||||||
|
import org.koitharu.kotatsu.parsers.util.nullIfEmpty
|
||||||
|
import org.koitharu.kotatsu.parsers.util.removeSuffix
|
||||||
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||||
|
import java.io.File
|
||||||
|
import java.nio.file.Files
|
||||||
|
import coil3.util.MimeTypeMap as CoilMimeTypeMap
|
||||||
|
|
||||||
|
object MimeTypes {
|
||||||
|
|
||||||
|
fun getMimeTypeFromExtension(fileName: String): MimeType? {
|
||||||
|
return CoilMimeTypeMap.getMimeTypeFromExtension(getNormalizedExtension(fileName) ?: return null)
|
||||||
|
?.toMimeTypeOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getMimeTypeFromUrl(url: String): MimeType? {
|
||||||
|
return CoilMimeTypeMap.getMimeTypeFromUrl(url)?.toMimeTypeOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getExtension(mimeType: MimeType?): String? {
|
||||||
|
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType?.toString() ?: return null)?.nullIfEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Blocking
|
||||||
|
fun probeMimeType(file: File): MimeType? {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
runCatchingCancellable {
|
||||||
|
Files.probeContentType(file.toPath())?.toMimeTypeOrNull()
|
||||||
|
}.getOrNull()?.let { return it }
|
||||||
|
}
|
||||||
|
return getMimeTypeFromExtension(file.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getNormalizedExtension(name: String): String? = name
|
||||||
|
.lowercase()
|
||||||
|
.removeSuffix('~')
|
||||||
|
.removeSuffix(".tmp")
|
||||||
|
.substringAfterLast('.', "")
|
||||||
|
.takeIf { it.length in 2..5 }
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
package org.koitharu.kotatsu.core.util.ext
|
||||||
|
|
||||||
|
import okhttp3.MediaType
|
||||||
|
|
||||||
|
private const val TYPE_IMAGE = "image"
|
||||||
|
private val REGEX_MIME = Regex("^\\w+/([-+.\\w]+|\\*)$", RegexOption.IGNORE_CASE)
|
||||||
|
|
||||||
|
@JvmInline
|
||||||
|
value class MimeType(private val value: String) {
|
||||||
|
|
||||||
|
val type: String?
|
||||||
|
get() = value.substringBefore('/', "").takeIfSpecified()
|
||||||
|
|
||||||
|
val subtype: String?
|
||||||
|
get() = value.substringAfterLast('/', "").takeIfSpecified()
|
||||||
|
|
||||||
|
private fun String.takeIfSpecified(): String? = takeUnless {
|
||||||
|
it.isEmpty() || it == "*"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String = value
|
||||||
|
}
|
||||||
|
|
||||||
|
fun MediaType.toMimeType(): MimeType = MimeType("$type/$subtype")
|
||||||
|
|
||||||
|
fun String.toMimeTypeOrNull(): MimeType? = if (REGEX_MIME.matches(this)) {
|
||||||
|
MimeType(lowercase())
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
val MimeType.isImage: Boolean
|
||||||
|
get() = type == TYPE_IMAGE
|
||||||
Loading…
Reference in New Issue