Skip to content

Commit

Permalink
Merge b6dec3b into 88a104c
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkruegger committed Jul 4, 2015
2 parents 88a104c + b6dec3b commit 251663a
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions lib/S3.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,62 @@ var S3StorageProvider = function(options) {
bucket: this._options.bucket,
region: this._options.region
})
}

this._cb = function(error, res, callback) {

if (error) {
return callback(error);
}

if (res && 'on' in res) {
if (res.statusCode >= 200 && res.statusCode <= 209) {
return callback(null, res.req.url);
}

var body = '';

res.on('data', function (chunk) {
body += chunk;
});

res.on('end', function () {
var err = new Error();
err.statusCode = res.statusCode;
err.body = body;

return callback(err);
});
} else {
if (res && 'req' in res){
callback(null, res.req.url);
} else {
callback();
}
}
};


S3StorageProvider.prototype.save = function(attachment, callback) {
this._client.putFile(attachment.path, this._options.path(attachment), {
'x-amz-acl': this._options.acl
}, function(error, message) {
callback(error, error ? undefined : message.req.url)
})
}
var self = this;

this._client.putFile(attachment.path, this._options.path(attachment),
{'x-amz-acl': this._options.acl }, function(error, res) {
self._cb(error, res, callback)
})
};

S3StorageProvider.prototype.remove = function(attachment, callback) {
var self = this;

if(!attachment.url) {
return callback()
return callback();
}

this._client.deleteFile(path.basename(attachment.url), callback)
// this._client.deleteFile(path.basename(attachment.url), callback)
this._client.deleteFile(path.basename(attachment.url), function(error, res) {
self._cb(error, res, callback)
});
};
}

module.exports = S3StorageProvider

0 comments on commit 251663a

Please sign in to comment.