Skip to content

Commit

Permalink
Chore: Add header for pdf compression (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Mar 22, 2018
1 parent edbf66c commit ad97a90
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export const X_REP_HINT_IMAGE = '[jpg?dimensions=2048x2048,png?dimensions=2048x2
export const X_REP_HINT_VIDEO_DASH = '[dash,mp4][filmstrip]';
export const X_REP_HINT_VIDEO_MP4 = '[mp4]';

// X-Box-Accept-Encoding Header for requesting non-gzipped representation
export const X_BOX_ACCEPT_ENCODING_HEADER = 'x-box-accept-encoding';
export const X_BOX_ACCEPT_ENCODING_IDENTITY = 'identity';

export const PDFJS_CSS_UNITS = 96.0 / 72.0; // Should match CSS_UNITS in pdf_viewer.js
export const PDFJS_MAX_AUTO_SCALE = 1.25; // Should match MAX_AUTO_SCALE in pdf_viewer.js
export const PDFJS_WIDTH_PADDING_PX = 40; // Should match SCROLLBAR_PADDING in pdf_viewer.js
Expand Down
5 changes: 2 additions & 3 deletions src/lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ export function canDownload(file, previewOptions) {

// Can download watermarked representation if base check passes, user has preview permissions, Preview is configured
// to force watermarked downloads, and the file is watermarked
const canDownloadWatermarked = passBaseDownloadCheck &&
hasPreviewPermission &&
shouldDownloadWM(file, previewOptions);
const canDownloadWatermarked =
passBaseDownloadCheck && hasPreviewPermission && shouldDownloadWM(file, previewOptions);

return canDownloadOriginal || canDownloadWatermarked;
}
12 changes: 6 additions & 6 deletions src/lib/viewers/box3d/Box3DRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ class Box3DRenderer extends EventEmitter {
if (!this.vrCommonLoadPromise) {
if (commonEntities) {
this.vrCommonLoadPromise = this.box3d.importEntitiesFromUrl(
`${this.staticBaseURI}third-party/model3d/${MODEL3D_STATIC_ASSETS_VERSION}/WebVR/${
commonEntities
}/entities.json`,
`${
this.staticBaseURI
}third-party/model3d/${MODEL3D_STATIC_ASSETS_VERSION}/WebVR/${commonEntities}/entities.json`,
{ isExternal: true }
);
} else {
Expand All @@ -539,9 +539,9 @@ class Box3DRenderer extends EventEmitter {
if (!this.vrGamepadLoadPromises[controllerName]) {
this.vrGamepadLoadPromises[controllerName] = this.vrCommonLoadPromise.then(() => {
return this.box3d.importEntitiesFromUrl(
`${this.staticBaseURI}third-party/model3d/${MODEL3D_STATIC_ASSETS_VERSION}/WebVR/${
controllerName
}/entities.json`,
`${
this.staticBaseURI
}third-party/model3d/${MODEL3D_STATIC_ASSETS_VERSION}/WebVR/${controllerName}/entities.json`,
{ isExternal: true }
);
});
Expand Down
10 changes: 9 additions & 1 deletion src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
DOC_STATIC_ASSETS_VERSION,
PERMISSION_DOWNLOAD,
PRELOAD_REP_NAME,
STATUS_SUCCESS
STATUS_SUCCESS,
X_BOX_ACCEPT_ENCODING_HEADER,
X_BOX_ACCEPT_ENCODING_IDENTITY
} from '../../constants';
import { checkPermission, getRepresentation } from '../../file';
import { get, createAssetUrlCreator, getMidpoint, getDistance, getClosestPageToPinch } from '../../util';
Expand Down Expand Up @@ -564,6 +566,12 @@ class DocBaseViewer extends BaseViewer {
};
}

// If range requests are enabled, request the non-gzip compressed version of the representation
if (!PDFJS.disableRange) {
docInitParams.httpHeaders = docInitParams.httpHeaders || {};
docInitParams.httpHeaders[X_BOX_ACCEPT_ENCODING_HEADER] = X_BOX_ACCEPT_ENCODING_IDENTITY;
}

// Start timing document load
this.startLoadTimer();

Expand Down
26 changes: 26 additions & 0 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
STATUS_ERROR,
STATUS_PENDING,
STATUS_SUCCESS,
X_BOX_ACCEPT_ENCODING_HEADER,
X_BOX_ACCEPT_ENCODING_IDENTITY
} from '../../../constants';

import { ICON_PRINT_CHECKMARK } from '../../../icons/icons';
Expand Down Expand Up @@ -850,6 +852,30 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
});
});

it('should set a x-box-accept-encoding header when range requests are enabled', () => {
docBase.options.location = {
locale: 'en-GB'
};
const isDisbled = PDFJS.disableRange;
sandbox.stub(Browser, 'isIOS').returns(false);
sandbox.stub(PDFJS, 'getDocument').returns(Promise.resolve({}));

PDFJS.disableRange = false;

return docBase.initViewer('').then(() => {
expect(PDFJS.getDocument).to.be.calledWith({
url: '',
rangeChunkSize: 524288,
httpHeaders: {
[X_BOX_ACCEPT_ENCODING_HEADER]: X_BOX_ACCEPT_ENCODING_IDENTITY
}
});

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

it('should resolve the loading task and set the document/viewer', () => {
const doc = {
url: 'url'
Expand Down
12 changes: 3 additions & 9 deletions src/lib/viewers/media/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,15 @@ const SETTINGS_TEMPLATE = `<div class="bp-media-settings">
<div class="bp-media-settings-arrow">${ICON_ARROW_LEFT}</div>
<div class="bp-media-settings-label" aria-label="${__('media_quality')}">${__('media_quality')}</div>
</div>
<div class="bp-media-settings-sub-item" data-type="quality" data-value=${
MEDIA_QUALITY_SD
} tabindex="0" role="menuitemradio">
<div class="bp-media-settings-sub-item" data-type="quality" data-value=${MEDIA_QUALITY_SD} tabindex="0" role="menuitemradio">
<div class="bp-media-settings-icon">${ICON_CHECK_MARK}</div>
<div class="bp-media-settings-value">480p</div>
</div>
<div class="bp-media-settings-sub-item" data-type="quality" data-value=${
MEDIA_QUALITY_HD
} tabindex="0" role="menuitemradio">
<div class="bp-media-settings-sub-item" data-type="quality" data-value=${MEDIA_QUALITY_HD} tabindex="0" role="menuitemradio">
<div class="bp-media-settings-icon">${ICON_CHECK_MARK}</div>
<div class="bp-media-settings-value">1080p</div>
</div>
<div class="bp-media-settings-sub-item bp-media-settings-selected" data-type="quality" data-value=${
MEDIA_QUALITY_AUTO
} tabindex="0" role="menuitemradio" aria-checked="true">
<div class="bp-media-settings-sub-item bp-media-settings-selected" data-type="quality" data-value=${MEDIA_QUALITY_AUTO} tabindex="0" role="menuitemradio" aria-checked="true">
<div class="bp-media-settings-icon">${ICON_CHECK_MARK}</div>
<div class="bp-media-settings-value">${__('media_quality_auto')}</div>
</div>
Expand Down

0 comments on commit ad97a90

Please sign in to comment.