|
// add this to ./routes/middleware.js |
|
var crypto = require('crypto'); |
|
var request = require('request'); |
|
var path = require("path"); |
|
var fs = require('fs'); |
|
var s3 = require('s3'); |
|
var image_cache = keystone.list('Imagecache').model; |
|
var temp_dir = path.join(process.cwd(), 'temp/'); |
|
if (!fs.existsSync(temp_dir)) { |
|
fs.mkdirSync(temp_dir); |
|
} |
|
var s3_client = s3.createClient({ |
|
multipartUploadThreshold: 209715200, // this is the default (20 MB) |
|
multipartUploadSize: 157286400, // this is the default (15 MB) |
|
s3Options: { |
|
accessKeyId: "ACCESS_KEY", |
|
secretAccessKey: "SECRET" |
|
}, |
|
}); |
|
|
|
// if you already have an initLocals, just add the locals.gi function to it |
|
exports.initLocals = function(req,res,next) { |
|
locals.gi = function(img) { |
|
|
|
// console.log('looking for image =>',img) |
|
var md5 = crypto.createHash('md5'); |
|
var hash = md5.update(img).digest('hex'); |
|
var db_image; |
|
function getImage(hash) { |
|
var response; |
|
image_cache.where({hash:hash}).findOne(function(err,data){ |
|
response = data |
|
}) |
|
while(response === undefined) { |
|
require('deasync').sleep(3); |
|
} |
|
return response; |
|
} |
|
db_image = getImage(hash) |
|
|
|
if(!db_image || !db_image.uploaded) { |
|
if(!db_image) { |
|
// console.log('starting image upload') |
|
image_cache.create({hash:hash,uploaded:0},function(err,$img){ |
|
request(img).pipe(fs.createWriteStream(temp_dir+"/"+hash+".jpg")).on('close', function (error, response, body) { |
|
var params = { |
|
localFile: temp_dir+"/"+hash+".jpg", |
|
s3Params: { |
|
Bucket: "YOUR_BUCKET", |
|
Key: hash+'.jpg', |
|
ACL:'public-read', |
|
ContentType:'image/jpeg' |
|
}, |
|
}; |
|
var uploader = s3_client.uploadFile(params); |
|
uploader.on('error', function(err) { |
|
$img.remove() |
|
}); |
|
uploader.on('end', function() { |
|
console.log('successful image upload',img) |
|
$img.uploaded = true; |
|
$img.save() |
|
}); |
|
}) |
|
}) |
|
} |
|
// console.log('returning image =>',img) |
|
return img |
|
} |
|
else { |
|
// console.log('returning image =>',req.protocol+'://YOUR_BUCKET.s3.amazonaws.com/'+hash+'.jpg') |
|
return req.protocol+'://YOUR_BUCKET.s3.amazonaws.com/'+hash+'.jpg' |
|
} |
|
} |
|
} |
I put something together to help with wysiwyg images that are saved on cloudinary as we were being hit by huge bandwidths.
Same concept as yours but caches content blocks in the model, also checks if the content changes
https://gist.github.com/stephencoe/a7a3581141582b02fc7e