diff --git a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt index dead45f0..85f22367 100644 --- a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt @@ -186,6 +186,7 @@ class SecurityConfig { authorize(POST, "/api/v1/accounts", permitAll) authorize("/auth/sign_up", hasRole("ANONYMOUS")) + authorize(GET, "/files", permitAll) authorize(GET, "/api/v1/accounts/verify_credentials", hasAnyScope("read", "read:accounts")) diff --git a/src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt b/src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt new file mode 100644 index 00000000..a7c4032e --- /dev/null +++ b/src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt @@ -0,0 +1,40 @@ +package dev.usbharu.hideout.core.interfaces.api.media + +import dev.usbharu.hideout.application.config.LocalStorageConfig +import dev.usbharu.hideout.core.service.media.FileTypeDeterminationService +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty +import org.springframework.core.io.PathResource +import org.springframework.core.io.Resource +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity +import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import java.nio.file.Path +import kotlin.io.path.name + +@Controller +@ConditionalOnProperty("hideout.storage.type", havingValue = "local", matchIfMissing = true) +class LocalFileController( + localStorageConfig: LocalStorageConfig, + private val fileTypeDeterminationService: FileTypeDeterminationService +) { + + private val savePath = Path.of(localStorageConfig.path).toAbsolutePath() + + @GetMapping("/files/{id}") + fun files(@PathVariable("id") id: String): ResponseEntity { + + val name = Path.of(id).name + val path = savePath.resolve(name) + + val mimeType = fileTypeDeterminationService.fileType(path, name) + val pathResource = PathResource(path) + + return ResponseEntity + .ok() + .contentType(MediaType(mimeType.type, mimeType.subtype)) + .contentLength(pathResource.contentLength()) + .body(pathResource) + } +}