feat: メディアのリネームを別クラスに切り出し

This commit is contained in:
usbharu 2023-11-15 20:50:03 +09:00
parent c9d802dae7
commit 477d12fa61
4 changed files with 50 additions and 3 deletions

View File

@ -0,0 +1,14 @@
package dev.usbharu.hideout.core.service.media
interface MediaFileRenameService {
/**
* メディアをリネームします
*
* @param uploadName アップロードされた時点でのファイル名
* @param uploadMimeType アップロードされた時点でのMimeType
* @param processedName 処理後のファイル名
* @param processedMimeType 処理後のMimeType
* @return リネーム後のファイル名
*/
fun rename(uploadName: String, uploadMimeType: MimeType, processedName: String, processedMimeType: MimeType): String
}

View File

@ -21,7 +21,8 @@ open class MediaServiceImpl(
private val mediaBlurhashService: MediaBlurhashService,
private val mediaRepository: MediaRepository,
private val mediaProcessServices: List<MediaProcessService>,
private val remoteMediaDownloadService: RemoteMediaDownloadService
private val remoteMediaDownloadService: RemoteMediaDownloadService,
private val renameService: MediaFileRenameService
) : MediaService {
override suspend fun uploadLocalMedia(mediaRequest: MediaRequest): EntityMedia {
val fileName = mediaRequest.file.name
@ -50,7 +51,12 @@ open class MediaServiceImpl(
)
val dataMediaSave = MediaSaveRequest(
renameService.rename(
mediaRequest.file.name,
mimeType,
process.filePath.fileName.toString(),
process.fileMimeType
),
"",
process.filePath,
process.thumbnailPath
@ -101,7 +107,12 @@ open class MediaServiceImpl(
val process = findMediaProcessor(mimeType).process(mimeType, remoteMedia.name, it.path, null)
val mediaSaveRequest = MediaSaveRequest(
renameService.rename(
remoteMedia.name,
mimeType,
process.filePath.fileName.toString(),
process.fileMimeType
),
"",
process.filePath,
process.thumbnailPath

View File

@ -0,0 +1,16 @@
package dev.usbharu.hideout.core.service.media
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import java.util.*
@Qualifier("uuid")
@Service
class UUIDMediaFileRenameService : MediaFileRenameService {
override fun rename(
uploadName: String,
uploadMimeType: MimeType,
processedName: String,
processedMimeType: MimeType
): String = "${UUID.randomUUID()}.${uploadMimeType.subtype}.${processedMimeType.subtype}"
}

View File

@ -10,6 +10,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.slf4j.MDCContext
import kotlinx.coroutines.withContext
import net.coobird.thumbnailator.Thumbnails
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import java.nio.file.Files
@ -83,6 +84,7 @@ class ImageMediaProcessService(private val imageMediaProcessorConfiguration: Ima
tempFile.outputStream().use {
if (ImageIO.write(bufferedImage, convertType, it).not()) {
logger.warn("Failed to save a temporary file. type: {} ,path: {}", convertType, tempFile)
throw MediaProcessException("Failed to save a temporary file.")
}
}
@ -93,4 +95,8 @@ class ImageMediaProcessService(private val imageMediaProcessorConfiguration: Ima
MimeType("image", convertType, FileType.Image).takeIf { genThumbnail }
)
}
companion object {
private val logger = LoggerFactory.getLogger(ImageMediaProcessService::class.java)
}
}