-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from wjordan/external_sources
Support simple external sources through environment variables
- Loading branch information
Showing
6 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Fetches an image from an external URL | ||
|
||
'use strict'; | ||
|
||
var stream, util, request; | ||
|
||
stream = require('stream'); | ||
util = require('util'); | ||
request = require('request'); | ||
|
||
function contentLength(bufs){ | ||
return bufs.reduce(function(sum, buf){ | ||
return sum + buf.length; | ||
}, 0); | ||
} | ||
|
||
function External(image, key, prefix){ | ||
/* jshint validthis:true */ | ||
if (!(this instanceof External)){ | ||
return new External(image, key, prefix); | ||
} | ||
stream.Readable.call(this, { objectMode : true }); | ||
this.image = image; | ||
this.ended = false; | ||
this.key = key; | ||
this.prefix = prefix; | ||
} | ||
|
||
util.inherits(External, stream.Readable); | ||
|
||
External.prototype._read = function(){ | ||
var _this = this, | ||
url, | ||
fbStream, | ||
bufs = []; | ||
|
||
if ( this.ended ){ return; } | ||
|
||
// pass through if there is an error on the image object | ||
if (this.image.isError()){ | ||
this.ended = true; | ||
this.push(this.image); | ||
return this.push(null); | ||
} | ||
|
||
url = this.prefix + '/' + this.image.path; | ||
|
||
this.image.log.time(this.key); | ||
|
||
fbStream = request.get(url); | ||
fbStream.on('data', function(d){ bufs.push(d); }); | ||
fbStream.on('error', function(err){ | ||
_this.image.error = new Error(err); | ||
}); | ||
fbStream.on('response', function(response) { | ||
if (response.statusCode !== 200) { | ||
_this.image.error = new Error('Error ' + response.statusCode + ':'); | ||
} | ||
}); | ||
fbStream.on('end', function(){ | ||
_this.image.log.timeEnd(_this.key); | ||
if(_this.image.isError()) { | ||
_this.image.error.message += Buffer.concat(bufs); | ||
} else { | ||
_this.image.contents = Buffer.concat(bufs); | ||
} | ||
_this.image.originalContentLength = contentLength(bufs); | ||
_this.ended = true; | ||
_this.push(_this.image); | ||
_this.push(null); | ||
}); | ||
|
||
}; | ||
|
||
|
||
module.exports = External; |
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