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
: [items])
.forEach(function(item){
func.call(that, item, ...args) })
.map(function(item){
return func.call(that, item, ...args) })
// XXX should we return anything in sync mode???
return Promise.resolve()
// queue mode...
@ -2725,14 +2726,17 @@ function(title, func){
return [e, ...args] })
: [items, ...args]))
// make a promise...
var res = new Promise(function(resolve, reject){
q.then(resolve, reject) })
return res } }),
return new Promise(function(resolve, reject){
q.then(resolve, reject) }) } }),
{
title,
toString: function(){
// XXX add opts of given...
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 =

View File

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

View File

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