UNPKG

@sastatesla/payment-gateway-sdk

Version:

A unified interface for integrating multiple payment providers (e.g., Razorpay, Cashfree) in your Node.js application.

130 lines 5.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.S3Provider = void 0; const aws_sdk_1 = require("aws-sdk"); const storageProvider_1 = require("./storageProvider"); const error_1 = require("../utils/error"); const path_1 = __importDefault(require("path")); const crypto_1 = __importDefault(require("crypto")); const inputParser_1 = require("../utils/inputParser"); const helpers_1 = __importDefault(require("../helpers/helpers")); class S3Provider extends storageProvider_1.StorageProvider { constructor(s3Config, cloudStorageConfig) { super(); this.s3Config = s3Config; this.cloudStorageConfig = cloudStorageConfig; this.bucketName = s3Config.bucketName; this.allowedFileTypes = cloudStorageConfig.allowedFileTypes; this.s3 = new aws_sdk_1.S3({ region: s3Config.region, accessKeyId: s3Config.accessKeyId, secretAccessKey: s3Config.secretAccessKey }); } async upload(filePath, options) { const parsedOptions = (0, inputParser_1.parseFileUploadOptions)(options); try { if (this.allowedFileTypes) { const mimeType = (0, helpers_1.default)(filePath); if (!mimeType || !this.allowedFileTypes.includes(mimeType)) { throw new error_1.StorageError(`Invalid file type: ${mimeType}`); } } const destination = options?.destination || path_1.default.basename(filePath); const fileContent = require("fs").readFileSync(filePath); const uploadParams = { Bucket: this.bucketName, Key: destination, Body: fileContent, ContentType: (0, helpers_1.default)(filePath) || undefined, Metadata: options?.metadata }; await this.s3.putObject(uploadParams).promise(); return { id: destination, url: `https://${this.bucketName}.s3.${this.s3Config.region}.amazonaws.com/${destination}`, provider: "s3" }; } catch (err) { throw new error_1.StorageError(`S3 upload failed: ${err.message}`, "S3_UPLOAD_ERROR", err); } } async uploadBuffer(file, options) { try { if (this.allowedFileTypes && (!file.mimetype || !this.allowedFileTypes.includes(file.mimetype))) { throw new error_1.StorageError(`Invalid file type: ${file.mimetype}`); } const extension = path_1.default.extname(file.originalname); const randomName = crypto_1.default.randomBytes(16).toString("hex") + extension; const destination = options?.destination || randomName; const uploadParams = { Bucket: this.bucketName, Key: destination, Body: file.buffer, ContentType: file.mimetype, Metadata: options?.metadata }; await this.s3.putObject(uploadParams).promise(); return { id: destination, url: `https://${this.bucketName}.s3.${this.s3Config.region}.amazonaws.com/${destination}`, provider: "s3" }; } catch (err) { throw new error_1.StorageError(`S3 uploadBuffer failed: ${err.message}`, "S3_UPLOAD_BUFFER_ERROR", err); } } async uploadBulk(filePaths, options) { return Promise.all(filePaths.map((filePath) => this.upload(filePath, options))); } async uploadBulkBuffer(files, options) { return Promise.all(files.map((file) => this.uploadBuffer(file, options))); } async delete(fileId) { try { await this.s3 .deleteObject({ Bucket: this.bucketName, Key: fileId }) .promise(); } catch (err) { throw new error_1.StorageError(`S3 delete failed: ${err.message}`, "S3_DELETE_ERROR", err); } } async deleteBulk(fileIds) { return Promise.all(fileIds.map((id) => this.delete(id))); } async createFolder(folderName) { try { const folderKey = folderName.replace(/\/$/, "") + "/"; await this.s3 .putObject({ Bucket: this.bucketName, Key: folderKey, Body: "" }) .promise(); } catch (err) { throw new error_1.StorageError(`S3 createFolder failed: ${err.message}`, "S3_CREATEFOLDER_ERROR", err); } } async uploadToFolder(folderName, filePath, options) { const fileName = path_1.default.basename(options?.destination ?? filePath); const destination = `${folderName.replace(/\/$/, "")}/${fileName}`; return this.upload(filePath, { ...options, destination }); } async uploadBufferToFolder(folderName, file, options) { const fileName = options?.destination ?? file.originalname; const destination = `${folderName.replace(/\/$/, "")}/${fileName}`; return this.uploadBuffer(file, { ...options, destination }); } } exports.S3Provider = S3Provider; //# sourceMappingURL=s3Provider.js.map