#13 Update FileController to use new HTTP OK util function

This commit is contained in:
William Nolin 2021-09-01 17:59:53 -04:00
parent a02099387e
commit e1ca6a8d83
2 changed files with 6 additions and 16 deletions

View File

@ -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<Resource> {
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<Void> {
return noContent {
fun delete(@RequestParam path: String): ResponseEntity<Void> =
noContent {
fileService.delete(path)
}
}
private fun created(path: String): ResponseEntity<Void> =
ResponseEntity

View File

@ -26,13 +26,13 @@ fun ok(action: () -> Unit): ResponseEntity<Void> {
return ResponseEntity.ok().build()
}
fun ok(file: Resource, mediaType: String? = null): ResponseEntity<Resource> {
return ResponseEntity.ok()
/** Creates a HTTP OK [ResponseEntity] for the given [file], with the given [mediaType]. */
fun ok(file: Resource, mediaType: String? = null): ResponseEntity<Resource> =
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 <T : Model> created(controllerPath: String, body: T): ResponseEntity<T> =