From 96b8ebab0450e708958d91f1c9d58ee98a91f42c Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sun, 29 May 2016 23:49:01 +0300 Subject: [PATCH] now .loadImages(..) will also try and load previews + refactoring and cleanup... Signed-off-by: Alex A. Naanou --- ui (gen4)/features/base.js | 18 +++--- ui (gen4)/features/cli.js | 8 +++ ui (gen4)/features/filesystem.js | 92 ++++++++++++++++++---------- ui (gen4)/file.js | 100 +++++++++++++++++++------------ ui (gen4)/lib/util.js | 2 +- 5 files changed, 143 insertions(+), 77 deletions(-) diff --git a/ui (gen4)/features/base.js b/ui (gen4)/features/base.js index c2297395..3ac7bc9e 100755 --- a/ui (gen4)/features/base.js +++ b/ui (gen4)/features/base.js @@ -196,18 +196,20 @@ actions.Actions({ return res }], + dataFromURLs: ['- File/', + function(lst, base){ + var imgs = images.Images.fromArray(lst, base) + return { + images: imgs, + data: data.Data.fromArray(imgs.keys()), + } + }], + // XXX should this be here??? // XXX should this use .load(..) // ...note if we use this it breaks, need to rethink... loadURLs: ['- File/Load a URL list', - function(lst, base){ - var imgs = images.Images.fromArray(lst, base) - - this.load({ - images: imgs, - data: data.Data.fromArray(imgs.keys()), - }) - }], + function(lst, base){ this.load(this.dataFromURLs(lst, base)) }], // XXX experimental... // ...the bad thing about this is that we can not extend this, diff --git a/ui (gen4)/features/cli.js b/ui (gen4)/features/cli.js index c8053e81..de67d151 100755 --- a/ui (gen4)/features/cli.js +++ b/ui (gen4)/features/cli.js @@ -8,6 +8,7 @@ define(function(require){ var module = {} //var DEBUG = DEBUG != null ? DEBUG : true +var util = require('lib/util') var actions = require('lib/actions') var features = require('lib/features') @@ -18,6 +19,11 @@ var core = require('features/core') var base = require('features/base') +if(typeof(process) != 'undefined'){ + var pathlib = requirejs('path') +} + + /*********************************************************************/ // XXX what we need here is: @@ -53,6 +59,8 @@ var CLIActions = actions.Actions({ // XXX is this correct??? path = path || this.location.path + path = util.normalizePath(path) + return this.loadImages(path) .then(function(){ // save base index... diff --git a/ui (gen4)/features/filesystem.js b/ui (gen4)/features/filesystem.js index 9eb7bc2f..14c8e7c2 100755 --- a/ui (gen4)/features/filesystem.js +++ b/ui (gen4)/features/filesystem.js @@ -124,6 +124,7 @@ var FileSystemLoaderActions = actions.Actions({ 'image-file-pattern': '*+(jpg|jpeg|png|JPG|JPEG|PNG)', 'image-file-read-stat': true, + 'image-file-skip-previews': true, // XXX if true and multiple indexes found, load only the first // without merging... @@ -161,7 +162,6 @@ var FileSystemLoaderActions = actions.Actions({ // .loadIndex(..) // XXX add a symmetric equivalent to .prepareIndexForWrite(..) so as // to enable features to load their data... - // XXX should this return a promise??? ...a clean promise??? // XXX look inside... loadIndex: ['- File/Load index', function(path, from_date, logger){ @@ -182,10 +182,6 @@ var FileSystemLoaderActions = actions.Actions({ // a-la glob).... //file.loadIndex(path, this.config['index-dir'], logger) return file.loadIndex(path, this.config['index-dir'], from_date, logger) - .catch(function(err){ - // XXX - console.error(err) - }) .then(function(res){ // XXX if res is empty load raw... @@ -284,56 +280,61 @@ var FileSystemLoaderActions = actions.Actions({ } }) }], + + // Load images... + // + // This will: + // - load images from path + // - load basic stat data + // - load previews from path if they exist... + // // XXX use the logger... // XXX add a recursive option... // ...might also be nice to add sub-dirs to ribbons... // XXX make image pattern more generic... - // XXX should this return a promise??? ...a clean promise??? loadImages: ['- File/Load images', function(path, logger){ if(path == null){ return } + // XXX get a logger... + logger = logger || this.logger + var that = this + path = util.normalizePath(path) // NOTE: we set this before we start the load so as to let // clients know what we are loading and not force them // to wait to find out... // XXX not sure if this is the way to go... + var location = this.__location = { path: path, method: 'loadImages', } + // get the image list... return new Promise(function(resolve, reject){ glob(path + '/'+ that.config['image-file-pattern'], {stat: !!that.config['image-file-read-stat']}) .on('error', function(err){ - console.log('!!!!', err) + console.error(err) reject(err) }) - /* - .on('match', function(img){ - // XXX stat stuff... - fse.statSync(img) - }) - */ .on('end', function(lst){ // XXX might be a good idea to make image paths relative to path... //lst = lst.map(function(p){ return pathlib.relative(base, p) }) - - that.loadURLs(lst, path) // XXX do we need to normalize paths after we get them from glob?? - //that.loadURLs(lst.map(pathlib.posix.normalize), path) - //that.loadURLs(lst - // .map(function(p){ return util.normalizePath(p) }), path) + //lst = lst.map(function(p){ return util.normalizePath(p) }), path) + + var data = that.dataFromURLs(lst, path) if(!!that.config['image-file-read-stat']){ var stats = this.statCache var p = pathlib.posix - that.images.forEach(function(gid, img){ + data.images.forEach(function(gid, img){ var stat = stats[p.join(img.base_path, img.path)] img.atime = stat.atime @@ -347,17 +348,40 @@ var FileSystemLoaderActions = actions.Actions({ }) } - // NOTE: we set it again because .loadURLs() does a clear - // before it starts loading... - // XXX is this a bug??? - that.__location = { - path: path, - method: 'loadImages', - } - - resolve(that) + // pass on the result... + resolve(data) }) }) + // load previews if they exist... + .then(function(data){ + if(that.config['image-file-skip-previews']){ + return data + } + + var index_dir = that.config['index-dir'] + var index_path = path +'/'+ index_dir + + return file.loadPreviews(index_path, null, index_dir) + .then(function(previews){ + previews = previews[index_path] + previews && Object.keys(previews).forEach(function(gid){ + if(gid in data.images){ + data.images[gid].preview = previews[gid].preview + } + }) + + return data + }) + }) + // load the data... + .then(function(data){ + that.load(data) + + // NOTE: we set it again because .load() does a .clear() + // before it starts loading which clears the .location + // too... + that.__location = location + }) }], // XXX auto-detect format or let the user chose... @@ -371,7 +395,7 @@ var FileSystemLoaderActions = actions.Actions({ //this.location.method = 'loadImages' }], - // XXX should this return a promise??? ...a clean promise??? + // XXX should this also try and load previews... // XXX revise logger... loadNewImages: ['File/Load new images', function(path, logger){ @@ -383,6 +407,8 @@ var FileSystemLoaderActions = actions.Actions({ } var that = this + path = util.normalizePath(path) + // cache the loaded images... var loaded = this.images.map(function(gid, img){ return img.path }) @@ -537,7 +563,9 @@ var FileSystemLoaderUIActions = actions.Actions({ browsePath: ['File/Browse file system...', widgets.makeUIDialog(function(base, callback){ var that = this + base = base || this.location.path || '/' + base = util.normalizePath(base) var o = browseWalk.makeWalk( null, base, this.config['image-file-pattern'], @@ -1027,7 +1055,7 @@ var pushToHistory = function(action, to_top, checker){ path = util.normalizePath(path) if(path){ this.pushURLToHistory( - util.normalizePath(path), + path, action, checker || 'checkPath') } @@ -1308,6 +1336,7 @@ var FileSystemWriterActions = actions.Actions({ path = path || this.location.loaded path = path && path.length == 1 ? path[0] : path + path = util.normalizePath(path) // XXX if(path instanceof Array){ @@ -1377,6 +1406,7 @@ var FileSystemWriterActions = actions.Actions({ // XXX is this correct??? path = path || './exported' + path = util.normalizePath(path) // XXX resolve env variables in path... // XXX @@ -1491,6 +1521,8 @@ var FileSystemWriterActions = actions.Actions({ var that = this var base_dir = this.location.path + path = util.normalizePath(path) + // XXX resolve env variables in path... // XXX diff --git a/ui (gen4)/file.js b/ui (gen4)/file.js index 36ca8925..a8fda06e 100755 --- a/ui (gen4)/file.js +++ b/ui (gen4)/file.js @@ -25,6 +25,7 @@ if(typeof(process) != 'undefined'){ var data = require('data') var images = require('images') +var util = require('lib/util') var tasks = require('lib/tasks') @@ -99,7 +100,7 @@ module.gGlob = function(){ var listIndexes = module.listIndexes = function(base, index_dir){ - return gGlob(base +'/**/'+ index_dir || INDEX_DIR) + return gGlob(base +'/**/'+ (index_dir || INDEX_DIR)) } @@ -136,6 +137,7 @@ var listJSON = module.listJSON = function(path, pattern){ pattern = pattern || '*' + path = util.normalizePath(path) return gGlob(path +'/'+ pattern +'.json') } @@ -168,6 +170,7 @@ var ensureDir = denodeify(fse.ensureDir) // XXX handle errors... function loadJSON(path){ + path = util.normalizePath(path) return loadFile(path).then(JSON.parse) } @@ -326,6 +329,7 @@ function(list, from_date, logger){ var loadSaveHistoryList = module.loadSaveHistoryList = function(path, index_dir, logger){ + path = util.normalizePath(path) index_dir = index_dir || INDEX_DIR return new Promise(function(resolve, reject){ @@ -461,6 +465,7 @@ function(path, index_dir, logger){ var loadIndex = module.loadIndex = function(path, index_dir, from_date, logger){ + path = util.normalizePath(path) if(index_dir && index_dir.emit != null){ logger = index_dir index_dir = from_date = null @@ -622,48 +627,66 @@ module.loadPreviews = function(base, previews, index_dir, absolute_path){ previews = previews || {} index_dir = index_dir || INDEX_DIR + base = util.normalizePath(base) - return new Promise(function(resolve, reject){ - listIndexes(base) - // XXX handle errors.... - //.on('error', function(err){ - //}) - .on('match', function(base){ - if(!(base in previews)){ - previews[base] = {} - } + // we got an explicit index.... + if(pathlib.basename(base) == index_dir){ + return new Promise(function(resolve, reject){ + if(!(base in previews)){ + previews[base] = {} + } - var images = previews[base] + var images = previews[base] - listPreviews(base) - // XXX handle errors.... - //.on('error', function(err){ - //}) - // preview name syntax: - // px/ - .jpg - .on('match', function(path){ - // get the data we need... - var gid = pathlib.basename(path).split(' - ')[0] - var res = pathlib.basename(pathlib.dirname(path)) + listPreviews(base) + // XXX handle errors.... + //.on('error', function(err){ + //}) + // preview name syntax: + // px/ - .jpg + .on('match', function(path){ + // get the data we need... + var gid = pathlib.basename(path).split(' - ')[0] + var res = pathlib.basename(pathlib.dirname(path)) - // build the structure if it does not exist... - if(!(gid in images)){ - images[gid] = {} - } - if(images[gid].preview == null){ - images[gid].preview = {} - } + // build the structure if it does not exist... + if(!(gid in images)){ + images[gid] = {} + } + if(images[gid].preview == null){ + images[gid].preview = {} + } - // add a preview... - // NOTE: this will overwrite a previews if they are found in - // several locations... - images[gid].preview[res] = index_dir +'/'+ path.split(index_dir)[1] - }) - }) - .on('end', function(){ - resolve(previews) - }) - }) + // add a preview... + // NOTE: this will overwrite a previews if they are found in + // several locations... + images[gid].preview[res] = + util.normalizePath(index_dir +'/'+ path.split(index_dir)[1]) + }) + .on('end', function(){ + resolve(previews) + }) + }) + + // find all sub indexes... + } else { + return new Promise(function(resolve, reject){ + var queue = [] + listIndexes(base, index_dir) + // XXX handle errors.... + //.on('error', function(err){ + //}) + .on('match', function(base){ + queue.push(loadPreviews(base, previews, index_dir, absolute_path)) + }) + .on('end', function(){ + Promise.all(queue) + .then(function(){ + resolve(previews) + }) + }) + }) + } } @@ -885,6 +908,7 @@ var FILENAME = '${DATE}-${KEYWORD}.${EXT}' var writeIndex = module.writeIndex = function(json, path, date, filename_tpl, logger){ + path = util.normalizePath(path) filename_tpl = filename_tpl || FILENAME // XXX for some reason this gets the unpatched node.js Date, so we // get the patched date explicitly... diff --git a/ui (gen4)/lib/util.js b/ui (gen4)/lib/util.js index 19c4a284..85b456df 100755 --- a/ui (gen4)/lib/util.js +++ b/ui (gen4)/lib/util.js @@ -188,7 +188,7 @@ module.normalizePath = function(path){ return typeof(path) == typeof('str') ? path // normalize the slashes... - .replace(/(\/)/g, '/') + .replace(/\\/g, '/') // remove duplicate '/' .replace(/(\/)\1+/g, '/') // remove trailing '/'