Files
pedwfrontend/actions/azurestorage.js
T

311 lines
9.5 KiB
JavaScript

import { v4 as uuidv4 } from "uuid";
import { BlobServiceClient, ContainerClient } from "@azure/storage-blob";
import {
DefaultAzureCredential,
InteractiveBrowserCredential,
EnvironmentCredential,
ClientSecretCredential,
} from "@azure/identity";
import { consoleLogger, hashAPIPath } from ".";
import _ from "lodash";
const STORAGE_PATH = process.env.AZURE_PEDW_STORAGE_ENDPOINT;
const STORAGE_CONTAINER = process.env.AZURE_PEDW_CONTAINER;
export const createContainer = async (containerName) => {
const creds = new DefaultAzureCredential();
//containerName = containerName.toLowerCase();
// console.log(
// "container name:",
// containerName,
// STORAGE_PATH + "/" + containerName
// );
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
const blobServiceClient = new BlobServiceClient(`${STORAGE_PATH}`, creds);
const createContainerResponse = await containerClient.createIfNotExists();
// console.log(
// `Created container ${containerName} successfully`,
// createContainerResponse.requestId
// );
// console.log("Containers:");
// for await (const container of blobServiceClient.listContainers()) {
// console.log(`- ${container.name}`);
// }
console.log("\n//////////////////\n container name :", containerName);
return containerName;
};
export const getContainers = async () => {
const creds = new DefaultAzureCredential();
const blobServiceClient = new BlobServiceClient(`${STORAGE_PATH}`, creds);
console.log("Containers:");
for await (const container of blobServiceClient.listContainers()) {
console.log(`- ${container.name}`);
}
};
export const getBlobs = async (containerName, casefolderID) => {
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
containerClient.createIfNotExists();
const blobObj = [];
for await (const blob of containerClient.listBlobsFlat({
prefix: casefolderID + "/files/",
})) {
let blobDocumentType = blob.name
.split("/")[2]
.slice(0, blob.name.split("/")[2].indexOf("_"));
blobObj.push({
"name": blob.name.split("/")[2],
"path": blob.name,
"documentType": blobDocumentType,
"versionId": blob.versionId,
"isCurrentVersion": blob.isCurrentVersion,
"contentLength": blob.properties.contentLength,
"contentType": blob.contentType,
"lastModified": blob.properties.lastModified,
"filepath":
"/api/file/downloadblob?container=" +
containerName +
"&casefolderID=" +
casefolderID +
"&blobname=" +
blob.name.split("/")[2],
"hashedfilepath": hashAPIPath(
"/api/file/downloadblob?container=" +
containerName +
"&casefolderID=" +
casefolderID +
"&blobname=" +
blob.name.split("/")[2]
),
"deletepath":
"/api/file/deleteblob?container=" +
containerName +
"&casefolderID=" +
casefolderID +
"&blobname=" +
blob.name.split("/")[2],
"hasheddeletepath": hashAPIPath(
"/api/file/deleteblob?container=" +
containerName +
"&casefolderID=" +
casefolderID +
"&blobname=" +
blob.name.split("/")[2]
),
"hashgetblobs": hashAPIPath(
"/api/file/getbloblist?container=" +
containerName +
"&casefolderID=" +
casefolderID
),
});
}
console.log("blobObj:", blobObj);
return blobObj;
};
export const createBlob = async (formContent, containerName, caseref) => {
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
formContent = JSON.parse(formContent);
let caseID = formContent.pinswg_name;
const content = JSON.stringify(formContent);
const blobName = caseID + "/" + caseID + ".json";
console.log("blobName:", blobName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(
content,
Buffer.byteLength(content)
);
return formContent.pinswg_name;
};
export const deleteBlob = async (containerName, blobName) => {
const creds = new DefaultAzureCredential();
const options = {
deleteSnapshots: "include", // or 'only'
};
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName.toLowerCase()}`,
creds
);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.deleteIfExists(options);
console.log(`deleted blob ${blobName}`);
return { "deleted": blobName };
};
export const uploadFile = async (formContent, containerName, foldername) => {
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
const files = formContent;
//console.log(...formContent);
console.log("files...", files, Object.keys(files).length, foldername);
for (const prop in files) {
console.log(`files[${prop}] = ${files[prop][0].size}`);
const blobName = foldername + "/files/" + files[prop][0].fieldName;
console.log(blobName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.uploadFile(
files[prop][0].path,
files[prop].size
);
console.log(
`Uploaded block blob ${files[prop][0].fieldName} successfully`,
uploadBlobResponse.requestId
);
}
};
export const downloadFile = async (containerName, blobName) => {
//console.log.apply(containerName, blobName);
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
const blobClient = containerClient.getBlobClient(blobName);
const downloadedBlob = await blobClient.download(0);
const downloaded = await streamToBuffer(downloadedBlob.readableStreamBody);
return downloaded;
};
export const downloadProgressFile = async (
containerName,
blobName,
casefolderID
) => {
//console.log.apply(containerName, blobName);
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
const blobClient = containerClient.getBlobClient(blobName);
console.log("herher:", containerName, blobName, casefolderID);
const downloadedBlob = await blobClient.download(0);
const downloaded = await streamToBuffer(downloadedBlob.readableStreamBody);
return downloaded;
};
const streamToBuffer = async (readableStream) => {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
readableStream.on("error", reject);
});
};
export const getProgressBlobs = async (containerName, caseReference) => {
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
containerClient.createIfNotExists();
console.log("thisi s the caasefolder:", caseReference);
let blobObj = [];
for await (const blob of containerClient.listBlobsFlat({
prefix: caseReference + "/" + caseReference + ".json",
})) {
blobObj.push({
"name": blob.name.split("/")[1],
"path": blob.name,
"versionId": blob.versionId,
"isCurrentVersion": blob.isCurrentVersion,
"contentLength": blob.properties.contentLength,
"contentType": blob.contentType,
"lastModified": blob.properties.lastModified,
"hashedfilepath": hashAPIPath(
"/api/file/downloadblob?container=" +
containerName +
"&casefolderID=" +
caseReference +
"&blobname=" +
blob.name.split("/")[1]
),
"hasheddeletepath": hashAPIPath(
"/api/file/deleteblob?container=" +
containerName +
"&casefolderID=" +
caseReference +
"&blobname=" +
blob.name.split("/")[1]
),
"hashgetblobs": hashAPIPath(
"/api/file/getbloblist?container=" +
containerName +
"&casefolderID=" +
caseReference
),
});
}
blobObj = _.sortBy(blobObj, [
function (o) {
return o.lastModified;
},
]).reverse()[0];
console.log("blobObj:", blobObj);
return blobObj;
};