-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
225 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -40,4 +40,5 @@ out/ | |
.DS_Store | ||
|
||
### OTHERS ### | ||
application-secret.yml | ||
application-secret.yml | ||
data |
13 changes: 13 additions & 0 deletions
13
common/src/main/kotlin/org/kkeunkkeun/pregen/common/domain/Constant.kt
This file contains 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,13 @@ | ||
package org.kkeunkkeun.pregen.common.domain | ||
|
||
class Constant { | ||
|
||
companion object { | ||
|
||
const val SESSION_ID_HEADER_NAME = "X-SESSION-ID" | ||
|
||
const val MAX_IMAGE_FILE_SIZE = 10 * 1024 * 1024 | ||
|
||
val ALLOWED_IMAGE_FILE_EXTENSIONS = setOf("image/jpeg", "image/png") | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
practice-websocket/src/main/kotlin/org/kkeunkkeun/pregen/practice/domain/Constant.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
2 changes: 2 additions & 0 deletions
2
...tion-api/src/main/kotlin/org/kkeunkkeun/pregen/presentation/PresentationApiApplication.kt
This file contains 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 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
7 changes: 7 additions & 0 deletions
7
...c/main/kotlin/org/kkeunkkeun/pregen/presentation/file/infrastructure/FileJpaRepository.kt
This file contains 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 org.kkeunkkeun.pregen.presentation.file.infrastructure | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.domain.File | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface FileJpaRepository: JpaRepository<File, Long> { | ||
} |
25 changes: 25 additions & 0 deletions
25
...src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/infrastructure/FileLocalWriter.kt
This file contains 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,25 @@ | ||
package org.kkeunkkeun.pregen.presentation.file.infrastructure | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.domain.File | ||
import org.kkeunkkeun.pregen.presentation.file.service.FileWriter | ||
import org.springframework.stereotype.Repository | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.nio.file.StandardCopyOption | ||
|
||
@Repository | ||
class FileLocalWriter: FileWriter { | ||
|
||
override fun writeMultipartFile(file: MultipartFile, path: String, physicalName: String): File { | ||
val targetLocation = Paths.get(path) | ||
|
||
// Resolve the file path to prevent overwriting issues and keep original file name | ||
val targetPath = targetLocation.resolve(physicalName) | ||
|
||
// Copy the file to the target location (replacing existing file with the same name) | ||
Files.copy(file.inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING) | ||
|
||
return File.from(file, path, physicalName) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/infrastructure/FileProperties.kt
This file contains 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 org.kkeunkkeun.pregen.presentation.file.infrastructure | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
||
@ConfigurationProperties(prefix = "file") | ||
data class FileProperties( | ||
val basePath: String = "", | ||
val thumbnailPath: String = "" | ||
) { | ||
val fullThumbnailPath: String | ||
get() = "$basePath/$thumbnailPath" | ||
.replace("//", "/") | ||
.replace(".", "") | ||
} |
15 changes: 15 additions & 0 deletions
15
.../main/kotlin/org/kkeunkkeun/pregen/presentation/file/infrastructure/FileRepositoryImpl.kt
This file contains 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,15 @@ | ||
package org.kkeunkkeun.pregen.presentation.file.infrastructure | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.domain.File | ||
import org.kkeunkkeun.pregen.presentation.file.service.FileRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class FileRepositoryImpl( | ||
private val fileJpaRepository: FileJpaRepository | ||
): FileRepository { | ||
|
||
override fun save(file: File) { | ||
fileJpaRepository.save(file) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...pi/src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/presentation/FileController.kt
This file contains 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,25 @@ | ||
package org.kkeunkkeun.pregen.presentation.file.presentation | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.service.FileUploadService | ||
import org.springframework.http.HttpStatus.CREATED | ||
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 | ||
|
||
@RestController | ||
@RequestMapping("/api/files") | ||
class FileController( | ||
private val fileUploadService: FileUploadService, | ||
) { | ||
|
||
@PostMapping("/upload") | ||
fun uploadFile(@RequestPart(name = "file") multipartFile: MultipartFile): ResponseEntity<FileResponse> { | ||
val fileEntity = fileUploadService.upload(multipartFile) | ||
|
||
return ResponseEntity.status(CREATED) | ||
.body(FileResponse.from(fileEntity)) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...-api/src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/presentation/FileResponse.kt
This file contains 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,15 @@ | ||
package org.kkeunkkeun.pregen.presentation.file.presentation | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.domain.File | ||
|
||
data class FileResponse( | ||
val id: Long, | ||
val path: String, | ||
) { | ||
|
||
companion object { | ||
fun from(file: File): FileResponse { | ||
return FileResponse(file.id!!, file.absolutePath) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ion-api/src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/service/FileRepository.kt
This file contains 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 org.kkeunkkeun.pregen.presentation.file.service | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.domain.File | ||
|
||
interface FileRepository { | ||
|
||
fun save(file: File) | ||
} |
38 changes: 38 additions & 0 deletions
38
...-api/src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/service/FileUploadService.kt
This file contains 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,38 @@ | ||
package org.kkeunkkeun.pregen.presentation.file.service | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.domain.File | ||
import org.kkeunkkeun.pregen.presentation.file.infrastructure.FileProperties | ||
import org.springframework.stereotype.Service | ||
import org.springframework.util.StringUtils | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.util.UUID | ||
|
||
@Service | ||
class FileUploadService( | ||
private val validator: FileValidator, | ||
private val fileRepository: FileRepository, | ||
private val fileWriter: FileWriter, | ||
private val fileProperties: FileProperties, | ||
) { | ||
|
||
fun upload(file: MultipartFile): File { | ||
validator.validate(file) | ||
|
||
val fileEntity = fileWriter.writeMultipartFile( | ||
file = file, | ||
path = fileProperties.fullThumbnailPath, | ||
physicalName = generateFilePhysicalName(file) | ||
) | ||
|
||
fileRepository.save(fileEntity) | ||
|
||
return fileEntity | ||
} | ||
|
||
private fun generateFilePhysicalName(file: MultipartFile): String { | ||
val extension = StringUtils.getFilenameExtension(file.originalFilename) | ||
val generatedFileName = "${UUID.randomUUID()}.$extension" | ||
|
||
return generatedFileName | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...tion-api/src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/service/FileValidator.kt
This file contains 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,27 @@ | ||
package org.kkeunkkeun.pregen.presentation.file.service | ||
|
||
import org.kkeunkkeun.pregen.common.domain.Constant.Companion.ALLOWED_IMAGE_FILE_EXTENSIONS | ||
import org.kkeunkkeun.pregen.common.domain.Constant.Companion.MAX_IMAGE_FILE_SIZE | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.multipart.MultipartFile | ||
|
||
@Service | ||
class FileValidator { | ||
|
||
fun validate(file: MultipartFile) { | ||
if (file.isEmpty) { | ||
throw IllegalStateException("The file is empty. Please select a non-empty file.") | ||
} | ||
|
||
// Check the file size (10MB = 10 * 1024 * 1024 bytes) | ||
val maxFileSize = MAX_IMAGE_FILE_SIZE | ||
if (file.size > maxFileSize) { | ||
throw IllegalStateException("The file exceeds the maximum allowed size of 10MB.") | ||
} | ||
|
||
// Check the file type | ||
if (!ALLOWED_IMAGE_FILE_EXTENSIONS.contains(file.contentType)) { | ||
throw IllegalStateException("Invalid file type. Only JPG and PNG files are allowed.") | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ntation-api/src/main/kotlin/org/kkeunkkeun/pregen/presentation/file/service/FileWriter.kt
This file contains 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 org.kkeunkkeun.pregen.presentation.file.service | ||
|
||
import org.kkeunkkeun.pregen.presentation.file.domain.File | ||
import org.springframework.web.multipart.MultipartFile | ||
|
||
interface FileWriter { | ||
|
||
fun writeMultipartFile(file: MultipartFile, path: String, physicalName: String): File | ||
|
||
} |
This file contains 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 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 |
---|---|---|
|
@@ -35,3 +35,6 @@ spring: | |
hibernate: | ||
format_sql: true | ||
use_sql_comments: true | ||
file: | ||
base-path: ./data | ||
thumbnail-path: /thumbnails |