From 049873779682225ddf2a89469f133218a6d51e69 Mon Sep 17 00:00:00 2001 From: huGgW Date: Sat, 2 Sep 2023 22:41:42 +0900 Subject: [PATCH] Feat: Add deprecated file controller for getting old files temporarily. --- .../mainImage/api/DeprecatedFileController.kt | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/DeprecatedFileController.kt diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/DeprecatedFileController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/DeprecatedFileController.kt new file mode 100644 index 00000000..ce0adfe1 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/DeprecatedFileController.kt @@ -0,0 +1,63 @@ +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.util.AntPathMatcher +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.RestController +import java.net.URLEncoder +import java.nio.file.Paths + +@RestController +@RequestMapping("/api/sites/default/files") +class DeprecatedFileController ( + @Value("\${oldFiles.path}") + private val oldFilesPath: String, +) { + @GetMapping("/{map}/**") + fun serveOldFile( + @PathVariable map: String, // Just for ensure at least one path variable + @RequestParam(defaultValue = "false") download: Boolean, + request: HttpServletRequest + ): ResponseEntity { + // Extract path from pattern + val fileSubDir = AntPathMatcher().extractPathWithinPattern( + "/api/sites/default/files/**", + request.servletPath + ).substringAfter("/api/sites/default/files/") + + val file = Paths.get(oldFilesPath, fileSubDir) + val resource = UrlResource(file.toUri()) + + return 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 = fileSubDir.substringAfterLast("/") + + val encodedFilename = URLEncoder.encode(originalFilename, Charsets.UTF_8.toString()).replace("+", "%20") + + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''$encodedFilename") + } + + ResponseEntity.ok() + .headers(headers) + .body(resource) + } else { + ResponseEntity.status(HttpStatus.NOT_FOUND).build() + } + } + +} \ No newline at end of file