UNPKG

multi-cloud-upload

Version:

A Node.js package that provides an easy interface to upload and manage files on both Cloudinary and AWS S3. This package supports both JavaScript and TypeScript projects, making it versatile and easy to integrate into various environments.

254 lines (249 loc) 8.38 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { CloudinaryProvider: () => CloudinaryProvider, S3Provider: () => S3Provider, StorageFactory: () => StorageFactory }); module.exports = __toCommonJS(src_exports); // src/CloudinaryProvider.ts var import_cloudinary = require("cloudinary"); var CloudinaryProvider = class { constructor(config) { import_cloudinary.v2.config(config); } /** * Uploads a file to Cloudinary. * @param file - The file data as a Buffer. * @param fileName - The name to assign to the uploaded file. * @param context - Additional context or metadata related to the upload. * @returns A Promise that resolves to the public ID of the uploaded file. */ async upload(file, fileName, context = {}) { try { const result = await import_cloudinary.v2.uploader.upload(`data:image/png;base64,${file.toString("base64")}`, { public_id: fileName, ...context }); return result; } catch (error) { console.error("Error uploading file to Cloudinary:", error); throw error; } } /** * Downloads a file from Cloudinary. * @param fileName - The name of the file to download. * @param context - Additional context or metadata related to the download. * @returns A Promise that resolves to the file data as a Buffer. */ async download(fileName, context = {}) { try { const result = await import_cloudinary.v2.api.resource(fileName, { ...context }); return result; } catch (error) { console.error("Error downloading file from Cloudinary:", error); throw error; } } /** * Deletes a file from Cloudinary. * @param fileName - The name of the file to delete. * @param context - Additional context or metadata related to the deletion. * @returns A Promise that resolves when the delete operation completes. */ async delete(fileName, context = {}) { try { const result = await import_cloudinary.v2.uploader.destroy(fileName); return result; } catch (error) { console.error("Error deleting file from Cloudinary:", error); throw error; } } /** * Lists all files in Cloudinary. * @returns A Promise that resolves to an array of public IDs of the listed files. */ async list(context = {}) { try { const result = await import_cloudinary.v2.api.resources(); return result; } catch (error) { console.error("Error listing files from Cloudinary:", error); throw error; } } /** * Generates a URL for accessing a file in Cloudinary. * @param fileName - The name of the file for which to generate the URL. * @param context - Additional context or parameters related to the URL generation. * @returns A Promise that resolves to the URL of the file. */ getUrl(fileName, context = {}) { try { return Promise.resolve(import_cloudinary.v2.url(fileName, { ...context })); } catch (error) { console.error("Error generating URL from Cloudinary:", error); return Promise.reject(error); } } }; // src/S3Provider.ts var import_client_s3 = require("@aws-sdk/client-s3"); var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner"); var S3Provider = class { s3; bucket; constructor(config) { const s3Config = { credentials: { accessKeyId: config.accessKeyId, secretAccessKey: config.secretAccessKey }, region: config.region }; this.s3 = new import_client_s3.S3Client({ ...s3Config }); this.bucket = config.bucket; } /** * Uploads a file to the S3 bucket. * @param file - The file data as a Buffer. * @param fileName - The name of the file to be uploaded. * @param context - Additional context or metadata related to the upload. * @returns A Promise that resolves to the data returned by the S3 service. */ async upload(file, fileName, context = {}) { try { const command = new import_client_s3.PutObjectCommand({ Bucket: this.bucket, Key: fileName, Body: file, ...context }); const data = await this.s3.send(command); return data; } catch (error) { console.error("Error uploading file:", error); throw error; } } /** * Downloads a file from the S3 bucket. * @param fileName - The name of the file to download. * @param context - Additional context or metadata related to the download. * @returns A Promise that resolves to the file data, which will be a readable stream for S3. */ async download(fileName, context = {}) { try { const command = new import_client_s3.GetObjectCommand({ Bucket: this.bucket, Key: fileName, ...context }); const data = await this.s3.send(command); return data; } catch (error) { console.error("Error downloading file:", error); throw error; } } /** * Deletes a file from the S3 bucket. * @param fileName - The name of the file to delete. * @param context - Additional context or metadata related to the deletion. * @returns A Promise that resolves to the data returned by the S3 service after the delete operation. */ async delete(fileName, context = {}) { try { const command = new import_client_s3.DeleteObjectCommand({ Bucket: this.bucket, Key: fileName, ...context }); const data = await this.s3.send(command); console.log(data, "delete result"); return data; } catch (error) { console.error("Error deleting file:", error); throw error; } } /** * Lists all files in the S3 bucket. * @param context - Additional context or metadata related to the list operation. * @returns A Promise that resolves to an array of file names in the bucket. */ async list(context = {}) { try { const command = new import_client_s3.ListObjectsV2Command({ Bucket: this.bucket, ...context }); const data = await this.s3.send(command); return data.Contents?.map((object) => object.Key) || []; } catch (error) { console.error("Error listing files:", error); throw error; } } /** * Generates a pre-signed URL for accessing a file in the S3 bucket. * @param fileName - The name of the file for which to generate the URL. * @param context - Additional context or metadata related to the URL generation. Can include an expiration time (`expiresIn`). * @returns A Promise that resolves to the pre-signed URL. */ async getUrl(fileName, context = {}) { try { const command = new import_client_s3.GetObjectCommand({ Bucket: this.bucket, Key: fileName, ...context }); const expiresIn = context.expiresIn || 60 * 5; const url = await (0, import_s3_request_presigner.getSignedUrl)(this.s3, command, { expiresIn }); console.log(url, "get url only"); return url; } catch (error) { console.error("Error generating signed URL:", error); throw error; } } }; // src/StorageFactory.ts var StorageFactory = class { static createProvider(type, config) { switch (type) { case "cloudinary": return new CloudinaryProvider(config); case "s3": return new S3Provider(config); default: throw new Error(`Unsupported provider type: ${type}`); } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CloudinaryProvider, S3Provider, StorageFactory });