moved the preview creation code to a separate module...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-05-25 22:09:03 +03:00
parent 6878422e4a
commit e40694b76b
4 changed files with 161 additions and 60 deletions

View File

@ -10,6 +10,7 @@ define(function(require){ var module = {}
var actions = require('lib/actions')
var features = require('lib/features')
var preview = require('lib/preview')
var core = require('features/core')
@ -17,7 +18,7 @@ try{
var sharp = requirejs('sharp')
} catch(err){
sharp = null
var sharp = null
}
if(typeof(process) != 'undefined'){
@ -41,7 +42,7 @@ if(typeof(process) != 'undefined'){
var SharpActions = actions.Actions({
config: {
'preview-path': '$INDEX/$RESOLUTIONpx',
'preview-path-template': '${INDEX}/${RESOLUTION}px/${GID} - ${NAME}.jpg',
'preview-normalized': true,
@ -87,9 +88,12 @@ var SharpActions = actions.Actions({
// XXX should this account for non-jpeg images???
// XXX do this in the background...
makePreviews: ['Sharp/Make image previews',
function(images, sizes, logger){
function(images, sizes, base_path, logger){
var that = this
logger = logger || this.logger
// get/normalize images...
images = images || this.current
// keywords...
images = images == 'all' ? this.data.getImages('all')
@ -97,7 +101,26 @@ var SharpActions = actions.Actions({
: images
images = images instanceof Array ? images : [images]
var cfg_sizes = this.config['preview-sizes'] || []
// NOTE: if base_path is not provided this will base the
// previews in .base_path for each image, usually this
// is where the index resides but might not be the
// case for compound indexes...
var data = {}
images.forEach(function(gid){
var img = that.images[gid]
var base = base_path || img.base_path || that.location.path
var d = data[base] = data[base] || []
d.push({
source: that.getImagePath(gid),
gid: gid,
})
})
// get/normalize sizes....
var cfg_sizes = this.config['preview-sizes'].slice() || []
cfg_sizes
.sort()
.reverse()
@ -116,64 +139,30 @@ var SharpActions = actions.Actions({
sizes = cfg_sizes
}
var that = this
return Promise.all(images.map(function(gid){
var data = that.images[gid]
var path = that.getImagePath(gid)
var path_tpl = that.config['preview-path-template']
.replace(/\$INDEX|\$\{INDEX\}/g, that.config['index-dir'] || '.ImageGrid')
var preview = data.preview = data.preview || {}
var img = sharp(path)
return img.metadata().then(function(metadata){
var orig_res = Math.max(metadata.width, metadata.height)
// now do the work...
return Promise.all(Object.keys(data).map(function(base_path){
return preview.makePreviews(
data[base_path],
sizes,
base_path,
path_tpl,
function(err, data){
if(data.status == 'done' || data.status == 'skipped'){
// get/make preview list...
var preview = that.images[data.gid].preview =
that.images[data.gid].preview || {}
return Promise.all(sizes.map(function(res){
// skip if image is smaller than res...
if(res >= orig_res){
return
}
preview[data.res + 'px'] = data.path
var ext = data.ext || ''
that.markChanged(data.gid)
}
// build the target path...
var target = (that.config['preview-path'] || '$INDEX')
.replace(/\$INDEX|\$\{INDEX\}/g, that.config['index-dir'])
.replace(/\$RESOLUTION|\$\{RESOLUTION\}/g, res)
// XXX do we need to account for non-jpeg extensions???
var target = pathlib.join(target, gid +' - '+ data.name + ext)
var base = data.base_path || that.location.path
var path = pathlib.join(base, target)
logger && logger.emit('queued', target)
return ensureDir(pathlib.dirname(path))
.then(function(){
// check if image exists...
if(fse.existsSync(path)){
preview[res + 'px'] = target
that.markChanged(gid)
logger && logger.emit('skipped', target)
return
}
return img.clone()
.resize(res, res)
.max()
.interpolateWith('nohalo')
.withMetadata()
.toFile(path)
.then(function(){
preview[res + 'px'] = target
that.markChanged(gid)
logger && logger.emit('done', target)
})
})
}))
})
logger && logger.emit(data.status, data.path)
})
}))
}],
})

View File

@ -2235,7 +2235,9 @@ module.FailsafeDevTools = core.ImageGridFeatures.Feature({
handlers: [
['start',
function(){
window.__devtools_failsafe && clearTimeout(window.__devtools_failsafe) }],
// NOTE: this is set in index.html
window.__devtools_failsafe
&& clearTimeout(window.__devtools_failsafe) }],
],
})

View File

@ -236,7 +236,7 @@ body {
if(window.require && window.nw){
window.__devtools_failsafe = setTimeout(function(){
nw.Window.get().showDevTools()
}, 1000)
}, 5000)
}
</script>

View File

@ -6,11 +6,121 @@
define(function(require){ var module = {}
//var DEBUG = DEBUG != null ? DEBUG : true
try{
var sharp = requirejs('sharp')
} catch(err){
var sharp = null
}
if(typeof(process) != 'undefined'){
var fse = requirejs('fs-extra')
var pathlib = requirejs('path')
var file = requirejs('./file')
}
/*********************************************************************/
if(typeof(process) != 'undefined'){
var ensureDir = file.denodeify(fse.ensureDir)
}
/*********************************************************************/
// images format:
// [
// {
// source: <source>,
// gid: <gid>,
// },
// ...
// ]
//
var makePreviews =
module.makePreviews =
function(images, sizes, base_path, target_tpl, callback){
var that = this
var target_path = (target_tpl
|| 'preview/${RESOLUTION}px/${NAME}.jpg')
// iterate images...
return Promise.all(images.map(function(data){
var gid = data.gid || ''
var source = data.source
var ext = pathlib.extname(source)
var name = pathlib.basename(source)
.replace(RegExp(ext + '$'), '')
var target = target_path
.replace(/\$NAME|\$\{NAME\}/g, name)
.replace(/\$GID|\$\{GID\}/g, gid)
var img = sharp(source)
// get metadata....
return img.metadata().then(function(metadata){
var orig_res = Math.max(metadata.width, metadata.height)
// process previews...
return Promise.all(sizes.map(function(res){
// skip if image is smaller than res...
if(res >= orig_res){
return
}
var rel = target
.replace(/\$RESOLUTION|\$\{RESOLUTION\}/g, res)
var full = pathlib.join(base_path || '', rel)
callback && callback(null, {
status: 'queued',
gid: gid,
res: res,
path: rel
})
// make the dir...
return ensureDir(pathlib.dirname(full))
.then(function(){
// check if image exists...
if(fse.existsSync(full)){
callback && callback(null, {
status: 'skipped',
gid: gid,
res: res,
path: rel
})
return
}
// make the actual previews...
return img.clone()
.resize(res, res)
.max()
// XXX this causes odd image errors
// ...white pixels in random black areas...
//.interpolateWith('nohalo')
.withMetadata()
.toFile(full)
.then(function(){
callback && callback(null, {
status: 'done',
gid: gid,
res: res,
path: rel
})
})
})
}))
})
}))
}