Skip to content

Commit

Permalink
Fix metadataMap and manualContentEncoding bugs; add maps option
Browse files Browse the repository at this point in the history
  • Loading branch information
clineamb committed Sep 22, 2015
1 parent 1f3ba85 commit 39402c4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 20 deletions.
58 changes: 58 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,65 @@ Use this to transform your file names before they're uploaded to your S3 bucket.
});
```

#### maps.ParamName {}

Type: `object` + `function`

**NEW IN 1.2**

Upon reviewing an issue with `metadataMap` and `manualContentEncoding`, a standard method for mapping each `s3.putObject` param was created. For now, `metadataMap` and `manualContentEncoding` are still available, but they will be depricated in the next major version (2.0).

Each property of the maps option must be a function and must match the paramter being mapped. The files' `keyname` will be passed through (keep in mind, this is after any `keyTransform` calls). The function should return the output S3 expects. [You can find more information and the available options here](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property).

For example, to define metadataMap and separate expirations in this way:

```js
var metadata_collection = { /* your info here */ };
var expirations = { /* your info here */ };

gulp.task("upload", function() {
gulp.src("./dir/to/upload/**")
.pipe(s3({
Bucket: 'example-bucket',
ACL: 'public-read',
maps: {
Metadata: function(keyname) {
path.basename(keyname); // just get the filename
return metadata_collection[keyname]; // return an object
},
Expires: function(keyname) {
path.basename(keyname); // just get the filename
return new Date(expirations[keyname]);
}
}
}));
});
```

If anything but a function is passed through, nothing will happen. If you want to send a consistent value to all of your files this way, just simply set the option straight in the main options like so:

```js
var expires = new Date();
expires.setUTCFullYear(2020);

gulp.task("upload", function() {
gulp.src("./dir/to/upload/**")
.pipe(s3({
Bucket: 'example-bucket',
ACL: 'public-read',
Metadata: {
"example1": "This is an example"
},
Expires: expires
}));
});
```


#### metadataMap

**NOTE**: It is preferred you use the maps.ParamsName method to define and map specific metadata to files. Also, if you set both `maps.Metadata` and this, `metadataMap` will take precedence.

Type: `object` or `function`

If you have constant metadata you want to attach to each object,
Expand Down Expand Up @@ -205,6 +261,8 @@ overwrite existing ones.

#### manualContentEncoding

**NOTE**: It is preferred you use the maps.ParamsName method to define and map specific Content Encoding values to files. If you set both `maps.ContentEncoding` and `manualContentEncoding`, `manualContentEncoding` will take priority.

Type: `string` or `function`

If you want to add a custom content-encoding header on a per file basis, you can
Expand Down
45 changes: 37 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ gulpPrefixer = function (AWS) {
stream = es.map(function (file, callback) {

var keyTransform, keyname, keyparts, filename,
mimetype, mime_lookup_name, metadata, contentEncoding
mimetype, mime_lookup_name, metadata, content_encoding,
mapped_params
;

if(file.isNull()) {
Expand Down Expand Up @@ -95,28 +96,56 @@ gulpPrefixer = function (AWS) {
// === metadataMap =====================================
// Map files (using the keyname) to a metadata object.
// ONLY if `options.Metadata` is undefined.
// ** WILL DEPRICATE IN 2.0.0 **

if (!options.Metadata && options.metadataMap) {
if (helper.isFunction(options.metadataMap)) {
if (_.isUndefined(options.Metadata) && options.metadataMap) {
if (_.isFunction(options.metadataMap)) {
metadata = options.metadataMap(keyname);
} else {
metadata = options.metadataMap;
}
} else if(_.isObject(options.Metadata)) {
metadata = options.Metadata;
}

// === manualContentEncoding ===========================
// Similar to metadataMap to put global / individual
// headers on each file object (only if
// options.ContentEncoding) is undefined. (1.2)
// ** WILL DEPRICATE IN 2.0.0 **

if (!options.ContentEncoding && options.manualContentEncoding) {
if(helper.isFunction(options.manualContentEncoding)) {
contentEncoding = options.manualContentEncoding(keyname);
if (_.isUndefined(options.ContentEncoding) && options.manualContentEncoding) {
if(_.isFunction(options.manualContentEncoding)) {
content_encoding = options.manualContentEncoding(keyname);
} else {
contentEncoding = options.manualContentEncoding;
content_encoding = options.manualContentEncoding;
}
} else if(_.isString(options.ContentEncoding)) {
content_encoding = options.ContentEncoding;
}

// === maps.ParamNames =================================
// This is a new mapper object that, if given in the
// options as `maps.ParamName`, and is a function, will
// run the given function and map that param data, given
// that the return value of the `maps.ParamName` function
// returns the appropriate type for that give putObject Param
// { Bucket: ... maps: { 'CacheControl': function()..., 'Expires': function()... }, etc. }
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
// *** NEW in 1.2 ***

if(!_.isUndefined(options.maps)) {

_.each(options.maps, function(mapRoutine, ParamName) {
console.log(mapRoutine, ParamName);

if(_.isFunction(mapRoutine)) {
options[ParamName] = mapRoutine(keyname);
}
});

console.log(options);
}

// === ETag Hash Comparison =============================
// *NEW* in 1.1; do a local hash comparison to reduce
Expand Down Expand Up @@ -166,7 +195,7 @@ gulpPrefixer = function (AWS) {
objOpts.Body = file.contents;
objOpts.ContentType = mimetype;
objOpts.Metadata = metadata;
objOpts.ContentEncoding = contentEncoding;
objOpts.ContentEncoding = content_encoding;

if (options.uploadNewFilesOnly && !head_data || !options.uploadNewFilesOnly) {

Expand Down
22 changes: 10 additions & 12 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ var Path = require('path')
;

module.exports = {
parsePath: function (path) {
var extname = Path.extname(path);

return {
'dirname': Path.dirname(path),
'basename': Path.basename(path, extname),
'extname': extname
};
},

buildName: function (dirs, filename) {
return Path.join(dirs, filename);
Expand All @@ -30,6 +21,7 @@ module.exports = {
'keyTransform',
'metadataMap',
'manualContentEncoding',
'maps',
'mimeTypeLookup',
'nameTransform',
'uploadNewFilesOnly',
Expand All @@ -39,7 +31,13 @@ module.exports = {
return _.omit(params, omit_array);
},

isFunction: function (fn) {
return _.isFunction(fn);
}
parsePath: function (path) {
var extname = Path.extname(path);

return {
'dirname': Path.dirname(path),
'basename': Path.basename(path, extname),
'extname': extname
};
},
};

0 comments on commit 39402c4

Please sign in to comment.