From 8c1becc504a38726046d20714f4dadab294a70fd Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 2 Nov 2023 19:34:07 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=AA=E3=83=A2=E3=83=BC=E3=83=88?= =?UTF-8?q?=E3=81=AE=E3=83=A1=E3=83=87=E3=82=A3=E3=82=A2=E3=82=92=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=82=AB=E3=83=AB=E3=81=AB=E4=BF=9D=E5=AD=98=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/service/media/MediaServiceImpl.kt | 58 +++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/media/MediaServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/media/MediaServiceImpl.kt index d439099d..aefbc05e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/media/MediaServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/media/MediaServiceImpl.kt @@ -8,6 +8,10 @@ import dev.usbharu.hideout.core.domain.model.media.MediaRepository import dev.usbharu.hideout.core.service.media.converter.MediaProcessService import dev.usbharu.hideout.mastodon.interfaces.api.media.MediaRequest import io.ktor.client.* +import io.ktor.client.request.* +import io.ktor.client.statement.* +import io.ktor.http.* +import io.ktor.util.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.slf4j.LoggerFactory @@ -96,15 +100,61 @@ class MediaServiceImpl( override suspend fun uploadRemoteMedia(remoteMedia: RemoteMedia): Media { logger.info("MEDIA Remote media. filename:${remoteMedia.name} url:${remoteMedia.url}") + val httpResponse = httpClient.get(remoteMedia.url) + val bytes = httpResponse.bodyAsChannel().toByteArray() + + val contentType = httpResponse.contentType()?.toString() + val fileType = + fileTypeDeterminationService.fileType(bytes, remoteMedia.name, contentType) + + if (fileType != FileType.Image) { + throw UnsupportedMediaException("FileType: $fileType is not supported.") + } + + val processedMedia = mediaProcessService.process( + fileType = fileType, + contentType = contentType.orEmpty(), + fileName = remoteMedia.name, + file = bytes, + thumbnail = null + ) + + val mediaSave = MediaSave( + "${UUID.randomUUID()}.${processedMedia.file.extension}", + "", + processedMedia.file.byteArray, + processedMedia.thumbnail?.byteArray + ) + + val save = try { + mediaDataStore.save(mediaSave) + } catch (e: Exception) { + logger.warn("Failed save media", e) + throw MediaSaveException("Failed save media.", e) + } + + if (save.success.not()) { + save as FaildSavedMedia + logger.warn("Failed save media. reason: ${save.reason}") + logger.warn(save.description, save.trace) + throw MediaSaveException("Failed save media.") + } + save as SuccessSavedMedia + + val blurhash = withContext(Dispatchers.IO) { + mediaBlurhashService.generateBlurhash(ImageIO.read(bytes.inputStream())) + } + + return mediaRepository.save( EntityMedia( id = mediaRepository.generateId(), name = remoteMedia.name, - url = remoteMedia.url, + url = save.url, remoteUrl = remoteMedia.url, - thumbnailUrl = remoteMedia.url, - type = FileType.Image, - blurHash = null + thumbnailUrl = save.thumbnailUrl, + type = fileType, + blurHash = blurhash ) ) }