Code coverage report for tasks/wrappers/s3.js

Statements: 94.64% (53 / 56)      Branches: 77.42% (24 / 31)      Functions: 100% (10 / 10)      Lines: 98.11% (52 / 53)      Ignored: none     

All files » tasks/wrappers/ » s3.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 901 1 1 1 1   1 11 11 11 11 11 1   11 11 1 10 2         11   11               1 11 11 11     1 4 2       1 1   1 1   1 1   1     1     1 1 1 1 1 1     1 1 1           1 1 1 1 1 1 1   1       1  
var Promise = require('es6-promise').Promise;
var AWSSDK = require('aws-sdk');
var mime = require('mime');
var log = require('../utils/log');
var fs = require('../utils/fs');
 
function AWS(location, destination, options){
    this.location = location;
    this.destination = this.addSlash(destination);
    AWSSDK.config.update({region: options.region || process.env.AWS_REGION || null});
    var credentials;
    if (options.profile) {
        credentials = new AWSSDK.SharedIniFileCredentials({profile: options.profile});
    }
    var auth;
    if (credentials) {
        auth = { credentials: credentials };
    } else if (options.accessKey && options.secret) {
        auth = {
            accessKeyId: options.accessKey,
            secretAccessKey: options.secret
        };
    }
    this.s3 = new AWSSDK.S3(auth);
 
    this.params = {
        Bucket : options.bucket || null,
        ACL    : options.acl || 'public-read',
        Key    : null,
        Body   : null
    };
}
 
AWS.prototype.addSlash = function(dir){
    if (dir.slice(-1) !== '/') dir = dir +'/';
    Iif (dir === '/') dir = dir.substring(1);
    return dir;
};
 
AWS.prototype.checkMandatory = function(key){
    if (!this.params[key]) {
        log.onError({message:'AWS: Missing config `' + key + '`'});
    }
};
 
AWS.prototype.setParams = function(fileObj){
    this.checkMandatory('Bucket');
 
    Eif (fileObj.ext) {
        this.params.ContentType = mime.lookup(fileObj.path);
    }
    Eif (fileObj.stat) {
        this.params.ContentLength = fileObj.stat.size;
    }
    this.params.Key = fileObj.path
        .replace(fileObj.base, this.destination || '')
        .replace(new RegExp('\\\\', 'g'), '/');
    this.params.Body = fileObj.contents;
};
 
AWS.prototype.upload = function(fileObj) {
    var self = this;
    return new Promise(function(resolve, reject){
        self.setParams(fileObj);
        self.s3.putObject(self.params, function(err) {
            Iif (err) {
                reject({message: 'S3::putObject "' + self.params.Key + '" error!\n' + err});
            } else {
                var msg ='   * "' + fileObj.relativeDir + fileObj.name + '" ';
                log.info(msg);
                resolve(msg);
            }
        });
    });
};
 
AWS.prototype.write = function(){
    var self = this;
    return fs.read(this.location).then(function(files){
        Iif (!files.length) log.info('No files found to release to AWS\n' + self.location);
        var promises = [];
        files.forEach(function(fileObj){
            promises.push(self.upload(fileObj));
        });
        return Promise.all(promises);
    });
};
 
module.exports = AWS;