Created
November 11, 2022 11:12
-
-
Save migueldoctor/fad118f911767a88f8beb2560c86ab34 to your computer and use it in GitHub Desktop.
End points for the application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kesizo.demo.services.uploadfilesservice.controller | |
import com.kesizo.demo.services.uploadfilesservice.data.Response | |
import com.kesizo.demo.services.uploadfilesservice.service.UploadFileService | |
import mu.KotlinLogging | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.core.io.Resource | |
import org.springframework.http.HttpHeaders | |
import org.springframework.http.HttpStatus | |
import org.springframework.http.ResponseEntity | |
import org.springframework.web.bind.annotation.* | |
import org.springframework.web.multipart.MultipartFile | |
@RestController | |
@RequestMapping("api/") | |
class UploadFileController { | |
private val log = KotlinLogging.logger {} | |
// 1) We inject the Service dependency | |
@Autowired | |
lateinit var uploadFileService:UploadFileService | |
// 2) We define the HTTP Post operation with the file as parameter | |
@PostMapping("/upload-file") | |
fun uploadFile(@RequestParam("file") file: MultipartFile): ResponseEntity<Response> { | |
log.info { "Requested uploading file: ${file.originalFilename}" } | |
// It invokes the service operation upload and depending on if it went well | |
// the file is stored on the file system or an error message is sent back to the user | |
return when(uploadFileService.upload(file).getOrDefault(false)) { | |
true -> ResponseEntity.status(HttpStatus.OK).body( | |
Response(message = "File upload successfully: ${file.originalFilename}")) | |
else -> ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body( | |
Response(message = "Error when trying to upload the file: ${file.originalFilename} !")) | |
} | |
} | |
//3) We define the HTTP Get method to serve the file or a message indicating the file doesn't exist | |
@GetMapping("/download/{filename:.+}") | |
@ResponseBody | |
fun getFile(@PathVariable filename: String): ResponseEntity<out Any> { | |
log.info { "Requested downloading the file: $filename" } | |
return when (val res = uploadFileService.download(filename).getOrNull()) { | |
is Resource -> ResponseEntity.status(HttpStatus.OK) | |
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=${filename}") | |
.body<Resource>(res) | |
else -> ResponseEntity.status(HttpStatus.EXPECTATION_FAILED) | |
.body(Response(message = "Error when trying to download the requested resource: $filename !")) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment