|
| 1 | +package io.writeopia.core.folders.sync |
| 2 | + |
| 3 | +import io.ktor.client.HttpClient |
| 4 | +import io.ktor.client.call.body |
| 5 | +import io.ktor.client.request.forms.formData |
| 6 | +import io.ktor.client.request.forms.submitFormWithBinaryData |
| 7 | +import io.ktor.client.statement.HttpResponse |
| 8 | +import io.ktor.http.ContentType |
| 9 | +import io.ktor.http.Headers |
| 10 | +import io.ktor.http.HttpHeaders |
| 11 | +import io.ktor.http.HttpStatusCode |
| 12 | +import io.writeopia.sdk.models.utils.ResultData |
| 13 | +import io.writeopia.sdk.repository.DocumentRepository |
| 14 | +import io.writeopia.sdk.serialization.request.ImageUploadRequest |
| 15 | +import kotlinx.io.buffered |
| 16 | +import kotlinx.io.files.Path |
| 17 | +import kotlinx.io.files.SystemFileSystem |
| 18 | +import kotlinx.io.readByteArray |
| 19 | + |
| 20 | +class ImageSync( |
| 21 | + private val client: HttpClient, |
| 22 | + private val baseUrl: String, |
| 23 | + private val documentRepository: DocumentRepository |
| 24 | +) { |
| 25 | + |
| 26 | + suspend fun syncAllImages(workspaceId: String, token: String) { |
| 27 | + val storySteps = documentRepository.queryUnsyncedImagesSteps() |
| 28 | + |
| 29 | + storySteps |
| 30 | + .filter { storyStep -> storyStep.path != null } |
| 31 | + .forEach { step -> |
| 32 | + val result = sendImageFromPath( |
| 33 | + imagePath = step.path!!, |
| 34 | + workspaceId = workspaceId, |
| 35 | + token = token |
| 36 | + ) |
| 37 | + |
| 38 | + if (result is ResultData.Complete) { |
| 39 | + val url = step.url |
| 40 | + |
| 41 | + if (url != null) { |
| 42 | + documentRepository.updateStoryStepUrl(url, step.id) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private suspend fun sendImageFromPath( |
| 49 | + imagePath: String, |
| 50 | + workspaceId: String, |
| 51 | + token: String |
| 52 | + ): ResultData<ImageUploadRequest> = |
| 53 | + try { |
| 54 | + val contentType = detectContentType(imagePath) |
| 55 | + |
| 56 | + val response: HttpResponse = client.submitFormWithBinaryData( |
| 57 | + url = "$baseUrl/api/workspace/$workspaceId/document/upload-image", |
| 58 | + formData = formData { |
| 59 | + append( |
| 60 | + "image", |
| 61 | + SystemFileSystem.source(Path(imagePath)).buffered().readByteArray(), |
| 62 | + Headers.build { |
| 63 | + append(HttpHeaders.ContentType, contentType.toString()) |
| 64 | + append( |
| 65 | + HttpHeaders.ContentDisposition, |
| 66 | + "filename=\"${imagePath.substringAfterLast("/")}\"" |
| 67 | + ) |
| 68 | + append(HttpHeaders.Authorization, "Bearer $token") |
| 69 | + } |
| 70 | + ) |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + if (response.status == HttpStatusCode.Created) { |
| 75 | + ResultData.Complete(response.body<ImageUploadRequest>()) |
| 76 | + } else { |
| 77 | + ResultData.Error() |
| 78 | + } |
| 79 | + } catch (e: Exception) { |
| 80 | + ResultData.Error(e) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +private fun detectContentType(path: String): ContentType { |
| 85 | + val extension = path.substringAfterLast(".", "").lowercase() |
| 86 | + return when (extension) { |
| 87 | + "jpg", "jpeg" -> ContentType.Image.JPEG |
| 88 | + "png" -> ContentType.Image.PNG |
| 89 | + "gif" -> ContentType.Image.GIF |
| 90 | + "webp" -> ContentType.Image.Any |
| 91 | + "svg" -> ContentType.Image.SVG |
| 92 | + else -> ContentType.Application.OctetStream // Fallback for unknown types |
| 93 | + } |
| 94 | +} |
0 commit comments