Skip to content

Commit

Permalink
Create paths for student version
Browse files Browse the repository at this point in the history
  • Loading branch information
eceeeren committed Dec 18, 2024
1 parent cc3bca2 commit 71f7466
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ private static Path actualPathForPublicAttachmentUnitFilePath(URI publicPath, St
String attachmentUnitId = path.getName(4).toString();
return getAttachmentUnitFilePath().resolve(Path.of(attachmentUnitId, filename));
}
else if (publicPath.toString().contains("/student")) {
String attachmentUnitId = path.getName(4).toString();
return getAttachmentUnitFilePath().resolve(Path.of(attachmentUnitId, "student", filename));
}
try {
String attachmentUnitId = path.getName(4).toString();
String slideId = path.getName(6).toString();
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public ResponseEntity<byte[]> getLecturePdfAttachmentsMerged(@PathVariable Long
* @return The requested file, 403 if the logged-in user is not allowed to access it, or 404 if the file doesn't exist
*/
@GetMapping("files/attachments/attachment-unit/{attachmentUnitId}/*")
@EnforceAtLeastStudent
@EnforceAtLeastEditorInCourse
public ResponseEntity<byte[]> getAttachmentUnitAttachment(@PathVariable Long attachmentUnitId) {
log.debug("REST request to get file for attachment unit : {}", attachmentUnitId);
AttachmentUnit attachmentUnit = attachmentUnitRepository.findByIdElseThrow(attachmentUnitId);
Expand Down Expand Up @@ -564,6 +564,30 @@ public ResponseEntity<byte[]> getAttachmentUnitAttachmentSlide(@PathVariable Lon
}
}

/**
* GET files/attachments/attachment-unit/{attachmentUnitId}/student/* : Get the student version of attachment unit by attachment unit id
*
* @param attachmentUnitId ID of the attachment unit, the student version belongs to
* @return The requested file, 403 if the logged-in user is not allowed to access it, or 404 if the file doesn't exist
*/
@GetMapping("files/attachments/attachment-unit/{attachmentUnitId}/student/*")
@EnforceAtLeastStudent
public ResponseEntity<byte[]> getAttachmentUnitHiddenAttachment(@PathVariable Long attachmentUnitId) {
log.debug("REST request to get the hidden version of attachment Unit : {}", attachmentUnitId);
AttachmentUnit attachmentUnit = attachmentUnitRepository.findByIdElseThrow(attachmentUnitId);
Attachment attachment = attachmentUnit.getAttachment();

// check if hidden link is available in the attachment
String hiddenLink = attachment.getHiddenLink();
if (hiddenLink == null) {
return buildFileResponse(getActualPathFromPublicPathString(attachment.getLink()), false);
}

String fileName = hiddenLink.substring(hiddenLink.lastIndexOf("/") + 1);

return buildFileResponse(FilePathService.getAttachmentUnitFilePath().resolve(Path.of(attachmentUnit.getId().toString(), "student")), fileName, false);
}

/**
* Builds the response with headers, body and content type for specified path and file name
*
Expand Down Expand Up @@ -714,4 +738,5 @@ private static void sanitizeFilenameElseThrow(String filename) {
throw new EntityNotFoundException("The filename contains invalid characters. Only characters a-z, A-Z, 0-9, '_', '.' and '-' are allowed!");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ public ResponseEntity<Attachment> updateAttachment(@PathVariable Long attachment
// Update hidden file logic
Path basePath = FilePathService.getAttachmentUnitFilePath().resolve(originalAttachment.getAttachmentUnit().getId().toString());
Path savePath = fileService.saveFile(hiddenFile, basePath, true);
attachment.setHiddenLink(FilePathService.publicPathForActualPath(savePath, originalAttachment.getAttachmentUnit().getId()).toString() + "v2");
attachment.setHiddenLink(FilePathService.publicPathForActualPath(savePath, originalAttachment.getAttachmentUnit().getId()).toString());

// Delete the old hidden file
if (originalAttachment.getHiddenLink() != null) {
URI oldHiddenPath = URI.create(originalAttachment.getHiddenLink());
fileService.schedulePathForDeletion(FilePathService.actualPathForPublicPathOrThrow(oldHiddenPath), 0);
fileService.evictCacheForPath(FilePathService.actualPathForPublicPathOrThrow(oldHiddenPath));
this.fileService.evictCacheForPath(FilePathService.actualPathForPublicPathOrThrow(oldHiddenPath));
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/webapp/app/lecture/pdf-preview/pdf-preview.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
* Updates the existing attachment file or creates a new hidden version of the attachment.
*/
async updateAttachmentWithFile(): Promise<void> {
const pdfFile = new File([this.currentPdfBlob()!], 'updatedAttachment.pdf', { type: 'application/pdf' });
const pdfFile = new File([this.currentPdfBlob()!], '.pdf', { type: 'application/pdf' });

if (pdfFile.size > MAX_FILE_SIZE) {
this.alertService.error('artemisApp.attachment.pdfPreview.fileSizeError');
Expand All @@ -175,7 +175,6 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
} else if (this.attachmentUnit()) {
const finalHiddenPages = this.getHiddenPages();
this.attachmentToBeEdited.set(this.attachmentUnit()!.attachment!);
//this.attachmentToBeEdited()!.version!++;
this.attachmentToBeEdited()!.uploadDate = dayjs();

const formData = new FormData();
Expand Down Expand Up @@ -206,6 +205,17 @@ export class PdfPreviewComponent implements OnInit, OnDestroy {
this.alertService.error('artemisApp.attachment.pdfPreview.hiddenAttachmentUpdateError', { error: error.message });
},
});
} else {
this.attachmentToBeEdited()!.lecture = this.attachmentUnit()!.lecture;
this.attachmentService.update(this.attachmentToBeEdited()!.id!, this.attachmentToBeEdited()!, pdfFile).subscribe({
next: () => {
this.alertService.success('artemisApp.attachment.pdfPreview.attachmentUpdateSuccess');
this.router.navigate(['course-management', this.course()?.id, 'lectures', this.attachmentUnit()!.lecture!.id, 'unit-management']);
},
error: (error) => {
this.alertService.error('artemisApp.attachment.pdfPreview.attachmentUpdateError', { error: error.message });
},
});
}
}
}
Expand Down

0 comments on commit 71f7466

Please sign in to comment.