From 78423ac98476617ff670a29ad75c86ee6cef1b58 Mon Sep 17 00:00:00 2001 From: Tony Jin Date: Mon, 17 Jul 2017 17:43:18 -0700 Subject: [PATCH] Update: Render Vera-protected HTML files (#220) Render Vera-protected files instead of displaying the raw HTML. --- build/karma.conf.js | 1 + src/lib/__tests__/util-test.js | 25 +++++++++++ src/lib/util.js | 13 +++++- src/lib/viewers/text/TextLoader.js | 18 ++++++++ .../viewers/text/__tests__/TextLoader-test.js | 44 +++++++++++++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/lib/viewers/text/__tests__/TextLoader-test.js diff --git a/build/karma.conf.js b/build/karma.conf.js index c526dc77d..78185f0be 100644 --- a/build/karma.conf.js +++ b/build/karma.conf.js @@ -1,3 +1,4 @@ +/* eslint-disable */ const webpackConfig = require('./webpack.karma.config'); const DOC_STATIC_ASSETS_VERSION = '0.130.0'; diff --git a/src/lib/__tests__/util-test.js b/src/lib/__tests__/util-test.js index 84b30ea56..3a5fe7f28 100644 --- a/src/lib/__tests__/util-test.js +++ b/src/lib/__tests__/util-test.js @@ -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'); diff --git a/src/lib/util.js b/src/lib/util.js index 04332c18f..d04e2ee98 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -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 @@ -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. * diff --git a/src/lib/viewers/text/TextLoader.js b/src/lib/viewers/text/TextLoader.js index 67559d7ab..278cdfee1 100644 --- a/src/lib/viewers/text/TextLoader.js +++ b/src/lib/viewers/text/TextLoader.js @@ -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'; @@ -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(); diff --git a/src/lib/viewers/text/__tests__/TextLoader-test.js b/src/lib/viewers/text/__tests__/TextLoader-test.js new file mode 100644 index 000000000..a56eeac99 --- /dev/null +++ b/src/lib/viewers/text/__tests__/TextLoader-test.js @@ -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); + }); + }); +});