develop #2
@@ -104,6 +104,18 @@ public class ReviewController {
|
||||
);
|
||||
}
|
||||
|
||||
@Operation(summary = "평가/모니터링 원본 파일 압축 다운로드")
|
||||
@GetMapping("/download/{performanceId}/{reviewType}")
|
||||
public ResponseEntity<Resource> downloadOriginalFileZip(
|
||||
@PathVariable Integer performanceId,
|
||||
@PathVariable Integer reviewType
|
||||
) {
|
||||
return download(
|
||||
reviewService.getOriginalFileZipResource(performanceId, reviewType),
|
||||
reviewService.getOriginalFileZipName(performanceId, reviewType)
|
||||
);
|
||||
}
|
||||
|
||||
@Operation(summary = "PDF 변환 파일 조회/다운로드")
|
||||
@GetMapping("/pdf/{performanceId}/{reviewType}/{userId}")
|
||||
public ResponseEntity<Resource> downloadPdfOrOriginalFile(
|
||||
|
||||
@@ -18,6 +18,8 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.PathResource;
|
||||
@@ -184,6 +186,95 @@ public class ReviewService extends AbstractFileService {
|
||||
return removeTimestampPrefix(getReviewEntity(performanceId, reviewType, userId).getUploadFileName());
|
||||
}
|
||||
|
||||
public PathResource getOriginalFileZipResource(Integer performanceId, Integer reviewType) {
|
||||
List<TPerformanceReviewEntity> reviews = performanceReviewRepository.findAll()
|
||||
.stream()
|
||||
.filter(review -> isSamePerformanceGroup(performanceId, review.getPerformanceId()))
|
||||
.filter(review -> Integer.valueOf(reviewType).equals(review.getReviewType()))
|
||||
.filter(review -> StringUtil.hasText(review.getUploadFileName()))
|
||||
.toList();
|
||||
if (reviews.isEmpty()) {
|
||||
throw BusinessException.notFound("REVIEW_FILE_NOT_FOUND", "평가/모니터링 파일이 없습니다.")
|
||||
.with("performanceId", performanceId)
|
||||
.with("reviewType", reviewType);
|
||||
}
|
||||
|
||||
try {
|
||||
Path zip = Files.createTempFile(getOriginalFileZipBaseName(performanceId, reviewType), ".zip");
|
||||
int entryCount = 0;
|
||||
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(zip))) {
|
||||
Set<String> entryNames = new HashSet<>();
|
||||
for (TPerformanceReviewEntity review : reviews) {
|
||||
Path sourceDir = reviewDir(review.getPerformanceId(), reviewType);
|
||||
Path source = sourceDir.resolve(review.getUploadFileName()).normalize();
|
||||
if (!source.startsWith(sourceDir) || !Files.exists(source) || !Files.isRegularFile(source)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String entryName = buildZipEntryName(review, entryNames);
|
||||
out.putNextEntry(new ZipEntry(entryName));
|
||||
Files.copy(source, out);
|
||||
out.closeEntry();
|
||||
entryCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (entryCount == 0) {
|
||||
Files.deleteIfExists(zip);
|
||||
throw BusinessException.notFound("REVIEW_FILE_NOT_FOUND", "평가/모니터링 파일이 없습니다.")
|
||||
.with("performanceId", performanceId)
|
||||
.with("reviewType", reviewType);
|
||||
}
|
||||
|
||||
return new PathResource(zip);
|
||||
} catch (IOException e) {
|
||||
throw BusinessException.internalError("REVIEW_FILE_ZIP_FAILED", "평가/모니터링 파일 압축 중 오류가 발생했습니다.");
|
||||
}
|
||||
}
|
||||
|
||||
public String getOriginalFileZipName(Integer performanceId, Integer reviewType) {
|
||||
return getOriginalFileZipBaseName(performanceId, reviewType) + ".zip";
|
||||
}
|
||||
|
||||
private String buildZipEntryName(TPerformanceReviewEntity review, Set<String> entryNames) {
|
||||
String userId = StringUtil.isNullOrBlank(review.getRegUser()) ? "user" : review.getRegUser();
|
||||
String fileName = removeTimestampPrefix(review.getUploadFileName());
|
||||
String entryName = sanitizeZipEntryName(review.getPerformanceId() + "_" + userId + "_" + fileName);
|
||||
if (entryNames.add(entryName)) {
|
||||
return entryName;
|
||||
}
|
||||
|
||||
String baseName = stripExtension(entryName);
|
||||
String extension = extractExtension(entryName);
|
||||
int index = 2;
|
||||
while (true) {
|
||||
String candidate = baseName + "_" + index + extension;
|
||||
if (entryNames.add(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private String sanitizeZipEntryName(String fileName) {
|
||||
String sanitized = Path.of(fileName).getFileName().toString()
|
||||
.replace('\\', '_')
|
||||
.replace('/', '_');
|
||||
return StringUtil.isNullOrBlank(sanitized) ? "review-file" : sanitized;
|
||||
}
|
||||
|
||||
private String getOriginalFileZipBaseName(Integer performanceId, Integer reviewType) {
|
||||
return "review_" + performanceId + "_" + reviewType;
|
||||
}
|
||||
|
||||
private boolean isSamePerformanceGroup(Integer requestedPerformanceId, Integer targetPerformanceId) {
|
||||
if (requestedPerformanceId == null || targetPerformanceId == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return requestedPerformanceId / 100 == targetPerformanceId / 100;
|
||||
}
|
||||
|
||||
public PathResource getPdfOrOriginalFileResource(Integer performanceId, Integer reviewType, String userId) {
|
||||
TPerformanceReviewEntity entity = getReviewEntity(performanceId, reviewType, userId);
|
||||
String fileName = StringUtil.isNullOrBlank(entity.getPdfFileName())
|
||||
|
||||
Reference in New Issue
Block a user