Skip to content

Commit

Permalink
Feat: Add deprecated file controller for getting old files temporarily.
Browse files Browse the repository at this point in the history
  • Loading branch information
huGgW committed Sep 2, 2023
1 parent ecb18a9 commit 0498737
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Resource> {
// 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()
}
}

}

0 comments on commit 0498737

Please sign in to comment.