@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
71 lines (70 loc) • 3.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadFilePath = exports.uploadFileURL = exports.uploadFileBuffer = exports.getUploadFilePublicUrl = exports.getUploadFileStorageUrl = exports.listBuckets = exports.initStorage = void 0;
const client_s3_1 = require("@aws-sdk/client-s3");
const lib_storage_1 = require("@aws-sdk/lib-storage");
const image_1 = require("../../plugins/image");
const storage_helper_1 = require("./storage-helper");
function initStorage(storage) {
const endpoint = storage.provider === "do_space" ? (0, storage_helper_1.getStorageHost)(storage) : undefined;
const s3 = new client_s3_1.S3({
endpoint,
credentials: {
accessKeyId: storage.auth.key_id,
secretAccessKey: storage.auth.key_secret,
},
region: storage.region,
});
return s3;
}
exports.initStorage = initStorage;
async function listBuckets(storage) {
const s3 = initStorage(storage);
const response = await s3.listBuckets({});
return response.Buckets;
}
exports.listBuckets = listBuckets;
function getUploadFileStorageUrl(bucketName, destFileName) {
return `https://storage.googleapis.com/${bucketName}/${destFileName}`;
}
exports.getUploadFileStorageUrl = getUploadFileStorageUrl;
function getUploadFilePublicUrl(storageHost, destFileName) {
return `https://${storageHost}/${destFileName}`;
}
exports.getUploadFilePublicUrl = getUploadFilePublicUrl;
async function uploadFileBuffer(storage, buffer, destFileName, options) {
const s3 = initStorage(storage);
const uploadParams = {
Bucket: storage.bucket,
Key: destFileName,
Body: buffer,
ContentEncoding: options === null || options === void 0 ? void 0 : options.contentEncoding,
CacheControl: options === null || options === void 0 ? void 0 : options.cacheControl, // Set cache-control headers
};
await new lib_storage_1.Upload({
client: s3,
params: uploadParams,
}).done();
return {
path: destFileName,
storageUrl: getUploadFileStorageUrl(storage.bucket, destFileName),
publicUrl: (options === null || options === void 0 ? void 0 : options.storageHost)
? getUploadFilePublicUrl(options === null || options === void 0 ? void 0 : options.storageHost, destFileName)
: getUploadFileStorageUrl(storage.bucket, destFileName),
};
}
exports.uploadFileBuffer = uploadFileBuffer;
async function uploadFileURL(storage, url, destFileName, options) {
const buffer = await (0, image_1.getImageBufferFromUrl)(url);
if (!buffer)
throw new Error(`Unable to get image buffer from "${url}"`);
return uploadFileBuffer(storage, buffer, destFileName, options);
}
exports.uploadFileURL = uploadFileURL;
async function uploadFilePath(storage, filePath, destFileName, options) {
const buffer = (0, image_1.readFileToBuffer)(filePath);
if (!buffer)
throw new Error(`Unable to get image buffer from "${filePath}"`);
return uploadFileBuffer(storage, buffer, destFileName, options);
}
exports.uploadFilePath = uploadFilePath;