Skip to content

Commit

Permalink
Admin > Image Resize & Move: Move RAW with sorted photo
Browse files Browse the repository at this point in the history
  • Loading branch information
danactive committed Dec 22, 2014
1 parent 7f519d2 commit b5b933e
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 100 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ gallery-dan
todo
node_modules
npm-debug.log
resizeImages
resizeImages
*.sublime-project
*.sublime-workspace
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ app.get(/(admin\/diff-album-path)/, function(request, response) {
app.post(/(admin\/preview-generator)/, function(request, response) {
adminImageManipulation.preview({"request": request, "response": response});
});
app.post(/(admin\/rename-photos)/, function(request, response) {
app.post(/(admin\/rename-assets)/, function(request, response) {
adminImageManipulation.rename({"request": request, "response": response});
});
app.post(/(admin\/resize-photo)/, function(request, response) {
Expand Down
118 changes: 68 additions & 50 deletions js/admin-image-manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ module.exports.preview = function (arg) {
function addToQueue() {
var item = json.items[count.imageInitiated];
if (item === undefined) {
if (constant.config.debug && constant.config.debug === true) {
console.log("addToQueue; item="+item+";");
}
return;
}
queue.push(item, function (errorEach) {
Expand Down Expand Up @@ -184,7 +181,16 @@ module.exports.preview = function (arg) {
);
};


/***
* #######
* # # # #### # # ##### ######
* # ## # # # # # # #
* ##### # # # #### # # # # #####
* # # # # # # # ##### #
* # # ## # # # # # # #
* ####### # # #### #### # # ######
*
*/
/**
Create directory or use existing directory
Expand Down Expand Up @@ -263,25 +269,28 @@ function _deletePath(arg, unitTestCallback) {
response = arg.response;
request = arg.request;
targetPath = path.join(path.dirname(__dirname), targetPath);
targetPath = decodeURIComponent(targetPath);

require('rimraf')(targetPath, function(err) {
var out;
if (err) {
errors.push("Delete failed on this path: " + targetPath + "; with this error message:" + err + ";");
}
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify(
{
"meta": {
"error": {
"count": errors.length,
"message": errors.join('; ')
},
"success": {
"message": targetPath + " folder successfully deleted."
}
out = {
"meta": {
"error": {
"count": errors.length,
"message": errors.join('; ')
}
}
));
};
if (errors.length === 0) {
out.meta.success = {
"message": targetPath + " folder successfully deleted."
};
}
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify(out));
if (unitTestCallback) {
unitTestCallback();
}
Expand Down Expand Up @@ -316,10 +325,11 @@ function _movePhotos(arg, callback) {
beforeRename,
callbackCount = 0,
destinationPath,
files = [],
assets = [],
fs = require('fs'),
isMoveToResize,
queue;
queue,
year;
if (arg === undefined) {
throw new ReferenceError(_error.missingArg);
}
Expand All @@ -334,59 +344,69 @@ function _movePhotos(arg, callback) {
destinationPath = decodeURIComponent(destinationPath);
isMoveToResize = (arg.moveToResize === "true" || arg.moveToResize === true);

if (isMoveToResize === true) {
year = arg.assets.sort[0].substring(0, 4);
_ensureDestinationFolder({"targetFolderName": year});
}

arg.assets.sort.forEach(function (id) {
beforeRename = decodeURIComponent(arg.assets[id].files[0].raw);
afterRename = (isMoveToResize) ? destinationPath + arg.assets[id].files[0].moved : arg.assets[id].files[0].renamed;
arg.assets[id].files.forEach(function (file) {
beforeRename = decodeURIComponent(file.raw);
afterRename = (isMoveToResize) ? destinationPath + file.moved : file.renamed;

if (constant.config.debug === true) {
console.log("_movePhotos: beforeRename(" + beforeRename + "); afterRename(" + afterRename + ")");
}
if (constant.config.debug === true) {
console.log("_movePhotos: beforeRename(" + beforeRename + "); afterRename(" + afterRename + ")");
}

if (beforeRename === undefined || beforeRename === "" || afterRename === undefined || afterRename === "") {
throw new TypeError(_error.emptyRenameFile);
}
files.push({
"source": {
"path": {
"type": "relative",
"value": beforeRename
}
},
"destination": {
"moved": isMoveToResize,
"path": {
"type": "absolute",
"value": afterRename
}
if (beforeRename === undefined || beforeRename === "" || afterRename === undefined || afterRename === "") {
throw new TypeError(_error.emptyRenameFile);
}
assets.push({
"destination": {
"moved": isMoveToResize,
"path": {
"type": "absolute",
"value": afterRename
}
},
"mediaType": file.mediaType,
"source": {
"path": {
"type": "relative",
"value": beforeRename
}
}
});
});
});

function possibleCallback() { // calculate if all async calls are complete
callbackCount++;

if (callbackCount === (files.length + 1)) { // +1 for drain
callback({"assets": files});
if (callbackCount === (assets.length + 1)) { // +1 for drain
callback({"assets": assets});
}
}

queue = require("async").queue(function (file, errorCallback) {
fs.exists(file.source.path.value, function (exists) {
var destinationPath = file.destination.path.value,
sourcePath = file.source.path.value;
fs.exists(sourcePath, function (exists) {
if (exists) {
fs.rename(file.source.path.value, file.destination.path.value, function (warningRename) {
fs.rename(sourcePath, destinationPath, function (warningRename) {
if (warningRename) {
console.log("Image renaming warning (_movePhotos): " + warningRename + "; Before filename=" + file.destination.path.value + "; After filename=" + file.source.path.value + ";");
console.log("Image renaming warning (_movePhotos): " + warningRename + "; Before filename=" + sourcePath + "; After filename=" + destinationPath + ";");
}
possibleCallback();
});
errorCallback();
} else {
console.log("Image does not exist (_movePhotos): " + file.source.path.value + ";");
console.log("Image does not exist (_movePhotos): " + sourcePath + ";");
}
});
}, 1);

queue.push(files, function(errorEach){
queue.push(assets, function(errorEach){
if (errorEach) {
throw errorEach;
}
Expand Down Expand Up @@ -537,13 +557,11 @@ module.exports.rename = function (arg, unitTestCallback) {
_movePhotos(
request.body,
function (payload) {
var out = {"files": payload.assets};

if (unitTestCallback) {
unitTestCallback(out);
unitTestCallback(payload);
} else {
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify(out));
response.end(JSON.stringify(payload));
}
}
);
Expand Down
83 changes: 47 additions & 36 deletions js/directory-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

//Push HTML to DOM
$.each(html, function (i, htm) {
$('.js-directory-list')
$('.js-directory-column')
.eq(i)
.html(htm)
.sortable(sortArgs);
Expand Down Expand Up @@ -73,10 +73,9 @@
$datepicker.datepicker( "destroy" );

$.ajax({
"url": '/admin/rename-photos',
"url": '/admin/rename-assets',
"method": 'post',
"data": {
"mediaType": "image",
"moveToResize": isMoveToResize,
"assets": generateFilenames(formattedDate)
},
Expand All @@ -102,8 +101,10 @@
};

if (isMoveToResize === true) {
$.each(response.files, function (x, file) {
resizeImage(file);
$.each(response.assets, function (x, asset) {
if (asset.mediaType === "image") {
resizeImage(asset);
}
});
deleteTempThumb();
output = xmlOutput;
Expand All @@ -122,12 +123,8 @@

generateFilenames = function (datePrefix) {
var generated,
out = {
"sort": []
},
photoCount = $(".js-directory-list").children('li[data-type=image]').length,
photoIndex = -1,
targetFolder = datePrefix.substring(0, 4);
photoCount = $(".js-directory-column").children('li[data-type=image]').length,
targetFolder = datePrefix.substring(0, 4) + "/";

generated = window.walkPath.getRenamedFiles({
"filePrefix": datePrefix,
Expand All @@ -137,34 +134,48 @@

xmlOutput = generated.xml;

// list of ordered filenames
$('.js-directory-list').each(function (x, dom) {
var $element = $(dom),
filenames = [], // sortable image filenames
id;
if ($element.children().length <= 0) {
return true; // continue
}
filenames = $element.sortable( "toArray", {"attribute": 'data-filename'});
$.each(filenames, function (i) {
photoIndex++;
id = generated.files[photoIndex];
out.sort.push(id);
out[id] = {
"files": [
{
"moved": targetFolder + "/" + generated.filenames[photoIndex],
"raw": qs.folder + filenames[i],
"renamed": qs.folder + generated.filenames[photoIndex]
}
]
};
});
});
return out;
return getSortedAssets(generated.files, generated.filenames, targetFolder, qs.folder);
};
}); // click
}
function getSortedAssets(renamedFiles, renamedFilenames, targetFolder, sourceFolder) {
var draggableIndex = -1,
out = {
"sort": []
};
// list of ordered filenames
$('.js-directory-column').each(function (x, column) {
var $asset,
$column = $(column),
$photo,
rawIds = [], // sortable image filenames
renamedId;
if ($column.children().length <= 0) {
return true; // continue
}
rawIds = $column.sortable( "toArray");

$.each(rawIds, function (i) {
draggableIndex++;
renamedId = renamedFiles[draggableIndex];
$photo = $("#" + rawIds[i]);
out.sort.push(renamedId);
out[renamedId] = {
"files": []
};
$('.js-directory-column > li[data-file=' + $photo.attr("data-file") + ']').each(function (xx, asset) {
$asset = $(asset);
out[renamedId].files.push({
"mediaType": $asset.attr("data-type"),
"moved": targetFolder + renamedFiles[draggableIndex] + $asset.attr("data-ext"),
"raw": sourceFolder + $asset.attr("data-filename"),
"renamed": sourceFolder + renamedFiles[draggableIndex] + $asset.attr("data-ext")
});
});
});
});
return out;
}
function loadNav() {
if (parent.text === "") {
$("#btnParentFolder").addClass("hide");
Expand Down
2 changes: 1 addition & 1 deletion public/views.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/admin-image-manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('Admin:', function () {
arg.request.body.moveToResize = false;

page.rename(arg, function (result) {
expect(result.files[0].destination.value).to.be(arg.request.body.assets.renamable.files.renamed);
expect(result.assets[0].destination.value).to.be(arg.request.body.assets.renamable.files.renamed);
done();
});
});
Expand All @@ -307,7 +307,7 @@ describe('Admin:', function () {
arg.request.body.assets.renamable.files[0].renamed = tempName;

page.rename(arg, function (result) {
expect(result.files[0].destination.value).to.be(arg.request.body.assets.renamable.files.renamed);
expect(result.assets[0].destination.value).to.be(arg.request.body.assets.renamable.files.renamed);
done();
});
});
Expand Down
8 changes: 5 additions & 3 deletions views/directory-list-item.dot
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{{ var arg = arguments[1],
id = it.name.replace(/[\.\s\(\)]/g, "_") + it.ext.replace(/[\.\s\(\)]/g, "_"),
name = it.name.replace(/[\.\s\(\)]/g, "_"),
tempThumbFolder = "_historyThumb";}}
{{? it.content.type === "folder" && it.name !== tempThumbFolder }}
<li><span>{{=it.name}}{{=it.ext}}</span><!--a href="{{=it.path.nav}}{{=encodeURIComponent(it.name)}}{{=it.ext}}&amp;preview=false">Directory list</a--> <a href="{{=it.path.nav}}{{=encodeURIComponent(it.name)}}/&amp;preview=true">Thumbnail preview</a></li>
<li><span>{{=it.name}}{{=it.ext}}</span> <a href="{{=it.path.nav}}{{=encodeURIComponent(it.name)}}/&amp;preview=true">Thumbnail preview</a></li>
{{?? it.content.type === "image" && arg.qs.preview === "true" }}
<li data-file="{{=it.name}}" data-ext="{{=it.ext}}" data-filename="{{=it.name}}{{=it.ext}}" data-type="{{=it.content.type}}"><a href="{{=it.path.rel}}{{=it.name}}{{=it.ext}}" target="_blank">{{=it.name}}{{=it.ext}}</a><img src="/img/spinner.gif" class="directory-thumb-wait"></li>
<li id="{{=id}}" data-file="{{=name}}" data-filename="{{=it.name}}{{=it.ext}}" data-ext="{{=it.ext}}" data-type="{{=it.content.type}}"><a href="{{=it.path.rel}}{{=it.name}}{{=it.ext}}" target="_blank">{{=it.name}}{{=it.ext}}</a><img src="/img/spinner.gif" class="directory-thumb-wait"></li>
{{?? it.content.type === "image" && arg.qs.preview === "false" }}
<li><a href="{{=it.path.rel}}{{=it.name}}{{=it.ext}}" target="_blank">{{=it.name}}{{=it.ext}}</a><img src="{{=it.path.rel}}{{=it.name}}{{=it.ext}}" class="directory-thumb"></li>
{{?? it.content.type === "folder" && it.name === tempThumbFolder }}
<li><span>{{=it.name}}{{=it.ext}}</span></li>
{{??}}
<li data-file="{{=it.name}}" data-ext="{{=it.ext}}" data-filename="{{=it.name}}{{=it.ext}}" data-type="{{=it.content.type}}"><span>{{=it.name}}{{=it.ext}}</span></li>
<li id="{{=id}}" data-file="{{=name}}" data-ext="{{=it.ext}}" data-filename="{{=it.name}}{{=it.ext}}" data-type="{{=it.content.type}}"><span>{{=it.name}}{{=it.ext}}</span></li>
{{?}}
Loading

0 comments on commit b5b933e

Please sign in to comment.