Skip to content

Instantly share code, notes, and snippets.

@gabrielstuff
Created December 15, 2016 17:08
Show Gist options
  • Save gabrielstuff/22ebbe76e06bbe104b49591c8cfea025 to your computer and use it in GitHub Desktop.
Save gabrielstuff/22ebbe76e06bbe104b49591c8cfea025 to your computer and use it in GitHub Desktop.
Precache images based on JSON response
'use strict'
var queue = require('async').queue
var cache = require("imgcache")
var _ = require('lodash')
// query the stories
// query content of eachStories
// then findURLInObject
// queue the query
let concurrency = 2
var q = async.queue(function(task, callback) {
createOrUpdateCache(task.imageUrl, function(err, success){
callback();
})
})
}, concurrency)
// assign a callback
q.drain = function() {
console.log('all items have been processed');
}
var createOrUpdateCache = function(url, callback){
ImgCache.isCached(url, function(path, success) {
if (success) {
console.log(`${url} is cached` )
callback && callback(null, 'success')
} else {
// not there, need to cache the image
ImgCache.cacheFile(url, function () {
callback && callback(null, 'success')
console.log(`${url} is now cached` )
});
}
}
var findURLInObject = function (JSONObject) {
var result = []
_.forOwn(JSONObject, function iterateObject(value, key) {
if (_.isArrayLikeObject(value)) {
_.each(value, function iterateArray(el, index) {
result = _.concat(result, findURLInObject(el))
})
} else if (_.isPlainObject(value)) {
result = _.concat(result, findURLInObject(value))
} else if (_.isString(value) && value.indexOf('.jpg') > -1) {
result.push(value)
}
})
return result
}
let imageArray = findURLInObject(JSONRESPONSE)
startPreload = function(images){
_.each(images, function(imageURL){
q.push({imageURL: imageURL}, function(err) {
if(err){
console.error(`error while processing ${imageURL}`)
} else {
console.log(`finished processing ${imageURL}`)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment