fix: リソースのクローズを行うように

This commit is contained in:
usbharu 2023-12-06 15:31:50 +09:00
parent 037bbd9da6
commit 21e91e58a7
1 changed files with 27 additions and 12 deletions

View File

@ -5,7 +5,9 @@ import dev.usbharu.hideout.application.config.LocalStorageConfig
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.StandardOpenOption
import kotlin.io.path.copyTo import kotlin.io.path.copyTo
import kotlin.io.path.createDirectories
import kotlin.io.path.deleteIfExists import kotlin.io.path.deleteIfExists
import kotlin.io.path.outputStream import kotlin.io.path.outputStream
@ -20,28 +22,43 @@ import kotlin.io.path.outputStream
* @param localStorageConfig LocalStorageConfig * @param localStorageConfig LocalStorageConfig
*/ */
class LocalFileSystemMediaDataStore( class LocalFileSystemMediaDataStore(
applicationConfig: ApplicationConfig, applicationConfig: ApplicationConfig, localStorageConfig: LocalStorageConfig
localStorageConfig: LocalStorageConfig
) : MediaDataStore { ) : 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/" private val publicUrl = localStorageConfig.publicUrl ?: "${applicationConfig.url}/files/"
init {
savePath.createDirectories()
}
override suspend fun save(dataMediaSave: MediaSave): SavedMedia { override suspend fun save(dataMediaSave: MediaSave): SavedMedia {
val fileSavePath = buildSavePath(savePath, dataMediaSave.name) val fileSavePath = buildSavePath(savePath, dataMediaSave.name)
val thumbnailSavePath = buildSavePath(savePath, "thumbnail-" + 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().use {
dataMediaSave.fileInputStream.inputStream().buffered().transferTo(fileSavePath.outputStream().buffered()) it.buffered().use { bufferedInputStream ->
fileSavePath.outputStream(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)
.use { outputStream -> outputStream.buffered().use { bufferedInputStream.transferTo(it) } }
}
}
return SuccessSavedMedia( return SuccessSavedMedia(
dataMediaSave.name, dataMediaSave.name, publicUrl + dataMediaSave.name, publicUrl + "thumbnail-" + dataMediaSave.name
publicUrl + dataMediaSave.name,
publicUrl + "thumbnail-" + dataMediaSave.name
) )
} }
@ -63,9 +80,7 @@ class LocalFileSystemMediaDataStore(
logger.info("SUCCESS Media upload. {}", dataSaveRequest.name) logger.info("SUCCESS Media upload. {}", dataSaveRequest.name)
return SuccessSavedMedia( return SuccessSavedMedia(
dataSaveRequest.name, dataSaveRequest.name, publicUrl + dataSaveRequest.name, publicUrl + "thumbnail-" + dataSaveRequest.name
publicUrl + dataSaveRequest.name,
publicUrl + "thumbnail-" + dataSaveRequest.name
) )
} }