provide to server work with posts

This commit is contained in:
InsanusMokrassar 2022-01-22 13:37:56 +06:00
parent 5e61c2a770
commit 4f96d2ce76
4 changed files with 29 additions and 62 deletions

View File

@ -35,7 +35,7 @@ import dev.inmo.postssystem.features.posts.server.ExposedServerPostsStorage
import dev.inmo.postssystem.features.posts.server.ServerPostsStorage import dev.inmo.postssystem.features.posts.server.ServerPostsStorage
import dev.inmo.postssystem.features.publication.server.PublicationManager import dev.inmo.postssystem.features.publication.server.PublicationManager
import dev.inmo.postssystem.features.publication.server.PublicationTarget import dev.inmo.postssystem.features.publication.server.PublicationTarget
import dev.inmo.postssystem.services.posts.server.ServerPostsServiceRoutingConfigurator import dev.inmo.postssystem.services.posts.server.*
import dev.inmo.postssystem.targets.telegram.publication.server.PublicationTargetTelegram import dev.inmo.postssystem.targets.telegram.publication.server.PublicationTargetTelegram
import io.ktor.application.featureOrNull import io.ktor.application.featureOrNull
import io.ktor.application.log import io.ktor.application.log
@ -177,6 +177,13 @@ fun getDIModule(
// Posts storage // Posts storage
single<ServerPostsStorage> { ExposedServerPostsStorage(get()) } single<ServerPostsStorage> { ExposedServerPostsStorage(get()) }
singleWithBinds {
ServerPostsService(
DefaultExposedReadPostsService(get<ServerPostsStorage>()),
DefaultWritePostsService(get<ServerPostsStorage>(), get<ServerContentStorage<Content>>())
)
}
// Routing configurators // Routing configurators
singleWithBinds { FilesRoutingConfigurator(get(), null, get()) } singleWithBinds { FilesRoutingConfigurator(get(), null, get()) }
singleWithBinds { StatusRoutingConfigurator } singleWithBinds { StatusRoutingConfigurator }

View File

@ -0,0 +1,11 @@
package dev.inmo.postssystem.services.posts.server
import dev.inmo.micro_utils.repos.ReadCRUDRepo
import dev.inmo.postssystem.features.posts.common.PostId
import dev.inmo.postssystem.features.posts.common.RegisteredPost
import dev.inmo.postssystem.features.posts.server.ServerReadPostsStorage
import dev.inmo.postssystem.services.posts.common.ReadPostsService
class DefaultExposedReadPostsService(
private val postsStorage: ServerReadPostsStorage
) : ReadPostsService, ReadCRUDRepo<RegisteredPost, PostId> by postsStorage

View File

@ -1,61 +0,0 @@
package dev.inmo.postssystem.services.posts.server
import dev.inmo.micro_utils.common.FileName
import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.ktor.common.StandardKtorSerialFormat
import dev.inmo.micro_utils.ktor.common.decodeHex
import dev.inmo.postssystem.features.content.common.ContentSerializer
import dev.inmo.postssystem.services.posts.common.FullNewPost
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.http.content.PartData
import io.ktor.request.receiveMultipart
import io.ktor.util.asStream
import io.ktor.util.pipeline.PipelineContext
import io.ktor.utils.io.core.use
suspend fun PipelineContext<Unit, ApplicationCall>.downloadFullNewPost(
serialFormat: StandardKtorSerialFormat
): FullNewPost {
val multipart = call.receiveMultipart()
val map = mutableMapOf<String, Any>()
var part = multipart.readPart()
while (part != null) {
val name = part.name
val capturedPart = part
when {
name == null -> {}
capturedPart is PartData.FormItem -> {
map[name] = serialFormat.decodeHex(
ContentSerializer,
capturedPart.value
)
}
capturedPart is PartData.FileItem -> {
val filename = FileName(capturedPart.originalFileName ?: error("File name is unknown for default part"))
val resultInput = MPPFile.createTempFile(
filename.nameWithoutExtension.let {
var resultName = it
while (resultName.length < 3) {
resultName += "_"
}
resultName
},
".${filename.extension}"
).apply {
outputStream().use { fileStream ->
capturedPart.provider().asStream().copyTo(fileStream)
}
}
}
else -> {}
}
part = multipart.readPart()
}
}

View File

@ -0,0 +1,10 @@
package dev.inmo.postssystem.services.posts.server
import dev.inmo.postssystem.services.posts.common.*
class ServerPostsService(
private val readPostsService: ReadPostsService,
private val writePostsService: WritePostsService
) : PostsService,
ReadPostsService by readPostsService,
WritePostsService by writePostsService