now .loadImages(..) will also try and load previews + refactoring and cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-05-29 23:49:01 +03:00
parent 803cbb84a5
commit 96b8ebab04
5 changed files with 143 additions and 77 deletions

View File

@ -196,18 +196,20 @@ actions.Actions({
return res 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 be here???
// XXX should this use .load(..) // XXX should this use .load(..)
// ...note if we use this it breaks, need to rethink... // ...note if we use this it breaks, need to rethink...
loadURLs: ['- File/Load a URL list', loadURLs: ['- File/Load a URL list',
function(lst, base){ function(lst, base){ this.load(this.dataFromURLs(lst, base)) }],
var imgs = images.Images.fromArray(lst, base)
this.load({
images: imgs,
data: data.Data.fromArray(imgs.keys()),
})
}],
// XXX experimental... // XXX experimental...
// ...the bad thing about this is that we can not extend this, // ...the bad thing about this is that we can not extend this,

View File

@ -8,6 +8,7 @@ define(function(require){ var module = {}
//var DEBUG = DEBUG != null ? DEBUG : true //var DEBUG = DEBUG != null ? DEBUG : true
var util = require('lib/util')
var actions = require('lib/actions') var actions = require('lib/actions')
var features = require('lib/features') var features = require('lib/features')
@ -18,6 +19,11 @@ var core = require('features/core')
var base = require('features/base') var base = require('features/base')
if(typeof(process) != 'undefined'){
var pathlib = requirejs('path')
}
/*********************************************************************/ /*********************************************************************/
// XXX what we need here is: // XXX what we need here is:
@ -53,6 +59,8 @@ var CLIActions = actions.Actions({
// XXX is this correct??? // XXX is this correct???
path = path || this.location.path path = path || this.location.path
path = util.normalizePath(path)
return this.loadImages(path) return this.loadImages(path)
.then(function(){ .then(function(){
// save base index... // save base index...

View File

@ -124,6 +124,7 @@ var FileSystemLoaderActions = actions.Actions({
'image-file-pattern': '*+(jpg|jpeg|png|JPG|JPEG|PNG)', 'image-file-pattern': '*+(jpg|jpeg|png|JPG|JPEG|PNG)',
'image-file-read-stat': true, 'image-file-read-stat': true,
'image-file-skip-previews': true,
// XXX if true and multiple indexes found, load only the first // XXX if true and multiple indexes found, load only the first
// without merging... // without merging...
@ -161,7 +162,6 @@ var FileSystemLoaderActions = actions.Actions({
// .loadIndex(..) // .loadIndex(..)
// XXX add a symmetric equivalent to .prepareIndexForWrite(..) so as // XXX add a symmetric equivalent to .prepareIndexForWrite(..) so as
// to enable features to load their data... // to enable features to load their data...
// XXX should this return a promise??? ...a clean promise???
// XXX look inside... // XXX look inside...
loadIndex: ['- File/Load index', loadIndex: ['- File/Load index',
function(path, from_date, logger){ function(path, from_date, logger){
@ -182,10 +182,6 @@ var FileSystemLoaderActions = actions.Actions({
// a-la glob).... // a-la glob)....
//file.loadIndex(path, this.config['index-dir'], logger) //file.loadIndex(path, this.config['index-dir'], logger)
return file.loadIndex(path, this.config['index-dir'], from_date, logger) return file.loadIndex(path, this.config['index-dir'], from_date, logger)
.catch(function(err){
// XXX
console.error(err)
})
.then(function(res){ .then(function(res){
// XXX if res is empty load raw... // 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 use the logger...
// XXX add a recursive option... // XXX add a recursive option...
// ...might also be nice to add sub-dirs to ribbons... // ...might also be nice to add sub-dirs to ribbons...
// XXX make image pattern more generic... // XXX make image pattern more generic...
// XXX should this return a promise??? ...a clean promise???
loadImages: ['- File/Load images', loadImages: ['- File/Load images',
function(path, logger){ function(path, logger){
if(path == null){ if(path == null){
return return
} }
// XXX get a logger...
logger = logger || this.logger
var that = this var that = this
path = util.normalizePath(path)
// NOTE: we set this before we start the load so as to let // NOTE: we set this before we start the load so as to let
// clients know what we are loading and not force them // clients know what we are loading and not force them
// to wait to find out... // to wait to find out...
// XXX not sure if this is the way to go... // XXX not sure if this is the way to go...
var location =
this.__location = { this.__location = {
path: path, path: path,
method: 'loadImages', method: 'loadImages',
} }
// get the image list...
return new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
glob(path + '/'+ that.config['image-file-pattern'], glob(path + '/'+ that.config['image-file-pattern'],
{stat: !!that.config['image-file-read-stat']}) {stat: !!that.config['image-file-read-stat']})
.on('error', function(err){ .on('error', function(err){
console.log('!!!!', err) console.error(err)
reject(err) reject(err)
}) })
/*
.on('match', function(img){
// XXX stat stuff...
fse.statSync(img)
})
*/
.on('end', function(lst){ .on('end', function(lst){
// XXX might be a good idea to make image paths relative to path... // XXX might be a good idea to make image paths relative to path...
//lst = lst.map(function(p){ return pathlib.relative(base, p) }) //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?? // XXX do we need to normalize paths after we get them from glob??
//that.loadURLs(lst.map(pathlib.posix.normalize), path) //lst = lst.map(function(p){ return util.normalizePath(p) }), path)
//that.loadURLs(lst
// .map(function(p){ return util.normalizePath(p) }), path) var data = that.dataFromURLs(lst, path)
if(!!that.config['image-file-read-stat']){ if(!!that.config['image-file-read-stat']){
var stats = this.statCache var stats = this.statCache
var p = pathlib.posix 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)] var stat = stats[p.join(img.base_path, img.path)]
img.atime = stat.atime img.atime = stat.atime
@ -347,17 +348,40 @@ var FileSystemLoaderActions = actions.Actions({
}) })
} }
// NOTE: we set it again because .loadURLs() does a clear // pass on the result...
// before it starts loading... resolve(data)
// XXX is this a bug???
that.__location = {
path: path,
method: 'loadImages',
}
resolve(that)
}) })
}) })
// 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... // XXX auto-detect format or let the user chose...
@ -371,7 +395,7 @@ var FileSystemLoaderActions = actions.Actions({
//this.location.method = 'loadImages' //this.location.method = 'loadImages'
}], }],
// XXX should this return a promise??? ...a clean promise??? // XXX should this also try and load previews...
// XXX revise logger... // XXX revise logger...
loadNewImages: ['File/Load new images', loadNewImages: ['File/Load new images',
function(path, logger){ function(path, logger){
@ -383,6 +407,8 @@ var FileSystemLoaderActions = actions.Actions({
} }
var that = this var that = this
path = util.normalizePath(path)
// cache the loaded images... // cache the loaded images...
var loaded = this.images.map(function(gid, img){ return img.path }) var loaded = this.images.map(function(gid, img){ return img.path })
@ -537,7 +563,9 @@ var FileSystemLoaderUIActions = actions.Actions({
browsePath: ['File/Browse file system...', browsePath: ['File/Browse file system...',
widgets.makeUIDialog(function(base, callback){ widgets.makeUIDialog(function(base, callback){
var that = this var that = this
base = base || this.location.path || '/' base = base || this.location.path || '/'
base = util.normalizePath(base)
var o = browseWalk.makeWalk( var o = browseWalk.makeWalk(
null, base, this.config['image-file-pattern'], null, base, this.config['image-file-pattern'],
@ -1027,7 +1055,7 @@ var pushToHistory = function(action, to_top, checker){
path = util.normalizePath(path) path = util.normalizePath(path)
if(path){ if(path){
this.pushURLToHistory( this.pushURLToHistory(
util.normalizePath(path), path,
action, action,
checker || 'checkPath') checker || 'checkPath')
} }
@ -1308,6 +1336,7 @@ var FileSystemWriterActions = actions.Actions({
path = path || this.location.loaded path = path || this.location.loaded
path = path && path.length == 1 ? path[0] : path path = path && path.length == 1 ? path[0] : path
path = util.normalizePath(path)
// XXX // XXX
if(path instanceof Array){ if(path instanceof Array){
@ -1377,6 +1406,7 @@ var FileSystemWriterActions = actions.Actions({
// XXX is this correct??? // XXX is this correct???
path = path || './exported' path = path || './exported'
path = util.normalizePath(path)
// XXX resolve env variables in path... // XXX resolve env variables in path...
// XXX // XXX
@ -1491,6 +1521,8 @@ var FileSystemWriterActions = actions.Actions({
var that = this var that = this
var base_dir = this.location.path var base_dir = this.location.path
path = util.normalizePath(path)
// XXX resolve env variables in path... // XXX resolve env variables in path...
// XXX // XXX

View File

@ -25,6 +25,7 @@ if(typeof(process) != 'undefined'){
var data = require('data') var data = require('data')
var images = require('images') var images = require('images')
var util = require('lib/util')
var tasks = require('lib/tasks') var tasks = require('lib/tasks')
@ -99,7 +100,7 @@ module.gGlob = function(){
var listIndexes = var listIndexes =
module.listIndexes = module.listIndexes =
function(base, index_dir){ 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 = module.listJSON =
function(path, pattern){ function(path, pattern){
pattern = pattern || '*' pattern = pattern || '*'
path = util.normalizePath(path)
return gGlob(path +'/'+ pattern +'.json') return gGlob(path +'/'+ pattern +'.json')
} }
@ -168,6 +170,7 @@ var ensureDir = denodeify(fse.ensureDir)
// XXX handle errors... // XXX handle errors...
function loadJSON(path){ function loadJSON(path){
path = util.normalizePath(path)
return loadFile(path).then(JSON.parse) return loadFile(path).then(JSON.parse)
} }
@ -326,6 +329,7 @@ function(list, from_date, logger){
var loadSaveHistoryList = var loadSaveHistoryList =
module.loadSaveHistoryList = module.loadSaveHistoryList =
function(path, index_dir, logger){ function(path, index_dir, logger){
path = util.normalizePath(path)
index_dir = index_dir || INDEX_DIR index_dir = index_dir || INDEX_DIR
return new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
@ -461,6 +465,7 @@ function(path, index_dir, logger){
var loadIndex = var loadIndex =
module.loadIndex = module.loadIndex =
function(path, index_dir, from_date, logger){ function(path, index_dir, from_date, logger){
path = util.normalizePath(path)
if(index_dir && index_dir.emit != null){ if(index_dir && index_dir.emit != null){
logger = index_dir logger = index_dir
index_dir = from_date = null index_dir = from_date = null
@ -622,48 +627,66 @@ module.loadPreviews =
function(base, previews, index_dir, absolute_path){ function(base, previews, index_dir, absolute_path){
previews = previews || {} previews = previews || {}
index_dir = index_dir || INDEX_DIR index_dir = index_dir || INDEX_DIR
base = util.normalizePath(base)
return new Promise(function(resolve, reject){ // we got an explicit index....
listIndexes(base) if(pathlib.basename(base) == index_dir){
// XXX handle errors.... return new Promise(function(resolve, reject){
//.on('error', function(err){ if(!(base in previews)){
//}) previews[base] = {}
.on('match', function(base){ }
if(!(base in previews)){
previews[base] = {}
}
var images = previews[base] var images = previews[base]
listPreviews(base) listPreviews(base)
// XXX handle errors.... // XXX handle errors....
//.on('error', function(err){ //.on('error', function(err){
//}) //})
// preview name syntax: // preview name syntax:
// <res>px/<gid> - <orig-filename>.jpg // <res>px/<gid> - <orig-filename>.jpg
.on('match', function(path){ .on('match', function(path){
// get the data we need... // get the data we need...
var gid = pathlib.basename(path).split(' - ')[0] var gid = pathlib.basename(path).split(' - ')[0]
var res = pathlib.basename(pathlib.dirname(path)) var res = pathlib.basename(pathlib.dirname(path))
// build the structure if it does not exist... // build the structure if it does not exist...
if(!(gid in images)){ if(!(gid in images)){
images[gid] = {} images[gid] = {}
} }
if(images[gid].preview == null){ if(images[gid].preview == null){
images[gid].preview = {} images[gid].preview = {}
} }
// add a preview... // add a preview...
// NOTE: this will overwrite a previews if they are found in // NOTE: this will overwrite a previews if they are found in
// several locations... // several locations...
images[gid].preview[res] = index_dir +'/'+ path.split(index_dir)[1] images[gid].preview[res] =
}) util.normalizePath(index_dir +'/'+ path.split(index_dir)[1])
}) })
.on('end', function(){ .on('end', function(){
resolve(previews) 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 = var writeIndex =
module.writeIndex = module.writeIndex =
function(json, path, date, filename_tpl, logger){ function(json, path, date, filename_tpl, logger){
path = util.normalizePath(path)
filename_tpl = filename_tpl || FILENAME filename_tpl = filename_tpl || FILENAME
// XXX for some reason this gets the unpatched node.js Date, so we // XXX for some reason this gets the unpatched node.js Date, so we
// get the patched date explicitly... // get the patched date explicitly...

View File

@ -188,7 +188,7 @@ module.normalizePath =
function(path){ function(path){
return typeof(path) == typeof('str') ? path return typeof(path) == typeof('str') ? path
// normalize the slashes... // normalize the slashes...
.replace(/(\/)/g, '/') .replace(/\\/g, '/')
// remove duplicate '/' // remove duplicate '/'
.replace(/(\/)\1+/g, '/') .replace(/(\/)\1+/g, '/')
// remove trailing '/' // remove trailing '/'