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) + } }