Browse Source
* [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 privatepull/3035/head
35 changed files with 297 additions and 155 deletions
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
import { Injectable } from "@angular/core"; |
||||
|
||||
import { FileDownloadService } from "@bitwarden/common/abstractions/fileDownload/fileDownload.service"; |
||||
import { FileDownloadBuilder } from "@bitwarden/common/abstractions/fileDownload/fileDownloadBuilder"; |
||||
import { FileDownloadRequest } from "@bitwarden/common/abstractions/fileDownload/fileDownloadRequest"; |
||||
import { Utils } from "@bitwarden/common/misc/utils"; |
||||
|
||||
import { BrowserApi } from "../browser/browserApi"; |
||||
import { SafariApp } from "../browser/safariApp"; |
||||
|
||||
@Injectable() |
||||
export class BrowserFileDownloadService implements FileDownloadService { |
||||
download(request: FileDownloadRequest): void { |
||||
const builder = new FileDownloadBuilder(request); |
||||
if (BrowserApi.isSafariApi) { |
||||
let data: BlobPart = null; |
||||
if (builder.blobOptions.type === "text/plain" && typeof request.blobData === "string") { |
||||
data = request.blobData; |
||||
} else { |
||||
builder.blob.arrayBuffer().then((buf) => { |
||||
data = Utils.fromBufferToB64(buf); |
||||
}); |
||||
} |
||||
SafariApp.sendMessageToApp( |
||||
"downloadFile", |
||||
JSON.stringify({ |
||||
blobData: data, |
||||
blobOptions: request.blobOptions, |
||||
fileName: request.fileName, |
||||
}), |
||||
true |
||||
); |
||||
} else { |
||||
if (navigator.msSaveOrOpenBlob) { |
||||
navigator.msSaveBlob(builder.blob, request.fileName); |
||||
} else { |
||||
const a = window.document.createElement("a"); |
||||
a.href = URL.createObjectURL(builder.blob); |
||||
a.download = request.fileName; |
||||
window.document.body.appendChild(a); |
||||
a.click(); |
||||
window.document.body.removeChild(a); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
import { Injectable } from "@angular/core"; |
||||
|
||||
import { FileDownloadService } from "@bitwarden/common/abstractions/fileDownload/fileDownload.service"; |
||||
import { FileDownloadBuilder } from "@bitwarden/common/abstractions/fileDownload/fileDownloadBuilder"; |
||||
import { FileDownloadRequest } from "@bitwarden/common/abstractions/fileDownload/fileDownloadRequest"; |
||||
|
||||
@Injectable() |
||||
export class DesktopFileDownloadService implements FileDownloadService { |
||||
download(request: FileDownloadRequest): void { |
||||
const a = window.document.createElement("a"); |
||||
a.href = URL.createObjectURL(new FileDownloadBuilder(request).blob); |
||||
a.download = request.fileName; |
||||
a.style.position = "fixed"; |
||||
window.document.body.appendChild(a); |
||||
a.click(); |
||||
window.document.body.removeChild(a); |
||||
} |
||||
} |
||||
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from "@angular/core"; |
||||
|
||||
import { FileDownloadService } from "@bitwarden/common/abstractions/fileDownload/fileDownload.service"; |
||||
import { FileDownloadBuilder } from "@bitwarden/common/abstractions/fileDownload/fileDownloadBuilder"; |
||||
import { FileDownloadRequest } from "@bitwarden/common/abstractions/fileDownload/fileDownloadRequest"; |
||||
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service"; |
||||
|
||||
@Injectable() |
||||
export class WebFileDownloadService implements FileDownloadService { |
||||
constructor(private platformUtilsService: PlatformUtilsService) {} |
||||
|
||||
download(request: FileDownloadRequest): void { |
||||
const builder = new FileDownloadBuilder(request); |
||||
const a = window.document.createElement("a"); |
||||
if (builder.downloadMethod === "save") { |
||||
a.download = request.fileName; |
||||
} else if (!this.platformUtilsService.isSafari()) { |
||||
a.target = "_blank"; |
||||
} |
||||
a.href = URL.createObjectURL(builder.blob); |
||||
a.style.position = "fixed"; |
||||
window.document.body.appendChild(a); |
||||
a.click(); |
||||
window.document.body.removeChild(a); |
||||
} |
||||
} |
||||
@ -0,0 +1,5 @@
@@ -0,0 +1,5 @@
|
||||
import { FileDownloadRequest } from "./fileDownloadRequest"; |
||||
|
||||
export abstract class FileDownloadService { |
||||
download: (request: FileDownloadRequest) => void; |
||||
} |
||||
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
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) {} |
||||
} |
||||
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
export type FileDownloadRequest = { |
||||
fileName: string; |
||||
blobData: BlobPart; |
||||
blobOptions?: BlobPropertyBag; |
||||
downloadMethod?: "save" | "open"; |
||||
}; |
||||
Loading…
Reference in new issue