From 037bbd9da6c36fd160cc25794a8d854f7cd013da Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:39:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20LocalFileSystemMediaDataStore.kt?= =?UTF-8?q?=E3=81=AB=E3=83=AD=E3=82=B0=E3=81=A8=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/LocalFileSystemMediaDataStore.kt | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt index 0e0ef8cb..3102fea5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt @@ -2,6 +2,7 @@ package dev.usbharu.hideout.core.service.media import dev.usbharu.hideout.application.config.ApplicationConfig import dev.usbharu.hideout.application.config.LocalStorageConfig +import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import java.nio.file.Path import kotlin.io.path.copyTo @@ -45,12 +46,22 @@ class LocalFileSystemMediaDataStore( } override suspend fun save(dataSaveRequest: MediaSaveRequest): SavedMedia { + logger.info("START Media upload. {}", dataSaveRequest.name) val fileSavePath = buildSavePath(savePath, dataSaveRequest.name) val thumbnailSavePath = buildSavePath(savePath, "thumbnail-" + dataSaveRequest.name) - dataSaveRequest.filePath.copyTo(fileSavePath) - dataSaveRequest.thumbnailPath?.copyTo(thumbnailSavePath) + val fileSavePathString = fileSavePath.toAbsolutePath().toString() + logger.info("MEDIA save. path: {}", fileSavePathString) + try { + dataSaveRequest.filePath.copyTo(fileSavePath) + dataSaveRequest.thumbnailPath?.copyTo(thumbnailSavePath) + } catch (e: Exception) { + logger.warn("FAILED to Save the media.", e) + return FaildSavedMedia("FAILED to Save the media.", "Failed copy to path: $fileSavePathString", e) + } + + logger.info("SUCCESS Media upload. {}", dataSaveRequest.name) return SuccessSavedMedia( dataSaveRequest.name, publicUrl + dataSaveRequest.name, @@ -64,9 +75,18 @@ class LocalFileSystemMediaDataStore( * @param id 削除するメディアのid [SuccessSavedMedia.name]を指定します。 */ override suspend fun delete(id: String) { - buildSavePath(savePath, id).deleteIfExists() - buildSavePath(savePath, "thumbnail-$id").deleteIfExists() + logger.info("START Media delete. id: {}", id) + try { + buildSavePath(savePath, id).deleteIfExists() + buildSavePath(savePath, "thumbnail-$id").deleteIfExists() + } catch (e: Exception) { + logger.warn("FAILED Media delete. id: {}", id, e) + } } private fun buildSavePath(savePath: Path, name: String): Path = savePath.resolve(name) + + companion object { + private val logger = LoggerFactory.getLogger(LocalFileSystemMediaDataStore::class.java) + } }