added cache preview generator and queue (tested but not yet hooked into the UI/logic)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-06-30 02:01:47 +04:00
parent 7739ce4add
commit ec4c347207

View File

@ -125,6 +125,139 @@ if(window.CEF_dumpJSON != null){
$('.title-bar .title').text(title)
}
// vips preview generation...
// XXX make this queuable...
window.makeImagePreviews = function(gid){
var img = IMAGES[gid]
var source = normalizePath(img.path)
var name = gid +' - '+ source.split('/').pop()
var compression = 90
var previews = []
// XXX get real sizes from config...
var sizes = [
150,
350,
900
]
// XXX take this from config...
var path = normalizePath(CACHE_DIR)
path = path.replace(fp, '')
// XXX get cur image size...
var size_getter = $.Deferred()
var _i = new Image()
_i.onload = function(){
size_getter.resolve(Math.max(parseInt(this.width), parseInt(this.height)))
}
_i.src = source
for(var i=0; i < sizes.length; i++){
var size = sizes[i]
var target_path = [ path, size+'px' ].join('/')
var deferred = $.Deferred()
previews.push(deferred)
// NOTE: for some magical reason writing this like this:
// (function(...){
// ...
// }(...))
// produces a "undefined is not a function" in except the
// first invocation...
var _f = function(size, target_path, deferred){
size_getter.done(function(source_size){
// skip previews larger than cur image...
if(fs.existsSync(target_path +'/'+ name) || source_size <= size){
console.log('>>> Preview:', name, '('+size+'): Skipped.')
return deferred.resolve()
}
// XXX check for errors...
fse.mkdirRecursive(target_path, function(err){
var preview_path = [target_path, name].join('/')
var factor = source_size / size
var cmd = 'vips im_shrink "$IN" "$OUT:$COMPRESSION" $FACTOR $FACTOR'
.replace(/\$IN/g, source.replace(fp, ''))
.replace(/\$OUT/g, preview_path)
.replace(/\$COMPRESSION/g, compression)
.replace(/\$FACTOR/g, factor)
proc.exec(cmd, function(error, stdout, stderr){
if(error != null){
console.error('>>> Error: preview:', stderr)
deferred.reject()
} else {
console.log('>>> Preview:', name, '('+size+'): Done.')
if(!('preview' in img)){
img.preview = {}
}
img.preview[size+'px'] = './' + CACHE_DIR +'/'+ preview_path.split(CACHE_DIR).pop()
// mark image dirty...
if(IMAGES_UPDATED.indexOf(gid) < 0){
IMAGES_UPDATED.push(gid)
}
deferred.resolve()
}
})
})
})
}
// NOTE: wrapping this in a closure saves the specific data that would
// otherwise be overwritten by the next loop iteration...
_f(size, target_path, deferred)
}
return $.when.apply(null, previews)
}
// XXX is this robust enough???
window._PREVIW_CREATE_QUEUE = null
window.makeImagesPreviewsQ = function(gids){
var previews = []
$.each(gids, function(i, e){
var deferred = $.Deferred()
var last = previews[previews.length-1]
// first in this set...
if(last == null){
if(_PREVIW_CREATE_QUEUE == null){
// if nothing in queue, start at once...
last = $.Deferred().resolve()
} else {
last = _PREVIW_CREATE_QUEUE
}
}
// append to deffered queue...
last.always(function(){
makeImagePreviews(e)
.always(function(){
deferred.resolve()
})
})
previews.push(deferred)
})
_PREVIW_CREATE_QUEUE = $.when.apply(null, previews)
return _PREVIW_CREATE_QUEUE
}
// load UI stuff...
$(function(){
$('<div class="title-bar"/>')