Merge pull request #8024 from Rob--W/issue-6643-pdf-attachment-in-pdfjs
Open PDF attachments in the viewer instead of an unconditional download
This commit is contained in:
commit
cf73f4bc2d
@ -27,6 +27,8 @@ limitations under the License.
|
|||||||
'ftp',
|
'ftp',
|
||||||
'file',
|
'file',
|
||||||
'chrome-extension',
|
'chrome-extension',
|
||||||
|
'blob',
|
||||||
|
'data',
|
||||||
// Chromium OS
|
// Chromium OS
|
||||||
'filesystem',
|
'filesystem',
|
||||||
// Chromium OS, shorthand for filesystem:<origin>/external/
|
// Chromium OS, shorthand for filesystem:<origin>/external/
|
||||||
|
@ -63,6 +63,8 @@
|
|||||||
"ftp:/*",
|
"ftp:/*",
|
||||||
"file:/*",
|
"file:/*",
|
||||||
"chrome-extension:/*",
|
"chrome-extension:/*",
|
||||||
|
"blob:*",
|
||||||
|
"data:*",
|
||||||
"filesystem:/*",
|
"filesystem:/*",
|
||||||
"drive:*"
|
"drive:*"
|
||||||
]
|
]
|
||||||
|
21
web/app.js
21
web/app.js
@ -574,14 +574,17 @@ var PDFViewerApplication = {
|
|||||||
setTitleUsingUrl: function pdfViewSetTitleUsingUrl(url) {
|
setTitleUsingUrl: function pdfViewSetTitleUsingUrl(url) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.baseUrl = url.split('#')[0];
|
this.baseUrl = url.split('#')[0];
|
||||||
try {
|
var title = getPDFFileNameFromURL(url, '');
|
||||||
this.setTitle(decodeURIComponent(
|
if (!title) {
|
||||||
pdfjsLib.getFilenameFromUrl(url)) || url);
|
try {
|
||||||
} catch (e) {
|
title = decodeURIComponent(pdfjsLib.getFilenameFromUrl(url)) || url;
|
||||||
// decodeURIComponent may throw URIError,
|
} catch (e) {
|
||||||
// fall back to using the unprocessed url in that case
|
// decodeURIComponent may throw URIError,
|
||||||
this.setTitle(url);
|
// fall back to using the unprocessed url in that case
|
||||||
|
title = url;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
this.setTitle(title);
|
||||||
},
|
},
|
||||||
|
|
||||||
setTitle: function pdfViewSetTitle(title) {
|
setTitle: function pdfViewSetTitle(title) {
|
||||||
@ -742,7 +745,9 @@ var PDFViewerApplication = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var url = this.baseUrl;
|
var url = this.baseUrl;
|
||||||
var filename = getPDFFileNameFromURL(url);
|
// Use this.url instead of this.baseUrl to perform filename detection based
|
||||||
|
// on the reference fragment as ultimate fallback if needed.
|
||||||
|
var filename = getPDFFileNameFromURL(this.url);
|
||||||
var downloadManager = this.downloadManager;
|
var downloadManager = this.downloadManager;
|
||||||
downloadManager.onerror = function (err) {
|
downloadManager.onerror = function (err) {
|
||||||
// This error won't really be helpful because it's likely the
|
// This error won't really be helpful because it's likely the
|
||||||
|
@ -86,6 +86,38 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
|
|||||||
this._renderedCapability.resolve();
|
this._renderedCapability.resolve();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_bindPdfLink:
|
||||||
|
function PDFAttachmentViewer_bindPdfLink(button, content, filename) {
|
||||||
|
var blobUrl;
|
||||||
|
button.onclick = function() {
|
||||||
|
if (!blobUrl) {
|
||||||
|
blobUrl = pdfjsLib.createObjectURL(
|
||||||
|
content, 'application/pdf', pdfjsLib.PDFJS.disableCreateObjectURL);
|
||||||
|
}
|
||||||
|
var viewerUrl;
|
||||||
|
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
|
||||||
|
// The current URL is the viewer, let's use it and append the file.
|
||||||
|
viewerUrl = '?file=' + encodeURIComponent(blobUrl + '#' + filename);
|
||||||
|
} else if (PDFJSDev.test('CHROME')) {
|
||||||
|
// In the Chrome extension, the URL is rewritten using the history API
|
||||||
|
// in viewer.js, so an absolute URL must be generated.
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
viewerUrl = chrome.runtime.getURL('/content/web/viewer.html') +
|
||||||
|
'?file=' + encodeURIComponent(blobUrl + '#' + filename);
|
||||||
|
} else {
|
||||||
|
// Let Firefox's content handler catch the URL and display the PDF.
|
||||||
|
// In Firefox PDFJS.disableCreateObjectURL is always false, so
|
||||||
|
// blobUrl is always a blob:-URL and never a data:-URL.
|
||||||
|
viewerUrl = blobUrl + '?' + encodeURIComponent(filename);
|
||||||
|
}
|
||||||
|
window.open(viewerUrl);
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
@ -124,11 +156,18 @@ var PDFAttachmentViewer = (function PDFAttachmentViewerClosure() {
|
|||||||
for (var i = 0; i < attachmentsCount; i++) {
|
for (var i = 0; i < attachmentsCount; i++) {
|
||||||
var item = attachments[names[i]];
|
var item = attachments[names[i]];
|
||||||
var filename = pdfjsLib.getFilenameFromUrl(item.filename);
|
var filename = pdfjsLib.getFilenameFromUrl(item.filename);
|
||||||
|
filename = pdfjsLib.removeNullCharacters(filename);
|
||||||
|
|
||||||
var div = document.createElement('div');
|
var div = document.createElement('div');
|
||||||
div.className = 'attachmentsItem';
|
div.className = 'attachmentsItem';
|
||||||
var button = document.createElement('button');
|
var button = document.createElement('button');
|
||||||
this._bindLink(button, item.content, filename);
|
button.textContent = filename;
|
||||||
button.textContent = pdfjsLib.removeNullCharacters(filename);
|
if (/\.pdf$/i.test(filename)) {
|
||||||
|
this._bindPdfLink(button, item.content, filename);
|
||||||
|
} else {
|
||||||
|
this._bindLink(button, item.content, filename);
|
||||||
|
}
|
||||||
|
|
||||||
div.appendChild(button);
|
div.appendChild(button);
|
||||||
this.container.appendChild(div);
|
this.container.appendChild(div);
|
||||||
}
|
}
|
||||||
|
@ -368,11 +368,15 @@ function noContextMenuHandler(e) {
|
|||||||
/**
|
/**
|
||||||
* Returns the filename or guessed filename from the url (see issue 3455).
|
* Returns the filename or guessed filename from the url (see issue 3455).
|
||||||
* url {String} The original PDF location.
|
* url {String} The original PDF location.
|
||||||
|
* defaultFilename {string} The value to return if the file name is unknown.
|
||||||
* @return {String} Guessed PDF file name.
|
* @return {String} Guessed PDF file name.
|
||||||
*/
|
*/
|
||||||
function getPDFFileNameFromURL(url) {
|
function getPDFFileNameFromURL(url, defaultFilename) {
|
||||||
var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
|
if (typeof defaultFilename === 'undefined') {
|
||||||
// SCHEME HOST 1.PATH 2.QUERY 3.REF
|
defaultFilename = 'document.pdf';
|
||||||
|
}
|
||||||
|
var reURI = /^(?:(?:[^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
|
||||||
|
// SCHEME HOST 1.PATH 2.QUERY 3.REF
|
||||||
// Pattern to get last matching NAME.pdf
|
// Pattern to get last matching NAME.pdf
|
||||||
var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
|
var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
|
||||||
var splitURI = reURI.exec(url);
|
var splitURI = reURI.exec(url);
|
||||||
@ -392,7 +396,7 @@ function getPDFFileNameFromURL(url) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return suggestedFilename || 'document.pdf';
|
return suggestedFilename || defaultFilename;
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeWheelEventDelta(evt) {
|
function normalizeWheelEventDelta(evt) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user