diff --git a/src/main/kotlin/dev/usbharu/hideout/application/config/MediaConfig.kt b/src/main/kotlin/dev/usbharu/hideout/application/config/MediaConfig.kt new file mode 100644 index 00000000..cf34e6ca --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/MediaConfig.kt @@ -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 +) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/domain/exception/media/RemoteMediaFileSizeException.kt b/src/main/kotlin/dev/usbharu/hideout/core/domain/exception/media/RemoteMediaFileSizeException.kt new file mode 100644 index 00000000..16c50cd8 --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/domain/exception/media/RemoteMediaFileSizeException.kt @@ -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 + } +} diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/media/RemoteMediaDownloadServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/media/RemoteMediaDownloadServiceImpl.kt index bd1014f8..e9e96645 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/media/RemoteMediaDownloadServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/media/RemoteMediaDownloadServiceImpl.kt @@ -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 } diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/resource/KtorResourceResolveService.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/resource/KtorResourceResolveService.kt index 4cd99bc8..06f04d7e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/resource/KtorResourceResolveService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/resource/KtorResourceResolveService.kt @@ -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) }