Added route for serving images from known disk location
This commit is contained in:
parent
89b798e32d
commit
ab44ecfef6
4
pom.xml
4
pom.xml
@ -34,6 +34,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.module</groupId>
|
<groupId>com.fasterxml.jackson.module</groupId>
|
||||||
<artifactId>jackson-module-kotlin</artifactId>
|
<artifactId>jackson-module-kotlin</artifactId>
|
||||||
|
42
src/main/kotlin/uk/co/neviyn/booru/ConfigurationProperty.kt
Normal file
42
src/main/kotlin/uk/co/neviyn/booru/ConfigurationProperty.kt
Normal file
@ -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<String> = listOf("png", "jpg")
|
||||||
|
}
|
||||||
|
|
||||||
|
class DirectoryValidator : ConstraintValidator<IsValidDirectory, File> {
|
||||||
|
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<KClass<*>> = [],
|
||||||
|
val payload: Array<KClass<out Payload>> = []
|
||||||
|
)
|
17
src/main/kotlin/uk/co/neviyn/booru/StaticResource.kt
Normal file
17
src/main/kotlin/uk/co/neviyn/booru/StaticResource.kt
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
spring.jpa.hibernate.ddl-auto=none
|
spring.jpa.hibernate.ddl-auto=none
|
||||||
spring.datasource.initialization-mode=always
|
spring.datasource.initialization-mode=always
|
||||||
spring.jpa.properties.hibernate.default_schema=booru
|
spring.jpa.properties.hibernate.default_schema=booru
|
||||||
|
images.types=jpg,png,gif
|
@ -2,4 +2,5 @@ spring.datasource.url=jdbc:h2:mem:testdb
|
|||||||
spring.datasource.driverClassName=org.h2.Driver
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
spring.flyway.enabled=false
|
spring.flyway.enabled=false
|
||||||
spring.jpa.hibernate.ddl-auto=update
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
spring.jpa.properties.hibernate.default_schema=booru
|
spring.jpa.properties.hibernate.default_schema=
|
||||||
|
images.directory=./images
|
Loading…
Reference in New Issue
Block a user