Skip to content

Commit 7c5bf67

Browse files
Merge pull request #387 from Podo-Store/develop
[FIX] 어드민 대본 다운로드
2 parents 12b66fe + 2e2df47 commit 7c5bf67

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/main/java/PodoeMarket/podoemarket/admin/service/AdminService.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
import org.springframework.data.domain.Sort;
2727
import org.springframework.stereotype.Service;
2828

29+
import java.io.ByteArrayInputStream;
2930
import java.io.ByteArrayOutputStream;
3031
import java.io.IOException;
3132
import java.io.InputStream;
3233
import java.time.LocalDateTime;
3334
import java.util.List;
3435
import java.util.UUID;
3536
import java.util.stream.Collectors;
37+
import java.util.zip.ZipEntry;
38+
import java.util.zip.ZipInputStream;
3639

3740
@RequiredArgsConstructor
3841
@Slf4j
@@ -145,20 +148,33 @@ public void checkExpire(final LocalDateTime updatedAt, final ProductStatus produ
145148
public byte[] downloadFile(final String fileKey) {
146149
try (S3Object s3Object = amazonS3.getObject(bucket, fileKey);
147150
InputStream inputStream = s3Object.getObjectContent();
148-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
151+
ByteArrayOutputStream zipBuffer = new ByteArrayOutputStream()) {
149152

150-
// 버퍼를 사용하여 데이터 읽기
151153
byte[] buffer = new byte[4096];
152154
int bytesRead;
153155
while ((bytesRead = inputStream.read(buffer)) != -1) {
154-
outputStream.write(buffer, 0, bytesRead);
156+
zipBuffer.write(buffer, 0, bytesRead);
155157
}
156158

157-
return outputStream.toByteArray();
158-
} catch (AmazonS3Exception e) {
159-
throw new RuntimeException("S3에서 파일을 찾을 수 없습니다: " + fileKey);
159+
// ZIP 해제
160+
try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipBuffer.toByteArray()))) {
161+
ZipEntry entry = zipInputStream.getNextEntry();
162+
163+
if (entry != null) {
164+
ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
165+
166+
// ZIP 안의 실제 PDF 바이트만 꺼냄
167+
while ((bytesRead = zipInputStream.read(buffer)) != -1) {
168+
pdfBuffer.write(buffer, 0, bytesRead);
169+
}
170+
171+
return pdfBuffer.toByteArray();
172+
} else {
173+
throw new RuntimeException("ZIP 파일 안에 PDF가 없습니다.");
174+
}
175+
}
160176
} catch (IOException e) {
161-
throw new RuntimeException("파일 다운로드 중 오류가 발생했습니다: " + fileKey);
177+
throw new RuntimeException("파일 다운로드 중 오류 발생: " + fileKey, e);
162178
}
163179
}
164180

0 commit comments

Comments
 (0)