@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
97 lines (96 loc) • 4.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadFilePath = exports.uploadFileURL = exports.uploadFileBuffer = exports.getUploadFilePublicUrl = exports.getUploadFileStorageUrl = exports.listBuckets = exports.initStorage = exports.initStorageByServiceAccount = void 0;
const storage_1 = require("@google-cloud/storage");
const image_1 = require("../../plugins/image");
function initStorageByServiceAccount(serviceAccount, bucketName, options) {
if (!serviceAccount)
throw new Error(`Service Account data is required.`);
// console.log("env.GOOGLE_SERVICE_ACCOUNT :>> ", env.GOOGLE_SERVICE_ACCOUNT);
// console.log("env.GOOGLE_SERVICE_ACCOUNT.replace() :>> ", env.GOOGLE_SERVICE_ACCOUNT?.replace(/\\n/g, "\n"));
const googleSA = JSON.parse(serviceAccount);
// console.log("googleSA :>> ", googleSA);
// console.log("googleSA.project_id :>> ", googleSA.project_id);
const storage = new storage_1.Storage({
projectId: googleSA.project_id,
credentials: googleSA,
});
const bucket = storage.bucket(bucketName);
return bucket;
}
exports.initStorageByServiceAccount = initStorageByServiceAccount;
function initStorage(storage, options) {
var _a;
return initStorageByServiceAccount((_a = storage.auth) === null || _a === void 0 ? void 0 : _a.service_account, storage.bucket, options);
}
exports.initStorage = initStorage;
async function listBuckets(storage) {
var _a;
const googleSA = JSON.parse((_a = storage.auth) === null || _a === void 0 ? void 0 : _a.service_account);
const _storage = new storage_1.Storage({
projectId: googleSA.project_id,
credentials: googleSA,
});
const buckets = await _storage.getBuckets();
console.log("google storage > buckets :>> ", buckets);
return 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) {
// initialize storage bucket
const bucket = initStorage(storage);
const file = bucket.file(destFileName);
return new Promise((resolve, reject) => {
try {
// Create a writable stream and specify the format of the data to be written
const stream = file.createWriteStream({
resumable: false,
gzip: (options === null || options === void 0 ? void 0 : options.contentEncoding) === "gzip",
metadata: {
contentType: "application/octet-stream",
cacheControl: options === null || options === void 0 ? void 0 : options.cacheControl,
},
});
stream.on("error", (err) => {
reject(err);
});
stream.on("finish", () => {
resolve({
path: destFileName,
storageUrl: getUploadFileStorageUrl(bucket.name, destFileName),
publicUrl: (options === null || options === void 0 ? void 0 : options.storageHost)
? getUploadFilePublicUrl(options === null || options === void 0 ? void 0 : options.storageHost, destFileName)
: getUploadFileStorageUrl(bucket.name, destFileName),
});
});
// Write the buffer to the file
stream.end(buffer);
}
catch (err) {
reject(err);
}
});
}
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;