Skip to content

Commit

Permalink
fix: 파일 업로드 경로 통일 (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeryboy authored Sep 4, 2023
1 parent f47b845 commit 0c83efb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data class NoticeDto(
val id: Long,
val title: String,
val description: String,
val author: String,
val author: String?,
val tags: List<String>,
val createdAt: LocalDateTime?,
val modifiedAt: LocalDateTime?,
Expand All @@ -23,7 +23,11 @@ data class NoticeDto(
) {

companion object {
fun of(entity: NoticeEntity, attachmentResponses: List<AttachmentResponse>, prevNext: Array<NoticeEntity?>?): NoticeDto = entity.run {
fun of(
entity: NoticeEntity,
attachmentResponses: List<AttachmentResponse>,
prevNext: Array<NoticeEntity?>?
): NoticeDto = entity.run {
NoticeDto(
id = this.id,
title = this.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ interface AttachmentService {
labEntity: LabEntity,
requestAttachment: MultipartFile
): AttachmentDto

fun uploadAllAttachments(
contentEntityType: AttachmentContentEntityType,
requestAttachments: List<MultipartFile>,
): List<AttachmentDto>

fun createAttachmentResponses(attachments: List<AttachmentEntity>?): List<AttachmentResponse>
fun updateAttachmentResponses(
contentEntity: AttachmentContentEntityType,
Expand All @@ -41,7 +43,7 @@ interface AttachmentService {
@Service
class AttachmentServiceImpl(
private val attachmentRepository: AttachmentRepository,
@Value("\${csereal_attachment.upload.path}")
@Value("\${csereal.upload.path}")
private val path: String,
private val endpointProperties: EndpointProperties,
) : AttachmentService {
Expand Down Expand Up @@ -73,6 +75,7 @@ class AttachmentServiceImpl(
)

}

@Transactional
override fun uploadAllAttachments(
contentEntity: AttachmentContentEntityType,
Expand Down Expand Up @@ -114,14 +117,14 @@ class AttachmentServiceImpl(
}

@Transactional
override fun createAttachmentResponses(attachments: List<AttachmentEntity>?): List<AttachmentResponse>{
override fun createAttachmentResponses(attachments: List<AttachmentEntity>?): List<AttachmentResponse> {
val list = mutableListOf<AttachmentResponse>()
if (attachments != null) {
for (attachment in attachments) {
if(attachment.isDeleted == false) {
if (attachment.isDeleted == false) {
val attachmentDto = AttachmentResponse(
name = attachment.filename,
url = "${endpointProperties.backend}/v1/attachment/${attachment.filename}",
url = "${endpointProperties.backend}/v1/file/${attachment.filename}",
bytes = attachment.size,
)
list.add(attachmentDto)
Expand All @@ -133,12 +136,15 @@ class AttachmentServiceImpl(
}

@Transactional
override fun updateAttachmentResponses(contentEntity: AttachmentContentEntityType, attachmentsList: List<AttachmentResponse>) {
override fun updateAttachmentResponses(
contentEntity: AttachmentContentEntityType,
attachmentsList: List<AttachmentResponse>
) {
val oldAttachments = contentEntity.bringAttachments().map { it.filename }

val attachmentsToRemove = oldAttachments - attachmentsList.map { it.name }

when(contentEntity) {
when (contentEntity) {
is SeminarEntity -> {
for (attachmentFilename in attachmentsToRemove) {
val attachmentEntity = attachmentRepository.findByFilename(attachmentFilename)
Expand All @@ -155,30 +161,36 @@ class AttachmentServiceImpl(
contentEntity.attachments.add(attachment)
attachment.news = contentEntity
}

is NoticeEntity -> {
contentEntity.attachments.add(attachment)
attachment.notice = contentEntity
}

is SeminarEntity -> {
contentEntity.attachments.add(attachment)
attachment.seminar = contentEntity
}

is AboutEntity -> {
contentEntity.attachments.add(attachment)
attachment.about = contentEntity
}

is AcademicsEntity -> {
contentEntity.attachments.add(attachment)
attachment.academics = contentEntity
}

is CourseEntity -> {
contentEntity.attachments.add(attachment)
attachment.course = contentEntity
}

is ResearchEntity -> {
contentEntity.attachments.add(attachment)
attachment.research = contentEntity
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.text.Charsets.UTF_8
@RequestMapping("/api/v1/file")
@RestController
class FileController(
@Value("\${csereal_mainImage.upload.path}")
@Value("\${csereal.upload.path}")
private val uploadPath: String
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ interface MainImageService {
contentEntityType: MainImageContentEntityType,
requestImage: MultipartFile,
): MainImageDto

fun createImageURL(image: MainImageEntity?): String?
}

@Service
class MainImageServiceImpl(
private val mainImageRepository: MainImageRepository,
@Value("\${csereal_mainImage.upload.path}")
@Value("\${csereal.upload.path}")
private val path: String,
private val endpointProperties: EndpointProperties
) : MainImageService {
Expand Down Expand Up @@ -119,6 +120,7 @@ class MainImageServiceImpl(
is ResearchEntity -> {
contentEntity.mainImage = mainImage
}

else -> {
throw WrongMethodTypeException("해당하는 엔티티가 없습니다")
}
Expand Down
18 changes: 4 additions & 14 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,9 @@ logging.level:
springframework:
security: DEBUG


csereal_mainImage:
upload:
path: ./attachment

csereal_attachment:
csereal:
upload:
path: ./attachment
path: ./files/

oldFiles:
path: ./cse-files
Expand All @@ -91,14 +86,9 @@ spring:
idsnucse:
redirect-uri: http://${URL}/api/v1/login/oauth2/code/idsnucse


csereal_mainImage:
upload:
path: /app/mainImage/

csereal_attachment:
csereal:
upload:
path: /app/attachment/
path: /app/files/

oldFiles:
path: /app/cse-files
Expand Down

0 comments on commit 0c83efb

Please sign in to comment.