Skip to content

Commit

Permalink
Fix: Download watermarked (#746)
Browse files Browse the repository at this point in the history
- Send content disposition header to force representation to be downloaded as attachment
- Expose asset path as method on viewer instead of directly accessing
  • Loading branch information
tonyjin authored and pramodsum committed Mar 28, 2018
1 parent 8b267aa commit de4475d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,11 @@ class Preview extends EventEmitter {
return;
}

// This allows the browser to download representation content
const params = Object.assign({ response_content_disposition_type: 'attachment' }, queryParams);
const downloadUrl = appendQueryParams(
this.viewer.createContentUrlWithAuthParams(contentUrlTemplate, this.viewer.options.viewer.ASSET),
queryParams
this.viewer.createContentUrlWithAuthParams(contentUrlTemplate, this.viewer.getAssetPath()),
params
);

DownloadReachability.downloadWithReachabilityCheck(downloadUrl);
Expand Down
9 changes: 6 additions & 3 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ describe('lib/Preview', () => {
};
preview.viewer = {
getRepresentation: sandbox.stub(),
getAssetPath: sandbox.stub(),
createContentUrlWithAuthParams: sandbox.stub(),
options: {
viewer: {
Expand Down Expand Up @@ -815,12 +816,14 @@ describe('lib/Preview', () => {
const url = 'someurl';

preview.viewer.getRepresentation.returns(representation);
preview.viewer.createContentUrlWithAuthParams.withArgs(template, '').returns(url);
preview.viewer.getAssetPath.returns('1.jpg');
preview.viewer.createContentUrlWithAuthParams.withArgs(template, '1.jpg').returns(url);

util.appendQueryParams.withArgs(url).returns(url);
util.appendQueryParams.returns(url);

preview.download();

expect(util.appendQueryParams).to.be.calledWith(url, { response_content_disposition_type: 'attachment' });
expect(DownloadReachability.downloadWithReachabilityCheck).to.be.calledWith(url);
});

Expand All @@ -829,7 +832,7 @@ describe('lib/Preview', () => {
file.shouldDownloadWM.returns(false);

const url = 'someurl';
util.appendQueryParams.withArgs(url).returns(url);
util.appendQueryParams.returns(url);

const promise = Promise.resolve({
download_url: url
Expand Down
27 changes: 18 additions & 9 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,24 @@ class BaseViewer extends EventEmitter {
}
}

/**
* Returns the representation used for Preview.
*
* @return {Object} Box representation used/to be used by Preview
*/
getRepresentation() {
return this.options.representation;
}

/**
* Returns the asset path to access representation content for Preview.
*
* @return {string} Asset path
*/
getAssetPath() {
return getProp(this, 'options.viewer.ASSET', '');
}

//--------------------------------------------------------------------------
// Annotations
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -1028,15 +1046,6 @@ class BaseViewer extends EventEmitter {
})
);
}

/**
* Returns the representation used for Preview.
*
* @return {Object} Box representation used/to be used by Preview
*/
getRepresentation() {
return this.options.representation;
}
}

export default BaseViewer;
28 changes: 21 additions & 7 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,27 @@ describe('lib/viewers/BaseViewer', () => {
});
});

describe('getRepresentation()', () => {
it('should return the representation the viewer is/will use to preview', () => {
base.options.representation = { some: 'stuff' };
expect(base.getRepresentation()).to.equal(base.options.representation);
});
});

describe('getAssetPath()', () => {
it('should return the asset path the viewer is/will use for preview representation content', () => {
base.options.viewer = {
ASSET: '1.jpg'
};
expect(base.getAssetPath()).to.equal(base.options.viewer.ASSET);
});

it('should return empty string if viewer does not have a special asset path', () => {
base.options.viewer = {};
expect(base.getAssetPath()).to.equal('');
});
});

describe('loadAnnotator()', () => {
const conf = {
annotationsEnabled: true,
Expand Down Expand Up @@ -1325,11 +1346,4 @@ describe('lib/viewers/BaseViewer', () => {
expect(combinedOptions.localizedStrings).to.not.be.undefined;
});
});

describe('getRepresentation()', () => {
it('should return the representation the viewer is/will use to preview', () => {
base.options.representation = { some: 'stuff' };
expect(base.getRepresentation()).to.equal(base.options.representation);
});
});
});

0 comments on commit de4475d

Please sign in to comment.