Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import java.net.URLConnection
import java.nio.file.Paths
import kotlin.io.path.exists

@RestController
class FileController(private val storageService: StorageService) {
data class FileUploadResponse(val path: String)

@PostMapping("/{uid}")
suspend fun fileUpload(
@PathVariable("uid") uid: String,
@RequestPart("file") file: Mono<FilePart>,
@CurrentSecurityContext securityContext: SecurityContext
@PathVariable("uid") uid: String,
@RequestPart("file") file: Mono<FilePart>,
@CurrentSecurityContext securityContext: SecurityContext
): Any {
if (securityContext.authentication.name != uid) throw OpexException(OpexError.UnAuthorized)
file.awaitFirstOrNull().apply {
Expand All @@ -39,12 +40,13 @@ class FileController(private val storageService: StorageService) {
@GetMapping("/{uid}/{filename}")
@ResponseBody
suspend fun fileDownload(
@PathVariable("uid") uid: String,
@PathVariable("filename") filename: String,
@CurrentSecurityContext securityContext: SecurityContext
@PathVariable("uid") uid: String,
@PathVariable("filename") filename: String,
@CurrentSecurityContext securityContext: SecurityContext
): ResponseEntity<ByteArray> {
if (securityContext.authentication.name != uid) throw OpexException(OpexError.UnAuthorized)
val path = Paths.get("").resolve("/opex-storage/$uid/$filename")
if (!storageService.exists(path.toString())) throw OpexException(OpexError.NotFound)
val file = storageService.load(path.toString())
val mimeType = URLConnection.getFileNameMap().getContentTypeFor(path.fileName.toString())
return ResponseEntity.ok().contentType(MediaType.parseMediaType(mimeType)).body(file.readBytes())
Expand All @@ -53,11 +55,11 @@ class FileController(private val storageService: StorageService) {
@GetMapping("/admin/download/{uid}/{filename}")
@ResponseBody
suspend fun adminFileDownload(
@PathVariable("uid") uid: String,
@PathVariable("filename") filename: String,
@CurrentSecurityContext securityContext: SecurityContext
@PathVariable("uid") uid: String,
@PathVariable("filename") filename: String
): ResponseEntity<ByteArray> {
val path = Paths.get("").resolve("/opex-storage/$uid/$filename")
if (!storageService.exists(path.toString())) throw OpexException(OpexError.NotFound)
val file = storageService.load(path.toString())
val mimeType = URLConnection.getFileNameMap().getContentTypeFor(path.fileName.toString())
return ResponseEntity.ok().contentType(MediaType.parseMediaType(mimeType)).body(file.readBytes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.io.FileInputStream

interface StorageService {
suspend fun store(path: String, file: FilePart)
suspend fun exists(filename: String): Boolean
suspend fun load(filename: String): FileInputStream
suspend fun delete(filename: String)
suspend fun deleteAll(folderName: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class StorageServiceImpl(private val resourceLoader: ResourceLoader) : StorageSe
file.transferTo(p).awaitFirstOrNull()
}

override suspend fun exists(filename: String): Boolean {
return Files.exists(Paths.get(filename))
}

override suspend fun load(filename: String): FileInputStream {
return ResourceUtils.getFile(filename).inputStream()
}
Expand Down