@sastatesla/payment-gateway-sdk
Version:
A unified interface for integrating multiple payment providers (e.g., Razorpay, Cashfree) in your Node.js application.
132 lines • 5.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DOSpacesProvider = void 0;
const aws_sdk_1 = __importDefault(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 DOSpacesProvider extends storageProvider_1.StorageProvider {
constructor(spacesConfig, cloudStorageConfig) {
super();
this.spacesConfig = spacesConfig;
this.cloudStorageConfig = cloudStorageConfig;
this.bucketName = spacesConfig.bucketName;
this.allowedFileTypes = cloudStorageConfig.allowedFileTypes;
this.s3 = new aws_sdk_1.default.S3({
endpoint: spacesConfig.endpoint,
region: spacesConfig.region,
accessKeyId: spacesConfig.accessKeyId,
secretAccessKey: spacesConfig.secretAccessKey,
signatureVersion: "v4"
});
}
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}.${this.spacesConfig.endpoint}/${destination}`,
provider: "do-spaces"
};
}
catch (err) {
throw new error_1.StorageError(`DO Spaces upload failed: ${err.message}`, "DO_SPACES_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}.${this.spacesConfig.endpoint}/${destination}`,
provider: "do-spaces"
};
}
catch (err) {
throw new error_1.StorageError(`DO Spaces uploadBuffer failed: ${err.message}`, "DO_SPACES_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(`DO Spaces delete failed: ${err.message}`, "DO_SPACES_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(`DO Spaces createFolder failed: ${err.message}`, "DO_SPACES_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.DOSpacesProvider = DOSpacesProvider;
//# sourceMappingURL=doSpacesProvider.js.map