lexica-dialog-core
Version:
Core server for Lexica dialog agent.
50 lines • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const https = require("https");
const http = require("http");
const AWS = require("aws-sdk");
const uuid = require("uuid");
const lodash_1 = require("lodash");
class S3Service {
constructor(s3Params) {
this.s3Params = s3Params;
this.s3 = new AWS.S3({
apiVersion: s3Params.apiVersion,
});
}
copy(path) {
return new Promise((resolve, reject) => {
if (path.startsWith('http') || path.startsWith('https')) {
const s3Key = uuid.v4();
const get = path.startsWith('https') ? https.get : http.get;
get(path, async (res) => {
try {
const headerContentType = res.headers['content-type'];
const contentType = lodash_1.isString(headerContentType) ? headerContentType : 'application/octet-stream';
await this.s3
.upload({
Body: res,
Bucket: this.s3Params.bucket,
ContentType: contentType,
Key: s3Key,
})
.promise();
resolve({
contentType,
path: s3Key,
});
}
catch (error) {
reject(error);
}
}).on('error', error => reject(error));
}
else {
reject(new Error('Unsupported path for copy file: ' + path));
}
});
}
}
exports.S3Service = S3Service;
exports.default = S3Service;
//# sourceMappingURL=S3Service.js.map