ghost-storage-adapter-s3
Advanced tools
Comparing version 2.0.0 to 2.0.1
199
index.js
@@ -9,4 +9,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _awsSdk = require('aws-sdk'); | ||
@@ -30,8 +28,2 @@ | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var readFileAsync = (0, _bluebird.promisify)(_fs.readFile); | ||
@@ -43,12 +35,8 @@ | ||
var Store = function (_BaseStore) { | ||
_inherits(Store, _BaseStore); | ||
function Store() { | ||
class Store extends _ghostStorageBase2.default { | ||
constructor() { | ||
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
_classCallCheck(this, Store); | ||
super(config); | ||
var _this = _possibleConstructorReturn(this, (Store.__proto__ || Object.getPrototypeOf(Store)).call(this, config)); | ||
_awsSdk2.default.config.setPromisesDependency(_bluebird2.default); | ||
@@ -64,114 +52,103 @@ | ||
_this.accessKeyId = accessKeyId; | ||
_this.bucket = bucket; | ||
_this.host = assetHost || 'https://s3' + (region === 'us-east-1' ? '' : '-' + region) + '.amazonaws.com/' + bucket; | ||
_this.pathPrefix = stripLeadingSlash(pathPrefix || ''); | ||
_this.region = region; | ||
_this.secretAccessKey = secretAccessKey; | ||
return _this; | ||
this.accessKeyId = accessKeyId; | ||
this.bucket = bucket; | ||
this.host = assetHost || `https://s3${region === 'us-east-1' ? '' : `-${region}`}.amazonaws.com/${bucket}`; | ||
this.pathPrefix = stripLeadingSlash(pathPrefix || ''); | ||
this.region = region; | ||
this.secretAccessKey = secretAccessKey; | ||
} | ||
_createClass(Store, [{ | ||
key: 'delete', | ||
value: function _delete(fileName, targetDir) { | ||
var _this2 = this; | ||
delete(fileName, targetDir) { | ||
var _this = this; | ||
var directory = targetDir || this.getTargetDir(this.pathPrefix); | ||
var directory = targetDir || this.getTargetDir(this.pathPrefix); | ||
return new _bluebird2.default(function (resolve, reject) { | ||
return _this2.s3().deleteObject({ | ||
Bucket: _this2.bucket, | ||
Key: stripLeadingSlash((0, _path.join)(directory, fileName)) | ||
}).promise().then(function () { | ||
return resolve(true); | ||
}).catch(function () { | ||
return resolve(false); | ||
}); | ||
return new _bluebird2.default(function (resolve, reject) { | ||
return _this.s3().deleteObject({ | ||
Bucket: _this.bucket, | ||
Key: stripLeadingSlash((0, _path.join)(directory, fileName)) | ||
}).promise().then(function () { | ||
return resolve(true); | ||
}).catch(function () { | ||
return resolve(false); | ||
}); | ||
} | ||
}, { | ||
key: 'exists', | ||
value: function exists(fileName) { | ||
var _this3 = this; | ||
}); | ||
} | ||
return new _bluebird2.default(function (resolve, reject) { | ||
return _this3.s3().getObject({ | ||
exists(fileName) { | ||
var _this2 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
return _this2.s3().getObject({ | ||
Bucket: _this2.bucket, | ||
Key: stripLeadingSlash(fileName) | ||
}).promise().then(function () { | ||
return resolve(true); | ||
}).catch(function () { | ||
return resolve(false); | ||
}); | ||
}); | ||
} | ||
s3() { | ||
return new _awsSdk2.default.S3({ | ||
accessKeyId: this.accessKeyId, | ||
bucket: this.bucket, | ||
region: this.region, | ||
secretAccessKey: this.secretAccessKey | ||
}); | ||
} | ||
save(image, targetDir) { | ||
var _this3 = this; | ||
var directory = targetDir || this.getTargetDir(this.pathPrefix); | ||
return new _bluebird2.default(function (resolve, reject) { | ||
_bluebird2.default.all([_this3.getUniqueFileName(image, directory), readFileAsync(image.path)]).then(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
fileName = _ref2[0], | ||
file = _ref2[1]; | ||
return _this3.s3().putObject({ | ||
ACL: 'public-read', | ||
Body: file, | ||
Bucket: _this3.bucket, | ||
CacheControl: `max-age=${30 * 24 * 60 * 60}`, | ||
ContentType: image.type, | ||
Key: stripLeadingSlash(fileName) | ||
}).promise().then(function () { | ||
return resolve(true); | ||
}).catch(function () { | ||
return resolve(false); | ||
return resolve(`${_this3.host}/${fileName}`); | ||
}); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
} | ||
}, { | ||
key: 's3', | ||
value: function s3() { | ||
return new _awsSdk2.default.S3({ | ||
accessKeyId: this.accessKeyId, | ||
bucket: this.bucket, | ||
region: this.region, | ||
secretAccessKey: this.secretAccessKey | ||
}); | ||
} | ||
}, { | ||
key: 'save', | ||
value: function save(image, targetDir) { | ||
var _this4 = this; | ||
}); | ||
} | ||
var directory = targetDir || this.getTargetDir(this.pathPrefix); | ||
serve() { | ||
var _this4 = this; | ||
return new _bluebird2.default(function (resolve, reject) { | ||
_bluebird2.default.all([_this4.getUniqueFileName(image, directory), readFileAsync(image.path)]).then(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
fileName = _ref2[0], | ||
file = _ref2[1]; | ||
return function (req, res, next) { | ||
_this4.s3().getObject({ | ||
Bucket: _this4.bucket, | ||
Key: stripLeadingSlash(req.path) | ||
}).on('httpHeaders', function (statusCode, headers, response) { | ||
res.set(headers); | ||
}).createReadStream().on('error', function (err) { | ||
res.status(404); | ||
console.log(err + '\nkey: ' + stripLeadingSlash(req.path)); | ||
next(); | ||
}).pipe(res); | ||
}; | ||
} | ||
return _this4.s3().putObject({ | ||
ACL: 'public-read', | ||
Body: file, | ||
Bucket: _this4.bucket, | ||
CacheControl: 'max-age=' + 30 * 24 * 60 * 60, | ||
ContentType: image.type, | ||
Key: stripLeadingSlash(fileName) | ||
}).promise().then(function () { | ||
return resolve(_this4.host + '/' + fileName); | ||
}); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: 'serve', | ||
value: function serve() { | ||
var _this5 = this; | ||
read() { | ||
/* | ||
* Dummy function as ghost needs it | ||
*/ | ||
} | ||
} | ||
return function (req, res, next) { | ||
_this5.s3().getObject({ | ||
Bucket: _this5.bucket, | ||
Key: stripLeadingSlash(req.path) | ||
}).on('httpHeaders', function (statusCode, headers, response) { | ||
res.set(headers); | ||
}).createReadStream().on('error', function (err) { | ||
res.status(404); | ||
console.log(err + '\nkey: ' + stripLeadingSlash(req.path)); | ||
next(); | ||
}).pipe(res); | ||
}; | ||
} | ||
}, { | ||
key: 'read', | ||
value: function read() { | ||
/* | ||
* Dummy function as ghost needs it | ||
*/ | ||
} | ||
}]); | ||
return Store; | ||
}(_ghostStorageBase2.default); | ||
exports.default = Store; | ||
module.exports = exports['default']; |
@@ -13,3 +13,13 @@ { | ||
"presets": [ | ||
"es2015" | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": "4.5" | ||
}, | ||
"exclude": [ | ||
"transform-es2015-classes" | ||
] | ||
} | ||
] | ||
] | ||
@@ -35,3 +45,3 @@ }, | ||
"babel-plugin-transform-object-rest-spread": "^6.8.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"babel-preset-env": "^1.6.0", | ||
"commitizen": "^2.8.6", | ||
@@ -67,3 +77,3 @@ "cz-conventional-changelog": "^1.2.0", | ||
}, | ||
"version": "2.0.0" | ||
"version": "2.0.1" | ||
} |
141005
10
224