Merge pull request #9379 from Snuffleupagus/contentDispositionFilename

[api-minor] Extract the Content-Disposition filename
This commit is contained in:
Tim van der Meij 2018-01-18 23:28:50 +01:00 committed by GitHub
commit 75dc2bbd35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 178 additions and 30 deletions

View File

@ -103,6 +103,16 @@ IPDFStreamReader.prototype = {
return null; return null;
}, },
/**
* Gets the Content-Disposition filename. It is defined after the headersReady
* promise is resolved.
* @returns {string|null} The filename, or `null` if the Content-Disposition
* header is missing/invalid.
*/
get filename() {
return null;
},
/** /**
* Gets PDF binary data length. It is defined after the headersReady promise * Gets PDF binary data length. It is defined after the headersReady promise
* is resolved. * is resolved.

View File

@ -1997,10 +1997,12 @@ var WorkerTransport = (function WorkerTransportClosure() {
getMetadata: function WorkerTransport_getMetadata() { getMetadata: function WorkerTransport_getMetadata() {
return this.messageHandler.sendWithPromise('GetMetadata', null). return this.messageHandler.sendWithPromise('GetMetadata', null).
then(function transportMetadata(results) { then((results) => {
return { return {
info: results[0], info: results[0],
metadata: (results[1] ? new Metadata(results[1]) : null), metadata: (results[1] ? new Metadata(results[1]) : null),
contentDispositionFilename: (this._fullReader ?
this._fullReader.filename : null),
}; };
}); });
}, },

View File

@ -17,8 +17,8 @@ import {
AbortException, assert, createPromiseCapability AbortException, assert, createPromiseCapability
} from '../shared/util'; } from '../shared/util';
import { import {
createResponseStatusError, validateRangeRequestCapabilities, createResponseStatusError, extractFilenameFromHeader,
validateResponseStatus validateRangeRequestCapabilities, validateResponseStatus
} from './network_utils'; } from './network_utils';
function createFetchOptions(headers, withCredentials) { function createFetchOptions(headers, withCredentials) {
@ -69,6 +69,7 @@ class PDFFetchStreamReader {
this._stream = stream; this._stream = stream;
this._reader = null; this._reader = null;
this._loaded = 0; this._loaded = 0;
this._filename = null;
let source = stream.source; let source = stream.source;
this._withCredentials = source.withCredentials; this._withCredentials = source.withCredentials;
this._contentLength = source.length; this._contentLength = source.length;
@ -100,11 +101,12 @@ class PDFFetchStreamReader {
this._reader = response.body.getReader(); this._reader = response.body.getReader();
this._headersCapability.resolve(); this._headersCapability.resolve();
const getResponseHeader = (name) => {
return response.headers.get(name);
};
let { allowRangeRequests, suggestedLength, } = let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
getResponseHeader: (name) => { getResponseHeader,
return response.headers.get(name);
},
isHttp: this._stream.isHttp, isHttp: this._stream.isHttp,
rangeChunkSize: this._rangeChunkSize, rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange, disableRange: this._disableRange,
@ -113,6 +115,8 @@ class PDFFetchStreamReader {
this._contentLength = suggestedLength; this._contentLength = suggestedLength;
this._isRangeSupported = allowRangeRequests; this._isRangeSupported = allowRangeRequests;
this._filename = extractFilenameFromHeader(getResponseHeader);
// We need to stop reading when range is supported and streaming is // We need to stop reading when range is supported and streaming is
// disabled. // disabled.
if (!this._isStreamingSupported && this._isRangeSupported) { if (!this._isStreamingSupported && this._isRangeSupported) {
@ -127,6 +131,10 @@ class PDFFetchStreamReader {
return this._headersCapability.promise; return this._headersCapability.promise;
} }
get filename() {
return this._filename;
}
get contentLength() { get contentLength() {
return this._contentLength; return this._contentLength;
} }

View File

@ -15,7 +15,8 @@
import { assert, createPromiseCapability, stringToBytes } from '../shared/util'; import { assert, createPromiseCapability, stringToBytes } from '../shared/util';
import { import {
createResponseStatusError, validateRangeRequestCapabilities createResponseStatusError, extractFilenameFromHeader,
validateRangeRequestCapabilities
} from './network_utils'; } from './network_utils';
import globalScope from '../shared/global_scope'; import globalScope from '../shared/global_scope';
@ -340,6 +341,7 @@ function PDFNetworkStreamFullRequestReader(manager, source) {
this._requests = []; this._requests = [];
this._done = false; this._done = false;
this._storedError = undefined; this._storedError = undefined;
this._filename = null;
this.onProgress = null; this.onProgress = null;
} }
@ -350,11 +352,13 @@ PDFNetworkStreamFullRequestReader.prototype = {
var fullRequestXhrId = this._fullRequestId; var fullRequestXhrId = this._fullRequestId;
var fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId); var fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId);
const getResponseHeader = (name) => {
return fullRequestXhr.getResponseHeader(name);
};
let { allowRangeRequests, suggestedLength, } = let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({ validateRangeRequestCapabilities({
getResponseHeader: (name) => { getResponseHeader,
return fullRequestXhr.getResponseHeader(name);
},
isHttp: this._manager.isHttp, isHttp: this._manager.isHttp,
rangeChunkSize: this._rangeChunkSize, rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange, disableRange: this._disableRange,
@ -367,6 +371,8 @@ PDFNetworkStreamFullRequestReader.prototype = {
this._isRangeSupported = true; this._isRangeSupported = true;
} }
this._filename = extractFilenameFromHeader(getResponseHeader);
var networkManager = this._manager; var networkManager = this._manager;
if (networkManager.isStreamingRequest(fullRequestXhrId)) { if (networkManager.isStreamingRequest(fullRequestXhrId)) {
// We can continue fetching when progressive loading is enabled, // We can continue fetching when progressive loading is enabled,
@ -429,6 +435,10 @@ PDFNetworkStreamFullRequestReader.prototype = {
} }
}, },
get filename() {
return this._filename;
},
get isRangeSupported() { get isRangeSupported() {
return this._isRangeSupported; return this._isRangeSupported;
}, },

View File

@ -16,6 +16,7 @@
import { import {
assert, MissingPDFException, UnexpectedResponseException assert, MissingPDFException, UnexpectedResponseException
} from '../shared/util'; } from '../shared/util';
import { getFilenameFromUrl } from './dom_utils';
function validateRangeRequestCapabilities({ getResponseHeader, isHttp, function validateRangeRequestCapabilities({ getResponseHeader, isHttp,
rangeChunkSize, disableRange, }) { rangeChunkSize, disableRange, }) {
@ -52,6 +53,18 @@ function validateRangeRequestCapabilities({ getResponseHeader, isHttp,
return returnValues; return returnValues;
} }
function extractFilenameFromHeader(getResponseHeader) {
const contentDisposition = getResponseHeader('Content-Disposition');
if (contentDisposition) {
let parts =
/.+;\s*filename=(?:'|")(.+\.pdf)(?:'|")/gi.exec(contentDisposition);
if (parts !== null && parts.length > 1) {
return getFilenameFromUrl(parts[1]);
}
}
return null;
}
function createResponseStatusError(status, url) { function createResponseStatusError(status, url) {
if (status === 404 || status === 0 && /^file:/.test(url)) { if (status === 404 || status === 0 && /^file:/.test(url)) {
return new MissingPDFException('Missing PDF "' + url + '".'); return new MissingPDFException('Missing PDF "' + url + '".');
@ -67,6 +80,7 @@ function validateResponseStatus(status) {
export { export {
createResponseStatusError, createResponseStatusError,
extractFilenameFromHeader,
validateRangeRequestCapabilities, validateRangeRequestCapabilities,
validateResponseStatus, validateResponseStatus,
}; };

View File

@ -22,7 +22,9 @@ let url = __non_webpack_require__('url');
import { import {
AbortException, assert, createPromiseCapability AbortException, assert, createPromiseCapability
} from '../shared/util'; } from '../shared/util';
import { validateRangeRequestCapabilities } from './network_utils'; import {
extractFilenameFromHeader, validateRangeRequestCapabilities
} from './network_utils';
const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//; const fileUriRegex = /^file:\/\/\/[a-zA-Z]:\//;
@ -78,6 +80,7 @@ class BaseFullReader {
let source = stream.source; let source = stream.source;
this._contentLength = source.length; // optional this._contentLength = source.length; // optional
this._loaded = 0; this._loaded = 0;
this._filename = null;
this._disableRange = source.disableRange || false; this._disableRange = source.disableRange || false;
this._rangeChunkSize = source.rangeChunkSize; this._rangeChunkSize = source.rangeChunkSize;
@ -97,6 +100,10 @@ class BaseFullReader {
return this._headersCapability.promise; return this._headersCapability.promise;
} }
get filename() {
return this._filename;
}
get contentLength() { get contentLength() {
return this._contentLength; return this._contentLength;
} }
@ -284,13 +291,14 @@ class PDFNodeStreamFullReader extends BaseFullReader {
this._headersCapability.resolve(); this._headersCapability.resolve();
this._setReadableStream(response); this._setReadableStream(response);
let { allowRangeRequests, suggestedLength, } = const getResponseHeader = (name) => {
validateRangeRequestCapabilities({
getResponseHeader: (name) => {
// Make sure that headers name are in lower case, as mentioned // Make sure that headers name are in lower case, as mentioned
// here: https://nodejs.org/api/http.html#http_message_headers. // here: https://nodejs.org/api/http.html#http_message_headers.
return this._readableStream.headers[name.toLowerCase()]; return this._readableStream.headers[name.toLowerCase()];
}, };
let { allowRangeRequests, suggestedLength, } =
validateRangeRequestCapabilities({
getResponseHeader,
isHttp: stream.isHttp, isHttp: stream.isHttp,
rangeChunkSize: this._rangeChunkSize, rangeChunkSize: this._rangeChunkSize,
disableRange: this._disableRange, disableRange: this._disableRange,
@ -301,6 +309,8 @@ class PDFNodeStreamFullReader extends BaseFullReader {
} }
// Setting right content length. // Setting right content length.
this._contentLength = suggestedLength; this._contentLength = suggestedLength;
this._filename = extractFilenameFromHeader(getResponseHeader);
}; };
this._request = null; this._request = null;

View File

@ -119,6 +119,7 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
function PDFDataTransportStreamReader(stream, queuedChunks) { function PDFDataTransportStreamReader(stream, queuedChunks) {
this._stream = stream; this._stream = stream;
this._done = false; this._done = false;
this._filename = null;
this._queuedChunks = queuedChunks || []; this._queuedChunks = queuedChunks || [];
this._requests = []; this._requests = [];
this._headersReady = Promise.resolve(); this._headersReady = Promise.resolve();
@ -143,6 +144,10 @@ var PDFDataTransportStream = (function PDFDataTransportStreamClosure() {
return this._headersReady; return this._headersReady;
}, },
get filename() {
return this._filename;
},
get isRangeSupported() { get isRangeSupported() {
return this._stream._isRangeSupported; return this._stream._isRangeSupported;
}, },

View File

@ -794,6 +794,7 @@ describe('api', function() {
expect(metadata.info['Title']).toEqual('Basic API Test'); expect(metadata.info['Title']).toEqual('Basic API Test');
expect(metadata.info['PDFFormatVersion']).toEqual('1.7'); expect(metadata.info['PDFFormatVersion']).toEqual('1.7');
expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test'); expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test');
expect(metadata.contentDispositionFilename).toEqual(null);
done(); done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);

View File

@ -14,8 +14,8 @@
*/ */
import { import {
createResponseStatusError, validateRangeRequestCapabilities, createResponseStatusError, extractFilenameFromHeader,
validateResponseStatus validateRangeRequestCapabilities, validateResponseStatus
} from '../../src/display/network_utils'; } from '../../src/display/network_utils';
import { import {
MissingPDFException, UnexpectedResponseException MissingPDFException, UnexpectedResponseException
@ -134,6 +134,84 @@ describe('network_utils', function() {
}); });
}); });
describe('extractFilenameFromHeader', function() {
it('returns null when content disposition header is blank', function() {
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return null;
}
})).toBeNull();
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return undefined;
}
})).toBeNull();
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return '';
}
})).toBeNull();
});
it('gets the filename from the response header', function() {
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'inline';
}
})).toBeNull();
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'attachment';
}
})).toBeNull();
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'attachment; filename="filename.pdf"';
}
})).toEqual('filename.pdf');
});
it('returns null when content disposition is form-data', function() {
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'form-data';
}
})).toBeNull();
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'form-data; name="filename.pdf"';
}
})).toBeNull();
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'form-data; name="filename.pdf"; filename="file.pdf"';
}
})).toEqual('file.pdf');
});
it('only extracts filename with pdf extension', function () {
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'attachment; filename="filename.png"';
}
})).toBeNull();
});
it('extension validation is case insensitive', function () {
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'form-data; name="fieldName"; filename="file.PdF"';
}
})).toEqual('file.PdF');
});
});
describe('createResponseStatusError', function() { describe('createResponseStatusError', function() {
it('handles missing PDF file responses', function() { it('handles missing PDF file responses', function() {
expect(createResponseStatusError(404, 'https://foo.com/bar.pdf')).toEqual( expect(createResponseStatusError(404, 'https://foo.com/bar.pdf')).toEqual(

View File

@ -154,6 +154,7 @@ let PDFViewerApplication = {
baseUrl: '', baseUrl: '',
externalServices: DefaultExternalServices, externalServices: DefaultExternalServices,
_boundEvents: {}, _boundEvents: {},
contentDispositionFilename: null,
// Called once when the document is loaded. // Called once when the document is loaded.
initialize(appConfig) { initialize(appConfig) {
@ -678,6 +679,7 @@ let PDFViewerApplication = {
this.downloadComplete = false; this.downloadComplete = false;
this.url = ''; this.url = '';
this.baseUrl = ''; this.baseUrl = '';
this.contentDispositionFilename = null;
this.pdfSidebar.reset(); this.pdfSidebar.reset();
this.pdfOutlineViewer.reset(); this.pdfOutlineViewer.reset();
@ -801,7 +803,8 @@ let PDFViewerApplication = {
let url = this.baseUrl; let url = this.baseUrl;
// Use this.url instead of this.baseUrl to perform filename detection based // Use this.url instead of this.baseUrl to perform filename detection based
// on the reference fragment as ultimate fallback if needed. // on the reference fragment as ultimate fallback if needed.
let filename = getPDFFileNameFromURL(this.url); let filename = this.contentDispositionFilename ||
getPDFFileNameFromURL(this.url);
let downloadManager = this.downloadManager; let downloadManager = this.downloadManager;
downloadManager.onerror = (err) => { downloadManager.onerror = (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
@ -1153,9 +1156,11 @@ let PDFViewerApplication = {
}); });
}); });
pdfDocument.getMetadata().then(({ info, metadata, }) => { pdfDocument.getMetadata().then(
({ info, metadata, contentDispositionFilename, }) => {
this.documentInfo = info; this.documentInfo = info;
this.metadata = metadata; this.metadata = metadata;
this.contentDispositionFilename = contentDispositionFilename;
// Provides some basic debug information // Provides some basic debug information
console.log('PDF ' + pdfDocument.fingerprint + ' [' + console.log('PDF ' + pdfDocument.fingerprint + ' [' +
@ -1178,7 +1183,10 @@ let PDFViewerApplication = {
} }
if (pdfTitle) { if (pdfTitle) {
this.setTitle(pdfTitle + ' - ' + document.title); this.setTitle(
`${pdfTitle} - ${contentDispositionFilename || document.title}`);
} else if (contentDispositionFilename) {
this.setTitle(contentDispositionFilename);
} }
if (info.IsAcroFormPresent) { if (info.IsAcroFormPresent) {

View File

@ -71,24 +71,26 @@ class PDFDocumentProperties {
return; return;
} }
// Get the document properties. // Get the document properties.
this.pdfDocument.getMetadata().then(({ info, metadata, }) => { this.pdfDocument.getMetadata().then(
({ info, metadata, contentDispositionFilename, }) => {
return Promise.all([ return Promise.all([
info, info,
metadata, metadata,
contentDispositionFilename || getPDFFileNameFromURL(this.url),
this._parseFileSize(this.maybeFileSize), this._parseFileSize(this.maybeFileSize),
this._parseDate(info.CreationDate), this._parseDate(info.CreationDate),
this._parseDate(info.ModDate) this._parseDate(info.ModDate)
]); ]);
}).then(([info, metadata, fileSize, creationDate, modificationDate]) => { }).then(([info, metadata, fileName, fileSize, creationDate, modDate]) => {
freezeFieldData({ freezeFieldData({
'fileName': getPDFFileNameFromURL(this.url), 'fileName': fileName,
'fileSize': fileSize, 'fileSize': fileSize,
'title': info.Title, 'title': info.Title,
'author': info.Author, 'author': info.Author,
'subject': info.Subject, 'subject': info.Subject,
'keywords': info.Keywords, 'keywords': info.Keywords,
'creationDate': creationDate, 'creationDate': creationDate,
'modificationDate': modificationDate, 'modificationDate': modDate,
'creator': info.Creator, 'creator': info.Creator,
'producer': info.Producer, 'producer': info.Producer,
'version': info.PDFFormatVersion, 'version': info.PDFFormatVersion,