Skip to content

Commit

Permalink
🔖 feat: release version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ronilsonalves committed Jun 16, 2023
1 parent 39ec99d commit 96a13ff
Show file tree
Hide file tree
Showing 3 changed files with 744 additions and 0 deletions.
97 changes: 97 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const StorageBase = require('ghost-storage-base');
const fs = require('fs');
const S3 = require('aws-sdk/clients/s3');


class Storage extends StorageBase {

constructor(config = {}) {
super(config);
const {
bucket,
endpoint,
accessKeyId,
secretAccessKey,
publicDomain
} = config;

this.bucket = bucket;
this.endpoint = endpoint;
this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
this.signatureVersion = 'v4';
this.domain = publicDomain;

this.s3 = new S3({
endpoint: this.endpoint,
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey,
signatureVersion: this.signatureVersion,
});
};

exists(filename) {
return s3.headObject({
Bucket: this.bucket,
Key: filename
}).promise()
.then(() => true)
.catch(() => false);
}

async save(image) {
const now = new Date();
const fileKey = `content/uploads/${now.getFullYear()}/${now.getMonth() + 1}/${now.getDate()}/${image.name}`;

return fs.promises.readFile(image.path)
.then(async (buffer) => {
const params = {
ACL: 'public-read',
Bucket: this.bucket,
Key: fileKey,
Body: buffer,
ContentType: image.type,
CacheControl: 'max-age=31536000'
};

await this.s3.putObject(params).promise();
return `${this.domain}/${fileKey}`;
});
}

serve() {
return (req, res, next) => {
const params = {
Bucket: this.bucket,
Key: req.path.replace(/^\//, '')
};

return this.s3.getObject(params)
.createReadStream()
.on('error', next())
.pipe(res);
};
};

async delete(fileName) {
try {
await this.s3.deleteObject({
Bucket: this.bucket,
Key: fileName
}).promise();
return true;
} catch {
return false;
}
}

async read(options) {
return this.s3.getObject({
Bucket: options.bucket,
Key: options.path
}).promise()
.then((data) => data.Body);
}
}

module.exports = Storage;
Loading

0 comments on commit 96a13ff

Please sign in to comment.