Skip to content

Commit

Permalink
Lectures: Fix file copying for attachment import (#9111)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesWt authored Jul 28, 2024
1 parent 510ddaf commit eac818a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -48,14 +45,21 @@ public class LectureImportService {

private final Optional<PyrisWebhookService> pyrisWebhookService;

private final FileService fileService;

private final SlideSplitterService slideSplitterService;

private final Optional<IrisSettingsRepository> irisSettingsRepository;

public LectureImportService(LectureRepository lectureRepository, LectureUnitRepository lectureUnitRepository, AttachmentRepository attachmentRepository,
Optional<PyrisWebhookService> pyrisWebhookService, Optional<IrisSettingsRepository> irisSettingsRepository) {
Optional<PyrisWebhookService> pyrisWebhookService, FileService fileService, SlideSplitterService slideSplitterService,
Optional<IrisSettingsRepository> irisSettingsRepository) {
this.lectureRepository = lectureRepository;
this.lectureUnitRepository = lectureUnitRepository;
this.attachmentRepository = attachmentRepository;
this.pyrisWebhookService = pyrisWebhookService;
this.fileService = fileService;
this.slideSplitterService = slideSplitterService;
this.irisSettingsRepository = irisSettingsRepository;
}

Expand Down Expand Up @@ -96,12 +100,13 @@ public Lecture importLecture(final Lecture importedLecture, final Course course)
log.debug("Importing attachments from lecture");
Set<Attachment> 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(),
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit eac818a

Please sign in to comment.