Skip to content

Commit

Permalink
feat(View Album): Dynamic thumbnail path (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Nov 13, 2016
1 parent eba3486 commit c6ca9a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
19 changes: 15 additions & 4 deletions plugins/album/lib/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const fs = require('fs');
const path = require('path');
const xml2js = require('xml2js');

const utils = require('../../utils/lib');

function title(item) {
const presentable = (...values) => values.every(value => value !== undefined && value !== '');
if (presentable(item.photoLoc, item.photoCity, item.photoDesc)) {
Expand Down Expand Up @@ -35,22 +37,31 @@ function caption(item) {
}
module.exports.caption = caption;

function thumbPath(item) {
return `/static/gallery-dan/media/thumbs/2016/${item.filename}`;
function thumbPath(item, gallery) {
if (!item || !item.filename) {
return undefined;
}

let filename = (typeof item.filename === 'string') ? item.filename : item.filename[0];
filename = filename.replace(utils.file.type(filename), 'jpg');
const year = filename.indexOf('-') >= 0 && filename.split('-')[0];
return `/static/gallery-${gallery}/media/thumbs/${year}/${filename}`;
}
module.exports.thumbPath = thumbPath;


function templatePrepare(result = {}) {
if (!result.album || !result.album.item) {
if (!result.album || !result.album.item || !result.album.meta) {
return result;
}

const gallery = result.album.meta.gallery;
const output = clone(result);

output.album.items = output.album.item.map((item) => {
item.caption = caption(item);
item.title = title(item);
item.path = thumbPath(item);
item.path = thumbPath(item, gallery);
return item;
});
delete output.album.item;
Expand Down
2 changes: 1 addition & 1 deletion plugins/album/test/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ testCases.push({
},
successView: (assert, response) => {
assert.ok(response, 'Has response');
assert.ok(response.indexOf('2012-fireplace.mp4') > 0, 'HTML string');
assert.ok(response.indexOf('2012-fireplace.jpg') > 0, 'HTML string');
assert.end();
},
error: (assert, error) => assert.fail(`Unexpected response found ${JSON.stringify(error)}`),
Expand Down
17 changes: 10 additions & 7 deletions plugins/album/test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ tape('Read album XML', { skip: false }, (describe) => {
assert.end();
});

describe.test('* Path', { skip: false }, (assert) => {
const item = { filename: '2016-12-31-01.jpg' };
const expectedPath = `/static/gallery-demo/media/thumbs/2016/${item.filename}`;
assert.equal(lib.thumbPath(item, 'demo'), expectedPath, 'Path');
assert.end();
});

describe.test('* Title', { skip: false }, (assert) => {
const item = { photoDesc: 'Description' };
assert.equal(lib.title(item), item.photoDesc, 'Description');
Expand Down Expand Up @@ -63,20 +70,16 @@ tape('Read album XML', { skip: false }, (describe) => {
assert.deepEqual(result.album.meta, mock.album.meta, 'Meta (w/ Items)');
assert.deepEqual(result.album.items[0].$, mock.album.item[0].$, 'Items (w/ Meta)');

delete mock.album.meta;
result = lib.templatePrepare(mock);
assert.deepEqual(result.album.items[0].$, mock.album.item[0].$, 'Items');

assert.end();
});

describe.test('* Prepare JSON for view template with enhancements', { skip: false }, (assert) => {
const mock = {
album: {
meta: 'Self talk',
meta: { gallery: 'demo' },
item: [{
$: { id: 1 },
filename: 'Filename.jpg',
filename: '2016-Filename.jpg',
photoDesc: 'Desc',
photoCity: 'City',
thumbCaption: 'Caption',
Expand All @@ -89,7 +92,7 @@ tape('Read album XML', { skip: false }, (describe) => {
assert.deepEqual(result.album.items[0].$, mock.album.item[0].$, 'Items (w/ Meta)');
assert.equal(result.album.items[0].caption, 'Caption', 'Caption');
assert.equal(result.album.items[0].title, 'City: Desc', 'Title');
assert.equal(result.album.items[0].path, '/static/gallery-dan/media/thumbs/2016/Filename.jpg', 'Path');
assert.equal(result.album.items[0].path, '/static/gallery-demo/media/thumbs/2016/2016-Filename.jpg', 'Path');

assert.end();
});
Expand Down

0 comments on commit c6ca9a7

Please sign in to comment.