From ab44ecfef6e93fac17e55c8f707615a6b7197c64 Mon Sep 17 00:00:00 2001 From: neviyn Date: Thu, 29 Apr 2021 11:32:23 +0100 Subject: [PATCH] Added route for serving images from known disk location --- pom.xml | 4 ++ .../co/neviyn/booru/ConfigurationProperty.kt | 42 +++++++++++++++++++ .../uk/co/neviyn/booru/StaticResource.kt | 17 ++++++++ src/main/resources/application.properties | 3 +- src/test/resources/application.properties | 3 +- 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/uk/co/neviyn/booru/ConfigurationProperty.kt create mode 100644 src/main/kotlin/uk/co/neviyn/booru/StaticResource.kt diff --git a/pom.xml b/pom.xml index 97ba9e8..dfba679 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + com.fasterxml.jackson.module jackson-module-kotlin diff --git a/src/main/kotlin/uk/co/neviyn/booru/ConfigurationProperty.kt b/src/main/kotlin/uk/co/neviyn/booru/ConfigurationProperty.kt new file mode 100644 index 0000000..e41139e --- /dev/null +++ b/src/main/kotlin/uk/co/neviyn/booru/ConfigurationProperty.kt @@ -0,0 +1,42 @@ +package uk.co.neviyn.booru + +import com.sun.istack.NotNull +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.context.annotation.Configuration +import org.springframework.validation.annotation.Validated +import java.io.File +import javax.validation.Constraint +import javax.validation.ConstraintValidator +import javax.validation.ConstraintValidatorContext +import javax.validation.Payload +import javax.validation.constraints.NotEmpty +import kotlin.reflect.KClass + + +@Configuration +@ConfigurationProperties(prefix = "images") +@Validated +class ImageConfigurationProperties { + @NotNull + @IsValidDirectory + var directory: File = File("") + + @NotEmpty + var types: List = listOf("png", "jpg") +} + +class DirectoryValidator : ConstraintValidator { + override fun isValid(value: File?, context: ConstraintValidatorContext?): Boolean { + if (value == null) return false + return value.exists() && value.isDirectory && value.canRead() && value.canWrite() + } +} + +@MustBeDocumented +@Constraint(validatedBy = [DirectoryValidator::class]) +@Target(AnnotationTarget.FIELD) +annotation class IsValidDirectory( + val message: String = "Invalid image directory", + val groups: Array> = [], + val payload: Array> = [] +) \ No newline at end of file diff --git a/src/main/kotlin/uk/co/neviyn/booru/StaticResource.kt b/src/main/kotlin/uk/co/neviyn/booru/StaticResource.kt new file mode 100644 index 0000000..cd338f9 --- /dev/null +++ b/src/main/kotlin/uk/co/neviyn/booru/StaticResource.kt @@ -0,0 +1,17 @@ +package uk.co.neviyn.booru + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.annotation.Configuration +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer + +@Configuration +class StaticResourceConfiguration @Autowired constructor(val imageConfigurationProperties: ImageConfigurationProperties) : WebMvcConfigurer { + override fun addResourceHandlers(registry: ResourceHandlerRegistry) { + // Mapping of /i endpoint to folder on disk that contains images + registry + .addResourceHandler(*(imageConfigurationProperties.types.map { "/i/*.$it" }.toTypedArray())) + .addResourceLocations(imageConfigurationProperties.directory.toURI().toString()) + super.addResourceHandlers(registry) + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index cc119c6..ac9feb4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,4 @@ spring.jpa.hibernate.ddl-auto=none spring.datasource.initialization-mode=always -spring.jpa.properties.hibernate.default_schema=booru \ No newline at end of file +spring.jpa.properties.hibernate.default_schema=booru +images.types=jpg,png,gif \ No newline at end of file diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 58dd511..97d3152 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -2,4 +2,5 @@ spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.flyway.enabled=false spring.jpa.hibernate.ddl-auto=update -spring.jpa.properties.hibernate.default_schema=booru \ No newline at end of file +spring.jpa.properties.hibernate.default_schema= +images.directory=./images \ No newline at end of file