-
Notifications
You must be signed in to change notification settings - Fork 1
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 #120 from humanmade/fix-proxy-file
Fix proxy file
- Loading branch information
Showing
7 changed files
with
123 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,52 @@ | ||
var http = require("http"), | ||
url = require("url"), | ||
path = require("path"), | ||
fs = require("fs"), | ||
tachyon= require( './index' ), | ||
var http = require("http"), | ||
url = require("url"), | ||
fs = require("fs"), | ||
tachyon = require( './index' ), | ||
args = process.argv.slice(2), | ||
port = Number( args[0] ) ? args[0] : 8080, | ||
debug = args.indexOf( '--debug' ) > -1 | ||
port = Number( args[0] ) ? args[0] : 8080, | ||
debug = args.indexOf( '--debug' ) > -1; | ||
|
||
http.createServer( function( request, response ) { | ||
var params = url.parse( request.url, true ) | ||
var params = url.parse( request.url, true ); | ||
|
||
if ( debug ) { | ||
console.log( Date(), request.url ) | ||
console.log( Date(), request.url ); | ||
} | ||
|
||
try { | ||
var imageData = fs.readFileSync( decodeURI( params.pathname.substr(1) ) ) | ||
var imageData = fs.readFileSync( decodeURI( params.pathname.substr(1) ) ); | ||
} catch ( err ) { | ||
response.writeHead( err.statusCode ? err.statusCode : 500 ) | ||
response.write( err.message ) | ||
return response.end() | ||
response.writeHead( err.statusCode ? err.statusCode : 500 ); | ||
response.write( err.message ); | ||
return response.end(); | ||
} | ||
|
||
params.query.key = decodeURI( params.pathname.substr(1) ) | ||
params.query.key = decodeURI( params.pathname.substr(1) ); | ||
|
||
return tachyon.resizeBuffer( imageData, params.query, function( err, data, info ) { | ||
|
||
if ( err ) { | ||
if ( err.message === 'fallback-to-original' ) { | ||
response.writeHead( 200, { | ||
'Content-Type': 'image/gif', | ||
'Content-Length': Buffer.byteLength( imageData ), | ||
} ); | ||
response.write( imageData ); | ||
return response.end(); | ||
} | ||
if ( debug ) { | ||
console.error( Date(), err ) | ||
console.error( Date(), err ); | ||
} | ||
response.writeHead( err.statusCode ? err.statusCode : 500 ) | ||
response.write( err.message ) | ||
return response.end() | ||
response.writeHead( err.statusCode ? err.statusCode : 500 ); | ||
response.write( err.message ); | ||
return response.end(); | ||
} | ||
response.writeHead( 200, { | ||
'Content-Type': 'image/' + info.format, | ||
'Content-Length': info.size | ||
}) | ||
response.write( data ) | ||
return response.end() | ||
'Content-Length': info.size, | ||
} ); | ||
response.write( data ); | ||
return response.end(); | ||
} ); | ||
}).listen( parseInt( port, 10 ) ) | ||
} ).listen( parseInt( port, 10 ) ); | ||
|
||
console.log( "Server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" ) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,35 @@ | ||
var AWS = require('aws-sdk'); | ||
|
||
var authenticatedRequest = !!process.env.S3_AUTHENTICATED_REQUEST | ||
|
||
function sendOriginal(region, bucket, key, callback) { | ||
var s3 = new AWS.S3(Object.assign({ region: region })); | ||
var s3Request = authenticatedRequest ? s3.makeRequest : s3.makeUnauthenticatedRequest | ||
return s3Request( | ||
'getObject', | ||
{ Bucket: bucket, Key: key }, | ||
function(err, data) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
var resp = { | ||
statusCode: 200, | ||
headers: { | ||
'Content-Type': data.ContentType, | ||
}, | ||
body: Buffer.from(data.Body).toString('base64'), | ||
isBase64Encoded: true, | ||
}; | ||
|
||
callback(null, resp); | ||
const AWS = require( 'aws-sdk' ); | ||
|
||
const authenticatedRequest = !!process.env.S3_AUTHENTICATED_REQUEST ? process.env.S3_AUTHENTICATED_REQUEST.toLowerCase() === 'true' : false; | ||
|
||
function sendOriginal( config, bucket, key, callback ) { | ||
const s3 = new AWS.S3(config); | ||
|
||
let request; | ||
if ( authenticatedRequest ) { | ||
request = s3.makeRequest( 'getObject', { Bucket: bucket, Key: key } ); | ||
} else { | ||
request = s3.makeUnauthenticatedRequest( 'getObject', { Bucket: bucket, Key: key } ); | ||
} | ||
|
||
request.send( function( err, data ) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
); | ||
|
||
var resp = { | ||
statusCode: 200, | ||
headers: { | ||
'Content-Type': data.ContentType, | ||
}, | ||
body: Buffer.from(data.Body).toString('base64'), | ||
isBase64Encoded: true, | ||
}; | ||
|
||
callback(null, resp); | ||
} ); | ||
|
||
return request; | ||
} | ||
|
||
module.exports = sendOriginal; |
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 |
---|---|---|
@@ -1,66 +1,90 @@ | ||
var http = require("http"), | ||
url = require("url"), | ||
path = require("path"), | ||
fs = require("fs"), | ||
os = require("os"), | ||
tachyon= require( './index' ), | ||
var http = require("http"), | ||
url = require("url"), | ||
fs = require("fs"), | ||
os = require("os"), | ||
tachyon = require( './index' ), | ||
proxyFile = require( './proxy-file' ), | ||
args = process.argv.slice(2), | ||
port = Number( args[0] ) ? args[0] : 8080, | ||
debug = args.indexOf( '--debug' ) > -1 | ||
port = Number( args[0] ) ? args[0] : 8080, | ||
debug = args.indexOf( '--debug' ) > -1; | ||
|
||
var config = {} | ||
var config = {}; | ||
if ( process.env.AWS_REGION && process.env.AWS_S3_BUCKET ) { | ||
config = { | ||
region: process.env.AWS_REGION, | ||
bucket: process.env.AWS_S3_BUCKET, | ||
endpoint: process.env.AWS_S3_ENDPOINT, | ||
} | ||
}; | ||
} else if ( fs.existsSync( 'config.json' ) ) { | ||
config = JSON.parse( fs.readFileSync( 'config.json' ) ) | ||
config = JSON.parse( fs.readFileSync( 'config.json' ) ); | ||
} | ||
|
||
http.createServer( function( request, response ) { | ||
var params = url.parse( request.url, true ) | ||
var params = url.parse( request.url, true ); | ||
|
||
if ( debug ) { | ||
console.log( Date(), request.url ) | ||
console.log( Date(), request.url ); | ||
} | ||
|
||
// healthcheck file | ||
if ( params.pathname === '/healthcheck.php' ) { | ||
response.writeHead( 200 ) | ||
response.write( 'All good.' ) | ||
return response.end() | ||
response.writeHead( 200 ); | ||
response.write( 'All good.' ); | ||
return response.end(); | ||
} | ||
|
||
// robots.txt | ||
if ( params.pathname === '/robots.txt' ) { | ||
response.writeHead( 200, { | ||
'Content-Type': 'text/plain' | ||
'Content-Type': 'text/plain', | ||
} ); | ||
response.write( 'User-agent: *' + os.EOL + 'Allow: /' ) | ||
return response.end() | ||
response.write( 'User-agent: *' + os.EOL + 'Allow: /' ); | ||
return response.end(); | ||
} | ||
|
||
const key = decodeURIComponent( params.pathname.substr(1) ).replace( '/uploads/tachyon/', '/uploads/' ); | ||
const args = params.query || {}; | ||
if ( typeof args.webp === 'undefined' ) { | ||
args.webp = !!( request.headers && request.headers['accept'] && request.headers['accept'].match( 'image/webp' ) ); | ||
} | ||
|
||
return tachyon.s3( config, decodeURIComponent( params.pathname.substr(1) ), params.query, function( err, data, info ) { | ||
return tachyon.s3( config, key, args, function( err, data, info ) { | ||
if ( err ) { | ||
if ( debug ) { | ||
console.error( Date(), err ) | ||
function callback( error, rsp ) { | ||
if ( error ) { | ||
if ( debug ) { | ||
console.error( Date(), error ); | ||
} | ||
response.writeHead( error.statusCode ? error.statusCode : 500, { | ||
'Cache-Control': 'no-cache', | ||
} ); | ||
response.write( error.message ); | ||
return response.end(); | ||
} | ||
response.writeHead( rsp.statusCode, Object.assign( { | ||
'Content-Type': 'image/gif', | ||
'Cache-Control': 'public, max-age=31557600', | ||
} ) ); | ||
response.write( Buffer.from( rsp.body, 'base64' ) ); | ||
return response.end(); | ||
} | ||
response.writeHead( err.statusCode ? err.statusCode : 500, { | ||
'Cache-Control': 'no-cache' | ||
} ) | ||
response.write( err.message ) | ||
return response.end() | ||
if ( err.message === 'fallback-to-original' ) { | ||
const s3config = { region: config.region }; | ||
if ( config.endpoint ) { | ||
s3config.endpoint = config.endpoint; | ||
} | ||
return proxyFile( s3config, config.bucket, key, callback ); | ||
} | ||
return callback( err ); | ||
} | ||
response.writeHead( 200, { | ||
'Content-Type': 'image/' + info.format, | ||
'Content-Length': info.size, | ||
'Cache-Control': 'public, max-age=31557600' | ||
}) | ||
response.write( data ) | ||
return response.end() | ||
'Cache-Control': 'public, max-age=31557600', | ||
} ); | ||
response.write( data ); | ||
return response.end(); | ||
} ); | ||
}).listen( parseInt( port, 10 ) ) | ||
} ).listen( parseInt( port, 10 ) ); | ||
|
||
console.log( "Server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" ) | ||
console.log( "Server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown" ); |