diff --git a/src/main/java/de/tum/in/www1/artemis/service/LectureImportService.java b/src/main/java/de/tum/in/www1/artemis/service/LectureImportService.java index 59fb3be846b1..baee4273aa21 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/LectureImportService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/LectureImportService.java @@ -1,9 +1,7 @@ package de.tum.in.www1.artemis.service; import static de.tum.in.www1.artemis.config.Constants.PROFILE_CORE; -import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; -import java.io.IOException; import java.net.URI; import java.nio.file.Path; import java.util.ArrayList; @@ -12,7 +10,6 @@ import java.util.Optional; import java.util.Set; -import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Profile; @@ -48,14 +45,21 @@ public class LectureImportService { private final Optional pyrisWebhookService; + private final FileService fileService; + + private final SlideSplitterService slideSplitterService; + private final Optional irisSettingsRepository; public LectureImportService(LectureRepository lectureRepository, LectureUnitRepository lectureUnitRepository, AttachmentRepository attachmentRepository, - Optional pyrisWebhookService, Optional irisSettingsRepository) { + Optional pyrisWebhookService, FileService fileService, SlideSplitterService slideSplitterService, + Optional irisSettingsRepository) { this.lectureRepository = lectureRepository; this.lectureUnitRepository = lectureUnitRepository; this.attachmentRepository = attachmentRepository; this.pyrisWebhookService = pyrisWebhookService; + this.fileService = fileService; + this.slideSplitterService = slideSplitterService; this.irisSettingsRepository = irisSettingsRepository; } @@ -96,12 +100,13 @@ public Lecture importLecture(final Lecture importedLecture, final Course course) log.debug("Importing attachments from lecture"); Set attachments = new HashSet<>(); for (Attachment attachment : importedLecture.getAttachments()) { - Attachment clonedAttachment = cloneAttachment(attachment); + Attachment clonedAttachment = cloneAttachment(lecture.getId(), attachment); clonedAttachment.setLecture(lecture); attachments.add(clonedAttachment); } lecture.setAttachments(attachments); attachmentRepository.saveAll(attachments); + // Send lectures to pyris if (pyrisWebhookService.isPresent() && irisSettingsRepository.isPresent()) { pyrisWebhookService.get().autoUpdateAttachmentUnitsInPyris(lecture.getCourse().getId(), @@ -143,9 +148,12 @@ else if (importedLectureUnit instanceof AttachmentUnit importedAttachmentUnit) { attachmentUnit.setLecture(newLecture); lectureUnitRepository.save(attachmentUnit); - Attachment attachment = cloneAttachment(importedAttachmentUnit.getAttachment()); + Attachment attachment = cloneAttachment(attachmentUnit.getId(), importedAttachmentUnit.getAttachment()); attachment.setAttachmentUnit(attachmentUnit); attachmentRepository.save(attachment); + if (attachment.getLink().endsWith(".pdf")) { + slideSplitterService.splitAttachmentUnitIntoSingleSlides(attachmentUnit); + } attachmentUnit.setAttachment(attachment); return attachmentUnit; } @@ -169,10 +177,11 @@ else if (importedLectureUnit instanceof ExerciseUnit) { /** * This helper function clones the {@code importedAttachment} (and duplicates its file) and returns it * + * @param entityId The id of the new entity to which the attachment is linked * @param importedAttachment The original attachment to be copied * @return The cloned attachment with the file also duplicated to the temp directory on disk */ - private Attachment cloneAttachment(final Attachment importedAttachment) { + private Attachment cloneAttachment(Long entityId, final Attachment importedAttachment) { log.debug("Creating a new Attachment from attachment {}", importedAttachment); Attachment attachment = new Attachment(); @@ -183,18 +192,16 @@ private Attachment cloneAttachment(final Attachment importedAttachment) { attachment.setAttachmentType(importedAttachment.getAttachmentType()); Path oldPath = FilePathService.actualPathForPublicPathOrThrow(URI.create(importedAttachment.getLink())); - Path tempPath = FilePathService.getTempFilePath().resolve(oldPath.getFileName()); - - try { - log.debug("Copying attachment file from {} to {}", oldPath, tempPath); - FileUtils.copyFile(oldPath.toFile(), tempPath.toFile(), REPLACE_EXISTING); - - // File was copied to a temp directory and will be moved once we persist the attachment - attachment.setLink(FilePathService.publicPathForActualPathOrThrow(tempPath, null).toString()); + Path newPath; + if (oldPath.toString().contains("/attachment-unit/")) { + newPath = FilePathService.getAttachmentUnitFilePath().resolve(entityId.toString()); } - catch (IOException e) { - log.error("Error while copying file", e); + else { + newPath = FilePathService.getLectureAttachmentFilePath().resolve(entityId.toString()); } + log.debug("Copying attachment file from {} to {}", oldPath, newPath); + Path savePath = fileService.copyExistingFileToTarget(oldPath, newPath); + attachment.setLink(FilePathService.publicPathForActualPathOrThrow(savePath, entityId).toString()); return attachment; } } diff --git a/src/test/java/de/tum/in/www1/artemis/lecture/LectureIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/lecture/LectureIntegrationTest.java index 0da05efa33e3..e5142c551b03 100644 --- a/src/test/java/de/tum/in/www1/artemis/lecture/LectureIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/lecture/LectureIntegrationTest.java @@ -126,8 +126,7 @@ void initTestCase() throws Exception { } private void addAttachmentToLecture() { - this.attachmentDirectOfLecture = LectureFactory.generateAttachment(null); - this.attachmentDirectOfLecture.setLink("/api/files/temp/example2.txt"); + this.attachmentDirectOfLecture = LectureFactory.generateAttachmentWithFile(null, this.lecture1.getId(), false); this.attachmentDirectOfLecture.setLecture(this.lecture1); this.attachmentDirectOfLecture = attachmentRepository.save(this.attachmentDirectOfLecture); this.lecture1.addAttachments(this.attachmentDirectOfLecture);