Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "firstandthird"
}
3 changes: 1 addition & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"></script>
<script>
$('div').fileUploader({
//action: '/upload',
action: '/upload/image/crop/305/173',
action: '/upload?width=50&height=50&x=50&y=50&quality=50',
postKey: 'file'
});
</script>
Expand Down
29 changes: 14 additions & 15 deletions example/server.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
var Hapi = require('hapi');
var port = process.env.PORT || 8080;
var server = new Hapi.Server();
var fs = require('fs');

server.connection({ port: port });
const Hapi = require('hapi');
const port = process.env.PORT || 8080;
const server = new Hapi.Server();
const fs = require('fs');
const path = require('path');
server.connection({ port });

server.register([
{
register: require('../'),
options: {
s3AccessKey: '',
s3SecretAccessKey: '',
s3Region: 'us-east-1',
s3Bucket: '',
contentTypes: ['image/jpeg'],
imagemagick: true,
maxBytes: 30000
}
}
], function(err) {
], (err) => {
if (err) {
throw err;
}
Expand All @@ -26,8 +22,11 @@ server.register([
{
path: '/',
method: 'GET',
handler: function(request, reply) {
fs.readFile(__dirname + '/index.html', 'utf8', function(err, html) {
handler: (request, reply) => {
fs.readFile(path.join(__dirname, 'index.html'), 'utf8', (fileErr, html) => {
if (fileErr) {
return reply(err);
}
reply(html);
});
}
Expand All @@ -42,7 +41,7 @@ server.register([
}
}
]);
server.start(function() {
server.start(() => {
console.log('Hapi server started @', server.info.uri);
});
});
141 changes: 0 additions & 141 deletions lib/handler.js

This file was deleted.

121 changes: 74 additions & 47 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,83 @@
var handler = require('./handler');
var AWS = require('aws-sdk');
var datefmt = require('datefmt');
var Hoek = require('hoek');
var slug = require('slug');

const Boom = require('boom');
const Joi = require('joi');
const slug = require('slug');
const s3put = require('s3put');
const defaults = require('lodash.defaults');
// Forces slug to use url encoding
slug.defaults.mode = 'rfc3986';

module.exports = function(plugin, options, next) {

options = options || {};

var endpoint = options.endpoint || '/upload';

options.contentTypes = options.contentTypes || [];

var s3 = new AWS.S3({
accessKeyId: options.s3AccessKey,
secretAccessKey: options.s3SecretAccessKey,
region: options.s3Region
});

var getFileKey = function(filename) {
filename = slug(filename);
var fileKey = datefmt('%Y-%m-%d', new Date()) + '/' + (+new Date) + '/' + filename;
return fileKey;
};

var getS3Url = function(fileKey) {
var s3UrlBase = 'https://'+options.s3Bucket+'.s3.amazonaws.com/';
return s3UrlBase + fileKey;
};

plugin.bind({
options: options,
s3: s3,
bucketName: options.s3Bucket,
Hapi: plugin.hapi,
getFileKey: options.getFileKey || getFileKey,
getS3Url: options.getS3Url || getS3Url
});
const defaultOptions = {
endpoint: '/upload', // endpoint where images should be POSTed
contentTypes: [], // list of mime-types the endpoint accepts
bucket: process.env.AWS_BUCKET,
profile: process.env.AWS_PROFILE,
quality: 100, // images aren't compressed by default
maxUploadSize: 1 // in mb
};

var maxBytes = {
payload: {
maxBytes: options.maxBytes || 1048576 // Hapi default (1MB)
module.exports = (plugin, options, next) => {
options = defaults(options, defaultOptions);
if (!options.profile) {
if (!options.access_key && !options.secret_key) {
return next('You must specify either a profile or an access/secret key to use AWS.');
}
};
}
if (!options.bucket) {
return next('You must specify a bucket name that you want to upload to.');
}

plugin.route([
{ path: endpoint, method: 'POST', config: Hoek.applyToDefaults(handler.upload, maxBytes) },
{ path: endpoint + '/image/{type}/{width}/{height}', method: 'POST', config: Hoek.applyToDefaults(handler.image, maxBytes) }
]);
// one route with query params for resize/crop
plugin.route({
config: {
payload: {
output: 'stream',
maxBytes: options.maxUploadSize * (1024 * 1024) // convert to bytes for hapi
},
validate: {
payload: {
file: Joi.any().required()
},
query: {
quality: Joi.number(),
x: Joi.number(),
y: Joi.number(),
height: Joi.number(),
width: Joi.number(),
gravity: Joi.string()
}
},
},
path: options.endpoint,
method: 'POST',
handler: (request, reply) => {
// make sure payload is palatable to s3put:
const file = request.payload.file;
if (!file.hapi.filename) {
return reply(Boom.badData('must be a file'));
}
if (options.contentTypes.length && options.contentTypes.indexOf(file.hapi.headers['content-type']) === -1) {
return reply(Boom.unsupportedMediaType('content-type not allowed'));
}
file.path = file.hapi.filename;
// pass any crop/compress options:
const query = request.query;
options.quality = query.quality ? query.quality : options.quality;
if (query.width && query.height) {
options.size = [query.width, query.height];
}
if (query.x && query.y) {
options.position = [query.x, query.y];
}
options.gravity = query.gravity;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have query here, but it's not in joi above

// call s3put to handle the upload:
s3put(file, options, (err, response) => {
if (err) {
plugin.log(err);
}
reply(response);
});
}
});
next();
};

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"url": "https://github.com/firstandthird/hapi-upload-s3.git"
},
"devDependencies": {
"eslint": "^3.9.1",
"eslint-config-firstandthird": "^3.0.2",
"eslint-plugin-import": "^2.1.0",
"hapi": "^8.1.0"
},
"dependencies": {
"aws-sdk": "^2.1.6",
"boom": "^2.6.1",
"datefmt": "^0.4.0",
"gm": "^1.17.0",
"hoek": "^2.11.0",
"joi": "^5.1.0",
"s3-upload-stream": "^1.0.7",
"lodash.defaults": "^4.2.0",
"s3put": "^1.0.0",
"slug": "^0.8.0"
}
}