mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
MPPFile
This commit is contained in:
parent
19ea2f340a
commit
b420d85be5
@ -2,6 +2,11 @@
|
||||
|
||||
## 0.5.13
|
||||
|
||||
* `Common`:
|
||||
* Add functionality for multiplatform working with files:
|
||||
* Main class for files `MPPFile`
|
||||
* Inline class for filenames work encapsulation `FileName`
|
||||
|
||||
## 0.5.12
|
||||
|
||||
* `Common`:
|
||||
|
@ -5,3 +5,18 @@ plugins {
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
jvmMain {
|
||||
dependencies {
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
}
|
||||
androidMain {
|
||||
dependencies {
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,17 @@ import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
|
||||
typealias ByteArrayAllocator = () -> ByteArray
|
||||
typealias SuspendByteArrayAllocator = suspend () -> ByteArray
|
||||
|
||||
val ByteArray.asAllocator: ByteArrayAllocator
|
||||
get() = { this }
|
||||
val ByteArray.asSuspendAllocator: SuspendByteArrayAllocator
|
||||
get() = { this }
|
||||
val ByteArrayAllocator.asSuspendAllocator: SuspendByteArrayAllocator
|
||||
get() = { this() }
|
||||
suspend fun SuspendByteArrayAllocator.asAllocator(): ByteArrayAllocator {
|
||||
return invoke().asAllocator
|
||||
}
|
||||
|
||||
object ByteArrayAllocatorSerializer : KSerializer<ByteArrayAllocator> {
|
||||
private val realSerializer = ByteArraySerializer()
|
||||
@ -17,7 +25,7 @@ object ByteArrayAllocatorSerializer : KSerializer<ByteArrayAllocator> {
|
||||
|
||||
override fun deserialize(decoder: Decoder): ByteArrayAllocator {
|
||||
val bytes = realSerializer.deserialize(decoder)
|
||||
return { bytes }
|
||||
return bytes.asAllocator
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: ByteArrayAllocator) {
|
||||
|
@ -0,0 +1,31 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.jvm.JvmInline
|
||||
|
||||
@Serializable
|
||||
@JvmInline
|
||||
value class FileName(val string: String) {
|
||||
val name: String
|
||||
get() = string.takeLastWhile { it != '/' }
|
||||
val extension: String
|
||||
get() = name.takeLastWhile { it != '.' }
|
||||
val nameWithoutExtension: String
|
||||
get() {
|
||||
val filename = name
|
||||
return filename.indexOfLast { it == '.' }.takeIf { it > -1 } ?.let {
|
||||
filename.substring(0, it)
|
||||
} ?: filename
|
||||
}
|
||||
override fun toString(): String = string
|
||||
}
|
||||
|
||||
|
||||
@PreviewFeature
|
||||
expect class MPPFile
|
||||
|
||||
expect val MPPFile.filename: FileName
|
||||
expect val MPPFile.filesize: Long
|
||||
expect val MPPFile.bytesAllocator: SuspendByteArrayAllocator
|
||||
suspend fun MPPFile.bytes() = bytesAllocator()
|
||||
|
@ -0,0 +1,32 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import org.khronos.webgl.ArrayBuffer
|
||||
import org.w3c.dom.ErrorEvent
|
||||
import org.w3c.files.File
|
||||
import org.w3c.files.FileReader
|
||||
import kotlin.js.Promise
|
||||
|
||||
actual typealias MPPFile = File
|
||||
|
||||
fun MPPFile.readBytesPromise() = Promise<ByteArray> { success, failure ->
|
||||
val reader = FileReader()
|
||||
reader.onload = {
|
||||
success((reader.result as ArrayBuffer).toByteArray())
|
||||
Unit
|
||||
}
|
||||
reader.onerror = {
|
||||
failure(Exception((it as ErrorEvent).message))
|
||||
Unit
|
||||
}
|
||||
reader.readAsArrayBuffer(this)
|
||||
}
|
||||
|
||||
private suspend fun MPPFile.dirtyReadBytes(): ByteArray = readBytesPromise().await()
|
||||
|
||||
actual val MPPFile.filename: FileName
|
||||
get() = FileName(name)
|
||||
actual val MPPFile.filesize: Long
|
||||
get() = size.toLong()
|
||||
@Warning("That is not optimized version of bytes allocator. Use asyncBytesAllocator everywhere you can")
|
||||
actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator
|
||||
get() = ::dirtyReadBytes
|
@ -0,0 +1,8 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.js.Promise
|
||||
|
||||
suspend fun <T> Promise<T>.await(): T = suspendCoroutine { cont ->
|
||||
then({ cont.resume(it) }, { cont.resumeWithException(it) })
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.doInIO
|
||||
import dev.inmo.micro_utils.coroutines.doOutsideOfCoroutine
|
||||
import java.io.File
|
||||
|
||||
actual typealias MPPFile = File
|
||||
|
||||
actual val MPPFile.filename: FileName
|
||||
get() = FileName(name)
|
||||
actual val MPPFile.filesize: Long
|
||||
get() = length()
|
||||
actual val MPPFile.bytesAllocator: SuspendByteArrayAllocator
|
||||
get() = {
|
||||
doInIO {
|
||||
doOutsideOfCoroutine {
|
||||
readBytes()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user