Skip to content

Commit

Permalink
feat(office): Add viewer option for max file size (#1235)
Browse files Browse the repository at this point in the history
* feat(office): Add viewer option for max file size

* feat(office): change option name

* feat(office): address PR feedback

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
Mingze and mergify[bot] authored Jul 14, 2020
1 parent 76fce43 commit 96942d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ class Preview extends EventEmitter {
}

// Determine the viewer to use
const viewer = loader.determineViewer(this.file, Object.keys(this.disabledViewers));
const viewer = loader.determineViewer(this.file, Object.keys(this.disabledViewers), this.options.viewers);

// Log the type of file
this.logger.setType(viewer.NAME);
Expand Down
10 changes: 6 additions & 4 deletions src/lib/viewers/office/OfficeLoader.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getProp from 'lodash/get';
import AssetLoader from '../AssetLoader';
import OfficeViewer from './OfficeViewer';
import { checkPermission } from '../../file';
Expand Down Expand Up @@ -29,14 +30,15 @@ class OfficeLoader extends AssetLoader {
/**
* @inheritdoc
*/
determineViewer(file, disabledViewers = []) {
determineViewer(file, disabledViewers = [], viewerOptions = {}) {
// The Office viewer is disabled when this is a password protected shared link
const isDisabledDueToPasswordProtectedSharedLink = file.shared_link && file.shared_link.is_password_enabled;
// If the user does not have permission to download the file, the file is larger than 5MB, or isDisabledDueToSharedLink is true,
// then disable the Office viewer
const maxFileSize = getProp(viewerOptions, 'Office.maxFileSize', FIVE_MB);
// If the user does not have permission to download the file, the file is larger than max file size,
// or isDisabledDueToSharedLink is true, then disable the Office viewer
if (
!checkPermission(file, PERMISSION_DOWNLOAD) ||
file.size > FIVE_MB ||
file.size > maxFileSize ||
isDisabledDueToPasswordProtectedSharedLink
) {
disabledViewers.push(OFFICE_VIEWER_NAME);
Expand Down
32 changes: 24 additions & 8 deletions src/lib/viewers/office/__tests__/OfficeLoader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import OfficeViewer from '../OfficeViewer';
import * as file from '../../../file';
import { PERMISSION_DOWNLOAD } from '../../../constants';

const FIVE_MB = 5242880;

const sandbox = sinon.sandbox.create();

describe('lib/viewers/office/OfficeLoader', () => {
Expand Down Expand Up @@ -43,9 +45,11 @@ describe('lib/viewers/office/OfficeLoader', () => {
});

it('should choose the Office viewer if it is not disabled and the file is a shared link that is not password-protected', () => {
const editedFakeFile = fakeFile;
editedFakeFile.shared_link = {
is_password_enabled: false,
const editedFakeFile = {
...fakeFile,
shared_link: {
is_password_enabled: false,
},
};
const viewer = OfficeLoader.determineViewer(editedFakeFile);
expect(viewer.NAME).to.equal('Office');
Expand All @@ -57,8 +61,7 @@ describe('lib/viewers/office/OfficeLoader', () => {
});

it('should not return a viewer if the file is too large', () => {
const editedFakeFile = fakeFile;
editedFakeFile.size = 5242881;
const editedFakeFile = { ...fakeFile, size: FIVE_MB + 1 };
const viewer = OfficeLoader.determineViewer(editedFakeFile, []);
expect(viewer).to.equal(undefined);
});
Expand All @@ -73,13 +76,26 @@ describe('lib/viewers/office/OfficeLoader', () => {
});

it('should not return a viewer if the file is a password-protected shared link', () => {
const editedFakeFile = fakeFile;
editedFakeFile.shared_link = {
is_password_enabled: true,
const editedFakeFile = {
...fakeFile,
shared_link: {
is_password_enabled: true,
},
};
const viewer = OfficeLoader.determineViewer(editedFakeFile, []);
expect(viewer).to.equal(undefined);
});

it('should respect maxFileSize in viewerOptions', () => {
const viewerOptions = {
Office: {
maxFileSize: FIVE_MB + 1,
},
};
const editedFakeFile = { ...fakeFile, size: FIVE_MB + 1 };
const viewer = OfficeLoader.determineViewer(editedFakeFile, [], viewerOptions);
expect(viewer).to.not.equal(undefined);
});
});
});
});

0 comments on commit 96942d6

Please sign in to comment.