|
| 1 | +package expo.modules.blob |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import expo.modules.kotlin.records.Field |
| 5 | +import expo.modules.kotlin.records.Record |
| 6 | +import expo.modules.kotlin.sharedobjects.SharedObject |
| 7 | +import expo.modules.kotlin.typedarray.TypedArray |
| 8 | +import expo.modules.kotlin.types.EitherOfThree |
| 9 | +import expo.modules.kotlin.types.Enumerable |
| 10 | + |
| 11 | +class Blob() : SharedObject() { |
| 12 | + var blobParts: List<InternalBlobPart> = listOf() |
| 13 | + var type: String = "" |
| 14 | + val size: Int by lazy { |
| 15 | + var size = 0 |
| 16 | + for (bp in blobParts) { |
| 17 | + size += bp.size() |
| 18 | + } |
| 19 | + return@lazy size |
| 20 | + } |
| 21 | + |
| 22 | + constructor(blobParts: List<InternalBlobPart>, type: String) : this() { |
| 23 | + this.blobParts = blobParts |
| 24 | + this.type = if(validType(type)) type.lowercase() else "" |
| 25 | + } |
| 26 | + |
| 27 | + fun text(): String { |
| 28 | + var str = "" |
| 29 | + for (bp in blobParts) { |
| 30 | + str += bp.text() |
| 31 | + } |
| 32 | + return str |
| 33 | + } |
| 34 | + |
| 35 | + private fun InternalBlobPart.offsetSlice(start: Int, end: Int, offset: Int): InternalBlobPart { |
| 36 | + var s: Int = start - offset |
| 37 | + var e: Int = end - offset |
| 38 | + if (s < 0) { |
| 39 | + s = 0 |
| 40 | + } |
| 41 | + if (e > this.size()) { |
| 42 | + e = this.size() |
| 43 | + } |
| 44 | + return when (this) { |
| 45 | + is InternalBlobPart.StringPart -> InternalBlobPart.StringPart(string.substring(s, e)) |
| 46 | + is InternalBlobPart.BlobPart -> InternalBlobPart.BlobPart(blob.slice(s, e, "")) |
| 47 | + is InternalBlobPart.BufferPart -> InternalBlobPart.BufferPart( |
| 48 | + buffer.slice(s..<e).toByteArray() |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fun slice(start: Int, end: Int, contentType: String): Blob { |
| 54 | + var i: Int = 0 |
| 55 | + val bps: MutableList<InternalBlobPart> = mutableListOf() |
| 56 | + |
| 57 | + for (bp in blobParts) { |
| 58 | + if (i + bp.size() <= start) { |
| 59 | + i += bp.size() |
| 60 | + continue |
| 61 | + } |
| 62 | + if (i >= end) { |
| 63 | + break |
| 64 | + } |
| 65 | + bps.add(bp.offsetSlice(start, end, i)) |
| 66 | + i += bp.size() |
| 67 | + } |
| 68 | + |
| 69 | + return Blob(bps, contentType) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +private fun validType(type : String): Boolean { |
| 74 | + Log.d("UT", "type: " + type + ", type length: " + type.length.toString()) |
| 75 | + for (c in type) { |
| 76 | + if (c.code < 0x20 || c.code > 0x7E) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + } |
| 80 | + return true; |
| 81 | +} |
| 82 | + |
| 83 | +typealias BlobPart = EitherOfThree<String, Blob, TypedArray> |
| 84 | + |
| 85 | +private fun TypedArray.bytes(): ByteArray { |
| 86 | + var ba = ByteArray(this.byteLength) |
| 87 | + |
| 88 | + for (i in 0..<this.byteLength) { |
| 89 | + ba[i] = this.readByte(i + this.byteOffset) |
| 90 | + } |
| 91 | + |
| 92 | + return ba |
| 93 | +} |
| 94 | + |
| 95 | +const val CR = '\r' |
| 96 | +const val LF = '\n' |
| 97 | +private fun String.toNativeNewlines(): String { |
| 98 | + var i = 0 |
| 99 | + var str = "" |
| 100 | + |
| 101 | + while (i < this.length) { |
| 102 | + if (this[i] == CR) { |
| 103 | + str += LF |
| 104 | + i += 1 |
| 105 | + if (i < this.length && this[i] == LF) { |
| 106 | + i += 1 |
| 107 | + } |
| 108 | + continue |
| 109 | + } |
| 110 | + str += this[i] |
| 111 | + i += 1 |
| 112 | + } |
| 113 | + return str |
| 114 | +} |
| 115 | + |
| 116 | +internal fun List<BlobPart>.internal(nativeNewlines: Boolean): List<InternalBlobPart> { |
| 117 | + return this.map() { bp: BlobPart -> |
| 118 | + if (bp.`is`(String::class)) { |
| 119 | + bp.get(String::class).let { |
| 120 | + val str = if (nativeNewlines) { |
| 121 | + it.toNativeNewlines() |
| 122 | + } else { |
| 123 | + it |
| 124 | + } |
| 125 | + InternalBlobPart.StringPart(str) |
| 126 | + } |
| 127 | + } else if (bp.`is`(Blob::class)) { |
| 128 | + bp.get(Blob::class).let { |
| 129 | + InternalBlobPart.BlobPart(it) |
| 130 | + } |
| 131 | + } else { |
| 132 | + bp.get(TypedArray::class).let { |
| 133 | + InternalBlobPart.BufferPart(it.bytes()) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +sealed class InternalBlobPart() { |
| 140 | + data class StringPart(val string: String) : InternalBlobPart() |
| 141 | + data class BlobPart(val blob: Blob) : InternalBlobPart() |
| 142 | + data class BufferPart(val buffer: ByteArray) : InternalBlobPart() |
| 143 | + |
| 144 | + fun size(): Int { |
| 145 | + return when (this) { |
| 146 | + is StringPart -> string.length |
| 147 | + is BlobPart -> blob.size |
| 148 | + is BufferPart -> buffer.size |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + fun text(): String { |
| 153 | + return when (this) { |
| 154 | + is StringPart -> string |
| 155 | + is BlobPart -> blob.text() |
| 156 | + is BufferPart -> buffer.decodeToString() |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +enum class EndingType(val str: String = "transparent") : Enumerable { |
| 162 | + TRANSPARENT("transparent"), NATIVE("native"), |
| 163 | +} |
| 164 | + |
| 165 | +internal const val DEFAULT_TYPE = "" |
| 166 | + |
| 167 | +class BlobOptionsBag : Record { |
| 168 | + @Field |
| 169 | + val type: String = "" |
| 170 | + |
| 171 | + @Field |
| 172 | + val endings: EndingType = EndingType.TRANSPARENT |
| 173 | +} |
0 commit comments