burdy
Version:
Open Source Headless Platform
154 lines (153 loc) • 6.03 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const aws_sdk_1 = __importDefault(require("aws-sdk"));
const multer_1 = __importDefault(require("multer"));
const multer_s3_1 = __importDefault(require("multer-s3"));
const uuid_1 = require("uuid");
const stream_1 = __importDefault(require("stream"));
class AwsS3FileDriver {
constructor() {
this.provider = 's3';
this.dir = process.env.AWS_S3_FOLDER || 'burdy/upload';
this.bucket = process.env.AWS_S3_BUCKET;
this.region = process.env.AWS_S3_REGION;
this.getPath = (key) => `${this.dir}/${key}`;
this.getUpload = () => (0, multer_1.default)({
storage: (0, multer_s3_1.default)({
s3: this.s3,
bucket: this.bucket,
metadata: (_req, file, cb) => {
cb(null, { fieldName: file.fieldname });
},
key: (_req, _file, cb) => {
cb(null, this.getPath((0, uuid_1.v4)()));
},
}),
});
this.getName = () => this.provider;
this.getKey = (key = '') => {
if (key.indexOf('/') > -1) {
return this.getPath(key.split('/').pop());
}
return this.getPath(key);
};
this.write = (key, data) => __awaiter(this, void 0, void 0, function* () {
yield this.s3
.putObject({
Key: this.getKey(key),
Body: data,
Bucket: this.bucket,
CacheControl: 'no-cache',
})
.promise();
return {
key,
provider: this.provider,
};
});
this.stat = (key) => __awaiter(this, void 0, void 0, function* () {
const head = yield this.s3
.headObject({
Key: this.getKey(key),
Bucket: this.bucket,
})
.promise();
return {
contentLength: head.ContentLength,
cacheControl: head.CacheControl,
contentType: head.ContentType,
};
});
this.read = (key) => __awaiter(this, void 0, void 0, function* () {
try {
const obj = yield this.s3
.getObject({
Key: this.getKey(key),
Bucket: this.bucket,
})
.promise();
return obj.Body;
}
catch (err) {
return null;
}
});
this.createReadStream = (key, options) => this.s3
.getObject({
Key: this.getKey(key),
Bucket: this.bucket,
Range: options === null || options === void 0 ? void 0 : options.range,
})
.createReadStream();
this.createWriteStream = (key) => {
const pass = new stream_1.default.PassThrough();
this.s3.upload({ Bucket: this.bucket, Key: this.getKey(key), Body: pass });
return pass;
};
this.uploadReadableStream = (key, stream) => __awaiter(this, void 0, void 0, function* () {
return this.s3
.upload({ Bucket: this.bucket, Key: this.getKey(key), Body: stream })
.promise();
});
this.delete = (params) => __awaiter(this, void 0, void 0, function* () {
if (params) {
if (Array.isArray(params)) {
const deleteParams = {
Bucket: this.bucket,
Delete: { Objects: [] },
};
params.forEach((key) => {
deleteParams.Delete.Objects.push({ Key: this.getKey(key) });
});
return yield this.s3.deleteObjects(deleteParams).promise();
}
return yield this.s3
.deleteObject({
Key: this.getKey(params),
Bucket: this.bucket,
})
.promise();
}
return null;
});
this.copy = (src, dest) => __awaiter(this, void 0, void 0, function* () {
yield this.s3
.copyObject({
Key: dest,
Bucket: this.bucket,
CopySource: this.getKey(src),
})
.promise();
return {
dest,
};
});
const options = {
region: this.region,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
};
if (process.env.AWS_S3_ENDPOINT) {
options.endpoint = new aws_sdk_1.default.Endpoint(process.env.AWS_S3_ENDPOINT);
}
if (process.env.AWS_S3_FORCEPATHSTYLE) {
options.s3ForcePathStyle = Boolean(process.env.AWS_S3_FORCEPATHSTYLE);
}
this.s3 = new aws_sdk_1.default.S3(options);
}
}
exports.default = AwsS3FileDriver;