diff --git a/.gitignore b/.gitignore index 71909ee81..e4bdf0e1d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ gallery-dan todo node_modules npm-debug.log -resizeImages \ No newline at end of file +resizeImages +*.sublime-project +*.sublime-workspace diff --git a/app.js b/app.js index 760fe979f..fb3fa1682 100644 --- a/app.js +++ b/app.js @@ -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) { diff --git a/js/admin-image-manipulation.js b/js/admin-image-manipulation.js index e7ab7dd36..60f34b8f6 100644 --- a/js/admin-image-manipulation.js +++ b/js/admin-image-manipulation.js @@ -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) { @@ -184,7 +181,16 @@ module.exports.preview = function (arg) { ); }; - +/*** +* ####### +* # # # #### # # ##### ###### +* # ## # # # # # # # +* ##### # # # #### # # # # ##### +* # # # # # # # ##### # +* # # ## # # # # # # # +* ####### # # #### #### # # ###### +* +*/ /** Create directory or use existing directory @@ -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(); } @@ -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); } @@ -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; } @@ -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)); } } ); diff --git a/js/directory-contents.js b/js/directory-contents.js index cfb839cd1..f8e861bd4 100644 --- a/js/directory-contents.js +++ b/js/directory-contents.js @@ -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); @@ -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) }, @@ -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; @@ -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, @@ -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"); diff --git a/public/views.js b/public/views.js index 78806b6ca..a5b298dcd 100644 --- a/public/views.js +++ b/public/views.js @@ -3,6 +3,6 @@ function encodeHTMLSource() { var encodeHTMLRules = { "&": "&", "<": "< String.prototype.encodeHTML=encodeHTMLSource(); var tmpl = {}; tmpl['directory-list-item']=function anonymous(it) { -var out=''; var arg = arguments[1],tempThumbFolder = "_historyThumb";if(it.content.type === "folder" && it.name !== tempThumbFolder){out+='