migrated .makeResizedImage(..) to queue...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-12-06 04:39:42 +03:00
parent 10c966f387
commit 9f4b877e37
3 changed files with 154 additions and 198 deletions

View File

@ -2694,8 +2694,9 @@ function(title, func){
;(items instanceof Array ? ;(items instanceof Array ?
items items
: [items]) : [items])
.forEach(function(item){ .map(function(item){
func.call(that, item, ...args) }) return func.call(that, item, ...args) })
// XXX should we return anything in sync mode???
return Promise.resolve() return Promise.resolve()
// queue mode... // queue mode...
@ -2725,14 +2726,17 @@ function(title, func){
return [e, ...args] }) return [e, ...args] })
: [items, ...args])) : [items, ...args]))
// make a promise... // make a promise...
var res = new Promise(function(resolve, reject){ return new Promise(function(resolve, reject){
q.then(resolve, reject) }) q.then(resolve, reject) }) } }),
return res } }),
{ {
title, title,
toString: function(){ toString: function(){
// XXX add opts of given...
return `core.queueHandler('${action.name}',\n\t${ return `core.queueHandler('${action.name}',\n\t${
object.normalizeIndent( '\t'+ func.toString() ) })` }, (arg_handler ?
object.normalizeIndent( '\t'+ arg_handler.toString() ) + ',\n\t'
: '')
+ object.normalizeIndent( '\t'+ func.toString() ) })` },
}) } }) }
var sessionQueueHandler = var sessionQueueHandler =

View File

@ -185,18 +185,9 @@ var SharpActions = actions.Actions({
// from filesystem.IndexFormat... // from filesystem.IndexFormat...
}, },
// XXX need to distinguish if something was written in the promise chain... // XXX revise return values...
// ...return false???
// ......should the return value be a bit more informative???
// something like:
// {
// gid: ..
// path: ..
// status: ..
// ...
// }
// XXX make backup name pattern configurable... // XXX make backup name pattern configurable...
// XXX add crop support... // XXX CROP ready for crop support...
makeResizedImage: ['- Image/', makeResizedImage: ['- Image/',
core.doc`Make resized image(s)... core.doc`Make resized image(s)...
@ -213,15 +204,16 @@ var SharpActions = actions.Actions({
options format: options format:
{ {
// output image name... // output image name / name pattern...
// //
// Used if processing a single image, ignored otherwise. // NOTE: for multiple images this should be a pattern and not an
// explicit name...
// NOTE: if not given this defaults to: "%n"
name: null | <str>, name: null | <str>,
// image name pattern and data... // image name pattern data...
// //
// NOTE: for more info on pattern see: .formatImageName(..) // NOTE: for more info on pattern see: .formatImageName(..)
pattern: null | <str>,
data: null | { .. }, data: null | { .. },
// if true and image is smaller than size enlarge it... // if true and image is smaller than size enlarge it...
@ -251,50 +243,26 @@ var SharpActions = actions.Actions({
NOTE: all options are optional. NOTE: all options are optional.
NOTE: this will not overwrite existing images. NOTE: this will not overwrite existing images.
`, `,
core.taskAction('makeResizedImage', function(ticket, images, size, path, options={}){ core.queueHandler('Make resized image',
var that = this // get/normalize images...
function(queue, images, size, path, options){
// sanity check... // sanity check...
if(arguments.length < 4){ if(arguments.length < 4){
ticket.reject()
throw new Error('.makeResizedImage(..): ' throw new Error('.makeResizedImage(..): '
+'need at least images, size and path.') } +'need at least: images, size and path.') }
return [
// setup runtime interactions... (!images || images == 'all') ?
//
// NOTE: we will resolve the ticket when we are fully done
// and not on stop...
var STOP = false
ticket
.onmessage('stop', function(){
STOP = true })
.then(function(){
// close progress bar...
// NOTE: if we have multiple tasks let the last one
// close the progress bar...
if(that.tasks.titled(ticket.title).length == 0){
logger
&& logger.emit('close') }
// cleanup...
delete that.__cache_metadata_reading })
var abort = function(){
that.tasks.stop(ticket.title) }
var CHUNK_SIZE = 4
// get/normalize images...
//images = images || this.current
images = images
|| 'all'
// keywords...
images = images == 'all' ?
this.data.getImages('all') this.data.getImages('all')
: images == 'current' ? : images == 'current' ?
this.current [this.current]
: images : images instanceof Array ?
images = images instanceof Array ?
images images
: [images] : [images],
...[...arguments].slice(2),
]},
function(image, size, path, options={}){
var that = this
// sizing... // sizing...
var fit = var fit =
typeof(size) == typeof('str') ? typeof(size) == typeof('str') ?
@ -309,7 +277,6 @@ var SharpActions = actions.Actions({
var { var {
// naming... // naming...
name, name,
pattern,
data, data,
// file handling... // file handling...
@ -319,23 +286,18 @@ var SharpActions = actions.Actions({
// transformations... // transformations...
transform, transform,
// XXX not implemented... // XXX CROP not implemented...
crop, //crop,
timestamp, timestamp,
logger, //logger,
} = options } = options
// defaults... // defaults...
pattern = pattern || '%n' name = name || '%n'
transform = transform === undefined ? transform = transform === undefined ?
true true
: transform : transform
timestamp = timestamp || Date.timeStamp() timestamp = timestamp || Date.timeStamp()
logger = logger !== false ?
(logger || this.logger)
: false
logger = logger
&& logger.push('Resize', {onclose: abort})
// backup... // backup...
// XXX make backup name pattern configurable... // XXX make backup name pattern configurable...
@ -345,25 +307,19 @@ var SharpActions = actions.Actions({
i++ } i++ }
return `${to}.${timestamp}.bak`+ (i || '') } return `${to}.${timestamp}.bak`+ (i || '') }
return images
.mapChunks(CHUNK_SIZE, function(gid){
if(STOP){
throw Array.STOP('aborted') }
// skip non-images... // skip non-images...
if(!['image', null, undefined] if(!['image', null, undefined]
.includes(that.images[gid].type)){ .includes(this.images[image].type)){
return false } // XXX what should we return???
return Promise.resolve() }
// paths... // paths...
var source = that.getImagePath(gid) var source = this.getImagePath(image)
var to = pathlib.join( var to = pathlib.resolve(
this.location.path,
pathlib.join(
path, path,
(images.length == 1 && name) ? this.formatImageName(name, image, data || {})))
name
: that.formatImageName(pattern, gid, data || {}))
logger && logger.emit('queued', to)
var img = sharp(source) var img = sharp(source)
return (skipSmaller ? return (skipSmaller ?
@ -376,7 +332,6 @@ var SharpActions = actions.Actions({
&& Math.max(m.width, m.height) < size) && Math.max(m.width, m.height) < size)
|| (fit == 'outside' || (fit == 'outside'
&& Math.min(m.width, m.height) < size)){ && Math.min(m.width, m.height) < size)){
//logger && logger.emit('skipping', gid)
return } return }
// continue... // continue...
return img }) return img })
@ -396,15 +351,14 @@ var SharpActions = actions.Actions({
fse.removeSync(to) fse.removeSync(to)
// skip... // skip...
} else { } else {
//logger && logger.emit('skipping', gid) return Promise.reject('target exists') } }
return } }
// write... // write...
return img return img
.clone() .clone()
// handle transform (.orientation / .flip) and .crop... // handle transform (.orientation / .flip) and .crop...
.run(function(){ .run(function(){
var img_data = that.images[gid] var img_data = that.images[image]
if(transform && (img_data.orientation || img_data.flipped)){ if(transform && (img_data.orientation || img_data.flipped)){
img_data.orientation img_data.orientation
&& this.rotate(img_data.orientation) && this.rotate(img_data.orientation)
@ -414,10 +368,10 @@ var SharpActions = actions.Actions({
img_data.flipped img_data.flipped
&& img_data.flipped.includes('vertical') && img_data.flipped.includes('vertical')
&& this.flop() && this.flop()
// XXX // XXX CROP
if(crop){ //if(crop){
// XXX // // XXX
} //}
}) })
.resize({ .resize({
width: size, width: size,
@ -428,16 +382,12 @@ var SharpActions = actions.Actions({
.withMetadata() .withMetadata()
.toFile(to) .toFile(to)
.then(function(){ .then(function(){
logger // XXX what should we return???
&& logger.emit('done', to) return to }) }) }) })],
return img }) }) }) })
.then(function(res){
ticket.resolve(res)
return res == 'aborted' ?
Promise.reject('aborted')
: res }) })],
// XXX move to core.queue... // XXX move to core.queue...
// XXX should this use .makeResizedImage(..) in sync mode???
// ...would be interesting to try a nested queue...
// XXX this does not update image.base_path -- is this correct??? // XXX this does not update image.base_path -- is this correct???
// XXX add support for offloading the processing to a thread/worker... // XXX add support for offloading the processing to a thread/worker...
makePreviews: ['Sharp|File/Make image $previews', makePreviews: ['Sharp|File/Make image $previews',
@ -559,7 +509,8 @@ var SharpActions = actions.Actions({
.replace(/\$RESOLUTION|\$\{RESOLUTION\}/g, parseInt(size)) .replace(/\$RESOLUTION|\$\{RESOLUTION\}/g, parseInt(size))
.replace(/\$GID|\$\{GID\}/g, gid) .replace(/\$GID|\$\{GID\}/g, gid)
.replace(/\$NAME|\$\{NAME\}/g, img.name) .replace(/\$NAME|\$\{NAME\}/g, img.name)
return that.makeResizedImage(gid, size, base, { // XXX do we need this to be sync???
return that.makeResizedImage('sync', gid, size, base, {
name, name,
skipSmaller: true, skipSmaller: true,
transform: false, transform: false,
@ -567,7 +518,8 @@ var SharpActions = actions.Actions({
false false
: logger, : logger,
}) })
.then(function([res]){ // XXX handle errors -- rejected because image exists...
.then(function(res){
i == sizes.length-1 i == sizes.length-1
&& gid_logger && gid_logger.emit('done', gid) && gid_logger && gid_logger.emit('done', gid)

View File

@ -250,7 +250,7 @@ var ProgressActions = actions.Actions({
this.config['progress-done-delay'] || 1000))) } }], this.config['progress-done-delay'] || 1000))) } }],
// handle logger progress... // handle logger progress...
// XXX revise... // XXX show progress after a timeout if still not finished...
handleLogItem: ['- System/', handleLogItem: ['- System/',
function(logger, path, status, ...rest){ function(logger, path, status, ...rest){
var msg = path.join(': ') var msg = path.join(': ')