update attachment & post

This commit is contained in:
jooho
2021-11-02 17:15:27 +09:00
parent 06975932b6
commit c50df972e9
13 changed files with 229 additions and 90 deletions

View File

@@ -19,7 +19,6 @@ import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
/**
@@ -125,7 +124,6 @@ public class AttachmentApiController {
*/
@GetMapping(value = "/api/v1/download/{uniqueId}")
public ResponseEntity<?> downloadFile(@PathVariable String uniqueId) {
AttachmentDownloadResponseDto downloadFile = attachmentService.downloadFile(uniqueId);
String mimeType = null;
@@ -166,6 +164,44 @@ public class AttachmentApiController {
return attachmentService.findByCode(attachmentCode);
}
/**
* 첨부파일 다운로드
*
* @param uniqueId
* @return
* @throws IOException
*/
@GetMapping(value = "/api/v1/attachments/download/{uniqueId}")
public ResponseEntity<?> downloadAttachment(@PathVariable String uniqueId) {
AttachmentDownloadResponseDto downloadFile = attachmentService.downloadAttachment(uniqueId);
String mimeType = null;
try {
// get mime type
URLConnection connection = new URL(downloadFile.getFile().getURL().toString()).openConnection();
mimeType = connection.getContentType();
} catch (IOException ex) {
log.error("download fail", ex);
throw new BusinessMessageException("Sorry. download fail... \uD83D\uDE3F");
}
if (mimeType == null) {
mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
}
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(downloadFile.getOriginalFileName(), StandardCharsets.UTF_8)
.build();
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, mimeType);
headers.setContentDisposition(contentDisposition);
return ResponseEntity.ok()
.headers(headers)
.body(downloadFile.getFile());
}
/**
* 첨부파일 저장 - 물리적 파일은 .temp로 저장 된 후 호출되어야 함
* 새롭게 attachment code를 생성해야 하는 경우

View File

@@ -1,6 +1,5 @@
package org.egovframe.cloud.portalservice.service.attachment;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
@@ -21,8 +20,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@@ -170,7 +167,7 @@ public class AttachmentService extends AbstractService {
}
/**
* 첨부파일 다운로드
* 첨부파일 다운로드 - 삭제 파일 불가
*
* @param uniqueId
* @return
@@ -180,6 +177,10 @@ public class AttachmentService extends AbstractService {
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
if (Boolean.TRUE.equals(attachment.getIsDelete())) {
throw new BusinessMessageException(getMessage("err.entity.not.found"));
}
Resource resource = storageUtils.downloadFile(attachment.getPhysicalFileName());
// 첨부파일 다운로드 할 때 마다 Download 횟수 + 1
@@ -205,6 +206,28 @@ public class AttachmentService extends AbstractService {
.collect(Collectors.toList());
}
/**
* 첨부파일 다운로드 - 삭제 파일 가능
*
* @param uniqueId
* @return
*/
public AttachmentDownloadResponseDto downloadAttachment(String uniqueId) {
Attachment attachment = attachmentRepository.findAllByUniqueId(uniqueId)
// 파일을 찾을 수 없습니다.
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.file.not_found") + " ID= " + uniqueId));
Resource resource = storageUtils.downloadFile(attachment.getPhysicalFileName());
// 첨부파일 다운로드 할 때 마다 Download 횟수 + 1
attachment.updateDownloadCnt();
return AttachmentDownloadResponseDto.builder()
.file(resource)
.originalFileName(attachment.getOriginalFileName())
.build();
}
/**
* 첨부파일 저장
*

View File

@@ -156,40 +156,5 @@ class AttachmentRepositoryTest {
System.out.println(byId.get());
}
@Test
public void 중복된유니크인덱스_오류() throws Exception {
//given
String code = UUID.randomUUID().toString();
String uniqueId = UUID.randomUUID().toString();
AttachmentId attachmentId1 = AttachmentId.builder()
.code(code)
.seq(1L).build();
Attachment attachment1 = Attachment.builder()
.attachmentId(attachmentId1)
.uniqueId(uniqueId)
.originalFileName("test1.png")
.physicalFileName(UUID.randomUUID().toString())
.size(1232L)
.build();
AttachmentId attachmentId2 = AttachmentId.builder()
.code(code)
.seq(2L)
.build();
Attachment attachment2 = Attachment.builder()
.attachmentId(attachmentId2)
.uniqueId(uniqueId)
.originalFileName("test2.png")
.physicalFileName(UUID.randomUUID().toString())
.size(1232L)
.build();
//when
attachmentRepository.save(attachment1);
attachmentRepository.save(attachment2);
//then
}
}

View File

@@ -27,8 +27,7 @@ INSERT INTO `authorization` (authorization_name,url_pattern_value,http_method_co
('사용자 정보 수정','/user-service/api/v1/users/info/?*','PUT',126,'65a00f65-8460-49af-98ec-042977e56f4b',now(),'65a00f65-8460-49af-98ec-042977e56f4b',now()),
('사용자 회원탈퇴','/user-service/api/v1/users/leave','POST',127,'65a00f65-8460-49af-98ec-042977e56f4b',now(),'65a00f65-8460-49af-98ec-042977e56f4b',now()),
('사용자 삭제','/user-service/api/v1/users/delete/?*','DELETE',128,'65a00f65-8460-49af-98ec-042977e56f4b',now(),'65a00f65-8460-49af-98ec-042977e56f4b',now()),
('사용자 소셜 정보 조회,'/user-service/api/v1/users/social','POST',129,'65a00f65-8460-49af-98ec-042977e56f4b',now(),'65a00f65-8460-49af-98ec-042977e56f4b',now());
('사용자 소셜 정보 조회','/user-service/api/v1/users/social','POST',129,'65a00f65-8460-49af-98ec-042977e56f4b',now(),'65a00f65-8460-49af-98ec-042977e56f4b',now());
INSERT INTO `role` (role_id,role_name,role_content,sort_seq,created_date) VALUES
('ROLE_ADMIN','시스템 관리자','시스템 관리자 권한',101,'2021-10-20 13:39:15'),