mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
[fix] Force send attachment to always download and never open (#2908)
* [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
This commit is contained in:
26
apps/web/src/app/services/webFileDownload.service.ts
Normal file
26
apps/web/src/app/services/webFileDownload.service.ts
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user