Skip to content

Commit

Permalink
New: Add gzip encoding param to pdf reps (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Sep 25, 2018
1 parent 2aa9914 commit 5137a01
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,9 @@ export const PREVIEW_SCRIPT_NAME = 'preview.js';

export const FILE_OPTION_FILE_VERSION_ID = 'fileVersionId';
export const FILE_OPTION_START = 'startAt';

// Query parameter for requesting compressed representations
export const QUERY_PARAM_ENCODING = 'encoding';
export const ENCODING_TYPES = {
GZIP: 'gzip'
};
24 changes: 21 additions & 3 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ import {
DOC_STATIC_ASSETS_VERSION,
PERMISSION_DOWNLOAD,
PRELOAD_REP_NAME,
STATUS_SUCCESS
STATUS_SUCCESS,
QUERY_PARAM_ENCODING,
ENCODING_TYPES
} from '../../constants';
import { checkPermission, getRepresentation } from '../../file';
import { get, createAssetUrlCreator, getMidpoint, getDistance, getClosestPageToPinch } from '../../util';
import {
appendQueryParams,
get,
createAssetUrlCreator,
getMidpoint,
getDistance,
getClosestPageToPinch
} from '../../util';
import { ICON_PRINT_CHECKMARK } from '../../icons/icons';
import { JS, PRELOAD_JS, CSS } from './docAssets';
import { ERROR_CODE, VIEWER_EVENT } from '../../events';
Expand Down Expand Up @@ -572,8 +581,17 @@ class DocBaseViewer extends BaseViewer {
: RANGE_REQUEST_CHUNK_SIZE_NON_US;
}

let url = pdfUrl;

// If range requests are disabled, request the gzip compressed version of the representation
if (PDFJS.disableRange) {
url = appendQueryParams(url, {
[QUERY_PARAM_ENCODING]: ENCODING_TYPES.GZIP
});
}

const docInitParams = {
url: pdfUrl,
url,
rangeChunkSize
};

Expand Down
35 changes: 30 additions & 5 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
PERMISSION_DOWNLOAD,
STATUS_ERROR,
STATUS_PENDING,
STATUS_SUCCESS
STATUS_SUCCESS,
QUERY_PARAM_ENCODING,
ENCODING_TYPES
} from '../../../constants';

import { ICON_PRINT_CHECKMARK } from '../../../icons/icons';
Expand Down Expand Up @@ -862,7 +864,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

return docBase.initViewer(url).then(() => {
expect(PDFJS.getDocument).to.be.calledWith({
url,
url: sinon.match.string,
rangeChunkSize
});
});
Expand All @@ -880,7 +882,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

return docBase.initViewer(url).then(() => {
expect(PDFJS.getDocument).to.be.calledWith({
url,
url: sinon.match.string,
rangeChunkSize: defaultChunkSize
});
});
Expand All @@ -898,7 +900,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

return docBase.initViewer(url).then(() => {
expect(PDFJS.getDocument).to.be.calledWith({
url,
url: sinon.match.string,
rangeChunkSize: largeChunkSize
});
});
Expand All @@ -913,7 +915,7 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {

return docBase.initViewer('').then(() => {
expect(PDFJS.getDocument).to.be.calledWith({
url: '',
url: sinon.match.string,
rangeChunkSize: 1048576,
httpHeaders: {
'If-None-Match': 'webkit-no-cache'
Expand All @@ -922,6 +924,29 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
});
});

it('should append encoding query parameter for gzip content when range requests are disabled', () => {
// en-US allows for disabled range requests
docBase.options.location = {
locale: 'en-US'
};
const defaultChunkSize = 1048576; // Taken from RANGE_REQUEST_CHUNK_SIZE_US
const url = 'www.myTestPDF.com/123456';
const paramsList = `${QUERY_PARAM_ENCODING}=${ENCODING_TYPES.GZIP}`;
const isDisabled = PDFJS.disableRange;
sandbox.stub(Browser, 'isIOS').returns(false);
sandbox.stub(PDFJS, 'getDocument').returns(Promise.resolve({}));
PDFJS.disableRange = true;
return docBase.initViewer(url).then(() => {
expect(PDFJS.getDocument).to.be.calledWith({
url: `${url}?${paramsList}`,
rangeChunkSize: defaultChunkSize
});

// Reset to original value
PDFJS.disableRange = isDisabled;
});
});

it('should resolve the loading task and set the document/viewer', () => {
const doc = {
url: 'url'
Expand Down

0 comments on commit 5137a01

Please sign in to comment.