-
Notifications
You must be signed in to change notification settings - Fork 1
Fix 서버 파일/메인이미지 폴더 분리 #374
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
base: develop
Are you sure you want to change the base?
Changes from 5 commits
6eb66d1
63dd3ca
60b9bfd
65d9581
0bcea87
7a6ce7f
03c5ced
d0a6f28
0170d7c
a44dfc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,5 +3,7 @@ package com.wafflestudio.csereal.common.entity | |
| import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity | ||
|
|
||
| interface MainImageAttachable { | ||
| val mainImage: MainImageEntity? | ||
| var mainImage: MainImageEntity? | ||
|
|
||
| fun getMainImageFolder(): String | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,4 +48,6 @@ class AcademicsEntity( | |
| ) | ||
| } | ||
| } | ||
|
|
||
| override fun getAttachmentFolder() = "attachment/academics" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,4 +44,6 @@ class ResearchEntity( | |
| ) | ||
| } | ||
| } | ||
|
|
||
| override fun getMainImageFolder() = "mainImage/research" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ class AttachmentEntity( | |
| @Column(unique = true) | ||
| val filename: String, | ||
|
|
||
| val folder: String? = null, | ||
|
||
|
|
||
| val attachmentsOrder: Int, | ||
| val size: Long, | ||
|
|
||
|
|
@@ -57,4 +59,8 @@ class AttachmentEntity( | |
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "council_file_id") | ||
| var councilFile: CouncilFileEntity? = null | ||
| ) : BaseTimeEntity() | ||
| ) : BaseTimeEntity() { | ||
| fun filePath(): String { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 수정해주세요보다는 방향성을 고민해보면 좋을 거 같아서 코멘트 남깁니다. 위에 문제를 벗어나기 위해서 각 entity에 method를 정의한다기보다는 다만 어떻게 하든 장단점은 있어보이네요..!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 원래 attachable -> String 형식으로 메소드를 AttachmentService에 만들었는데, 저장 위치는 attachable이라면 가져야 할 속성이라 생각해 이렇게 수정했습니다. |
||
| return if (folder.isNullOrBlank()) return filename else "$folder/$filename" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import org.springframework.data.repository.findByIdOrNull | |
| import org.springframework.stereotype.Service | ||
| import org.springframework.transaction.annotation.Transactional | ||
| import org.springframework.web.multipart.MultipartFile | ||
| import java.lang.invoke.WrongMethodTypeException | ||
| import java.nio.file.Files | ||
| import java.nio.file.Paths | ||
|
|
||
|
|
@@ -53,22 +54,25 @@ class AttachmentServiceImpl( | |
| private val eventPublisher: ApplicationEventPublisher | ||
| ) : AttachmentService { | ||
| override fun uploadAttachmentInLabEntity(labEntity: LabEntity, requestAttachment: MultipartFile): AttachmentDto { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 흠 얘도 추후에 추상화가 되면 좋겠군요.. |
||
| Files.createDirectories(Paths.get(path)) | ||
| val folder = "attachment/lab" | ||
| val uploadDir = Paths.get(path, folder) | ||
| Files.createDirectories(uploadDir) | ||
|
|
||
| val timeMillis = System.currentTimeMillis() | ||
|
|
||
| val filename = "${timeMillis}_${requestAttachment.originalFilename}" | ||
| val totalFilename = path + filename | ||
| val saveFile = Paths.get(totalFilename) | ||
| val saveFile = Paths.get(path, folder, filename) | ||
| requestAttachment.transferTo(saveFile) | ||
|
|
||
| val attachment = AttachmentEntity( | ||
| filename = filename, | ||
| folder = folder, | ||
| attachmentsOrder = 1, | ||
| size = requestAttachment.size | ||
| ) | ||
|
|
||
| labEntity.pdf = attachment | ||
| attachment.lab = labEntity | ||
| attachmentRepository.save(attachment) | ||
|
|
||
| return AttachmentDto( | ||
|
|
@@ -83,20 +87,22 @@ class AttachmentServiceImpl( | |
| contentEntityType: AttachmentAttachable, | ||
| requestAttachments: List<MultipartFile> | ||
| ): List<AttachmentDto> { | ||
| Files.createDirectories(Paths.get(path)) | ||
| val folder = contentEntityType.getAttachmentFolder() | ||
| val uploadDir = Paths.get(path, folder) | ||
| Files.createDirectories(uploadDir) | ||
|
|
||
| val attachmentsList = mutableListOf<AttachmentDto>() | ||
|
|
||
| for ((index, requestAttachment) in requestAttachments.withIndex()) { | ||
| val timeMillis = System.currentTimeMillis() | ||
|
|
||
| val filename = "${timeMillis}_${requestAttachment.originalFilename}" | ||
| val totalFilename = path + filename | ||
| val saveFile = Paths.get(totalFilename) | ||
| val saveFile = uploadDir.resolve(filename) | ||
| requestAttachment.transferTo(saveFile) | ||
|
|
||
| val attachment = AttachmentEntity( | ||
| filename = filename, | ||
| folder = folder, | ||
| attachmentsOrder = index + 1, | ||
| size = requestAttachment.size | ||
| ) | ||
|
|
@@ -124,7 +130,7 @@ class AttachmentServiceImpl( | |
| attachmentDto = AttachmentResponse( | ||
| id = attachment.id, | ||
| name = attachment.filename.substringAfter("_"), | ||
| url = "${endpointProperties.backend}/v1/file/${attachment.filename}", | ||
| url = "${endpointProperties.backend}/v1/file/${attachment.filePath()}", | ||
| bytes = attachment.size | ||
| ) | ||
| } | ||
|
|
@@ -142,7 +148,7 @@ class AttachmentServiceImpl( | |
| val attachmentDto = AttachmentResponse( | ||
| id = attachment.id, | ||
| name = attachment.filename.substringAfter("_"), | ||
| url = "${endpointProperties.backend}/v1/file/${attachment.filename}", | ||
| url = "${endpointProperties.backend}/v1/file/${attachment.filePath()}", | ||
| bytes = attachment.size | ||
| ) | ||
| list.add(attachmentDto) | ||
|
|
@@ -170,7 +176,7 @@ class AttachmentServiceImpl( | |
|
|
||
| @Transactional | ||
| override fun deleteAttachment(attachment: AttachmentEntity) { | ||
| val fileDirectory = path + attachment.filename | ||
| val fileDirectory = path + attachment.filePath() | ||
| attachmentRepository.delete(attachment) | ||
| eventPublisher.publishEvent(FileDeleteEvent(fileDirectory)) | ||
| } | ||
|
|
@@ -217,6 +223,10 @@ class AttachmentServiceImpl( | |
| contentEntity.attachments.add(attachment) | ||
| attachment.councilFile = contentEntity | ||
| } | ||
|
|
||
| else -> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메서드도 main image처럼 추후 간단하게 할 수 있다면 좋겠네용... |
||
| throw WrongMethodTypeException("파일을 엔티티에 연결할 수 없습니다") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,13 +26,12 @@ class FileController( | |
| private val uploadPath: String, | ||
| private val endpointProperties: EndpointProperties | ||
| ) { | ||
|
|
||
| @GetMapping("/{filename:.+}") | ||
| @GetMapping("/{*filepath}") | ||
| fun serveFile( | ||
| @PathVariable filename: String, | ||
| @PathVariable filepath: String, | ||
| request: HttpServletRequest | ||
| ): ResponseEntity<Resource> { | ||
| val file = Paths.get(uploadPath, filename) | ||
| val file = Paths.get(uploadPath, filepath) | ||
| val resource = UrlResource(file.toUri()) | ||
|
|
||
| if (resource.exists() || resource.isReadable) { | ||
|
|
@@ -41,6 +40,7 @@ class FileController( | |
|
|
||
| headers.contentType = MediaType.parseMediaType(contentType ?: "application/octet-stream") | ||
|
|
||
| val filename = filepath.substringAfterLast("/") | ||
| val originalFilename = filename.substringAfter("_") | ||
| val encodedFilename = URLEncoder.encode(originalFilename, UTF_8.toString()).replace("+", "%20") | ||
|
|
||
|
|
@@ -69,7 +69,7 @@ class FileController( | |
| val saveFile = Paths.get(totalFilename) | ||
| file.transferTo(saveFile) | ||
|
|
||
| val imageUrl = "${endpointProperties.backend}/v1/file/$filename" | ||
| val imageUrl = "/v1/file/$filename" | ||
|
|
||
| results.add( | ||
| UploadFileInfo( | ||
|
|
@@ -93,9 +93,12 @@ class FileController( | |
| } | ||
|
|
||
| @PreAuthorize("hasRole('STAFF')") | ||
| @DeleteMapping("/{filename:.+}") | ||
| fun deleteFile(@PathVariable filename: String): ResponseEntity<Any> { | ||
| val file = Paths.get(uploadPath, filename) | ||
| @DeleteMapping("/{*filepath}") | ||
| fun deleteFile( | ||
| @PathVariable filepath: String, | ||
| request: HttpServletRequest | ||
|
||
| ): ResponseEntity<Any> { | ||
| val file = Paths.get(uploadPath, filepath) | ||
|
|
||
| if (Files.exists(file)) { | ||
| Files.delete(file) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,13 @@ class MainImageEntity( | |
| @Column(unique = true) | ||
| val filename: String, | ||
|
|
||
| val folder: String? = null, | ||
|
||
|
|
||
| val imagesOrder: Int, | ||
| val size: Long | ||
|
|
||
| ) : BaseTimeEntity() | ||
| ) : BaseTimeEntity() { | ||
| fun filePath(): String { | ||
| return if (folder.isNullOrBlank()) return filename else "$folder/$filename" | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto