-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
145 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
@OptIn(ExperimentalUnsignedTypes::class) | ||
enum class MagicNumber( | ||
vararg magic: UByte, | ||
private val test: ( | ||
data: ByteArray, | ||
magic: ByteArray | ||
) -> Boolean = { data, magic -> | ||
data.startsWith(magic) | ||
} | ||
) { | ||
|
||
PDF(*"%PDF-".toUByteArray()), | ||
JPEG(0xFFu, 0xD8u, 0xFFu), | ||
PNG(0x89u, 0x50u, 0x4Eu, 0x47u, 0x0Du, 0x0Au, 0x1Au, 0x0Au), | ||
GIF(*"GIF8".toUByteArray()), | ||
WEBP(*"WEBP".toUByteArray(), test = { data, magic -> | ||
(data.size >= 12) && data.slice(8..11).toByteArray().contentEquals(magic) | ||
}); | ||
|
||
private val magic = magic.toUByteArray() | ||
|
||
companion object { | ||
fun find(data: ByteArray): MagicNumber? = | ||
entries.find { it.test(data, it.magic.toByteArray()) } | ||
} | ||
|
||
} | ||
|
||
fun ByteArray.findMagicNumber(): MagicNumber? = MagicNumber.find(this) | ||
|
||
@OptIn(ExperimentalUnsignedTypes::class) | ||
private fun String.toUByteArray() = toCharArray().map { | ||
it.code.toUByte() | ||
}.toUByteArray() | ||
|
||
fun ByteArray.startsWith( | ||
prefix: ByteArray | ||
): Boolean = | ||
(size >= prefix.size) | ||
&& slice(prefix.indices) | ||
.toByteArray() | ||
.contentEquals(prefix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
import java.io.File | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
|
||
fun Document(path: String): Document = Document(File(path)) | ||
fun Document(path: String): Document = Document { | ||
path(path) | ||
} | ||
|
||
fun Document.Builder.path(path: String) = file(File(path)) | ||
|
||
// TODO in the future this can be moved to jvmAndPosixMain | ||
// TODO in the future, if more types are supported, the magic number should be used to determine the media type. | ||
@OptIn(ExperimentalEncodingApi::class) | ||
fun Document(path: File): Document = Document( | ||
source = Document.Source( | ||
mediaType = Document.MediaType.APPLICATION_PDF, | ||
data = Base64.encode(path.readBytes()) | ||
) | ||
) | ||
fun Document.Builder.file(file: File) { | ||
bytes = file.readBytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.xemantic.anthropic.content | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
|
||
class MagicNumberTest { | ||
|
||
@Test | ||
fun shouldDetectImageMediaType() { | ||
File( | ||
"test-data/minimal.pdf" | ||
).readBytes().findMagicNumber() shouldBe MagicNumber.PDF | ||
File( | ||
"test-data/minimal.jpg" | ||
).readBytes().findMagicNumber() shouldBe MagicNumber.JPEG | ||
File( | ||
"test-data/minimal.png" | ||
).readBytes().findMagicNumber() shouldBe MagicNumber.PNG | ||
File( | ||
"test-data/minimal.gif" | ||
).readBytes().findMagicNumber() shouldBe MagicNumber.GIF | ||
File( | ||
"test-data/minimal.webp" | ||
).readBytes().findMagicNumber() shouldBe MagicNumber.WEBP | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.