mirror of
https://github.com/flynx/ImageGrid.git
synced 2025-10-29 10:20:08 +00:00
refactoring: preparing to make .loadImages(..) and .loadNewImages(..) reuse most of their code...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
96b8ebab04
commit
00b3cf8e60
@ -170,6 +170,8 @@ var FileSystemLoaderActions = actions.Actions({
|
|||||||
if(path == null){
|
if(path == null){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
path = util.normalizePath(path)
|
||||||
|
|
||||||
if(from_date && from_date.emit != null){
|
if(from_date && from_date.emit != null){
|
||||||
logger = from_date
|
logger = from_date
|
||||||
from_date = null
|
from_date = null
|
||||||
@ -281,6 +283,84 @@ var FileSystemLoaderActions = actions.Actions({
|
|||||||
})
|
})
|
||||||
}],
|
}],
|
||||||
|
|
||||||
|
getImagesInPath: ['- File/',
|
||||||
|
function(path, read_stat, skip_preview_search, logger){
|
||||||
|
if(path == null){
|
||||||
|
return
|
||||||
|
}
|
||||||
|
read_stat = read_stat == null ?
|
||||||
|
this.config['image-file-read-stat']
|
||||||
|
: read_stat
|
||||||
|
skip_preview_search = skip_preview_search == null ?
|
||||||
|
this.config['image-file-skip-previews']
|
||||||
|
: skip_preview_search
|
||||||
|
|
||||||
|
// XXX get a logger...
|
||||||
|
logger = logger || this.logger
|
||||||
|
|
||||||
|
var that = this
|
||||||
|
path = util.normalizePath(path)
|
||||||
|
|
||||||
|
// get the image list...
|
||||||
|
return new Promise(function(resolve, reject){
|
||||||
|
glob(path + '/'+ that.config['image-file-pattern'], {stat: !!read_stat})
|
||||||
|
.on('error', function(err){
|
||||||
|
console.error(err)
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
.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) })
|
||||||
|
// XXX do we need to normalize paths after we get them from glob??
|
||||||
|
//lst = lst.map(function(p){ return util.normalizePath(p) }), path)
|
||||||
|
|
||||||
|
var imgs = images.Images.fromArray(lst, path)
|
||||||
|
|
||||||
|
if(!!read_stat){
|
||||||
|
var stats = this.statCache
|
||||||
|
var p = pathlib.posix
|
||||||
|
|
||||||
|
imgs.forEach(function(gid, img){
|
||||||
|
var stat = stats[p.join(img.base_path, img.path)]
|
||||||
|
|
||||||
|
img.atime = stat.atime
|
||||||
|
img.mtime = stat.mtime
|
||||||
|
img.ctime = stat.ctime
|
||||||
|
img.birthtime = stat.birthtime
|
||||||
|
|
||||||
|
img.size = stat.size
|
||||||
|
|
||||||
|
// XXX do we need anything else???
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// pass on the result...
|
||||||
|
resolve(imgs)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
// load previews if they exist...
|
||||||
|
.then(function(imgs){
|
||||||
|
if(skip_preview_search){
|
||||||
|
return imgs
|
||||||
|
}
|
||||||
|
|
||||||
|
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 imgs){
|
||||||
|
imgs[gid].preview = previews[gid].preview
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return imgs
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}],
|
||||||
|
|
||||||
// Load images...
|
// Load images...
|
||||||
//
|
//
|
||||||
// This will:
|
// This will:
|
||||||
@ -291,6 +371,7 @@ var FileSystemLoaderActions = actions.Actions({
|
|||||||
// 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 add option to preserve/update .data
|
||||||
// XXX make image pattern more generic...
|
// XXX make image pattern more generic...
|
||||||
loadImages: ['- File/Load images',
|
loadImages: ['- File/Load images',
|
||||||
function(path, logger){
|
function(path, logger){
|
||||||
@ -315,73 +396,23 @@ var FileSystemLoaderActions = actions.Actions({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get the image list...
|
// get the image list...
|
||||||
return new Promise(function(resolve, reject){
|
return this.getImagesInPath(
|
||||||
glob(path + '/'+ that.config['image-file-pattern'],
|
path,
|
||||||
{stat: !!that.config['image-file-read-stat']})
|
that.config['image-file-read-stat'],
|
||||||
.on('error', function(err){
|
that.config['image-file-skip-previews'],
|
||||||
console.error(err)
|
logger)
|
||||||
reject(err)
|
// load the data...
|
||||||
|
.then(function(imgs){
|
||||||
|
that.load({
|
||||||
|
images: imgs,
|
||||||
|
data: data.Data.fromArray(imgs.keys()),
|
||||||
})
|
})
|
||||||
.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) })
|
|
||||||
// XXX do we need to normalize paths after we get them from glob??
|
|
||||||
//lst = lst.map(function(p){ return util.normalizePath(p) }), path)
|
|
||||||
|
|
||||||
var data = that.dataFromURLs(lst, path)
|
// NOTE: we set it again because .load() does a .clear()
|
||||||
|
// before it starts loading which clears the .location
|
||||||
if(!!that.config['image-file-read-stat']){
|
// too...
|
||||||
var stats = this.statCache
|
that.__location = location
|
||||||
var p = pathlib.posix
|
})
|
||||||
|
|
||||||
data.images.forEach(function(gid, img){
|
|
||||||
var stat = stats[p.join(img.base_path, img.path)]
|
|
||||||
|
|
||||||
img.atime = stat.atime
|
|
||||||
img.mtime = stat.mtime
|
|
||||||
img.ctime = stat.ctime
|
|
||||||
img.birthtime = stat.birthtime
|
|
||||||
|
|
||||||
img.size = stat.size
|
|
||||||
|
|
||||||
// XXX do we need anything else???
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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...
|
// XXX auto-detect format or let the user chose...
|
||||||
|
|||||||
@ -366,7 +366,8 @@ function(path, index_dir, logger){
|
|||||||
.then(function(paths){
|
.then(function(paths){
|
||||||
// start loading...
|
// start loading...
|
||||||
Promise.all(paths.map(function(p){
|
Promise.all(paths.map(function(p){
|
||||||
var path = pathlib.normalize(p +'/'+ index_dir)
|
//var path = pathlib.normalize(p +'/'+ index_dir)
|
||||||
|
var path = util.normalizePath(p +'/'+ index_dir)
|
||||||
return loadSaveHistoryList(path, index_dir)
|
return loadSaveHistoryList(path, index_dir)
|
||||||
.then(function(obj){
|
.then(function(obj){
|
||||||
// NOTE: considering that all the paths within
|
// NOTE: considering that all the paths within
|
||||||
@ -466,6 +467,7 @@ var loadIndex =
|
|||||||
module.loadIndex =
|
module.loadIndex =
|
||||||
function(path, index_dir, from_date, logger){
|
function(path, index_dir, from_date, logger){
|
||||||
path = util.normalizePath(path)
|
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
|
||||||
@ -589,7 +591,8 @@ function(path, index_dir, from_date, logger){
|
|||||||
.then(function(paths){
|
.then(function(paths){
|
||||||
// start loading...
|
// start loading...
|
||||||
Promise.all(paths.map(function(p){
|
Promise.all(paths.map(function(p){
|
||||||
var path = pathlib.normalize(p +'/'+ index_dir)
|
//var path = pathlib.normalize(p +'/'+ index_dir)
|
||||||
|
var path = util.normalizePath(p +'/'+ index_dir)
|
||||||
return loadIndex(path, index_dir, from_date, logger)
|
return loadIndex(path, index_dir, from_date, logger)
|
||||||
.then(function(obj){
|
.then(function(obj){
|
||||||
// NOTE: considering that all the paths within
|
// NOTE: considering that all the paths within
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user