diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/FileController.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/FileController.kt index c7879d6..b4cfd14 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/FileController.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/FileController.kt @@ -3,8 +3,6 @@ package dev.fyloz.colorrecipesexplorer.rest import dev.fyloz.colorrecipesexplorer.model.ConfigurationType import dev.fyloz.colorrecipesexplorer.service.config.ConfigurationService import dev.fyloz.colorrecipesexplorer.service.files.WriteableFileService -import org.springframework.core.io.ByteArrayResource -import org.springframework.core.io.Resource import org.springframework.http.MediaType import org.springframework.http.ResponseEntity import org.springframework.security.access.prepost.PreAuthorize @@ -24,14 +22,7 @@ class FileController( fun upload( @RequestParam path: String, @RequestParam(required = false) mediaType: String? - ): ResponseEntity { - val file = fileService.read(path) - return ResponseEntity.ok() - .header("Content-Disposition", "filename=${getFileNameFromPath(path)}") - .contentLength(file.contentLength()) - .contentType(MediaType.parseMediaType(mediaType ?: DEFAULT_MEDIA_TYPE)) - .body(file) - } + ) = ok(fileService.read(path), mediaType) @PutMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE]) @PreAuthorize("hasAnyAuthority('WRITE_FILE')") @@ -46,11 +37,10 @@ class FileController( @DeleteMapping @PreAuthorize("hasAnyAuthority('WRITE_FILE')") - fun delete(@RequestParam path: String): ResponseEntity { - return noContent { + fun delete(@RequestParam path: String): ResponseEntity = + noContent { fileService.delete(path) } - } private fun created(path: String): ResponseEntity = ResponseEntity diff --git a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/RestUtils.kt b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/RestUtils.kt index 36b892e..23f115d 100644 --- a/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/RestUtils.kt +++ b/src/main/kotlin/dev/fyloz/colorrecipesexplorer/rest/RestUtils.kt @@ -26,13 +26,13 @@ fun ok(action: () -> Unit): ResponseEntity { return ResponseEntity.ok().build() } -fun ok(file: Resource, mediaType: String? = null): ResponseEntity { - return ResponseEntity.ok() +/** Creates a HTTP OK [ResponseEntity] for the given [file], with the given [mediaType]. */ +fun ok(file: Resource, mediaType: String? = null): ResponseEntity = + ResponseEntity.ok() .header("Content-Disposition", "filename=${file.filename}") .contentLength(file.contentLength()) .contentType(MediaType.parseMediaType(mediaType ?: DEFAULT_MEDIA_TYPE)) .body(file) -} /** Creates a HTTP CREATED [ResponseEntity] from the given [body] with the location set to [controllerPath]/id. */ fun created(controllerPath: String, body: T): ResponseEntity =