Skip to content

Commit

Permalink
Merge pull request #50 from taking/taking
Browse files Browse the repository at this point in the history
Feat: Qsheet 일괄 다운로드, File 다운로드 중복 예외처리
  • Loading branch information
taking authored Oct 29, 2023
2 parents 5f1a81f + 178593c commit 758b35e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 66 deletions.
37 changes: 19 additions & 18 deletions nw/src/main/java/lab/cherry/nw/controller/QsheetController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,7 +124,7 @@ public Map<String, Object> 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);
Expand Down
75 changes: 30 additions & 45 deletions nw/src/main/java/lab/cherry/nw/service/Impl/QsheetServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -317,57 +325,34 @@ public Page<QsheetEntity> findPageByOrgId(String orgSeq, Pageable pageable) {
return qsheetRepository.findPageByOrgid(orgSeq, pageable);
}

public byte[] download(List<String> users) {
return null;
public Map<String, Object> download(String[] qsheetIds) {

// List<UserEntity> userList = new ArrayList<>();
// List<byte[]> 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<String, Object> 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;
}
}
}
3 changes: 2 additions & 1 deletion nw/src/main/java/lab/cherry/nw/service/QsheetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,5 +26,5 @@ public interface QsheetService {
Page<QsheetEntity> findPageByUserId(String userSeq, Pageable pageable);
Page<QsheetEntity> findPageByOrgId(String orgSeq, Pageable pageable);
// void updateOrgById(String id, List<String> orgIds);
byte[] download(List<String> users);
Map<String, Object> download(String[] qsheetSeq);
}
13 changes: 12 additions & 1 deletion nw/src/main/java/lab/cherry/nw/util/FormatConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,5 +100,14 @@ public static Map<String, Integer> 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();
}
}

}

0 comments on commit 758b35e

Please sign in to comment.