Skip to content

Commit

Permalink
Remove some unused options
Browse files Browse the repository at this point in the history
  • Loading branch information
pzdr7 committed Oct 24, 2024
1 parent daa19ac commit 65e33ba
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/main/webapp/app/shared/http/file-uploader.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { MAX_FILE_SIZE } from 'app/shared/constants/input.constants';
import { lastValueFrom } from 'rxjs';
import { UPLOAD_MARKDOWN_FILE_EXTENSIONS } from 'app/shared/constants/file-extensions.constants';
Expand All @@ -8,24 +8,42 @@ export interface FileUploadResponse {
path?: string;
}

type Options = {
keepFileName: boolean;
};

@Injectable({ providedIn: 'root' })
export class FileUploaderService {
private readonly http = inject(HttpClient);
readonly acceptedMarkdownFileExtensions = UPLOAD_MARKDOWN_FILE_EXTENSIONS;
constructor(private http: HttpClient) {}

/**
* Uploads a file for the markdown editor to the server.
* @param file The file to upload
* @param fileName The name of the file
* @param options The options dictionary (e.g, { keepFileName: true })
* @return A promise with the response from the server or an error
*/
uploadMarkdownFile(file: Blob | File, fileName?: string, options?: Options): Promise<FileUploadResponse> {
const fileExtension = fileName ? fileName.split('.').pop()!.toLocaleLowerCase() : (file as File).name.split('.').pop()!.toLocaleLowerCase();
uploadMarkdownFile(file: File): Promise<FileUploadResponse> {
const fileExtension = file.name.split('.').pop()!.toLocaleLowerCase();
if (!this.acceptedMarkdownFileExtensions.includes(fileExtension)) {
return Promise.reject(
new Error(
'Unsupported file type! Only the following file extensions are allowed: ' + this.acceptedMarkdownFileExtensions.map((extension) => `.${extension}`).join(', '),
),
);
}

if (file.size > MAX_FILE_SIZE) {
return Promise.reject(new Error('File is too big! Maximum allowed file size: ' + MAX_FILE_SIZE / (1024 * 1024) + ' MB.'));
}

const formData = new FormData();
formData.append('file', file, file.name);
return lastValueFrom(this.http.post<FileUploadResponse>(`/api/markdown-file-upload?keepFileName=${false}`, formData));
}

uploadMarkdownFileInCurrentMetisConversation(file: File, courseId: number | undefined, conversationId: number | undefined): Promise<FileUploadResponse> {
if (!courseId || !conversationId) {
return Promise.reject(new Error(`No course or conversation available for the file upload.`));
}

// TODO refactor
const fileExtension = (file as File).name.split('.').pop()!.toLocaleLowerCase();
if (!this.acceptedMarkdownFileExtensions.includes(fileExtension)) {
return Promise.reject(
new Error(
Expand All @@ -34,13 +52,13 @@ export class FileUploaderService {
);
}

// TODO: adjust file size
if (file.size > MAX_FILE_SIZE) {
return Promise.reject(new Error('File is too big! Maximum allowed file size: ' + MAX_FILE_SIZE / (1024 * 1024) + ' MB.'));
}

const keepFileName = !!options?.keepFileName;
const formData = new FormData();
formData.append('file', file, fileName);
return lastValueFrom(this.http.post<FileUploadResponse>(`/api/markdown-file-upload?keepFileName=${keepFileName}`, formData));
formData.append('file', file, file.name);
return lastValueFrom(this.http.post<FileUploadResponse>(`/api/files/courses/${courseId}/conversations/${conversationId}`, formData));
}
}

0 comments on commit 65e33ba

Please sign in to comment.