UNPKG

react-s3-typescript

Version:

A npm package to upload your files into AWS S3 Bucket directly using react

164 lines 7.28 kB
"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 short_uuid_1 = __importDefault(require("short-uuid")); const ErrorThrower_1 = require("./ErrorThrower"); const Url_1 = __importDefault(require("./Url")); const client_s3_1 = require("@aws-sdk/client-s3"); const type_teller_1 = require("type-teller"); const lib_storage_1 = require("@aws-sdk/lib-storage"); class ReactS3Client { constructor(config) { this.config = config; } uploadFile(file, newFileName) { return __awaiter(this, void 0, void 0, function* () { (0, ErrorThrower_1.throwUploadError)(this.config, file); let fileExtension = ''; let mimType = ''; if (file instanceof File) { if (file.name) { fileExtension = file.name.split('.').pop() || ''; } if (!fileExtension && file.type != null) { fileExtension = file.type.split('/').pop() || ''; } } if (Buffer.isBuffer(file)) { const fileType = yield (0, type_teller_1.detectFileType)(file); fileExtension = (fileType === null || fileType === void 0 ? void 0 : fileType.ext) || ''; mimType = (fileType === null || fileType === void 0 ? void 0 : fileType.mime) || ''; } const fileName = `${newFileName || short_uuid_1.default.generate()}${fileExtension && '.' + fileExtension}`; const dirName = (this.config.dirName ? this.config.dirName + '/' : '').replace(/([^:]\/)\/+/g, '$1'); const key = `${dirName}${fileName}`; const url = (0, Url_1.default)(this.config); const client = new client_s3_1.S3Client({ region: this.config.region, credentials: { accessKeyId: this.config.accessKeyId, secretAccessKey: this.config.secretAccessKey, } }); const params = { Bucket: this.config.bucketName, Key: key, Body: file, Metadata: { uuid: "14365123651274", tag: "", }, ContentType: file instanceof File ? file.type : mimType }; const command = new client_s3_1.PutObjectCommand(params); const data = yield client.send(command); if (data["$metadata"]["httpStatusCode"] !== 200) return Promise.reject(data); return Promise.resolve({ bucket: this.config.bucketName, key, location: `${url}/${key}`, status: 0, }); }); } uploadWithProgress(file, progressCallback, newFileName) { return __awaiter(this, void 0, void 0, function* () { (0, ErrorThrower_1.throwUploadError)(this.config, file); let fileExtension = ''; let mimType = ''; if (file instanceof File) { if (file.name) { fileExtension = file.name.split('.').pop() || ''; } if (!fileExtension && file.type != null) { fileExtension = file.type.split('/').pop() || ''; } } if (Buffer.isBuffer(file)) { const fileType = yield (0, type_teller_1.detectFileType)(file); fileExtension = (fileType === null || fileType === void 0 ? void 0 : fileType.ext) || ''; mimType = (fileType === null || fileType === void 0 ? void 0 : fileType.mime) || ''; } const fileName = `${newFileName || short_uuid_1.default.generate()}${fileExtension && '.' + fileExtension}`; const dirName = (this.config.dirName ? this.config.dirName + '/' : '').replace(/([^:]\/)\/+/g, '$1'); const key = `${dirName}${fileName}`; const url = (0, Url_1.default)(this.config); const client = new client_s3_1.S3Client({ region: this.config.region, credentials: { accessKeyId: this.config.accessKeyId, secretAccessKey: this.config.secretAccessKey, } }); const params = { Bucket: this.config.bucketName, Key: key, Body: file, Metadata: { uuid: "14365123651274", tag: "", }, ContentType: file instanceof File ? file.type : mimType }; const upload = new lib_storage_1.Upload({ client: client, params: params }); upload.on("httpUploadProgress", progress => { progressCallback(Number(progress.loaded) / Number(progress.total), progress); }); const data = yield upload.done(); if (data["$metadata"]["httpStatusCode"] !== 200) return Promise.reject(data); return Promise.resolve({ bucket: this.config.bucketName, key: data.Key || key, location: data.Location || url, status: 0, }); }); } deleteFile(key) { return __awaiter(this, void 0, void 0, function* () { const client = new client_s3_1.S3Client({ region: this.config.region, credentials: { accessKeyId: this.config.accessKeyId, secretAccessKey: this.config.secretAccessKey, } }); const params = { Bucket: this.config.bucketName, Key: key, }; const deleteCommand = new client_s3_1.DeleteObjectCommand(params); return new Promise((resolve, reject) => { client.send(deleteCommand, (err) => { if (err) { reject(err); } else { resolve({ message: "File Deleted Successfully!", key: key }); } }); }); }); } } exports.default = ReactS3Client; //# sourceMappingURL=react-aws-s3.js.map