Skip to content

Commit

Permalink
Fixed detection of corrupted images. Fixes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Mar 8, 2016
1 parent 474787a commit 527883d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2016-03-08 / 1.2.0

* `fastimage.threshold` can now accept value less than or equal to zero to disable the feature and try to download/open the entire file/stream. This fixes detection of corrupted files.

### 2016-03-07 / 1.1.1

* Bumped version in package.json.
Expand Down
4 changes: 3 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ if(typeof Promise === "undefined")
/**
* Gets or sets the maximum number of bytes to read to attempt an identification before giving up and state that the source is not an image.
*
* Choosing a value less than or equal to zero will disable the feature, trying to download/open the entire file/stream.
*
* @alias module:fastimage.threshold
* @param {number} [value] - The new value to set. Passing `null` will restore the default value (4096).
* @returns {number} The current value
Expand Down Expand Up @@ -194,7 +196,7 @@ if(typeof Promise === "undefined")
info = identify(buffer, url, realUrl, size, time);

// Informations found or threshold reached. End the transfer and call the callback.
if(info || buffer.length >= threshold){
if(info || (threshold > 0 && buffer.length >= threshold)){ // eslint-disable-line no-extra-parens
this.abort();

if(info)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastimage",
"version": "1.1.1",
"version": "1.2.0",
"description": "FastImage finds the size or type of an image given its URL by fetching as little as needed.",
"main": "main.js",
"files": [
Expand Down
67 changes: 14 additions & 53 deletions test/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,62 +415,23 @@
});
});
});
/*
describe("outgoing request", function(){
var waitForTestToCallRequest, defaultUserAgent;
beforeEach(function(){
defaultUserAgent = "Fast Image - Node Image Lookup - https://www.npmjs.com/package/fastimage";
waitForTestToCallRequest = new Promise(function (res) {
sinon.stub(request, "get", function (outgoingRequest) {
res(outgoingRequest);
var ret = {
on: function () {
return ret;
}
};
return ret;
});
});
});
afterEach(function(){
request.get.restore();
});
it("should contain a default user agent string", function (done) {
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent);
done();
}).catch(done);
fastimage.info("http://example.com/");
});

it("should contain a default user agent string", function (done) {
var newUserAgentString = "Hello World!";
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(newUserAgentString);
done();
}).catch(done);
fastimage.userAgent(newUserAgentString);
fastimage.info("http://example.com/");
});
describe("side cases", function(){
// This is a file which is corrupted. To correctly recognize the threshold must be disabled.
it("https://upload.wikimedia.org/wikipedia/commons/b/b2/%27Journey_to_the_Center_of_the_Earth%27_by_%C3%89douard_Riou_38.jpg", function(done){
fastimage.type("https://upload.wikimedia.org/wikipedia/commons/b/b2/%27Journey_to_the_Center_of_the_Earth%27_by_%C3%89douard_Riou_38.jpg", function(error, info){
verify(done, function(){
expect(error.code).to.be("UNSUPPORTED_TYPE");

it("should have a standard setter/getter for user agent string", function (done) {
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent);
done();
}).catch(done);
fastimage.userAgent("this will never actually be used");
expect(fastimage.userAgent()).to.eql("this will never actually be used");
fastimage.userAgent(null);
expect(fastimage.userAgent()).to.eql(defaultUserAgent);
fastimage.info("http://example.com/");
fastimage.threshold(-1);
fastimage.type("https://upload.wikimedia.org/wikipedia/commons/b/b2/%27Journey_to_the_Center_of_the_Earth%27_by_%C3%89douard_Riou_38.jpg", function(error, info){
verify(done, function(){
expect(info).to.equal("jpg");
});
});
});
});
});
});
*/
});
})();

0 comments on commit 527883d

Please sign in to comment.