mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-21 15:48:06 +02:00
Change getPDFFileNameFromURL
to ignore data:
URLs for performance reasons (issue 8263)
The patch also changes the `defaultFilename` to use the ES6 default parameter notation, and fixes the formatting of the JSDoc comment. Finally, since `getPDFFileNameFromURL` currently has no unit-tests, a few basic ones are added to avoid regressions.
This commit is contained in:
parent
3888a993b1
commit
84472b30ee
2 changed files with 151 additions and 12 deletions
|
@ -353,15 +353,26 @@ function noContextMenuHandler(e) {
|
|||
e.preventDefault();
|
||||
}
|
||||
|
||||
function isDataSchema(url) {
|
||||
var i = 0, ii = url.length;
|
||||
while (i < ii && url[i].trim() === '') {
|
||||
i++;
|
||||
}
|
||||
return url.substr(i, 5).toLowerCase() === 'data:';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the filename or guessed filename from the url (see issue 3455).
|
||||
* url {String} The original PDF location.
|
||||
* defaultFilename {string} The value to return if the file name is unknown.
|
||||
* @return {String} Guessed PDF file name.
|
||||
* @param {string} url - The original PDF location.
|
||||
* @param {string} defaultFilename - The value returned if the filename is
|
||||
* unknown, or the protocol is unsupported.
|
||||
* @returns {string} Guessed PDF filename.
|
||||
*/
|
||||
function getPDFFileNameFromURL(url, defaultFilename) {
|
||||
if (typeof defaultFilename === 'undefined') {
|
||||
defaultFilename = 'document.pdf';
|
||||
function getPDFFileNameFromURL(url, defaultFilename = 'document.pdf') {
|
||||
if (isDataSchema(url)) {
|
||||
console.warn('getPDFFileNameFromURL: ' +
|
||||
'ignoring "data:" URL for performance reasons.');
|
||||
return defaultFilename;
|
||||
}
|
||||
var reURI = /^(?:(?:[^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
|
||||
// SCHEME HOST 1.PATH 2.QUERY 3.REF
|
||||
|
@ -369,8 +380,8 @@ function getPDFFileNameFromURL(url, defaultFilename) {
|
|||
var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
|
||||
var splitURI = reURI.exec(url);
|
||||
var suggestedFilename = reFilename.exec(splitURI[1]) ||
|
||||
reFilename.exec(splitURI[2]) ||
|
||||
reFilename.exec(splitURI[3]);
|
||||
reFilename.exec(splitURI[2]) ||
|
||||
reFilename.exec(splitURI[3]);
|
||||
if (suggestedFilename) {
|
||||
suggestedFilename = suggestedFilename[0];
|
||||
if (suggestedFilename.indexOf('%') !== -1) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue