From 178593c4a96e4970e8c389e9d51d7bfbe2a3e400 Mon Sep 17 00:00:00 2001 From: taking Date: Sun, 29 Oct 2023 06:45:44 +0000 Subject: [PATCH] =?UTF-8?q?Feat:=20Qsheet=20=EC=9D=BC=EA=B4=84=20=EB=8B=A4?= =?UTF-8?q?=EC=9A=B4=EB=A1=9C=EB=93=9C,=20File=20=EB=8B=A4=EC=9A=B4?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EC=A4=91=EB=B3=B5=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nw/controller/QsheetController.java | 37 ++++----- .../nw/service/Impl/FileServiceImpl.java | 3 +- .../nw/service/Impl/QsheetServiceImpl.java | 75 ++++++++----------- .../lab/cherry/nw/service/QsheetService.java | 3 +- .../lab/cherry/nw/util/FormatConverter.java | 13 +++- 5 files changed, 65 insertions(+), 66 deletions(-) diff --git a/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java b/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java index 693a85b..7610cd6 100644 --- a/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java +++ b/nw/src/main/java/lab/cherry/nw/controller/QsheetController.java @@ -1,6 +1,7 @@ package lab.cherry.nw.controller; import java.util.List; +import java.util.Map; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -9,13 +10,11 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; @@ -183,24 +182,26 @@ public ResponseEntity findByQsheetId(@PathVariable("id") String id) { return new ResponseEntity<>(qsheetService.findById(id), new HttpHeaders(), HttpStatus.OK); } -// /** -// * [QsheetController] 큐시트 사용자 파일 다운로드 함수 -// * -// * @return 큐시트 사용자 파일을 반환합니다. -// * -// * Author : taking(taking@duck.com) -// */ -// @PostMapping("/download") -// @Operation(summary = "큐시트 사용자 파일 다운로드", description = "큐시트 사용자 파일을 다운로드합니다.") -// public ResponseEntity downloadQsheetBySeq(@RequestBody QsheetEntity.QsheetDownloadDto qsheetDownloadDto) { - -// log.info("[QsheetController] downloadQsheetBySeq...!"); + /** + * [QsheetController] 큐시트 사용자 파일 다운로드 함수 + * + * @return 큐시트 사용자 파일을 반환합니다. + * + * Author : taking(taking@duck.com) + */ + @PostMapping("/download/{qsheetIds}") + @Operation(summary = "큐시트 사용자 파일 다운로드", description = "큐시트 사용자 파일을 다운로드합니다.") + public ResponseEntity downloadQsheetBySeq(@PathVariable("qsheetIds") String[] qsheetIds) { + log.info("[QsheetController] downloadQsheetBySeq...!"); -// HttpHeaders headers = new HttpHeaders(); -// headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "download.zip"); -// return new ResponseEntity<>(qsheetService.download(qsheetDownloadDto.getUser()), new HttpHeaders(), HttpStatus.OK); + Map val = qsheetService.download(qsheetIds); -// } + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.parseMediaType("application/zip")); + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + val.get("name") + "\""); + + return new ResponseEntity<>(val.get("data"), headers, HttpStatus.OK); + } } \ No newline at end of file diff --git a/nw/src/main/java/lab/cherry/nw/service/Impl/FileServiceImpl.java b/nw/src/main/java/lab/cherry/nw/service/Impl/FileServiceImpl.java index 0d76289..54c156f 100644 --- a/nw/src/main/java/lab/cherry/nw/service/Impl/FileServiceImpl.java +++ b/nw/src/main/java/lab/cherry/nw/service/Impl/FileServiceImpl.java @@ -34,6 +34,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FilenameUtils; +import org.bson.types.ObjectId; @Slf4j @Service @@ -123,7 +124,7 @@ public Map downloadFiles(String key, String value) { ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream)) { for (GridFSFile file : allFiles) { - String fileName = FilenameUtils.getBaseName(file.getFilename()) + (Math.random() * 10) + FilenameUtils.getExtension(file.getFilename()); + String fileName = FilenameUtils.getBaseName(file.getFilename()) + "-" + new ObjectId() + "." + FilenameUtils.getExtension(file.getFilename()); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); diff --git a/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java b/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java index fb6c9d2..d41798c 100644 --- a/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java +++ b/nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java @@ -1,16 +1,23 @@ package lab.cherry.nw.service.Impl; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.time.Instant; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.io.FilenameUtils; import org.bson.types.ObjectId; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import com.mongodb.client.gridfs.model.GridFSFile; import lab.cherry.nw.error.enums.ErrorCode; import lab.cherry.nw.error.exception.CustomException; import lab.cherry.nw.error.exception.EntityNotFoundException; @@ -26,6 +33,7 @@ import lab.cherry.nw.service.QsheetLogService; import lab.cherry.nw.service.QsheetService; import lab.cherry.nw.service.UserService; +import lab.cherry.nw.util.FormatConverter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -317,57 +325,34 @@ public Page findPageByOrgId(String orgSeq, Pageable pageable) { return qsheetRepository.findPageByOrgid(orgSeq, pageable); } - public byte[] download(List users) { - return null; + public Map download(String[] qsheetIds) { - // List userList = new ArrayList<>(); - // List userData = new ArrayList<>(); - - // for(String user : users) { - - // if (userService.checkId(user)) { - // UserEntity _user = userService.findById(user); - // userList.add(_user); - - // // String objectName = _user.getId() + "/"; - // // userData.add(fileService.downloadZip("user", objectName)); - // } - // } - - // if(userList.size() > 1) { - - // log.error("userList 가 1명 초과인 경우 # ", userList.size()); + + try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream)) { + for (String qsheetId : qsheetIds) { - // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - // try (ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream)) { - // for (UserEntity user : userList) { + QsheetEntity qsheetEntity = findById(qsheetId); - // String objectName = user.getId() +"/"; + String qsheetName = qsheetEntity.getName() + "-" + new ObjectId() + ".zip"; - // byte[] objectData = fileService.downloadZip("user", objectName); + ZipEntry zipEntry = new ZipEntry(qsheetName); + zipOut.putNextEntry(zipEntry); - // // Zip 아카이브에 객체 추가 - // ZipArchiveEntry zipEntry = new ZipArchiveEntry(user.getUsername() + ".zip"); - // zipOut.putNextEntry(zipEntry); - // zipOut.write(objectData); - // zipOut.closeEntry(); + Object objectData = fileService.downloadFiles("seq", qsheetId).get("data"); + zipOut.write(FormatConverter.convertObjectToBytes(objectData)); + zipOut.closeEntry(); + } + zipOut.finish(); - // } - // } catch (IOException e) { - // log.error("{}", e); - // } + Map returnVal = new HashMap<>(); + returnVal.put("name", "download" + ".zip"); + returnVal.put("data", byteArrayOutputStream.toByteArray()); - // byte[] zipBytes = byteArrayOutputStream.toByteArray(); - - // return zipBytes; - - // } else { - - // UserEntity user = userService.findById(users.get(0)); - - // String objectName = "사용자/" + user.getId(); - - // return fileService.downloadZip("user", objectName); - // } + return returnVal; + } catch (IOException e) { + log.error("Error creating and sending the zip file: {}", e); + return null; + } } } \ No newline at end of file diff --git a/nw/src/main/java/lab/cherry/nw/service/QsheetService.java b/nw/src/main/java/lab/cherry/nw/service/QsheetService.java index 11cc89e..aa9a75b 100644 --- a/nw/src/main/java/lab/cherry/nw/service/QsheetService.java +++ b/nw/src/main/java/lab/cherry/nw/service/QsheetService.java @@ -2,6 +2,7 @@ import lab.cherry.nw.model.QsheetEntity; import java.util.List; +import java.util.Map; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; @@ -25,5 +26,5 @@ public interface QsheetService { Page findPageByUserId(String userSeq, Pageable pageable); Page findPageByOrgId(String orgSeq, Pageable pageable); // void updateOrgById(String id, List orgIds); -byte[] download(List users); +Map download(String[] qsheetSeq); } \ No newline at end of file diff --git a/nw/src/main/java/lab/cherry/nw/util/FormatConverter.java b/nw/src/main/java/lab/cherry/nw/util/FormatConverter.java index 60a1cf4..9754a98 100644 --- a/nw/src/main/java/lab/cherry/nw/util/FormatConverter.java +++ b/nw/src/main/java/lab/cherry/nw/util/FormatConverter.java @@ -6,9 +6,11 @@ import lombok.extern.slf4j.Slf4j; import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.io.ObjectOutputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -98,5 +100,14 @@ public static Map convertDateFormat(String _date) { e.printStackTrace(); } return returnVal; - } + } + + public static byte[] convertObjectToBytes(Object obj) throws IOException { + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + try (ObjectOutputStream ois = new ObjectOutputStream(boas)) { + ois.writeObject(obj); + return boas.toByteArray(); + } +} + } \ No newline at end of file