diff --git a/lib/quickthumb.js b/lib/quickthumb.js index 1ef5490..ceb6a54 100644 --- a/lib/quickthumb.js +++ b/lib/quickthumb.js @@ -85,7 +85,11 @@ qt.static = function(root, options){ return sendfile(res, file); }); + + // Either callback or sendfile has been called so we are done. + return; } + callback(); }); }); diff --git a/package.json b/package.json index 71c0b17..ef989c3 100644 --- a/package.json +++ b/package.json @@ -33,5 +33,8 @@ "dependencies": { "imagemagick": "git://github.com/rsms/node-imagemagick.git", "mkdirp": "~0.3.3" + }, + "devDependencies": { + "express": "^4.13.3" } } diff --git a/public/images/cape cod modified.jpg b/public/images/cape cod modified.jpg new file mode 100644 index 0000000..83c0f55 Binary files /dev/null and b/public/images/cape cod modified.jpg differ diff --git a/test/express-test.js b/test/express-test.js index 547134f..9cde681 100644 --- a/test/express-test.js +++ b/test/express-test.js @@ -1,16 +1,23 @@ var express = require('express'), - app = express(), - qt = require('../'), - filename = 'cape cod.jpg'; + app = express(), + qt = require('../'), + os = require('os'), + path = require('path'), + fs = require('fs'), + + filename = 'cape cod.jpg', + publicDir = path.join(__dirname, '../public'), + imageDir = path.join(publicDir, 'images') + cacheDir = path.join(os.tmpdir(), 'quickthumb/cache'); // Crop -app.use('/public/crop', qt.static(__dirname + '/../public', { - cacheDir : '/tmp/cache', +app.use('/public/crop', qt.static(publicDir, { + cacheDir : cacheDir, quality : .95 })); // Resize -app.use('/public/resize', qt.static(__dirname + '/../public', { +app.use('/public/resize', qt.static(publicDir, { type : 'resize', })); @@ -31,10 +38,48 @@ app.get('/', function(req, res){ }); h += '
' + img(type, '?dim=800x100') + '
'; }); - h += '
original
' + img('crop','') + ''; + h += '
original
' + img('crop','') + '
'; + + // --- Image cache test + h += '
cache test

' + + '
' + + ''; + + h += ''; + res.send(h); }); +// --- Image cache test +var cacheTestImage = 'cache_test.jpg'; +var modifiedImage = 'cape cod modified.jpg'; +var redirectPath = '/public/crop/images/' + cacheTestImage + + '?dim=x200&' + new Date().getTime(); + +app.get('/test/:step', function(req, res) { + switch (req.params.step) { + case '1': // Step 1: No cache, image has to be generated + + // Create image for cache test + fs.createReadStream(path.join(imageDir, filename)).pipe( + fs.createWriteStream(path.join(imageDir, cacheTestImage))); + + // Cached version is deleted so that it has to be generated + fs.unlink(path.join(cacheDir, 'crop/x200/images', cacheTestImage), + function(){/* ignore errors */}); + break; + + case '2': // Step 2: Image has to be regenerated since it was modified + + // File is modifed so the cached version should be updated + fs.createReadStream(path.join(imageDir, modifiedImage)).pipe( + fs.createWriteStream(path.join(imageDir, cacheTestImage))); + break; + + } + + res.redirect(redirectPath); +}); app.listen(3000); console.log("running on http://127.0.0.1:3000");