Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,46 @@ var es = require('event-stream');
var knox = require('knox');
var gutil = require('gulp-util');
var mime = require('mime');
mime.default_type = 'text/plain';
mime.defaultType = 'text/plain';

module.exports = function (aws, options) {
module.exports = function (aws, options, directories) {
options = options || {};
directories = directories || [];

if (!options.delay) { options.delay = 0; }

var client = knox.createClient(aws);
var waitTime = 0;
var regexGzip = /\.([a-z]{2,})\.gz$/i;
var regexGeneral = /\.([a-z]{2,})$/i;
var i, rootIndex, rootPath;

return es.mapSync(function (file) {

// Verify this is a file
if (!file.isBuffer()) { return file; }

var uploadPath = file.path.replace(file.base, options.uploadPath || '');
if(directories.length) {
// Get the root path based on provided directories
rootPath = file.path.split('/');
for(i = 0; i < directories.length; i++) {
rootIndex = rootPath.indexOf(directories[i]);
if(rootIndex > -1) {
rootPath = rootPath.splice(0, rootIndex);
rootPath = rootPath.join('/');
break;
}
}

// Trim the trailing '/', if present and if it is the last char of string
if(options.uploadPath.lastIndexOf('/') === options.uploadPath.length - 1){
options.uploadPath = options.uploadPath
.substring(0, options.uploadPath.length - 1);
}
} else {
rootPath = file.base;
}

var uploadPath = file.path.replace(rootPath, options.uploadPath || '');
uploadPath = uploadPath.replace(new RegExp('\\\\', 'g'), '/');
var headers = { 'x-amz-acl': 'public-read' };
if (options.headers) {
Expand Down Expand Up @@ -51,9 +73,9 @@ module.exports = function (aws, options) {

client.putBuffer(file.contents, uploadPath, headers, function(err, res) {
if (err || res.statusCode !== 200) {
gutil.log(gutil.colors.red('[FAILED]', file.path + " -> " + uploadPath));
gutil.log(gutil.colors.red('[FAILED]', file.path + ' -> ' + uploadPath));
} else {
gutil.log(gutil.colors.green('[SUCCESS]', file.path + " -> " + uploadPath));
gutil.log(gutil.colors.green('[SUCCESS]', file.path + ' -> ' + uploadPath));
res.resume();
}
});
Expand Down