Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mojotech/mojo-ghost-s3-adapter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mojotech/mojo-ghost-s3-adapter - npm Package Compare versions

Comparing version 0.9.0 to 1.0.0

183

index.js

@@ -1,4 +0,1 @@

/**
* Created by MojoTech on 9/17/2017.
*/
'use strict';

@@ -17,105 +14,115 @@

class MojoGhostS3Adapter extends BaseAdapter {
// `s3Client` allows for Dependency injection for testing
constructor(config, s3Client) {
super();
// `s3Client` allows for Dependency injection for testing
constructor(config, s3Client) {
super();
if (!config || isEmpty(config.bucket)) {
return reject('invalid mojo-ghost-s3-adapter config - bucket is required');
}
if (!config || isEmpty(config.bucket)) {
return reject('invalid mojo-ghost-s3-adapter config - bucket is required');
}
if (isEmpty(config.region)) {
return reject('invalid mojo-ghost-s3-adapter config - region is required');
}
if (isEmpty(config.region)) {
return reject('invalid mojo-ghost-s3-adapter config - region is required');
}
if (isEmpty(config.accessKeyId) || isEmpty(config.secretAccessKey)) {
return reject('invalid mojo-ghost-s3-adapter config - accessKeyId and secretAccessKey are required');
}
this.config = config;
this.imageCacheMaxAge = 1000 * 365 * 24 * 60 * 60; // 365 days
this._s3Client = s3Client ? s3Client : new S3({
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
bucket: config.bucket,
region: config.region
});
if (isEmpty(config.accessKeyId) || isEmpty(config.secretAccessKey)) {
return reject('invalid mojo-ghost-s3-adapter config - accessKeyId and secretAccessKey are required');
}
exists(fileName) {
return new Promise(function (resolve, reject) {
https.get(fileName, function(res) {
resolve(200 === res.statusCode);
});
this.config = config;
this.assetHost = config.asssetHost
? config.assetHost
: util.format('https://s3.%s.amazonaws.com/%s', config.region, config.bucket);
this.imageCacheMaxAge = 1000 * 365 * 24 * 60 * 60; // 365 days
this._s3Client = s3Client
? s3Client
: new S3({
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
bucket: config.bucket,
region: config.region,
});
}
}
save(image, targetDir) {
const config = this.config;
const s3 = this._s3Client;
const cacheMaxAge = this.imageCacheMaxAge;
const curDate = new Date();
exists(fileName) {
return new Promise(function(resolve, reject) {
https.get(fileName, function(res) {
resolve(200 === res.statusCode);
});
});
}
return new Promise(function(resolve, reject) {
const stream = fs.createReadStream(image.path, { autoClose: true });
const targetFolder = path.join(config.folder || '', util.format('%s/%s/%s', curDate.getFullYear(), curDate.getMonth(), curDate.getDate()));
const formattedFilename = path.parse(image.originalname);
const targetFilename = formattedFilename.name + formattedFilename.ext;
const targetKey = path.join(targetFolder, targetFilename);
save(image, targetDir) {
const config = this.config;
const s3 = this._s3Client;
const cacheMaxAge = this.imageCacheMaxAge;
const assetHost = this.assetHost;
const curDate = new Date();
console.log('mojo-ghost-s3-adapter: putObject', image.path, config.bucket, targetKey);
return new Promise(function(resolve, reject) {
const stream = fs.createReadStream(image.path, { autoClose: true });
const targetFolder = path.join(
config.folder || '',
util.format('%s/%s/%s', curDate.getFullYear(), curDate.getMonth(), curDate.getDate())
);
const formattedFilename = path.parse(image.originalname);
const targetFilename = formattedFilename.name + formattedFilename.ext;
const targetKey = path.join(targetFolder, targetFilename);
const s3Params = {
ACL: 'public-read',
Body: stream,
Bucket: config.bucket,
Key: targetKey,
ContentType: image.type,
CacheControl: 'max-age=' + cacheMaxAge
};
s3.putObject(s3Params, function(error, data) {
if (error) {
return reject(error);
}
console.log('mojo-ghost-s3-adapter: putObject', image.path, config.bucket, targetKey);
return resolve(util.format('https://s3.%s.amazonaws.com/%s/%s', config.region, config.bucket, targetKey));
});
});
}
const s3Params = {
ACL: 'public-read',
Body: stream,
Bucket: config.bucket,
Key: targetKey,
ContentType: image.type,
CacheControl: 'max-age=' + cacheMaxAge,
};
s3.putObject(s3Params, function(error, data) {
if (error) {
return reject(error);
}
// Middleware for serving image files that used to be served from content/images
// * We uploaded all those images to S3, but the post content still references theme locally,
// so we proxy that request to S3 instead now.
serve() {
const config = this.config;
const s3 = this._s3Client;
return resolve(util.format('%s/%s', assetHost, targetKey));
});
});
}
return function customServe(req, res, next) {
const s3Params = {
Bucket: config.bucket,
Key: req.path.replace(/(^\/)|(\/$)/g, '')
};
// Middleware for serving image files that used to be served from content/images
// * We uploaded all those images to S3, but the post content still references theme locally,
// so we proxy that request to S3 instead now.
serve() {
const config = this.config;
const s3 = this._s3Client;
s3.getObject(s3Params)
.on('httpHeaders', function(statusCode, headers, response) {
res.set(headers);
})
.createReadStream()
.on('error', function(err) {
console.log('mojo-ghost-s3-adapter: serve#error', err);
res.status(404);
next();
})
.pipe(res);
}
}
return function customServe(req, res, next) {
const s3Params = {
Bucket: config.bucket,
Key: req.path.replace(/(^\/)|(\/$)/g, ''),
};
delete() {
return Promise.reject('not implemented');
}
s3
.getObject(s3Params)
.on('httpHeaders', function(statusCode, headers, response) {
res.set(headers);
})
.createReadStream()
.on('error', function(err) {
console.log('mojo-ghost-s3-adapter: serve#error', err);
res.status(404);
next();
})
.pipe(res);
};
}
read() {
return Promise.reject('not implemented');
}
delete() {
return Promise.reject('not implemented');
}
read() {
return Promise.reject('not implemented');
}
}
module.exports = MojoGhostS3Adapter;
{
"name": "@mojotech/mojo-ghost-s3-adapter",
"version": "0.9.0",
"version": "1.0.0",
"description": "Ghost storage adapter to store and serve images from AWS S3",

@@ -35,3 +35,13 @@ "main": "index.js",

"ghost-storage-base": "0.0.1"
},
"devDependencies": {
"prettier": "^1.7.0"
},
"prettier": {
"printWidth": 120,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"jsxBracketSameLine": false
}
}

@@ -21,5 +21,5 @@ # mojo-ghost-s3-adapter

```bash
$ npm install mojo-ghost-s3-adapter
$ npm install @mojotech/mojo-ghost-s3-adapter
$ mkdir -p content/adapters/storage
$ cp -r node_modules/mojo-ghost-s3-compat content/adapters/storage
$ cp -r node_modules/@mojotech/mojo-ghost-s3-compat content/adapters/storage
```

@@ -52,3 +52,3 @@ ## Configuration

```javascript
assetHost: 'https://cdn.yourdomain.com/'
assetHost: 'https://cdn.yourdomain.com'
```

@@ -55,0 +55,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc