Created
October 24, 2012 20:05
-
-
Save max-mapper/3948494 to your computer and use it in GitHub Desktop.
fetch images and store in pouchdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path') | |
var Pouch = require('pouchdb') | |
var async = require('async') | |
var request = require('request') | |
var queue = async.queue(queueWorker, 5) // 5 requests at a time | |
var imageURLs = [ | |
"http://substack.net/images/ben.png", | |
"http://substack.net/images/bed.png" | |
] | |
queue.drain = function() { | |
console.log('downloaded all images!') | |
} | |
function queueWorker(opts, callback) { | |
var db = opts.db | |
var imageURL = opts.imageURL | |
request(imageURL, function(err, resp, image) { | |
if (err) return callback(err) | |
var contentType = resp.headers['content-type'] | |
saveImageData(db, imageURL, image, contentType , callback) | |
}) | |
} | |
function saveImageData(db, imageURL, imageData, contentType, callback) { | |
var extension = path.extname(imageURL) | |
var filename = path.basename(imageURL, extension) | |
db.put({_id: filename}, function(err, response) { | |
db.putAttachment(response.id + '/image', response.rev, imageData, contentType, callback) | |
}) | |
} | |
Pouch("ldb://images", function(err, db) { | |
if (err) return console.log(err) | |
imageURLs.forEach(function(imageURL) { | |
queue.push({db: db, imageURL: imageURL}, function() { | |
console.log('stored', imageURL) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment