From 2bc9b17cdcd46b823e8ba5e2ddb70946546cd11f Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 12 Aug 2024 22:13:16 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=BB=E5=83=8F=E8=AA=AD=E3=81=BF?= =?UTF-8?q?=E8=BE=BC=E3=81=BF=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/core/config/SecurityConfig.kt | 1 + .../api/media/LocalFileController.kt | 29 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SecurityConfig.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SecurityConfig.kt index 59069df4..95ce0b6c 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SecurityConfig.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/config/SecurityConfig.kt @@ -82,6 +82,7 @@ class SecurityConfig { authorize(GET, "/auth/sign_up", hasRole("ANONYMOUS")) authorize(POST, "/auth/sign_up", permitAll) authorize(GET, "/users/{username}/posts/{postId}", permitAll) + authorize(GET, "/files/*", permitAll) authorize(anyRequest, authenticated) } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt index d41762c0..cb003b77 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt @@ -16,21 +16,44 @@ package dev.usbharu.hideout.core.interfaces.api.media +import dev.usbharu.hideout.core.config.LocalStorageConfig +import dev.usbharu.hideout.core.external.media.FileTypeDeterminer import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.core.io.ClassPathResource +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) -interface LocalFileController { +class LocalFileController( + localStorageConfig: LocalStorageConfig, + private val fileTypeDeterminationService: FileTypeDeterminer +) { + + private val savePath = Path.of(localStorageConfig.path).toAbsolutePath() @GetMapping("/files/{id}") - fun files(@PathVariable("id") id: String): ResponseEntity + 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) + } @GetMapping("/users/{user}/icon.jpg", "/users/{user}/header.jpg") fun icons(): ResponseEntity { @@ -41,4 +64,4 @@ interface LocalFileController { .contentLength(pathResource.contentLength()) .body(pathResource) } -} +} \ No newline at end of file