Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: support for numeric file id #695

Merged
merged 3 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
findScriptLocation,
appendQueryParams,
replacePlaceholders,
stripAuthFromString
stripAuthFromString,
isValidFileId
} from './util';
import {
isDownloadHostBlocked,
Expand Down Expand Up @@ -294,13 +295,16 @@ class Preview extends EventEmitter {
const fileIds = [];

fileOrIds.forEach((fileOrId) => {
if (fileOrId && typeof fileOrId === 'string') {
if (fileOrId && isValidFileId(fileOrId)) {
// String id found in the collection
fileIds.push(fileOrId);
} else if (fileOrId && typeof fileOrId === 'object' && typeof fileOrId.id === 'string') {
fileIds.push(fileOrId.toString());
} else if (fileOrId && typeof fileOrId === 'object' && isValidFileId(fileOrId.id)) {
// Possible well-formed file object found in the collection
fileIds.push(fileOrId.id);
files.push(fileOrId);
const wellFormedFileObj = Object.assign({}, fileOrId, {
id: fileOrId.id.toString()
});
fileIds.push(wellFormedFileObj.id);
files.push(wellFormedFileObj);
} else {
throw new Error('Bad collection provided!');
}
Expand Down Expand Up @@ -663,8 +667,8 @@ class Preview extends EventEmitter {
const fileVersionId = this.getFileOption(fileIdOrFile, FILE_OPTION_FILE_VERSION_ID) || '';

// Check what was passed to preview.show()—string file ID or some file object
if (typeof fileIdOrFile === 'string') {
const fileId = fileIdOrFile;
if (typeof fileIdOrFile === 'string' || typeof fileIdOrFile === 'number') {
const fileId = fileIdOrFile.toString();

// If we want to load by file version ID, use that as key for cache
const cacheKey = fileVersionId ? { fileVersionId } : { fileId };
Expand Down
8 changes: 7 additions & 1 deletion src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe('lib/Preview', () => {
});

it('should set the preview collection to an array of file ids when files passed in', () => {
const files = ['1', { id: '2' }, '3', { id: '4' }, { id: '5' }];
let files = ['1', { id: '2' }, 3, { id: '4' }, { id: 5 }];

preview.updateCollection(files);
expect(stubs.updateFileCache).to.be.calledWith([{ id: '2' }, { id: '4' }, { id: '5' }]);
Expand Down Expand Up @@ -879,6 +879,12 @@ describe('lib/Preview', () => {
expect(file.getCachedFile).to.be.calledWith(preview.cache, { fileId });
});

it('should fetch file from cache and convert file id to string when file id passed as a number', () => {
const fileId = 123;
preview.load(fileId);
expect(file.getCachedFile).to.be.calledWith(preview.cache, { fileId : fileId.toString() });
});

it('should fetch file from cache using file version ID as key if file version ID is in options', () => {
const fileId = '123';
const fileVersionId = '1234';
Expand Down
22 changes: 22 additions & 0 deletions src/lib/__tests__/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,4 +837,26 @@ describe('lib/util', () => {
expect(util.getProp(a, 'foo.bar', 'default')).to.equal('default');
});
});

describe('isValidFileId()', () => {
it('should be valid if fileId is a numeric string', () => {
expect(util.isValidFileId('1')).to.be.true;
});

it('should be valid if fileId is a number', () => {
expect(util.isValidFileId(1)).to.be.true;
});

it('should be invalid if fileId is undefined', () => {
expect(util.isValidFileId()).to.be.false;
});

it('should be invalid if fileId is NaN', () => {
expect(util.isValidFileId(NaN)).to.be.false;
});

it('should be invalid if fileId is a mixed string', () => {
expect(util.isValidFileId('1234foo')).to.be.false;
});
});
});
2 changes: 1 addition & 1 deletion src/lib/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,4 @@ if (!String.prototype.startsWith) {
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
};
}
/* eslint-enable */
/* eslint-enable */
12 changes: 12 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,15 @@ export function getProp(object, propPath, defaultValue) {

return value !== undefined ? value : defaultValue;
}

/**
* Checks that a fileId is of the expected type.
* A fileId can be a number, or a string represenation of a number
*
* @param {number} fileId - The file id
* @return {boolean} True if it is valid
*/
export function isValidFileId(fileId) {
// Tests that the string or number contains all numbers
return /^\d+$/.test(fileId);
}
2 changes: 1 addition & 1 deletion src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class BaseViewer extends EventEmitter {

/** @property {Object} - Viewer startAt options */
startAt;

/** @property {boolean} - Has the viewer retried downloading the content */
hasRetriedContentDownload = false;

Expand Down