diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/FileController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/FileController.kt new file mode 100644 index 00000000..57e372d4 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/FileController.kt @@ -0,0 +1,68 @@ +package com.wafflestudio.csereal.core.resource.mainImage.api + +import jakarta.servlet.http.HttpServletRequest +import org.springframework.beans.factory.annotation.Value +import org.springframework.core.io.Resource +import org.springframework.core.io.UrlResource +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.* +import java.net.URLEncoder +import java.nio.file.Files +import java.nio.file.Paths +import kotlin.text.Charsets.UTF_8 + + +@RequestMapping("/file") +@RestController +class FileController( + @Value("\${csereal.upload.path}") + private val uploadPath: String +) { + + @GetMapping("/{filename:.+}") + fun serveFile( + @PathVariable filename: String, + @RequestParam(defaultValue = "false") download: Boolean, + request: HttpServletRequest + ): ResponseEntity { + val file = Paths.get(uploadPath, filename) + val resource = UrlResource(file.toUri()) + + if (resource.exists() || resource.isReadable) { + val contentType: String? = request.servletContext.getMimeType(resource.file.absolutePath) + val headers = HttpHeaders() + + headers.contentType = + org.springframework.http.MediaType.parseMediaType(contentType ?: "application/octet-stream") + + if (download) { + val originalFilename = filename.substringAfter("_") + + val encodedFilename = URLEncoder.encode(originalFilename, UTF_8.toString()).replace("+", "%20") + + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''$encodedFilename") + } + + return ResponseEntity.ok() + .headers(headers) + .body(resource) + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build() + } + } + + @DeleteMapping("/{filename:.+}") + fun deleteFile(@PathVariable filename: String): ResponseEntity { + val file = Paths.get(uploadPath, filename) + + if (Files.exists(file)) { + Files.delete(file) + return ResponseEntity.status(HttpStatus.NO_CONTENT).build() + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("파일을 찾을 수 없습니다.") + } + } + +} diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/MainImageController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/MainImageController.kt deleted file mode 100644 index e74a26da..00000000 --- a/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/MainImageController.kt +++ /dev/null @@ -1,13 +0,0 @@ -package com.wafflestudio.csereal.core.resource.mainImage.api - -import com.wafflestudio.csereal.core.resource.mainImage.service.ImageService -import org.springframework.web.bind.annotation.* - - -@RequestMapping("/image") -@RestController -class MainImageController( - private val imageService: ImageService -) { - -} \ No newline at end of file diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/service/MainImageService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/service/MainImageService.kt index c6d769e5..c6e4c138 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/service/MainImageService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/service/MainImageService.kt @@ -58,11 +58,11 @@ class ImageServiceImpl( val filename = "${timeMillis}_${requestImage.originalFilename}" val totalFilename = path + filename - val saveFile = Paths.get("$totalFilename.$extension") + val saveFile = Paths.get(totalFilename) requestImage.transferTo(saveFile) val totalThumbnailFilename = "${path}thumbnail_$filename" - val thumbnailFile = Paths.get("$totalThumbnailFilename.$extension") + val thumbnailFile = Paths.get(totalThumbnailFilename) Thumbnailator.createThumbnail(saveFile.toFile(), thumbnailFile.toFile(), 100, 100); val image = MainImageEntity( @@ -91,7 +91,7 @@ class ImageServiceImpl( @Transactional override fun createImageURL(image: MainImageEntity?): String? { return if (image != null) { - "${endpointProperties.backend}/image/${image.filename}" + "${endpointProperties.backend}/file/${image.filename}" } else null }