Serve public icon files
All checks were successful
build-and-deploy / build-deploy (push) Successful in 33s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 33s
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package com.android.trisolarisserver.controller
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.core.io.FileSystemResource
|
||||
import org.springframework.http.HttpHeaders
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.ResponseEntity
|
||||
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.RestController
|
||||
import java.nio.file.Files
|
||||
@@ -29,4 +34,21 @@ class IconFiles(
|
||||
.toList()
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/png/{filename}")
|
||||
fun getPng(@PathVariable filename: String): ResponseEntity<FileSystemResource> {
|
||||
if (filename.contains("..") || filename.contains("/") || filename.contains("\\")) {
|
||||
return ResponseEntity.badRequest().build()
|
||||
}
|
||||
val file = Paths.get(pngRoot, filename)
|
||||
if (!Files.exists(file) || !Files.isRegularFile(file)) {
|
||||
return ResponseEntity.notFound().build()
|
||||
}
|
||||
val resource = FileSystemResource(file)
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_PNG)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"${file.fileName}\"")
|
||||
.contentLength(resource.contentLength())
|
||||
.body(resource)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user