feat: リモートのメディアのファイルサイズ制限ができるように

This commit is contained in:
usbharu 2024-01-25 13:20:27 +09:00
parent 99081d6483
commit 5db462d9d8
4 changed files with 55 additions and 3 deletions

View File

@ -0,0 +1,8 @@
package dev.usbharu.hideout.application.config
import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties("hideout.media")
data class MediaConfig(
val remoteMediaFileSizeLimit: Long = 3000000L
)

View File

@ -0,0 +1,21 @@
package dev.usbharu.hideout.core.domain.exception.media
import java.io.Serial
class RemoteMediaFileSizeException : MediaFileSizeException {
constructor() : super()
constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause)
constructor(message: String?, cause: Throwable?, enableSuppression: Boolean, writableStackTrace: Boolean) : super(
message,
cause,
enableSuppression,
writableStackTrace
)
companion object {
@Serial
private const val serialVersionUID: Long = 9188247721397839435L
}
}

View File

@ -1,5 +1,7 @@
package dev.usbharu.hideout.core.service.media
import dev.usbharu.hideout.application.config.MediaConfig
import dev.usbharu.hideout.core.domain.exception.media.RemoteMediaFileSizeException
import dev.usbharu.hideout.core.service.resource.KtorResourceResolveService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@ -8,7 +10,10 @@ import java.nio.file.Path
import kotlin.io.path.outputStream
@Service
class RemoteMediaDownloadServiceImpl(private val resourceResolveService: KtorResourceResolveService) :
class RemoteMediaDownloadServiceImpl(
private val resourceResolveService: KtorResourceResolveService,
private val mediaConfig: MediaConfig
) :
RemoteMediaDownloadService {
override suspend fun download(url: String): Path {
logger.info("START Download remote file. url: {}", url)
@ -23,6 +28,11 @@ class RemoteMediaDownloadServiceImpl(private val resourceResolveService: KtorRes
}
}
val contentLength = createTempFile.toFile().length()
if (contentLength >= mediaConfig.remoteMediaFileSizeLimit) {
throw RemoteMediaFileSizeException("File size is too large. $contentLength >= ${mediaConfig.remoteMediaFileSizeLimit}")
}
logger.info("SUCCESS Download remote file. url: {}", url)
return createTempFile
}

View File

@ -1,12 +1,22 @@
package dev.usbharu.hideout.core.service.resource
import dev.usbharu.hideout.application.config.MediaConfig
import dev.usbharu.hideout.core.domain.exception.media.RemoteMediaFileSizeException
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.http.*
import org.springframework.stereotype.Service
@Service
open class KtorResourceResolveService(private val httpClient: HttpClient, private val cacheManager: CacheManager) :
open class KtorResourceResolveService(
private val httpClient: HttpClient,
private val cacheManager: CacheManager,
private val mediaConfig: MediaConfig
) :
ResourceResolveService {
var sizeLimit = mediaConfig.remoteMediaFileSizeLimit
override suspend fun resolve(url: String): ResolveResponse {
cacheManager.putCache(getCacheKey(url)) {
runResolve(url)
@ -16,7 +26,10 @@ open class KtorResourceResolveService(private val httpClient: HttpClient, privat
protected suspend fun runResolve(url: String): ResolveResponse {
val httpResponse = httpClient.get(url)
val contentLength = httpResponse.contentLength()
if ((contentLength ?: sizeLimit) >= sizeLimit) {
throw RemoteMediaFileSizeException("File size is too large. $contentLength >= $sizeLimit")
}
return KtorResolveResponse(httpResponse)
}