smart-file-upload
Version:
Smart file upload
90 lines • 3.54 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3Service = void 0;
const client_s3_1 = require("@aws-sdk/client-s3");
const singleton_1 = require("./config/singleton");
const fs = __importStar(require("fs/promises"));
const path_service_1 = require("./common/path.service");
class S3Service {
constructor() {
this.maxPartSize = +process.env.MAX_PART_SIZE * 1000000;
this.s3Client = singleton_1.SingletonS3Client.get();
this.bucketName = process.env.S3_BUCKET_NAME;
this.pathService = new path_service_1.PathService();
this.parts = [];
}
async upload(fileName) {
await this.createMultiPartUpload(fileName);
await this.uploadPart(fileName);
await this.completeMultiPartUpload(fileName);
}
async createMultiPartUpload(fileName) {
const input = {
Bucket: this.bucketName,
Key: fileName,
};
const command = new client_s3_1.CreateMultipartUploadCommand(input);
const output = await this.s3Client.send(command);
this.uploadId = output.UploadId;
}
async uploadPart(fileName) {
const buffer = await fs.readFile(this.pathService.getFilePath(fileName));
const n = Math.ceil(buffer.length / this.maxPartSize);
const promises = [];
for (let i = 0; i < n; i++) {
const start = i * this.maxPartSize;
const end = start + this.maxPartSize;
const input = {
Bucket: this.bucketName,
Key: fileName,
Body: buffer.slice(start, end),
PartNumber: i + 1,
UploadId: this.uploadId,
};
const command = new client_s3_1.UploadPartCommand(input);
promises.push(this.s3Client.send(command));
}
const output = await Promise.all(promises);
for (let i = 0; i < output.length; i++) {
this.parts.push({
ETag: output[i].ETag,
PartNumber: i + 1,
});
}
}
async completeMultiPartUpload(fileName) {
const input = {
Bucket: this.bucketName,
Key: fileName,
UploadId: this.uploadId,
MultipartUpload: { Parts: this.parts },
};
const command = new client_s3_1.CompleteMultipartUploadCommand(input);
await this.s3Client.send(command);
}
}
exports.S3Service = S3Service;
//# sourceMappingURL=s3.service.js.map