Skip to content

Commit

Permalink
Update: Render Vera-protected HTML files (#220)
Browse files Browse the repository at this point in the history
Render Vera-protected files instead of displaying the raw HTML.
  • Loading branch information
tonyjin authored Jul 18, 2017
1 parent 814aa75 commit 78423ac
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable */
const webpackConfig = require('./webpack.karma.config');

const DOC_STATIC_ASSETS_VERSION = '0.130.0';
Expand Down
25 changes: 25 additions & 0 deletions src/lib/__tests__/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,31 @@ describe('lib/util', () => {
});
});

describe('isVeraProtectedFile()', () => {
[
'some.vera.pdf.html',
'.vera.test.html',
'blah.vera..html',
'another.vera.3.html',
'test.vera.html'
].forEach((fileName) => {
it('should return true if file is named like a Vera-protected file', () => {
expect(util.isVeraProtectedFile({ name: fileName })).to.be.true;
});
});

[
'vera.pdf.html',
'test.vera1.pdf.html',
'blah.vera..htm',
'another.verahtml',
].forEach((fileName) => {
it('should return false if file is not named like a Vera-protected file', () => {
expect(util.isVeraProtectedFile({ name: fileName })).to.be.false;
});
});
});

describe('setDimensions()', () => {
it('should set dimensions for the specified element', () => {
const element = document.createElement('div');
Expand Down
13 changes: 12 additions & 1 deletion src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ export function replacePlaceholders(string, placeholderValues) {
}

/**
* Check to see if a file requires a Box3D viewer to be viewed
* Check to see if a file requires a Box3D viewer to be viewed.
*
* @param {Object} file - The file to check
* @return {boolean} True if the file needs a Box3D 360 degree viewer to be viewed
Expand All @@ -619,6 +619,17 @@ export function requires360Viewer(file) {
return basename.endsWith('360');
}

/**
* Check to see if file is a Vera-protected file.
*
* @param {Object} file - File to check
* @return {boolean} Whether file is a Vera-protected HTML file
*/
export function isVeraProtectedFile(file) {
// Vera protected files will match this regex
return /.*\.(vera\..*|vera)\.html/i.test(file.name);
}

/**
* Set width/height for an element.
*
Expand Down
18 changes: 18 additions & 0 deletions src/lib/viewers/text/TextLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AssetLoader from '../AssetLoader';
import PlainTextViewer from './PlainTextViewer';
import MarkdownViewer from './MarkdownViewer';
import CSVViewer from './CSVViewer';
import { isVeraProtectedFile } from '../../util';
import { ORIGINAL_REP_NAME } from '../../constants';
import { HTML_EXTENSIONS, TXT_EXTENSIONS } from './extensions';

Expand Down Expand Up @@ -43,6 +44,23 @@ class TextLoader extends AssetLoader {
super();
this.viewers = VIEWERS;
}

/**
* @inheritdoc
*/
determineViewer(file, disabledViewers = []) {
const viewer = super.determineViewer(file, disabledViewers);

// If file is a Vera-protected file, do not return the TextLoader and instead let
// the determineViewer check fall back to the PDF document viewer, which renders
// the file - Vera wraps their encrypted files in a HTML file that when rendered,
// displays a Vera-branded message to view the file with Vera's application
if (viewer && isVeraProtectedFile(file)) {
return undefined;
}

return viewer;
}
}

export default new TextLoader();
44 changes: 44 additions & 0 deletions src/lib/viewers/text/__tests__/TextLoader-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable no-unused-expressions */
import TextLoader from '../TextLoader';
import * as util from '../../../util';

let file;
const sandbox = sinon.sandbox.create();

describe('lib/viewers/text/TextLoader', () => {
beforeEach(() => {
TextLoader.viewers = [
{
REP: 'ORIGINAL',
EXT: 'html'
}
];

file = {
extension: 'html',
representations: {
entries: [
{
representation: 'ORIGINAL'
}
]
}
};
});

afterEach(() => {
sandbox.verifyAndRestore();
});

describe('determineViewer()', () => {
it('should return viewer if file is not a Vera-protected file', () => {
sandbox.stub(util, 'isVeraProtectedFile').returns(false);
expect(TextLoader.determineViewer(file)).to.equal(TextLoader.viewers[0]);
});

it('should return undefined if file is a Vera-protected file', () => {
sandbox.stub(util, 'isVeraProtectedFile').returns(true);
expect(TextLoader.determineViewer(file)).to.equal(undefined);
});
});
});

0 comments on commit 78423ac

Please sign in to comment.