scoop:scoop.http.server:0.1.0
src/files/media.scoop
Selected production source · src/files/media.scoop
1
package scoop.http.server3
import scoop.platform.fs.Path4
import scoop.text.lowercaseAscii6
internal fun fileContentType(path: Path): String {7
val name = path.fileName8
if (!present(name)) return "application/octet-stream"9
val bytes = name!!.toByteArray()10
var dot = -1L11
var index = 0L12
for (value in bytes) { if (value == 46) dot = index; index += 1L }13
if (dot < 0L) return "application/octet-stream"14
val start = dot + 1L15
val suffix = Array<Byte>(bytes.size - start) { offset -> bytes[start + offset] }16
for (value in suffix) if (value < 0) return "application/octet-stream"17
return when (String.fromUtf8(suffix).lowercaseAscii()) {18
case "html" -> "text/html; charset=utf-8"19
case "htm" -> "text/html; charset=utf-8"20
case "txt" -> "text/plain; charset=utf-8"21
case "css" -> "text/css; charset=utf-8"22
case "js" -> "text/javascript; charset=utf-8"23
case "json" -> "application/json"24
case "xml" -> "application/xml"25
case "svg" -> "image/svg+xml"26
case "png" -> "image/png"27
case "jpg" -> "image/jpeg"28
case "jpeg" -> "image/jpeg"29
case "gif" -> "image/gif"30
case "webp" -> "image/webp"31
case "pdf" -> "application/pdf"32
case "wasm" -> "application/wasm"33
else -> "application/octet-stream"34
}35
}