Comparing version 1.5.1 to 1.6.0
{ | ||
"name": "immp", | ||
"description": "Image Manipulation Middleware Proxy", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"repository": "garrows/IMMP", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -9,3 +9,4 @@ var _ = require('underscore'), | ||
https = require('https'), | ||
http = require('http'); | ||
http = require('http'), | ||
path = require('path'); | ||
@@ -63,2 +64,3 @@ module.exports = function (_config) { | ||
image.location = image.location.trim().replace(/^\//, ''); | ||
image.path = image.location; | ||
image.location = _req.protocol + '://' + _req.headers.host + '/' + image.location; | ||
@@ -80,2 +82,3 @@ } | ||
try { | ||
// TODO: remove this sync? sounds like a good idea | ||
stat = fs.statSync(config.cacheFolder + '/' + image.hash); | ||
@@ -114,17 +117,45 @@ createdAt = cast(stat.mtime, 'date').getTime(); | ||
function (_callback) { | ||
if(config.allowProxy) { | ||
var client = http; | ||
if(image.location.indexOf('https://') === 0) { | ||
client = https; | ||
} | ||
client.get(image.location, function (_httpResponse) { | ||
if(_httpResponse.statusCode >= 400) return _callback(new Error('status ' + _httpResponse.statusCode)); | ||
_callback(null, _httpResponse); | ||
}).on('error', function (_error) { | ||
_callback(_error); | ||
}); | ||
} else { | ||
if(!config.allowProxy) { | ||
var imageStream = fs.createReadStream(config.imageDir + image.location); | ||
_callback(null, imageStream); | ||
return _callback(null, imageStream); | ||
} | ||
var client = image.location.indexOf('https://') === 0 ? https : http; | ||
var sourceImageHash = image.hash = crypto.createHash('sha1').update(image.path).digest('hex'); | ||
var sourceImageCacheFilename = path.join(config.cacheFolder, 'source-' + sourceImageHash); | ||
async.waterfall([ | ||
function (wDone) { | ||
var sourceImageStream = fs.createReadStream(sourceImageCacheFilename); | ||
sourceImageStream.on('open', function () { | ||
return wDone(null, sourceImageStream); | ||
}); | ||
sourceImageStream.on('error', function (error) { | ||
// If the cached image is not found, it's fine to ignore the error. | ||
// Subsequently, if the error is not to do with a missing cache image, we don't | ||
// want to stop serving images, so log an error and continue. | ||
if(error.code !== 'ENOENT') { | ||
console.error('Error retrieving source image cache', error, error.stack); | ||
} | ||
return wDone(null, null); | ||
}); | ||
}, | ||
function (sourceImageStream, wDone) { | ||
if(sourceImageStream) return wDone(null, sourceImageStream); | ||
client.get(image.location, function (_httpResponse) { | ||
if(_httpResponse.statusCode >= 400) return wDone(new Error('status ' + _httpResponse.statusCode)); | ||
var writeImageStream = fs.createWriteStream(sourceImageCacheFilename) | ||
.on('error', wDone) | ||
.on('close', function () { | ||
return wDone(null, fs.createReadStream(sourceImageCacheFilename)); | ||
}); | ||
_httpResponse.pipe(writeImageStream); | ||
}).on('error', wDone); | ||
}, | ||
], _callback); | ||
}, | ||
@@ -142,3 +173,5 @@ | ||
bufferStream: true | ||
}, _callback); | ||
}, function (error, format) { | ||
return _callback(error, format); | ||
}); | ||
}, | ||
@@ -303,2 +336,2 @@ function (_format, _callback) { | ||
}; | ||
}; | ||
}; |
@@ -550,2 +550,39 @@ var async = require('async'), | ||
describe('proxy file caching', function () { | ||
var robotImage; | ||
before(function (_done) { | ||
// Clear file cache. | ||
var immpFiles = fs.readdirSync(immpPath); | ||
immpFiles.forEach(function (_file) { | ||
fs.unlinkSync(path.join(immpPath, '/', _file)); | ||
}); | ||
_done(); | ||
}); | ||
it('should work with no cached image', function (_done) { | ||
http.get(serverUrl + '/im/?image=/images/robot.jpg', function (_httpResponse) { | ||
_httpResponse.statusCode.should.eql(200); | ||
_httpResponse.headers['content-type'].should.eql('image/jpeg'); | ||
_done(); | ||
}); | ||
}); | ||
it('should have created a cached image', function (_done) { | ||
var crypto = require('crypto'); | ||
var cachedFileName = path.join(immpPath, 'source-' + crypto.createHash('sha1').update('images/robot.jpg').digest('hex')); | ||
fs.readFile(cachedFileName, function (error, data) { | ||
_done(error); | ||
}); | ||
}); | ||
it('should work with a cached image', function (_done) { | ||
http.get(serverUrl + '/im/?image=/images/robot.jpg', function (_httpResponse) { | ||
_httpResponse.statusCode.should.eql(200); | ||
_httpResponse.headers['content-type'].should.eql('image/jpeg'); | ||
_done(); | ||
}); | ||
}); | ||
}); | ||
describe('with convertTo set', function () { | ||
@@ -594,3 +631,2 @@ | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1333525
737