-
Notifications
You must be signed in to change notification settings - Fork 0
upload file #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
upload file #54
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
29c9cc9
feat ( #53 ) : PathList
coehgns df33741
feat ( #53 ) : ImageFileConverter
coehgns 3a52655
feat ( #53 ) : PhotoJpaEntity
coehgns 506701f
feat ( #53 ) : PhotoJpaRepository
coehgns e9f4176
feat ( #53 ) : UploadFilePort
coehgns f6f2007
feat ( #53 ) : WebException
coehgns e98bae4
feat ( #53 ) : WebFileExceptions
coehgns 6654be6
feat ( #53 ) : FileConverter
coehgns cf82228
feat ( #53 ) : FileExtensions
coehgns 6008d21
feat ( #53 ) : FileUploadUseCase
coehgns 5694188
feat ( #53 ) : GenerateFileUrlPort
coehgns bad8c73
feat ( #53 ) : FileController
coehgns 7474b35
feat ( #53 ) : AwsS3Adapter
coehgns dcfee81
feat ( #53 ) : AwsS3Config
coehgns 242ed5e
feat ( #53 ) : AwsProperties
coehgns 53d8b99
feat ( #53 ) : AwsCredentialsProperties
coehgns e42db3c
refactor ( #53 ) : ApplicationDetailResponse photo path 추가
coehgns ddb5e8c
refactor ( #53 ) : ApplicationJpaEntity
coehgns 923f64c
refactor ( #53 ) : ApplicationQueryUseCase
coehgns 1087db0
build ( #53 ) : aws s3 의존성 추가
coehgns dd44240
refactor ( #53 ) : FileController 파일 구조 수정
coehgns e44014f
feat ( #53 ) : AwsRegionProperties
coehgns 4ef5128
refactor ( #53 ) : AwsProperties
coehgns 3811e3a
refactor ( #53 ) : AwsS3Config
coehgns 1003f70
refactor ( #53 ) : PhotoJpaRepository
coehgns db82271
refactor ( #53 ) : FileUploadUseCase
coehgns 84b479d
refactor ( #53 ) : AwsS3Adapter
coehgns 15d4e6d
refactor ( #53 ) : FileController
coehgns f1823b9
refactor ( #53 ) : BusinessException
coehgns f25fdab
refactor ( #53 ) : FileExceptions
coehgns ce24d72
refactor ( #53 ) : SecurityConfig
coehgns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
casper-application-domain/src/main/kotlin/hs/kr/entrydsm/domain/file/object/PathList.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package hs.kr.entrydsm.domain.file.`object` | ||
|
|
||
| object PathList { | ||
| const val PHOTO = "entry_photo/" | ||
| } |
5 changes: 5 additions & 0 deletions
5
...-application-domain/src/main/kotlin/hs/kr/entrydsm/domain/file/spi/GenerateFileUrlPort.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package hs.kr.entrydsm.domain.file.spi | ||
|
|
||
| interface GenerateFileUrlPort { | ||
| fun generateFileUrl(fileName: String, path: String): String | ||
| } |
7 changes: 7 additions & 0 deletions
7
casper-application-domain/src/main/kotlin/hs/kr/entrydsm/domain/file/spi/UploadFilePort.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package hs.kr.entrydsm.domain.file.spi | ||
|
|
||
| import java.io.File | ||
|
|
||
| interface UploadFilePort { | ||
| fun upload(file: File, path: String): String | ||
| } |
6 changes: 6 additions & 0 deletions
6
...r-application-domain/src/main/kotlin/hs/kr/entrydsm/global/exception/BusinessException.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package hs.kr.entrydsm.global.exception | ||
|
|
||
| abstract class BusinessException( | ||
| open val status: Int, | ||
| override val message: String, | ||
| ) : RuntimeException() |
6 changes: 6 additions & 0 deletions
6
casper-application-domain/src/main/kotlin/hs/kr/entrydsm/global/exception/WebException.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package hs.kr.entrydsm.global.exception | ||
|
|
||
| abstract class WebException( | ||
| open val status: Int, | ||
| override val message: String, | ||
| ) : RuntimeException() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...main/kotlin/hs/kr/entrydsm/application/domain/application/domain/entity/PhotoJpaEntity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package hs.kr.entrydsm.application.domain.application.domain.entity | ||
|
|
||
| import jakarta.persistence.Column | ||
| import jakarta.persistence.Entity | ||
| import jakarta.persistence.Id | ||
| import jakarta.persistence.Table | ||
| import java.util.UUID | ||
|
|
||
| @Entity | ||
| @Table(name = "tbl_photo") | ||
| class PhotoJpaEntity( | ||
| @Id | ||
| val userId: UUID, | ||
|
|
||
| @Column(name = "photo_path", nullable = false) | ||
| var photo: String, | ||
| ) |
10 changes: 10 additions & 0 deletions
10
...lin/hs/kr/entrydsm/application/domain/application/domain/repository/PhotoJpaRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package hs.kr.entrydsm.application.domain.application.domain.repository | ||
|
|
||
| import hs.kr.entrydsm.application.domain.application.domain.entity.PhotoJpaEntity | ||
| import org.springframework.data.jpa.repository.JpaRepository | ||
| import java.util.UUID | ||
|
|
||
| interface PhotoJpaRepository : JpaRepository<PhotoJpaEntity, UUID> { | ||
|
|
||
| fun findByUserId(userId: UUID): PhotoJpaEntity? | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...rc/main/kotlin/hs/kr/entrydsm/application/domain/application/usecase/FileUploadUseCase.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package hs.kr.entrydsm.application.domain.application.usecase | ||
|
|
||
| import hs.kr.entrydsm.application.domain.application.domain.entity.PhotoJpaEntity | ||
| import hs.kr.entrydsm.application.domain.application.domain.repository.PhotoJpaRepository | ||
| import hs.kr.entrydsm.application.global.security.SecurityAdapter | ||
| import hs.kr.entrydsm.domain.file.spi.UploadFilePort | ||
coehgns marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import hs.kr.entrydsm.domain.file.`object`.PathList | ||
| import org.springframework.stereotype.Component | ||
| import org.springframework.transaction.annotation.Transactional | ||
| import java.io.File | ||
|
|
||
| @Component | ||
| class FileUploadUseCase( | ||
| private val uploadFilePort: UploadFilePort, | ||
| private val photoJpaRepository: PhotoJpaRepository, | ||
| private val securityAdapter: SecurityAdapter, | ||
| ) { | ||
| @Transactional | ||
| fun execute(file: File): String { | ||
| val userId = securityAdapter.getCurrentUserId() | ||
| val photo = uploadFilePort.upload(file, PathList.PHOTO) | ||
|
|
||
| photoJpaRepository.findByUserId(userId)?.apply { | ||
| this.photo = photo | ||
| photoJpaRepository.save(this) | ||
| } ?: photoJpaRepository.save( | ||
| PhotoJpaEntity( | ||
| userId = userId, | ||
| photo = photo | ||
| ) | ||
| ) | ||
|
|
||
| return photo | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
...ure/src/main/kotlin/hs/kr/entrydsm/application/domain/file/presentation/FileController.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package hs.kr.entrydsm.application.domain.file.presentation | ||
|
|
||
| import hs.kr.entrydsm.application.domain.application.usecase.FileUploadUseCase | ||
| import hs.kr.entrydsm.application.domain.file.presentation.converter.ImageFileConverter | ||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.web.bind.annotation.PostMapping | ||
| import org.springframework.web.bind.annotation.RequestMapping | ||
| import org.springframework.web.bind.annotation.RequestPart | ||
| import org.springframework.web.bind.annotation.RestController | ||
| import org.springframework.web.multipart.MultipartFile | ||
|
|
||
| @RequestMapping("/photo") | ||
| @RestController | ||
| class FileController( | ||
| private val fileUploadUseCase: FileUploadUseCase | ||
| ) { | ||
| @PostMapping | ||
| fun uploadPhoto(@RequestPart(name = "image") file: MultipartFile): ResponseEntity<Map<String, String>> { | ||
| val photoUrl = fileUploadUseCase.execute( | ||
| file.let(ImageFileConverter::transferTo) | ||
| ) | ||
| return ResponseEntity.ok(mapOf("fileName" to photoUrl)) | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...ain/kotlin/hs/kr/entrydsm/application/domain/file/presentation/converter/FileConverter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package hs.kr.entrydsm.application.domain.file.presentation.converter | ||
|
|
||
| import hs.kr.entrydsm.application.domain.file.presentation.exception.WebFileExceptions | ||
| import org.springframework.web.multipart.MultipartFile | ||
| import java.io.File | ||
| import java.io.FileOutputStream | ||
| import java.util.UUID | ||
|
|
||
| interface FileConverter { | ||
| val MultipartFile.extension: String | ||
| get() = originalFilename?.substringAfterLast(".", "")?.uppercase() ?: "" | ||
|
|
||
| fun isCorrectExtension(multipartFile: MultipartFile): Boolean | ||
|
|
||
| fun transferTo(multipartFile: MultipartFile): File { | ||
| if (!isCorrectExtension(multipartFile)) { | ||
| throw WebFileExceptions.InvalidExtension() | ||
| } | ||
|
|
||
| return transferFile(multipartFile) | ||
| } | ||
|
|
||
| private fun transferFile(multipartFile: MultipartFile): File { | ||
| return File("${UUID.randomUUID()}_${multipartFile.originalFilename}") | ||
| .apply { | ||
| FileOutputStream(this).use { | ||
| it.write(multipartFile.bytes) | ||
| } | ||
| } | ||
| } | ||
coehgns marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
8 changes: 8 additions & 0 deletions
8
...in/kotlin/hs/kr/entrydsm/application/domain/file/presentation/converter/FileExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package hs.kr.entrydsm.application.domain.file.presentation.converter | ||
|
|
||
| object FileExtensions { | ||
| const val JPG = "JPG" | ||
| const val JPEG = "JPEG" | ||
| const val PNG = "PNG" | ||
| const val HEIC = "HEIC" | ||
| } |
16 changes: 16 additions & 0 deletions
16
...otlin/hs/kr/entrydsm/application/domain/file/presentation/converter/ImageFileConverter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package hs.kr.entrydsm.application.domain.file.presentation.converter | ||
|
|
||
| import hs.kr.entrydsm.application.domain.file.presentation.converter.FileExtensions.HEIC | ||
| import hs.kr.entrydsm.application.domain.file.presentation.converter.FileExtensions.JPEG | ||
| import hs.kr.entrydsm.application.domain.file.presentation.converter.FileExtensions.JPG | ||
| import hs.kr.entrydsm.application.domain.file.presentation.converter.FileExtensions.PNG | ||
| import org.springframework.web.multipart.MultipartFile | ||
|
|
||
| object ImageFileConverter : FileConverter { | ||
| override fun isCorrectExtension(multipartFile: MultipartFile): Boolean { | ||
| return when (multipartFile.extension) { | ||
| JPG, JPEG, PNG, HEIC -> true | ||
| else -> false | ||
| } | ||
| } | ||
coehgns marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
23 changes: 23 additions & 0 deletions
23
...in/kotlin/hs/kr/entrydsm/application/domain/file/presentation/exception/FileExceptions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package hs.kr.entrydsm.application.domain.file.presentation.exception | ||
|
|
||
| import hs.kr.entrydsm.global.exception.BusinessException | ||
|
|
||
| sealed class FileExceptions( | ||
| override val status: Int, | ||
| override val message: String, | ||
| ) : BusinessException(status, message) { | ||
| // 400 | ||
| class NotValidContent(message: String = NOT_VALID_CONTENT) : FileExceptions(400, message) | ||
|
|
||
| // 404 | ||
| class PathNotFound(message: String = PATH_NOT_FOUND) : FileExceptions(404, message) | ||
|
|
||
| // 500 | ||
| class IOInterrupted(message: String = IO_INTERRUPTED) : FileExceptions(500, message) | ||
|
|
||
| companion object { | ||
| private const val NOT_VALID_CONTENT = "파일의 내용이 올바르지 않습니다." | ||
| private const val PATH_NOT_FOUND = "경로를 찾을 수 없습니다." | ||
| private const val IO_INTERRUPTED = "파일 입출력 처리가 중단되었습니다." | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
...kotlin/hs/kr/entrydsm/application/domain/file/presentation/exception/WebFileExceptions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package hs.kr.entrydsm.application.domain.file.presentation.exception | ||
|
|
||
| import hs.kr.entrydsm.global.exception.WebException | ||
|
|
||
| sealed class WebFileExceptions( | ||
| override val status: Int, | ||
| override val message: String, | ||
| ) : WebException(status, message) { | ||
| class InvalidExtension(message: String = INVALID_EXTENSION) : WebFileExceptions(400, message) | ||
|
|
||
| companion object { | ||
| private const val INVALID_EXTENSION = "확장자가 유효하지 않습니다." | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
...on-infrastructure/src/main/kotlin/hs/kr/entrydsm/application/global/config/AwsS3Config.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package hs.kr.entrydsm.application.global.config | ||
|
|
||
| import com.amazonaws.auth.AWSStaticCredentialsProvider | ||
| import com.amazonaws.auth.BasicAWSCredentials | ||
| import com.amazonaws.services.s3.AmazonS3Client | ||
| import com.amazonaws.services.s3.AmazonS3ClientBuilder | ||
| import hs.kr.entrydsm.application.global.storage.AwsCredentialsProperties | ||
| import hs.kr.entrydsm.application.global.storage.AwsProperties | ||
| import hs.kr.entrydsm.application.global.storage.AwsRegionProperties | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
| import org.springframework.context.annotation.Bean | ||
| import org.springframework.context.annotation.Configuration | ||
|
|
||
| @Configuration | ||
| @EnableConfigurationProperties(AwsProperties::class, AwsCredentialsProperties::class) | ||
| class AwsS3Config( | ||
| private val awsCredentialsProperties: AwsCredentialsProperties, | ||
| private val awsRegionProperties: AwsRegionProperties | ||
| ) { | ||
|
|
||
| @Bean | ||
| fun amazonS3Client(): AmazonS3Client { | ||
| val credentials = BasicAWSCredentials(awsCredentialsProperties.accessKey, awsCredentialsProperties.secretKey) | ||
|
|
||
| return AmazonS3ClientBuilder.standard() | ||
| .withRegion(awsRegionProperties.static) | ||
| .withCredentials(AWSStaticCredentialsProvider(credentials)) | ||
| .build() as AmazonS3Client | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...ure/src/main/kotlin/hs/kr/entrydsm/application/global/storage/AwsCredentialsProperties.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package hs.kr.entrydsm.application.global.storage | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
|
||
| @ConfigurationProperties("cloud.aws.credentials") | ||
| class AwsCredentialsProperties( | ||
| val accessKey: String, | ||
| val secretKey: String, | ||
| ) |
8 changes: 8 additions & 0 deletions
8
...infrastructure/src/main/kotlin/hs/kr/entrydsm/application/global/storage/AwsProperties.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package hs.kr.entrydsm.application.global.storage | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
|
||
| @ConfigurationProperties("cloud.aws.s3") | ||
| class AwsProperties( | ||
| val bucket: String | ||
| ) |
8 changes: 8 additions & 0 deletions
8
...tructure/src/main/kotlin/hs/kr/entrydsm/application/global/storage/AwsRegionProperties.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package hs.kr.entrydsm.application.global.storage | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
|
||
| @ConfigurationProperties("cloud.aws.region") | ||
| class AwsRegionProperties( | ||
| val static: String | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.