-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaType.kt
82 lines (66 loc) · 2.04 KB
/
MediaType.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright 2024 Kazimierz Pogoda / Xemantic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xemantic.ai.file.magic
import kotlinx.io.files.Path
/**
* A subset of MIME types and associated type detectors.
*/
@OptIn(ExperimentalUnsignedTypes::class)
public enum class MediaType(
public val mime: String,
private val detect: (ByteArray) -> Boolean
) {
JPEG(
mime = "image/jpeg",
detect = { it.startsWith(0xFFu, 0xD8u, 0xFFu) }
),
PNG(
mime = "image/png",
detect = { it.startsWith(0x89u, 0x50u, 0x4Eu, 0x47u, 0x0Du, 0x0Au, 0x1Au, 0x0Au) }
),
GIF(
mime = "image/gif",
detect = { it.startsWith("GIF87") || it.startsWith("GIF89") }
),
WEBP(
mime = "image/webp",
detect = { (it.size >= 12) && it.sliceArray(8..11).startsWith("WEBP") }
),
PDF(
mime = "application/pdf",
detect = { it.startsWith("%PDF-") }
);
public companion object {
public fun detect(
bytes: ByteArray
): MediaType? = entries.find {
it.detect(bytes)
}
}
}
public fun ByteArray.startsWith(
data: String
): Boolean = (size >= data.length)
&& sliceArray(0 ..< data.length)
.contentEquals(data.encodeToByteArray())
@OptIn(ExperimentalUnsignedTypes::class)
public fun ByteArray.startsWith(
vararg bytes: UByte
): Boolean = (size >= bytes.size)
&& sliceArray(0 ..< bytes.size)
.toUByteArray().contentEquals(bytes)
public fun ByteArray.detectMediaType(): MediaType? = MediaType.detect(this)
public fun Path.detectMediaType(): MediaType? = readBytes().detectMediaType()