working on preview erporting options...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-04-07 17:50:22 +03:00
parent e86a471e63
commit 0e026c2f86
2 changed files with 132 additions and 90 deletions

View File

@ -117,6 +117,8 @@ var SharpActions = actions.Actions({
// XXX should this account for non-jpeg images??? // XXX should this account for non-jpeg images???
// XXX BUG?: this breaks on PNG images...
// XXX log: count gids and not specific images...
makePreviews: ['Sharp|File/Make image $previews', makePreviews: ['Sharp|File/Make image $previews',
core.doc`Make image previews core.doc`Make image previews
@ -140,6 +142,10 @@ var SharpActions = actions.Actions({
logger = logger || this.logger logger = logger || this.logger
logger = logger && logger.push('Previews') logger = logger && logger.push('Previews')
// XXX
var logger_mode = 'report-gids'
//var logger_mode = 'report-previews'
// get/normalize images... // get/normalize images...
//images = images || this.current //images = images || this.current
@ -150,6 +156,19 @@ var SharpActions = actions.Actions({
: images : images
images = images instanceof Array ? images : [images] images = images instanceof Array ? images : [images]
//
// Format:
// {
// <base_path>: [
// {
// source: <image-path>,
// gid: <gid>,
// },
// ...
// ],
// ...
// }
//
// NOTE: if base_path is not provided this will base the // NOTE: if base_path is not provided this will base the
// previews in .base_path for each image, usually this // previews in .base_path for each image, usually this
// is where the index resides but might not be the // is where the index resides but might not be the
@ -160,12 +179,10 @@ var SharpActions = actions.Actions({
var base = base_path || img.base_path || that.location.path var base = base_path || img.base_path || that.location.path
var d = data[base] = data[base] || [] var d = data[base] = data[base] || []
d.push({ d.push({
source: that.getImagePath(gid), source: that.getImagePath(gid),
gid: gid, gid: gid,
}) }) })
})
// get/normalize sizes.... // get/normalize sizes....
@ -191,8 +208,11 @@ var SharpActions = actions.Actions({
var path_tpl = that.config['preview-path-template'] var path_tpl = that.config['preview-path-template']
.replace(/\$INDEX|\$\{INDEX\}/g, that.config['index-dir'] || '.ImageGrid') .replace(/\$INDEX|\$\{INDEX\}/g, that.config['index-dir'] || '.ImageGrid')
var post_handler = function(err, data){ var post_handler = function(err, data){
if(data.status == 'done' || data.status == 'skipped'){ if(data.res != 'all'
&& (data.status == 'done'
|| data.status == 'skipped')){
// get/make preview list... // get/make preview list...
var img = that.images[data.gid] var img = that.images[data.gid]
var preview = img.preview = var preview = img.preview =
@ -210,48 +230,56 @@ var SharpActions = actions.Actions({
that.markChanged('images', [data.gid]) that.markChanged('images', [data.gid])
} }
logger && logger.emit(data.status, data.path) // NOTE: this will handle both 'queue' and 'resolved' statuses...
} logger &&
( logger_mode == 'report-gids' ?
// report gid-level progress...
(data.res == 'all'
&& logger.emit(data.status, data.path))
// report preview-level progress...
: (data.res != 'all'
&& logger.emit(data.status, data.path)) ) }
// now do the work (async)... // now do the work (async)...
if(this.previewConstructorWorker){ if(this.previewConstructorWorker){
return Promise.all(Object.keys(data).map(function(base_path){ return Promise.all(
return new Promise(function(resolve, reject){ Object.keys(data)
var ticket = Date.now() .map(function(base_path){
while(ticket in that.previewConstructorWorker.__post_handlers){ return new Promise(function(resolve, reject){
ticket = Date.now() do {
} var ticket = Date.now()
} while(ticket in that.previewConstructorWorker.__post_handlers)
that.previewConstructorWorker.send({ that.previewConstructorWorker.send({
ticket: ticket, ticket: ticket,
images: data[base_path],
images: data[base_path], sizes: sizes,
sizes: sizes, base_path: base_path,
base_path: base_path, target_tpl: path_tpl,
target_tpl: path_tpl, })
}) that.previewConstructorWorker.__post_handlers[ticket] =
that.previewConstructorWorker.__post_handlers[ticket] = function(err, data){ function(err, data){
// XXX if(err){
if(err){ return reject(err)
reject(err) }
} data == 'completed' ?
if(data == 'completed'){ resolve()
resolve() : post_handler(err, data) }
}) }))
} else {
post_handler(err, data)
}
}
})
}))
// now do the work (sync)... // now do the work (sync)...
} else { } else {
return Promise.all(Object.keys(data).map(function(base_path){ return Promise.all(
return preview.makePreviews( Object.keys(data)
data[base_path], sizes, base_path, path_tpl, post_handler) // NOTE: this will handle images batched by .base_path...
})) .map(function(base_path){
} return preview.makePreviews(
data[base_path],
sizes,
base_path,
path_tpl,
post_handler) }))}
}], }],
}) })

View File

@ -41,6 +41,7 @@ if(typeof(process) != 'undefined'){
// ... // ...
// ] // ]
// //
// XXX add a callback call when a gid is done...
var makePreviews = var makePreviews =
module.makePreviews = module.makePreviews =
function(images, sizes, base_path, target_tpl, callback){ function(images, sizes, base_path, target_tpl, callback){
@ -54,6 +55,12 @@ function(images, sizes, base_path, target_tpl, callback){
var gid = data.gid || '' var gid = data.gid || ''
var source = data.source var source = data.source
callback && callback(null, {
status: 'queued',
gid: gid,
res: 'all',
})
var ext = pathlib.extname(source) var ext = pathlib.extname(source)
var name = pathlib.basename(source) var name = pathlib.basename(source)
.replace(RegExp(ext + '$'), '') .replace(RegExp(ext + '$'), '')
@ -68,59 +75,66 @@ function(images, sizes, base_path, target_tpl, callback){
var orig_res = Math.max(metadata.width, metadata.height) var orig_res = Math.max(metadata.width, metadata.height)
// process previews... // process previews...
return Promise.all(sizes.map(function(res){ return Promise
// skip if image is smaller than res... .all(sizes
if(res >= orig_res){ .map(function(res){
return // 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,
orientation: metadata.orientation,
})
return
} }
// make the actual previews... var rel = target
return img.clone() .replace(/\$RESOLUTION|\$\{RESOLUTION\}/g, res)
.resize({ var full = pathlib.join(base_path || '', rel)
width: res,
height: res, callback && callback(null, {
fit: 'inside', 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,
orientation: metadata.orientation,
})
return }
// make the actual previews...
return img.clone()
.resize({
width: res,
height: res,
fit: 'inside',
})
.withMetadata()
.toFile(full)
.then(function(){
callback
&& callback(null, {
status: 'done',
gid: gid,
res: res,
path: rel,
orientation: metadata.orientation, }) })
}) })
.withMetadata() }))
.toFile(full) // report a gid is done...
.then(function(){ .then(function(){
callback callback
&& callback(null, { && callback(null, {
status: 'done', status: 'done',
gid: gid, gid: gid,
res: res, res: 'all',
path: rel, }) }) })
orientation: metadata.orientation, }) })
})
}))
})
})) }))
} }