-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add deprecated file controller for getting old files temporarily.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...n/kotlin/com/wafflestudio/csereal/core/resource/mainImage/api/DeprecatedFileController.kt
This file contains 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
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() | ||
} | ||
} | ||
|
||
} |