1313 * limitations under the License.
1414 */
1515
16- /** @typedef {import("./interfaces").IDownloadManager } IDownloadManager */
17-
18- import { createValidAbsoluteUrl , isPdfFile } from "pdfjs-lib" ;
16+ import { BaseDownloadManager } from "./base_download_manager.js" ;
17+ import { createValidAbsoluteUrl } from "pdfjs-lib" ;
1918
2019if ( typeof PDFJSDev !== "undefined" && ! PDFJSDev . test ( "CHROME || GENERIC" ) ) {
2120 throw new Error (
@@ -24,101 +23,41 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("CHROME || GENERIC")) {
2423 ) ;
2524}
2625
27- function download ( blobUrl , filename ) {
28- const a = document . createElement ( "a" ) ;
29- if ( ! a . click ) {
30- throw new Error ( 'DownloadManager: "a.click()" is not supported.' ) ;
31- }
32- a . href = blobUrl ;
33- a . target = "_parent" ;
34- // Use a.download if available. This increases the likelihood that
35- // the file is downloaded instead of opened by another PDF plugin.
36- if ( "download" in a ) {
37- a . download = filename ;
38- }
39- // <a> must be in the document for recent Firefox versions,
40- // otherwise .click() is ignored.
41- ( document . body || document . documentElement ) . append ( a ) ;
42- a . click ( ) ;
43- a . remove ( ) ;
44- }
45-
46- /**
47- * @implements {IDownloadManager}
48- */
49- class DownloadManager {
50- #openBlobUrls = new WeakMap ( ) ;
51-
52- downloadData ( data , filename , contentType ) {
53- const blobUrl = URL . createObjectURL (
54- new Blob ( [ data ] , { type : contentType } )
55- ) ;
56- download ( blobUrl , filename ) ;
57- }
58-
59- /**
60- * @returns {boolean } Indicating if the data was opened.
61- */
62- openOrDownloadData ( data , filename , dest = null ) {
63- const isPdfData = isPdfFile ( filename ) ;
64- const contentType = isPdfData ? "application/pdf" : "" ;
65-
66- if (
67- ( typeof PDFJSDev === "undefined" || ! PDFJSDev . test ( "COMPONENTS" ) ) &&
68- isPdfData
69- ) {
70- let blobUrl = this . #openBlobUrls. get ( data ) ;
71- if ( ! blobUrl ) {
72- blobUrl = URL . createObjectURL ( new Blob ( [ data ] , { type : contentType } ) ) ;
73- this . #openBlobUrls. set ( data , blobUrl ) ;
74- }
75- let viewerUrl ;
76- if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "GENERIC" ) ) {
77- // The current URL is the viewer, let's use it and append the file.
78- viewerUrl = "?file=" + encodeURIComponent ( blobUrl + "#" + filename ) ;
79- } else if ( PDFJSDev . test ( "CHROME" ) ) {
80- // In the Chrome extension, the URL is rewritten using the history API
81- // in viewer.js, so an absolute URL must be generated.
82- viewerUrl =
83- // eslint-disable-next-line no-undef
84- chrome . runtime . getURL ( "/content/web/viewer.html" ) +
85- "?file=" +
86- encodeURIComponent ( blobUrl + "#" + filename ) ;
87- }
88- if ( dest ) {
89- viewerUrl += `#${ escape ( dest ) } ` ;
90- }
91-
92- try {
93- window . open ( viewerUrl ) ;
94- return true ;
95- } catch ( ex ) {
96- console . error ( "openOrDownloadData:" , ex ) ;
97- // Release the `blobUrl`, since opening it failed, and fallback to
98- // downloading the PDF file.
99- URL . revokeObjectURL ( blobUrl ) ;
100- this . #openBlobUrls. delete ( data ) ;
26+ class DownloadManager extends BaseDownloadManager {
27+ _triggerDownload ( blobUrl , originalUrl , filename , isAttachment = false ) {
28+ if ( ! blobUrl && ! isAttachment ) {
29+ // Fallback to downloading non-attachments by their URL.
30+ if ( ! createValidAbsoluteUrl ( originalUrl , "http://example.com" ) ) {
31+ throw new Error ( `_triggerDownload - not a valid URL: ${ originalUrl } ` ) ;
10132 }
33+ blobUrl = originalUrl + "#pdfjs.action=download" ;
10234 }
10335
104- this . downloadData ( data , filename , contentType ) ;
105- return false ;
36+ const a = document . createElement ( "a" ) ;
37+ a . href = blobUrl ;
38+ a . target = "_parent" ;
39+ // Use a.download if available. This increases the likelihood that
40+ // the file is downloaded instead of opened by another PDF plugin.
41+ if ( "download" in a ) {
42+ a . download = filename ;
43+ }
44+ // <a> must be in the document for recent Firefox versions,
45+ // otherwise .click() is ignored.
46+ ( document . body || document . documentElement ) . append ( a ) ;
47+ a . click ( ) ;
48+ a . remove ( ) ;
10649 }
10750
108- download ( data , url , filename ) {
109- let blobUrl ;
110- if ( data ) {
111- blobUrl = URL . createObjectURL (
112- new Blob ( [ data ] , { type : "application/pdf" } )
113- ) ;
114- } else {
115- if ( ! createValidAbsoluteUrl ( url , "http://example.com" ) ) {
116- console . error ( `download - not a valid URL: ${ url } ` ) ;
117- return ;
118- }
119- blobUrl = url + "#pdfjs.action=download" ;
51+ _getOpenDataUrl ( blobUrl , filename , dest = null ) {
52+ if ( typeof PDFJSDev !== "undefined" && PDFJSDev . test ( "COMPONENTS" ) ) {
53+ throw new Error ( "Opening data is not supported in `COMPONENTS` builds." ) ;
54+ }
55+ // The current URL is the viewer, let's use it and append the file.
56+ let url = "?file=" + encodeURIComponent ( blobUrl + "#" + filename ) ;
57+ if ( dest ) {
58+ url += `#${ escape ( dest ) } ` ;
12059 }
121- download ( blobUrl , filename ) ;
60+ return url ;
12261 }
12362}
12463
0 commit comments