Skip to content

Commit

Permalink
Lectures: Fix lecture unit file attachment names (#9721)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonEntholzer authored Nov 10, 2024
1 parent 6e37a8f commit bf2fbbd
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,11 @@ public ResponseEntity<byte[]> getExamUserImage(@PathVariable Long examUserId) {
@EnforceAtLeastStudent
public ResponseEntity<byte[]> getLectureAttachment(@PathVariable Long lectureId, @PathVariable String filename) {
log.debug("REST request to get file : {}", filename);
sanitizeFilenameElseThrow(filename);
String fileNameWithoutSpaces = filename.replaceAll(" ", "_");
sanitizeFilenameElseThrow(fileNameWithoutSpaces);

List<Attachment> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ResponseEntity<Attachment> 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);
Expand Down Expand Up @@ -123,7 +123,7 @@ public ResponseEntity<Attachment> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ export class AttachmentUnitComponent extends LectureUnitDirective<AttachmentUnit
getFileName(): string {
if (this.lectureUnit().attachment?.link) {
const link = this.lectureUnit().attachment!.link!;
return link.substring(link.lastIndexOf('/') + 1);
const filename = link.substring(link.lastIndexOf('/') + 1);
return this.fileService.replaceAttachmentPrefixAndUnderscores(filename);
}
return '';
}

/**
* Downloads the file
*/
handleDownload() {
this.logEvent();

if (this.lectureUnit().attachment?.link) {
const link = this.lectureUnit().attachment!.link!;
this.fileService.downloadFile(link);
this.fileService.downloadFile(this.fileService.replaceAttachmentPrefixAndUnderscores(link));
this.onCompletion.emit({ lectureUnit: this.lectureUnit(), completed: true });
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class CourseLectureDetailsComponent extends AbstractScienceComponent impl
downloadAttachment(downloadUrl?: string): void {
if (!this.isDownloadingLink && downloadUrl) {
this.isDownloadingLink = downloadUrl;
this.fileService.downloadFile(downloadUrl);
this.fileService.downloadFile(this.fileService.replaceLectureAttachmentPrefixAndUnderscores(downloadUrl));
this.isDownloadingLink = undefined;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/webapp/app/shared/http/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,20 @@ export class FileService {
} while (mapOfFiles && mapOfFiles.has(name));
return name;
}

/**
* Removes the prefix from the file name, and replaces underscore with spaces
* @param link
*/
replaceAttachmentPrefixAndUnderscores(link: string): string {
return link.replace(/AttachmentUnit_\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}_/, '').replace(/_/g, ' ');
}

/**
* Removes the prefix from the file name, and replaces underscore with spaces
* @param link
*/
replaceLectureAttachmentPrefixAndUnderscores(link: string): string {
return link.replace(/LectureAttachment_\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}_/, '').replace(/_/g, ' ');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ import { MockProvider } from 'ng-mocks';
import { provideHttpClient } from '@angular/common/http';
import { MockTranslateService } from '../../../helpers/mocks/service/mock-translate.service';
import { ScienceService } from 'app/shared/science/science.service';
import { IconDefinition, faFile, faFileCsv, faFileImage, faFilePdf } from '@fortawesome/free-solid-svg-icons';
import {
IconDefinition,
faFile,
faFileArchive,
faFileCode,
faFileCsv,
faFileExcel,
faFileImage,
faFileLines,
faFilePdf,
faFilePen,
faFilePowerpoint,
faFileWord,
} from '@fortawesome/free-solid-svg-icons';
import { MockFileService } from '../../../helpers/mocks/service/mock-file.service';

describe('AttachmentUnitComponent', () => {
let scienceService: ScienceService;
Expand Down Expand Up @@ -39,7 +53,7 @@ describe('AttachmentUnitComponent', () => {
provide: TranslateService,
useClass: MockTranslateService,
},
MockProvider(FileService),
{ provide: FileService, useClass: MockFileService },
MockProvider(ScienceService),
],
}).compileComponents();
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ export class MockFileService {
getTemplateFile = () => {
return of();
};

replaceLectureAttachmentPrefixAndUnderscores = (link: string) => link;
replaceAttachmentPrefixAndUnderscores = (link: string) => link;
}

0 comments on commit bf2fbbd

Please sign in to comment.