mirror of https://github.com/usbharu/Hideout.git
feat: メディアのリネームを別クラスに切り出し
This commit is contained in:
parent
c9d802dae7
commit
477d12fa61
|
@ -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
|
||||||
|
}
|
|
@ -21,7 +21,8 @@ open class MediaServiceImpl(
|
||||||
private val mediaBlurhashService: MediaBlurhashService,
|
private val mediaBlurhashService: MediaBlurhashService,
|
||||||
private val mediaRepository: MediaRepository,
|
private val mediaRepository: MediaRepository,
|
||||||
private val mediaProcessServices: List<MediaProcessService>,
|
private val mediaProcessServices: List<MediaProcessService>,
|
||||||
private val remoteMediaDownloadService: RemoteMediaDownloadService
|
private val remoteMediaDownloadService: RemoteMediaDownloadService,
|
||||||
|
private val renameService: MediaFileRenameService
|
||||||
) : MediaService {
|
) : MediaService {
|
||||||
override suspend fun uploadLocalMedia(mediaRequest: MediaRequest): EntityMedia {
|
override suspend fun uploadLocalMedia(mediaRequest: MediaRequest): EntityMedia {
|
||||||
val fileName = mediaRequest.file.name
|
val fileName = mediaRequest.file.name
|
||||||
|
@ -50,7 +51,12 @@ open class MediaServiceImpl(
|
||||||
)
|
)
|
||||||
|
|
||||||
val dataMediaSave = MediaSaveRequest(
|
val dataMediaSave = MediaSaveRequest(
|
||||||
|
renameService.rename(
|
||||||
|
mediaRequest.file.name,
|
||||||
|
mimeType,
|
||||||
process.filePath.fileName.toString(),
|
process.filePath.fileName.toString(),
|
||||||
|
process.fileMimeType
|
||||||
|
),
|
||||||
"",
|
"",
|
||||||
process.filePath,
|
process.filePath,
|
||||||
process.thumbnailPath
|
process.thumbnailPath
|
||||||
|
@ -101,7 +107,12 @@ open class MediaServiceImpl(
|
||||||
val process = findMediaProcessor(mimeType).process(mimeType, remoteMedia.name, it.path, null)
|
val process = findMediaProcessor(mimeType).process(mimeType, remoteMedia.name, it.path, null)
|
||||||
|
|
||||||
val mediaSaveRequest = MediaSaveRequest(
|
val mediaSaveRequest = MediaSaveRequest(
|
||||||
|
renameService.rename(
|
||||||
|
remoteMedia.name,
|
||||||
|
mimeType,
|
||||||
process.filePath.fileName.toString(),
|
process.filePath.fileName.toString(),
|
||||||
|
process.fileMimeType
|
||||||
|
),
|
||||||
"",
|
"",
|
||||||
process.filePath,
|
process.filePath,
|
||||||
process.thumbnailPath
|
process.thumbnailPath
|
||||||
|
|
|
@ -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}"
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.slf4j.MDCContext
|
import kotlinx.coroutines.slf4j.MDCContext
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import net.coobird.thumbnailator.Thumbnails
|
import net.coobird.thumbnailator.Thumbnails
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.beans.factory.annotation.Qualifier
|
import org.springframework.beans.factory.annotation.Qualifier
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
@ -83,6 +84,7 @@ class ImageMediaProcessService(private val imageMediaProcessorConfiguration: Ima
|
||||||
|
|
||||||
tempFile.outputStream().use {
|
tempFile.outputStream().use {
|
||||||
if (ImageIO.write(bufferedImage, convertType, it).not()) {
|
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.")
|
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 }
|
MimeType("image", convertType, FileType.Image).takeIf { genThumbnail }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val logger = LoggerFactory.getLogger(ImageMediaProcessService::class.java)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue