diff --git a/src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java b/src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java index 5b73b836b9ad..0e90597a25bd 100644 --- a/src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java +++ b/src/main/java/de/tum/cit/aet/artemis/core/web/FileResource.java @@ -417,10 +417,11 @@ public ResponseEntity getExamUserImage(@PathVariable Long examUserId) { @EnforceAtLeastStudent public ResponseEntity getLectureAttachment(@PathVariable Long lectureId, @PathVariable String filename) { log.debug("REST request to get file : {}", filename); - sanitizeFilenameElseThrow(filename); + String fileNameWithoutSpaces = filename.replaceAll(" ", "_"); + sanitizeFilenameElseThrow(fileNameWithoutSpaces); List lectureAttachments = attachmentRepository.findAllByLectureId(lectureId); - Attachment attachment = lectureAttachments.stream().filter(lectureAttachment -> filename.equals(Path.of(lectureAttachment.getLink()).getFileName().toString())).findAny() + Attachment attachment = lectureAttachments.stream().filter(lectureAttachment -> lectureAttachment.getLink().endsWith(fileNameWithoutSpaces)).findAny() .orElseThrow(() -> new EntityNotFoundException("Attachment", filename)); // get the course for a lecture attachment diff --git a/src/main/java/de/tum/cit/aet/artemis/lecture/web/AttachmentResource.java b/src/main/java/de/tum/cit/aet/artemis/lecture/web/AttachmentResource.java index 3b5e44b6ac2f..6c280a0ce0be 100644 --- a/src/main/java/de/tum/cit/aet/artemis/lecture/web/AttachmentResource.java +++ b/src/main/java/de/tum/cit/aet/artemis/lecture/web/AttachmentResource.java @@ -92,7 +92,7 @@ public ResponseEntity createAttachment(@RequestPart Attachment attac attachment.setId(null); Path basePath = FilePathService.getLectureAttachmentFilePath().resolve(attachment.getLecture().getId().toString()); - Path savePath = fileService.saveFile(file, basePath, false); + Path savePath = fileService.saveFile(file, basePath, true); attachment.setLink(FilePathService.publicPathForActualPath(savePath, attachment.getLecture().getId()).toString()); Attachment result = attachmentRepository.save(attachment); @@ -123,7 +123,7 @@ public ResponseEntity updateAttachment(@PathVariable Long attachment if (file != null) { Path basePath = FilePathService.getLectureAttachmentFilePath().resolve(originalAttachment.getLecture().getId().toString()); - Path savePath = fileService.saveFile(file, basePath, false); + Path savePath = fileService.saveFile(file, basePath, true); attachment.setLink(FilePathService.publicPathForActualPath(savePath, originalAttachment.getLecture().getId()).toString()); // Delete the old file URI oldPath = URI.create(originalAttachment.getLink()); diff --git a/src/main/webapp/app/lecture/lecture-attachments.component.ts b/src/main/webapp/app/lecture/lecture-attachments.component.ts index 6d89f669946a..6bb35a0e84af 100644 --- a/src/main/webapp/app/lecture/lecture-attachments.component.ts +++ b/src/main/webapp/app/lecture/lecture-attachments.component.ts @@ -196,7 +196,7 @@ export class LectureAttachmentsComponent implements OnInit, OnDestroy { downloadAttachment(downloadUrl: string): void { if (!this.isDownloadingAttachmentLink) { this.isDownloadingAttachmentLink = downloadUrl; - this.fileService.downloadFile(downloadUrl); + this.fileService.downloadFile(this.fileService.replaceLectureAttachmentPrefixAndUnderscores(downloadUrl)); this.isDownloadingAttachmentLink = undefined; } } diff --git a/src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts b/src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts index bc5b4ed321dc..5634aeda7a5e 100644 --- a/src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts +++ b/src/main/webapp/app/overview/course-lectures/attachment-unit/attachment-unit.component.ts @@ -37,17 +37,21 @@ export class AttachmentUnitComponent extends LectureUnitDirective { let scienceService: ScienceService; @@ -39,7 +53,7 @@ describe('AttachmentUnitComponent', () => { provide: TranslateService, useClass: MockTranslateService, }, - MockProvider(FileService), + { provide: FileService, useClass: MockFileService }, MockProvider(ScienceService), ], }).compileComponents(); @@ -82,6 +96,13 @@ describe('AttachmentUnitComponent', () => { ['pdf', faFilePdf], ['csv', faFileCsv], ['png', faFileImage], + ['zip', faFileArchive], + ['txt', faFileLines], + ['doc', faFileWord], + ['json', faFileCode], + ['xls', faFileExcel], + ['ppt', faFilePowerpoint], + ['odf', faFilePen], ['exotic', faFile], ])('should use correct icon for extension', async (extension: string, icon: IconDefinition) => { const getAttachmentIconSpy = jest.spyOn(component, 'getAttachmentIcon'); diff --git a/src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts b/src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts index 82565adce6de..ae528e16f0ea 100644 --- a/src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts +++ b/src/test/javascript/spec/helpers/mocks/service/mock-file.service.ts @@ -12,4 +12,7 @@ export class MockFileService { getTemplateFile = () => { return of(); }; + + replaceLectureAttachmentPrefixAndUnderscores = (link: string) => link; + replaceAttachmentPrefixAndUnderscores = (link: string) => link; }