From 4fd5c487c65d4a49a6b53ad3039bb281bf8e58e5 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 6 Dec 2023 21:37:00 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20#188=20=E3=81=A7=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=82=A8=E3=83=B3=E3=83=89=E3=83=9D=E3=82=A4?= =?UTF-8?q?=E3=83=B3=E3=83=88=E3=81=A7=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=82=92=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=84?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/config/SecurityConfig.kt | 1 + .../api/media/LocalFileController.kt | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/main/kotlin/dev/usbharu/hideout/core/interfaces/api/media/LocalFileController.kt 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) + } +} From a614459485be169602de35bc4b061c6211fef381 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 6 Dec 2023 21:45:14 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feature:=20=E6=9A=AB=E5=AE=9A=E3=81=A7?= =?UTF-8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC=E3=82=A2=E3=82=A4=E3=82=B3?= =?UTF-8?q?=E3=83=B3=E3=82=92=E8=BF=94=E5=8D=B4=E3=81=99=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/application/config/SecurityConfig.kt | 2 ++ .../core/interfaces/api/media/LocalFileController.kt | 12 ++++++++++++ 2 files changed, 14 insertions(+) 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 85f22367..59caf58d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/SecurityConfig.kt @@ -187,6 +187,8 @@ class SecurityConfig { authorize("/auth/sign_up", hasRole("ANONYMOUS")) authorize(GET, "/files", permitAll) + authorize(GET, "/users/*/icon.jpg", permitAll) + authorize(GET, "/users/*/header.jpg", 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 index a7c4032e..772e7b46 100644 --- 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 @@ -3,6 +3,7 @@ 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.ClassPathResource import org.springframework.core.io.PathResource import org.springframework.core.io.Resource import org.springframework.http.MediaType @@ -37,4 +38,15 @@ class LocalFileController( .contentLength(pathResource.contentLength()) .body(pathResource) } + + @GetMapping("/users/{user}/icon.jpg", "/users/{user}/header.jpg") + fun icons(): ResponseEntity { + + val pathResource = ClassPathResource("icon.png") + return ResponseEntity + .ok() + .contentType(MediaType.IMAGE_PNG) + .contentLength(pathResource.contentLength()) + .body(pathResource) + } } From de2761e3cf120b4edb8dcffeef80d1e6a125ab09 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 6 Dec 2023 22:19:56 +0900 Subject: [PATCH 3/3] style: fix lint --- .../hideout/activitypub/domain/model/Person.kt | 2 ++ .../hideout/application/config/SpringConfig.kt | 1 - .../interfaces/api/media/LocalFileController.kt | 2 -- .../media/LocalFileSystemMediaDataStore.kt | 17 ++++++++++------- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Person.kt b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Person.kt index c04ba736..cba9eeaf 100644 --- a/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Person.kt +++ b/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/Person.kt @@ -20,6 +20,7 @@ constructor( var following: String? ) : Object(add(type, "Person")), HasId, HasName { + @Suppress("CyclomaticComplexMethod", "CognitiveComplexMethod") override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false @@ -43,6 +44,7 @@ constructor( return true } + @Suppress("CyclomaticComplexMethod") override fun hashCode(): Int { var result = super.hashCode() result = 31 * result + name.hashCode() diff --git a/src/main/kotlin/dev/usbharu/hideout/application/config/SpringConfig.kt b/src/main/kotlin/dev/usbharu/hideout/application/config/SpringConfig.kt index f3e088ab..c05229b7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/application/config/SpringConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/application/config/SpringConfig.kt @@ -42,7 +42,6 @@ data class S3StorageConfig( val secretKey: String ) - /** * メディアの保存にローカルファイルシステムを使用する際のコンフィグ * 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 index 772e7b46..555cb592 100644 --- 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 @@ -25,7 +25,6 @@ class LocalFileController( @GetMapping("/files/{id}") fun files(@PathVariable("id") id: String): ResponseEntity { - val name = Path.of(id).name val path = savePath.resolve(name) @@ -41,7 +40,6 @@ class LocalFileController( @GetMapping("/users/{user}/icon.jpg", "/users/{user}/header.jpg") fun icons(): ResponseEntity { - val pathResource = ClassPathResource("icon.png") return ResponseEntity .ok() diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt index a7300081..412b50a5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/media/LocalFileSystemMediaDataStore.kt @@ -12,8 +12,6 @@ import kotlin.io.path.createDirectories import kotlin.io.path.deleteIfExists import kotlin.io.path.outputStream -@Service -@ConditionalOnProperty("hideout.storage.type", havingValue = "local", matchIfMissing = true) /** * ローカルファイルシステムにメディアを保存します * @@ -23,8 +21,11 @@ import kotlin.io.path.outputStream * @param applicationConfig ApplicationConfig * @param localStorageConfig LocalStorageConfig */ +@Service +@ConditionalOnProperty("hideout.storage.type", havingValue = "local", matchIfMissing = true) class LocalFileSystemMediaDataStore( - applicationConfig: ApplicationConfig, localStorageConfig: LocalStorageConfig + applicationConfig: ApplicationConfig, + localStorageConfig: LocalStorageConfig ) : MediaDataStore { private val savePath: Path = Path.of(localStorageConfig.path).toAbsolutePath() @@ -39,7 +40,6 @@ class LocalFileSystemMediaDataStore( val fileSavePath = buildSavePath(savePath, dataMediaSave.name) val thumbnailSavePath = buildSavePath(savePath, "thumbnail-" + dataMediaSave.name) - dataMediaSave.thumbnailInputStream?.inputStream()?.use { it.buffered().use { bufferedInputStream -> thumbnailSavePath.outputStream(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) @@ -51,7 +51,6 @@ class LocalFileSystemMediaDataStore( } } - dataMediaSave.fileInputStream.inputStream().use { it.buffered().use { bufferedInputStream -> fileSavePath.outputStream(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE) @@ -60,7 +59,9 @@ class LocalFileSystemMediaDataStore( } return SuccessSavedMedia( - dataMediaSave.name, publicUrl + dataMediaSave.name, publicUrl + "thumbnail-" + dataMediaSave.name + dataMediaSave.name, + publicUrl + dataMediaSave.name, + publicUrl + "thumbnail-" + dataMediaSave.name ) } @@ -82,7 +83,9 @@ class LocalFileSystemMediaDataStore( logger.info("SUCCESS Media upload. {}", dataSaveRequest.name) return SuccessSavedMedia( - dataSaveRequest.name, publicUrl + dataSaveRequest.name, publicUrl + "thumbnail-" + dataSaveRequest.name + dataSaveRequest.name, + publicUrl + dataSaveRequest.name, + publicUrl + "thumbnail-" + dataSaveRequest.name ) }