From 21e91e58a7e1db7f01448c3a64bb8d04b2a62af7 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:31:50 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=83=AA=E3=82=BD=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E3=81=AE=E3=82=AF=E3=83=AD=E3=83=BC=E3=82=BA=E3=82=92=E8=A1=8C?= =?UTF-8?q?=E3=81=86=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../media/LocalFileSystemMediaDataStore.kt | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 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 3102fea5..420e82bf 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 @@ -5,7 +5,9 @@ import dev.usbharu.hideout.application.config.LocalStorageConfig import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import java.nio.file.Path +import java.nio.file.StandardOpenOption import kotlin.io.path.copyTo +import kotlin.io.path.createDirectories import kotlin.io.path.deleteIfExists import kotlin.io.path.outputStream @@ -20,28 +22,43 @@ import kotlin.io.path.outputStream * @param localStorageConfig LocalStorageConfig */ class LocalFileSystemMediaDataStore( - applicationConfig: ApplicationConfig, - localStorageConfig: LocalStorageConfig + applicationConfig: ApplicationConfig, localStorageConfig: LocalStorageConfig ) : MediaDataStore { - private val savePath: Path = Path.of(localStorageConfig.path) + private val savePath: Path = Path.of(localStorageConfig.path).toAbsolutePath() private val publicUrl = localStorageConfig.publicUrl ?: "${applicationConfig.url}/files/" + init { + savePath.createDirectories() + } + override suspend fun save(dataMediaSave: MediaSave): SavedMedia { val fileSavePath = buildSavePath(savePath, dataMediaSave.name) val thumbnailSavePath = buildSavePath(savePath, "thumbnail-" + dataMediaSave.name) + dataMediaSave.thumbnailInputStream?.inputStream()?.use { + it.buffered().use { bufferedInputStream -> + thumbnailSavePath.outputStream(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) + .use { outputStream -> + outputStream.buffered().use { + bufferedInputStream.transferTo(it) + } + } + } + } - dataMediaSave.thumbnailInputStream?.inputStream()?.buffered() - ?.transferTo(thumbnailSavePath.outputStream().buffered()) - dataMediaSave.fileInputStream.inputStream().buffered().transferTo(fileSavePath.outputStream().buffered()) + + dataMediaSave.fileInputStream.inputStream().use { + it.buffered().use { bufferedInputStream -> + fileSavePath.outputStream(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) + .use { outputStream -> outputStream.buffered().use { bufferedInputStream.transferTo(it) } } + } + } return SuccessSavedMedia( - dataMediaSave.name, - publicUrl + dataMediaSave.name, - publicUrl + "thumbnail-" + dataMediaSave.name + dataMediaSave.name, publicUrl + dataMediaSave.name, publicUrl + "thumbnail-" + dataMediaSave.name ) } @@ -63,9 +80,7 @@ class LocalFileSystemMediaDataStore( logger.info("SUCCESS Media upload. {}", dataSaveRequest.name) return SuccessSavedMedia( - dataSaveRequest.name, - publicUrl + dataSaveRequest.name, - publicUrl + "thumbnail-" + dataSaveRequest.name + dataSaveRequest.name, publicUrl + dataSaveRequest.name, publicUrl + "thumbnail-" + dataSaveRequest.name ) }