refactor: LocalFileSystemMediaDataStore.ktにログとエラーハンドリングを追加

This commit is contained in:
usbharu 2023-12-06 14:39:31 +09:00
parent 9cdbe195a6
commit 037bbd9da6
1 changed files with 24 additions and 4 deletions

View File

@ -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)
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) {
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)
}
}