55import org .example .backend .domain .classroom .exception .ClassroomErrorCode ;
66import org .example .backend .domain .classroom .exception .ClassroomException ;
77import org .example .backend .domain .classroom .repository .ClassroomRepository ;
8+ import org .example .backend .domain .lectureNote .converter .FileToMultipartFileConverter ;
9+ import org .example .backend .domain .lectureNote .converter .LibreOfficeConverter ;
810import org .example .backend .domain .lectureNote .dto .response .LectureNoteKeyResponseDTO ;
911import org .example .backend .domain .lectureNote .dto .response .LectureNoteResponseDTO ;
1012import org .example .backend .domain .lectureNote .entity .LectureNote ;
1719import org .springframework .stereotype .Service ;
1820import org .springframework .web .multipart .MultipartFile ;
1921
22+ import java .io .File ;
2023import java .io .IOException ;
2124import java .util .ArrayList ;
2225import java .util .List ;
@@ -29,31 +32,67 @@ public class LectureNoteServiceImpl implements LectureNoteService {
2932
3033 private final S3Service s3Service ;
3134 private final LectureNoteRepository lectureNoteRepository ;
32- private final ClassroomRepository classroomRepository ; // ✅ 추가
35+ private final ClassroomRepository classroomRepository ;
3336 private final LectureNoteMappingRepository lectureNoteMappingRepository ;
37+ private final LibreOfficeConverter libreOfficeConverter ;
3438
3539 public List <LectureNote > uploadLectureNotes (UUID classId , List <MultipartFile > files ) throws IOException {
36- // 1. 여러 파일에 대해 처리
3740 List <LectureNote > lectureNotes = new ArrayList <>();
3841
39- // 2. 각 파일 업로드 처리
4042 for (MultipartFile file : files ) {
41- // 1) S3에 업로드
42- String key = "lecture_note/" + classId + "/" + UUID .randomUUID () + "/" + file .getOriginalFilename ();
43- String fileUrl = s3Service .uploadFile (file , key );
44-
45- // 2) classId로 Classroom 엔티티 조회
46- Classroom classroom = classroomRepository .findById (classId )
47- .orElseThrow (() -> new ClassroomException (ClassroomErrorCode .CLASS_NOT_FOUND ));
48-
49- // 3) LectureNote 객체 생성
50- LectureNote lectureNote = LectureNote .builder ()
51- .noteUrl (key )
52- .classroom (classroom ) // Classroom 객체 넣기
53- .build ();
54-
55- // 4) LectureNote 저장
56- lectureNotes .add (lectureNoteRepository .save (lectureNote ));
43+ String originalFilename = Objects .requireNonNull (file .getOriginalFilename (), "파일명이 필요합니다" );
44+ boolean isPptx = originalFilename .toLowerCase ().endsWith (".pptx" );
45+
46+ File tempSrc = null ;
47+ File convertedPdf = null ;
48+ String uploadFileName ;
49+ String key ;
50+ MultipartFile pdfMultipart ;
51+
52+ try {
53+ if (isPptx ) {
54+ // MultipartFile -> File
55+ tempSrc = new File (System .getProperty ("java.io.tmpdir" ), originalFilename );
56+ file .transferTo (tempSrc );
57+
58+ try {
59+ // PPTX → PDF 변환
60+ convertedPdf = libreOfficeConverter .convertPptxToPdf (tempSrc );
61+
62+ // File -> MultipartFile
63+ pdfMultipart = new FileToMultipartFileConverter (convertedPdf , "application/pdf" );
64+ } catch (Exception e ) {
65+ throw new IOException ("PPTX→PDF 변환 실패: " + originalFilename , e );
66+ }
67+
68+ uploadFileName = convertedPdf .getName ();
69+
70+ // S3 업로드
71+ key = "lecture_note/" + classId + "/" + UUID .randomUUID () + "/" + uploadFileName ;
72+ s3Service .uploadFile (pdfMultipart , key );
73+
74+ } else {
75+ // pptx 외 다른 파일들 그대로 업로드
76+ uploadFileName = originalFilename ;
77+ key = "lecture_note/" + classId + "/" + UUID .randomUUID () + "/" + uploadFileName ;
78+ s3Service .uploadFile (file , key );
79+ }
80+
81+ Classroom classroom = classroomRepository .findById (classId )
82+ .orElseThrow (() -> new ClassroomException (ClassroomErrorCode .CLASS_NOT_FOUND ));
83+
84+ LectureNote lectureNote = LectureNote .builder ()
85+ .noteUrl (key )
86+ .classroom (classroom )
87+ .build ();
88+
89+ lectureNotes .add (lectureNoteRepository .save (lectureNote ));
90+
91+ } finally {
92+ // pdf로 변환하며 생긴 임시 파일 삭제
93+ if (tempSrc != null && tempSrc .exists ()) tempSrc .delete ();
94+ if (convertedPdf != null && convertedPdf .exists ()) convertedPdf .delete ();
95+ }
5796 }
5897
5998 return lectureNotes ;
0 commit comments