UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

129 lines (128 loc) 5.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadFilePath = exports.uploadFileURL = exports.uploadFileBuffer = exports.listBuckets = exports.initStorage = exports.getCurrentStorage = void 0; const client_s3_1 = require("@aws-sdk/client-s3"); const process_1 = require("process"); const image_1 = require("../image"); const string_1 = require("../string"); const helper_1 = require("./helper"); function getCurrentStorage() { if (!process_1.env.CLOUDFLARE_CDN_BUCKET_NAME || !process_1.env.CLOUDFLARE_CDN_ACCESS_KEY || !process_1.env.CLOUDFLARE_CDN_SECRET_KEY) throw new Error("Cloudflare credentials are not defined"); return { provider: "cloudflare", region: "auto", bucket: process_1.env.CLOUDFLARE_CDN_BUCKET_NAME, accessKey: process_1.env.CLOUDFLARE_CDN_ACCESS_KEY, secretKey: process_1.env.CLOUDFLARE_CDN_SECRET_KEY, endpoint: process_1.env.CLOUDFLARE_CDN_ENDPOINT_URL, baseUrl: process_1.env.CLOUDFLARE_CDN_BASE_URL, basePath: `/${process_1.env.CLOUDFLARE_CDN_PROJECT_NAME}/${process_1.env.NODE_ENV}`, }; } exports.getCurrentStorage = getCurrentStorage; async function initStorage(storage) { const s3 = new client_s3_1.S3({ region: storage.region || "auto", endpoint: storage.endpoint, credentials: { accessKeyId: storage.accessKey, secretAccessKey: storage.secretKey, }, forcePathStyle: true, }); return s3; } exports.initStorage = initStorage; async function listBuckets(storage) { const s3 = await initStorage(storage); try { const response = await s3.listBuckets({}); console.log("storage-upload > listBuckets() > response :>>", response); return response.Buckets; } catch (error) { console.error("storage-upload > listBuckets() > error :>>", error); throw new Error(`Failed to list buckets: ${error instanceof Error ? error.message : String(error)}`); } } exports.listBuckets = listBuckets; async function uploadFileBuffer(buffer, destFileName, options) { const { storage = getCurrentStorage() } = options || {}; if (!storage) throw new Error("Storage is not defined"); if (destFileName.startsWith("/")) destFileName = destFileName.slice(1); // new file name const extensionMatch = destFileName.match(/\.[0-9a-z]+$/i); const extension = extensionMatch ? extensionMatch[0] : ""; const nameWithoutExtension = extension ? destFileName.slice(0, -extension.length) : destFileName; destFileName = `${(0, string_1.makeSlugByName)(nameWithoutExtension)}${extension}`; if (options === null || options === void 0 ? void 0 : options.debug) console.log("uploadFileBuffer :>>", { storage }); const s3 = await initStorage(storage); const mimeType = (0, helper_1.guessMimeTypeByBuffer)(buffer); if (options === null || options === void 0 ? void 0 : options.debug) console.log("uploadFileBuffer :>>", { mimeType }); const path = `${storage.basePath && storage.basePath.startsWith("/") ? storage.basePath.slice(1) : storage.basePath}/${destFileName}`; if (options === null || options === void 0 ? void 0 : options.debug) console.log("uploadFileBuffer :>>", { path }); const uploadParams = { Bucket: storage.bucket, Key: path, Body: buffer, ContentType: mimeType, CacheControl: (options === null || options === void 0 ? void 0 : options.cacheControl) || "max-age=31536000, s-maxage=31536000", ContentEncoding: options === null || options === void 0 ? void 0 : options.contentEncoding, // Set file to be gzip-encoded }; if (options === null || options === void 0 ? void 0 : options.debug) console.log("uploadFileBuffer :>>", { uploadParams }); // process upload try { // New SDK: // const command = new PutObjectCommand(uploadParams); // const data = await s3.send(command); // Old SDK: const data = await s3.putObject(uploadParams); // Even older SDK: // const data = await new Upload({ // client: s3, // params: uploadParams, // }).done(); if (options === null || options === void 0 ? void 0 : options.debug) console.log("uploadFileBuffer :>>", { data }); return { provider: storage.provider, path, storageUrl: (0, helper_1.getUploadFileOriginEndpointUrl)(storage, destFileName), publicUrl: (0, helper_1.getUploadFilePublicUrl)(storage, destFileName), }; } catch (error) { if (error instanceof Error) { console.error("Upload error:", error.message); // Log additional details if available if ("code" in error) { console.error("Error code:", error.code); } } throw error; } } exports.uploadFileBuffer = uploadFileBuffer; async function uploadFileURL(url, destFileName, options) { const buffer = await (0, image_1.getImageBufferFromUrl)(url); if (options === null || options === void 0 ? void 0 : options.debug) console.log("uploadFileURL :>>", { buffer }); if (!buffer) throw new Error(`Unable to get image buffer from "${url}"`); return uploadFileBuffer(buffer, destFileName, options); } exports.uploadFileURL = uploadFileURL; async function uploadFilePath(filePath, destFileName, options) { const buffer = (0, image_1.readFileToBuffer)(filePath); if (!buffer) throw new Error(`Unable to get image buffer from "${filePath}"`); return uploadFileBuffer(buffer, destFileName, options); } exports.uploadFilePath = uploadFilePath;