mirror of
https://github.com/bitwarden/browser
synced 2025-12-10 21:33:27 +00:00
* [refactor] Introduce a file download service * [refactor] Point platformUtilsService.saveFile() callers to fileDownloadService.download() instead * [refactor] Remove platformUtilsService.saveFile() * [fix] Force send attachments to always download and never open * [fix] Remove the window property from FileDownloadRequest * [fix] Move FileDownloadRequest to /abstractions/fileDownload * [fix] Simplify FileDownloadRequest to a type * [fix] Move BrowserApi.saveFile logic into BrowserFileDownloadService * [fix] Use proper blob types for file downloads * [fix] forceDownload -> downloadMethod on FileDownloadRequest * [fix] Remove fileType from FileDownloadRequest * [fix] Make fileType private
51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { FileDownloadRequest } from "./fileDownloadRequest";
|
|
|
|
export class FileDownloadBuilder {
|
|
get blobOptions(): any {
|
|
const options = this._request.blobOptions ?? {};
|
|
if (options.type == null) {
|
|
options.type = this.fileType;
|
|
}
|
|
return options;
|
|
}
|
|
|
|
get blob(): Blob {
|
|
if (this.blobOptions != null) {
|
|
return new Blob([this._request.blobData], this.blobOptions);
|
|
} else {
|
|
return new Blob([this._request.blobData]);
|
|
}
|
|
}
|
|
|
|
get downloadMethod(): "save" | "open" {
|
|
if (this._request.downloadMethod != null) {
|
|
return this._request.downloadMethod;
|
|
}
|
|
return this.fileType != "application/pdf" ? "save" : "open";
|
|
}
|
|
|
|
private get fileType() {
|
|
const fileNameLower = this._request.fileName.toLowerCase();
|
|
if (fileNameLower.endsWith(".pdf")) {
|
|
return "application/pdf";
|
|
} else if (fileNameLower.endsWith(".xlsx")) {
|
|
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
} else if (fileNameLower.endsWith(".docx")) {
|
|
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
} else if (fileNameLower.endsWith(".pptx")) {
|
|
return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
|
} else if (fileNameLower.endsWith(".csv")) {
|
|
return "text/csv";
|
|
} else if (fileNameLower.endsWith(".png")) {
|
|
return "image/png";
|
|
} else if (fileNameLower.endsWith(".jpg") || fileNameLower.endsWith(".jpeg")) {
|
|
return "image/jpeg";
|
|
} else if (fileNameLower.endsWith(".gif")) {
|
|
return "image/gif";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
constructor(private readonly _request: FileDownloadRequest) {}
|
|
}
|