cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-01 15:25:50 +03:00
parent 32f9d5450e
commit e620063915
2 changed files with 1 additions and 350 deletions

View File

@ -9,8 +9,6 @@
var actions = require('lib/actions')
var features = require('lib/features')
// XXX REMOVE...
//var preview = require('lib/preview')
var core = require('features/core')
@ -82,46 +80,7 @@ var SharpActions = actions.Actions({
// from filesystem.IndexFormat...
},
/* XXX REMOVE ???
// NOTE: post handlers are pushed in .makePreviews(..)
// XXX might be a good idea to make this a bit more generic...
// XXX might be a good idea to use tasks to throttle....
startPreviewWorker: ['- Sharp/',
function(){
var that = this
if(this.previewConstructorWorker){
return
}
this.previewConstructorWorker = cp.fork(
'./workers/preview-constructor.js', {
cwd: process.cwd(),
})
.on('message', function(res){
if(res.err){
// XXX
console.error(res)
} else {
var ticket = res.ticket
// clear listener...
if(res.status == 'completed'){
that.previewConstructorWorker.__post_handlers[res.ticket](null, 'completed')
delete that.previewConstructorWorker.__post_handlers[res.ticket]
} else {
that.previewConstructorWorker.__post_handlers[res.ticket](res.err, res.data)
}
}
})
this.previewConstructorWorker.__post_handlers = {} }],
stopPreviewWorker: ['- Sharp/',
function(){
this.previewConstructorWorker && this.previewConstructorWorker.kill()
delete this.previewConstructorWorker }],
//*/
// XXX need to destinguish if something was written in the promise chain...
// 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:
@ -446,171 +405,6 @@ var SharpActions = actions.Actions({
return [gid, size, name] }) }) })
.flat()) }],
/* XXX REMOVE...
// XXX not sure why are we messing with orientation here...
// XXX should this account for non-jpeg images???
// XXX log: count gids and not specific images...
_makePreviews: ['Sharp|File/Make image $previews',
core.doc`Make image previews
Make previews for all images...
.makePreviews()
.makePreviews('all')
-> actions
Make previews for current image...
.makePreviews('current')
-> actions
Make previews for specific image(s)...
.makePreviews(gid)
.makePreviews([gid, gid, ..])
-> actions
`,
function(images, sizes, base_path, logger){
var that = this
var logger_mode = this.config['preview-progress-mode'] || 'gids'
logger = logger || this.logger
logger = logger && logger.push('Previews')
// get/normalize images...
//images = images || this.current
images = images
|| 'all'
// keywords...
images = images == 'all' ?
this.data.getImages('all')
: images == 'current' ?
this.current
: 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
// 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()
if(sizes){
sizes = sizes instanceof Array ? sizes : [sizes]
// normalize to preview size...
sizes = (this.config['preview-normalized'] ?
sizes
.map(function(s){
return cfg_sizes.filter(function(c){ return c >= s }).pop() || s })
: sizes)
.unique()
} else {
sizes = cfg_sizes }
var path_tpl = that.config['preview-path-template']
.replace(/\$INDEX|\$\{INDEX\}/g, that.config['index-dir'] || '.ImageGrid')
var post_handler = function(err, data){
if(data.res != 'all'
&& (data.status == 'done'
|| data.status == 'skipped')){
// get/make preview list...
var img = that.images[data.gid]
var preview = img.preview =
img.preview || {}
// save previews...
preview[data.res + 'px'] = data.path
var o = normalizeOrientation(data.orientation)
// save orientation...
img.orientation = o.orientation
img.flipped = o.flipped
that.markChanged('images', [data.gid]) }
// NOTE: this will handle both 'queue' and 'resolved' statuses...
logger &&
( logger_mode == 'gids' ?
// report gid-level progress...
(data.res == 'all'
&& logger.emit(data.status, data.gid))
// report preview-level progress...
: (data.res != 'all'
&& logger.emit(data.status, data.path)) ) }
// now do the work (async)...
if(this.previewConstructorWorker){
return Promise.all(
Object.keys(data)
.map(function(base_path){
return new Promise(function(resolve, reject){
do {
var ticket = Date.now()
} while(ticket in that.previewConstructorWorker.__post_handlers)
that.previewConstructorWorker.send({
ticket: ticket,
images: data[base_path],
sizes: sizes,
base_path: base_path,
target_tpl: path_tpl,
})
that.previewConstructorWorker.__post_handlers[ticket] =
function(err, data){
if(err){
return reject(err)
}
data == 'completed' ?
resolve()
: post_handler(err, data) } }) }))
// now do the work (sync)...
} else {
return Promise.all(
Object.keys(data)
// 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

@ -1,143 +0,0 @@
/**********************************************************************
*
*
*
**********************************************************************/
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
(function(require){ var module={} // make module AMD/node compatible...
/*********************************************************************/
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('imagegrid/file')
}
/*********************************************************************/
if(typeof(process) != 'undefined'){
var ensureDir = file.denodeify(fse.ensureDir)
}
/*********************************************************************/
// images format:
// [
// {
// source: <source>,
// gid: <gid>,
// },
// ...
// ]
//
// XXX add a callback call when a gid is done...
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
callback && callback(null, {
status: 'queued',
gid: gid,
res: 'all',
})
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,
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, }) })
})
}))
// report a gid is done...
.then(function(){
callback
&& callback(null, {
status: 'done',
gid: gid,
res: 'all',
}) }) })
}))
}
/**********************************************************************
* vim:set ts=4 sw=4 : */ return module })