Skip to content

Commit

Permalink
Merge pull request #120 from humanmade/fix-proxy-file
Browse files Browse the repository at this point in the history
Fix proxy file
  • Loading branch information
roborourke authored Jul 17, 2020
2 parents 5e564f9 + 1955590 commit 56f3cd9
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 87 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM lambci/lambda:nodejs10.x
COPY node_modules/ /var/task/node_modules
COPY server.js /var/task/
COPY index.js /var/task
COPY proxy-file.js /var/task

ARG AWS_REGION
ARG AWS_S3_BUCKET
Expand Down
2 changes: 1 addition & 1 deletion lambda-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports.handler = function(event, context, callback) {
) {
if (err) {
if (err.message === 'fallback-to-original') {
return proxyFile(region, bucket, key, callback);
return proxyFile({ region: region }, bucket, key, callback);
} else if ( err.code === 'AccessDenied' ) {
// An AccessDenied error means the file is either protected, or doesn't exist.
// We don't get a NotFound error because Tachyon makes unauthenticated calls
Expand Down
54 changes: 30 additions & 24 deletions local-server.js
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" )
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "node-tachyon",
"version": "2.2.1",
"version": "2.3.2",
"repository": {
"type": "git",
"url": "https://github.com/humanmade/node-tachyon.git"
},
"description": "human made tachyon in node",
"description": "Human Made Tachyon in node",
"main": "index.js",
"config": {
"bucket": "",
Expand Down
57 changes: 31 additions & 26 deletions proxy-file.js
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;
90 changes: 57 additions & 33 deletions server.js
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" );

0 comments on commit 56f3cd9

Please sign in to comment.