update attachment & post
This commit is contained in:
@@ -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를 생성해야 하는 경우
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 첨부파일 저장
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user