add default input providers

This commit is contained in:
InsanusMokrassar 2022-03-06 18:06:57 +06:00
parent 91927d3b50
commit f5b71dcb84
11 changed files with 78 additions and 34 deletions

View File

@ -5,6 +5,7 @@ import dev.inmo.postssystem.features.files.common.FullFileInfo
import dev.inmo.micro_utils.common.*
import dev.inmo.micro_utils.mime_types.KnownMimeTypes
import dev.inmo.micro_utils.mime_types.findBuiltinMimeType
import dev.inmo.postssystem.features.common.common.BytesBasedInputProvider
import io.ktor.utils.io.core.ByteReadPacket
import kotlinx.coroutines.flow.MutableStateFlow
import org.khronos.webgl.ArrayBuffer
@ -27,9 +28,8 @@ fun uploadFileCallbackForHTMLInputChange(
FullFileInfo(
FileName(file.name),
findBuiltinMimeType(file.type) ?: KnownMimeTypes.Any,
) {
ByteReadPacket(bytes)
}
BytesBasedInputProvider(bytes)
)
)
}

View File

@ -12,9 +12,9 @@ kotlin {
dependencies {
api libs.microutils.common
api libs.microutils.serialization.typedserializer
api libs.klock
api "io.insert-koin:koin-core:$koin_version"
api "com.benasher44:uuid:$uuid_version"
api "com.soywiz.korlibs.klock:klock:$klock_version"
api "io.ktor:ktor-http:$ktor_version"
}
}

View File

@ -1,30 +1,47 @@
package dev.inmo.postssystem.features.common.common
import dev.inmo.micro_utils.common.ByteArrayAllocatorSerializer
import dev.inmo.micro_utils.common.MPPFile
import io.ktor.utils.io.core.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ByteArraySerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.Serializable
typealias SimpleInputProvider = () -> Input
@Serializable(SimpleInputProviderSerializer::class)
interface SimpleInputProvider {
operator fun invoke(): Input
}
@Serializable
class BytesBasedInputProvider(
private val bytes: ByteArray
) : SimpleInputProvider {
override fun invoke(): Input {
return ByteReadPacket(bytes)
}
}
@Serializable(SimpleInputProviderSerializer::class)
expect class FileBasedInputProvider : SimpleInputProvider {
val file: MPPFile
}
object SimpleInputProviderSerializer : KSerializer<SimpleInputProvider> {
private val serializer = ByteArraySerializer()
override val descriptor: SerialDescriptor
get() = ByteArrayAllocatorSerializer.descriptor
get() = serializer.descriptor
override fun deserialize(decoder: Decoder): SimpleInputProvider {
val allocator = ByteArrayAllocatorSerializer.deserialize(decoder)
return {
ByteReadPacket(allocator.invoke())
}
val bytes = serializer.deserialize(decoder)
return BytesBasedInputProvider(bytes)
}
override fun serialize(encoder: Encoder, value: SimpleInputProvider) {
ByteArrayAllocatorSerializer.serialize(
encoder
) {
serializer.serialize(
encoder,
value().readBytes()
}
)
}
}

View File

@ -0,0 +1,14 @@
package dev.inmo.postssystem.features.common.common
import dev.inmo.micro_utils.common.MPPFile
import io.ktor.utils.io.core.ByteReadPacket
import io.ktor.utils.io.core.Input
import kotlinx.serialization.Serializable
@Serializable(SimpleInputProviderSerializer::class)
actual class FileBasedInputProvider internal constructor(
actual val file: MPPFile,
private val bytes: ByteArray
) : SimpleInputProvider {
override fun invoke(): Input = ByteReadPacket(bytes)
}

View File

@ -0,0 +1,13 @@
package dev.inmo.postssystem.features.common.common
import dev.inmo.micro_utils.common.MPPFile
import io.ktor.utils.io.core.Input
import io.ktor.utils.io.streams.asInput
import kotlinx.serialization.Serializable
@Serializable(SimpleInputProviderSerializer::class)
actual class FileBasedInputProvider(
actual val file: MPPFile
) : SimpleInputProvider {
override fun invoke(): Input = file.inputStream().asInput()
}

View File

@ -3,6 +3,7 @@ package dev.inmo.postssystem.features.files.common
import dev.inmo.postssystem.features.files.common.storage.ReadFilesStorage
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
import dev.inmo.postssystem.features.common.common.FileBasedInputProvider
import io.ktor.utils.io.streams.asInput
import java.io.File
@ -37,10 +38,9 @@ class DiskReadFilesStorage(
id,
FullFileInfo(
it.fileInfo.name,
it.fileInfo.mimeType
) {
id.file.inputStream().asInput()
}
it.fileInfo.mimeType,
FileBasedInputProvider(id.file)
)
)
}

View File

@ -16,12 +16,6 @@ koin_version=3.1.2
ktor_version=1.6.7
logback_version=1.2.10
uuid_version=0.3.1
klock_version=2.5.2
# Server
kotlin_exposed_version=0.37.3
psql_version=42.3.0
# JS

View File

@ -2,11 +2,12 @@
kotlin = "1.6.10"
kotlin-serialization = "1.3.2"
jsuikit = "0.0.41"
jsuikit = "0.0.42"
compose = "1.1.0"
microutils = "0.9.9"
microutils = "0.9.10"
tgbotapi = "0.38.6"
ktor = "1.6.7"
klock = "2.6.1"
exposed = "0.37.3"
psql = "42.3.0"
@ -46,6 +47,8 @@ microutils-serialization-typedserializer = { module = "dev.inmo:micro_utils.seri
tgbotapi = { module = "dev.inmo:tgbotapi", version.ref = "tgbotapi" }
klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" }
androidx-test-junit = { module = "androidx.test.ext:junit", version.ref = "android-test-junit" }
androidx-espresso = { module = "androidx.test.espresso:espresso-core", version.ref = "android-espresso-core" }

File diff suppressed because one or more lines are too long

View File

@ -45,7 +45,7 @@ class ClientWritePostsService(
when (content) {
is BinaryContent -> append(
i.toString(),
InputProvider(block = content.inputProvider),
InputProvider(block = content.inputProvider::invoke),
headers {
append(HttpHeaders.ContentType, content.mimeType.raw)
append(HttpHeaders.ContentDisposition, "filename=\"${content.filename.name}\"")
@ -86,7 +86,7 @@ class ClientWritePostsService(
when (it) {
is BinaryContent -> append(
i.toString(),
InputProvider(block = it.inputProvider),
InputProvider(block = it.inputProvider::invoke),
headers {
append(HttpHeaders.ContentType, it.mimeType.raw)
append(HttpHeaders.ContentDisposition, "filename=\"${it.filename.name}\"")

View File

@ -9,6 +9,7 @@ import dev.inmo.micro_utils.repos.ktor.common.crud.createRouting
import dev.inmo.micro_utils.repos.ktor.common.crud.updateRouting
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.removeRoute
import dev.inmo.micro_utils.repos.ktor.server.crud.configureReadStandardCrudRepoRoutes
import dev.inmo.postssystem.features.common.common.FileBasedInputProvider
import dev.inmo.postssystem.features.content.common.*
import dev.inmo.postssystem.features.posts.common.*
import dev.inmo.postssystem.services.posts.common.*
@ -78,8 +79,9 @@ class ServerPostsServiceRoutingConfigurator(
list.add(
name to BinaryContent(
filename,
mimeType
) { resultInput.inputStream().asInput() }
mimeType,
FileBasedInputProvider(resultInput)
)
)
}
else -> {}
@ -139,8 +141,9 @@ class ServerPostsServiceRoutingConfigurator(
list.add(
name to BinaryContent(
filename,
mimeType
) { resultInput.inputStream().asInput() }.either()
mimeType,
FileBasedInputProvider(resultInput)
).either()
)
}
else -> {}