mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-21 07:38:07 +02:00
Address the final round of review comments for Content-Disposition filename extraction
This patch updates the `IPDFStreamReader` interface and ensures that the interface/implementation of `network.js`, `fetch_stream.js`, `node_stream.js`, and `transport_stream.js` all match properly. The unit-tests are also adjusted, to more closely replicate the actual behaviour of the various actual `IPDFStreamReader` implementations. Finally, this patch adjusts the use of the Content-Disposition filename when setting the title in the viewer, and adds `PDFDocumentProperties` support as well.
This commit is contained in:
parent
eb1f6f4c24
commit
69a8336cf1
12 changed files with 151 additions and 121 deletions
|
@ -794,6 +794,7 @@ describe('api', function() {
|
|||
expect(metadata.info['Title']).toEqual('Basic API Test');
|
||||
expect(metadata.info['PDFFormatVersion']).toEqual('1.7');
|
||||
expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test');
|
||||
expect(metadata.contentDispositionFilename).toEqual(null);
|
||||
done();
|
||||
}).catch(function (reason) {
|
||||
done.fail(reason);
|
||||
|
|
|
@ -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() {
|
||||
it('handles missing PDF file responses', function() {
|
||||
expect(createResponseStatusError(404, 'https://foo.com/bar.pdf')).toEqual(
|
||||
|
@ -175,62 +253,4 @@ describe('network_utils', function() {
|
|||
expect(validateResponseStatus(undefined)).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('extractFilenameFromHeader', function () {
|
||||
it('returns null when content disposition header is blank', function() {
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return null;
|
||||
})).toBeNull();
|
||||
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return undefined;
|
||||
})).toBeNull();
|
||||
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return '';
|
||||
})).toBeNull();
|
||||
});
|
||||
|
||||
it('gets the filename from the response header', function () {
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return 'Content-Disposition: inline';
|
||||
})).toBeNull();
|
||||
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return 'Content-Disposition: attachment';
|
||||
})).toBeNull();
|
||||
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return 'Content-Disposition: attachment; filename="filename.pdf"';
|
||||
})).toBe('filename.pdf');
|
||||
});
|
||||
|
||||
it('returns null when content disposition is form-data', function () {
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return 'Content-Disposition: form-data';
|
||||
})).toBeNull();
|
||||
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return 'Content-Disposition: form-data; name="filename"';
|
||||
})).toBeNull();
|
||||
|
||||
expect(extractFilenameFromHeader(function () {
|
||||
return 'Content-Disposition: form-data; ' +
|
||||
'name="filename"; filename="file.pdf"';
|
||||
})).toBe('file.pdf');
|
||||
});
|
||||
|
||||
it('Only extracts file names with pdf extension', function () {
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return 'Content-Disposition: attachment; filename="filename.png"';
|
||||
})).toBeNull();
|
||||
});
|
||||
|
||||
it('Extension validation is case insensitive', function () {
|
||||
expect(extractFilenameFromHeader(function() {
|
||||
return 'Content-Disposition: form-data; ' +
|
||||
'name="fieldName"; filename="file.PdF"';
|
||||
})).toBe('file.PdF');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue