Files
pedwfrontend/actions/azurestorage.js
T
2022-09-12 15:54:55 +01:00

470 lines
14 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 deleteBlobCase = async (containerName, blobName) => {
const creds = new DefaultAzureCredential();
const options = {
deleteSnapshots: "include", // or 'only'
};
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
console.log("blob to delete:", blobName);
for await (const blob of containerClient.listBlobsFlat({
prefix: blobName,
})) {
console.log(" ------ :", blob.name);
containerClient.deleteBlob(blob.name);
}
containerClient.deleteBlob(blobName);
console.log(`deleted blob ${blobName}`);
for await (const blob of containerClient.listBlobsFlat()) {
console.log(" ------ :", blob.name);
}
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);
const downloadedBlob = await blobClient.download(0);
const downloaded = await streamToBuffer(downloadedBlob.readableStreamBody);
return downloaded;
};
export const downloadAllProgressFiles = async (
containerName,
progressBlobObj
) => {
//console.log(progressBlobObj);
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
let blobClient = {};
let downloadedBlob = {};
let downloaded = "";
let blobCount = 0;
for (const prop in progressBlobObj) {
//console.log(`${prop}: ${progressBlobObj[prop].path}`);
blobClient = containerClient.getBlobClient(progressBlobObj[prop].path);
downloadedBlob = await blobClient.download(0);
downloaded =
downloaded +
(await streamToBuffer(downloadedBlob.readableStreamBody)) +
",";
blobCount++;
}
downloaded = downloaded.substring(0, downloaded.length - 1);
//console.log(downloaded);
return '{ "@odata.count": ' + blobCount + ',"value": [' + 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 getCaseBlob = async (
containerName,
caseReference,
formContent
) => {
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
console.log("have got to create case");
containerClient.createIfNotExists();
let blobCount = 0;
for await (const blob of containerClient.listBlobsFlat({
prefix: caseReference,
})) {
blobCount++;
}
const content = JSON.stringify(formContent);
const blobName = caseReference + "/case/" + caseReference + ".json";
console.log("blobName:", blobName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobResponse = await blockBlobClient.upload(
content,
Buffer.byteLength(content)
);
return blobName;
};
export const getProgressBlobs = async (containerName, caseReference) => {
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
containerClient.createIfNotExists();
let blobCount = 0;
for await (const blob of containerClient.listBlobsFlat({
prefix: caseReference,
})) {
blobCount++;
}
//console.log("this is the caasefolder:", caseReference, blobCount);
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("blobObjwwwww:", blobObj);
return blobObj;
};
export const getAllProgressBlobs = async (containerName) => {
const creds = new DefaultAzureCredential();
const containerClient = new ContainerClient(
`${STORAGE_PATH}/${containerName}`,
creds
);
containerClient.createIfNotExists();
let blobCount = 0;
for await (const blob of containerClient.listBlobsFlat({
// prefix: caseReference,
})) {
blobCount++;
}
//console.log("this is the caasefolder:", caseReference, blobCount);
let blobObj = [];
for await (const blob of containerClient.listBlobsFlat({
//prefix: caseReference + "/" + caseReference + ".json",
})) {
blob.name.split("/")[1].indexOf(".json") > 0 &&
blob.name.split("/")[1].indexOf("undefined") < 0 &&
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 +
// ),
// "hasheddeletepath": hashAPIPath(
// "/api/file/deleteblob?container=" +
// containerName +
// ),
// "hashgetblobs": hashAPIPath(
// "/api/file/getbloblist?container=" +
// containerName +
// ),
});
}
//console.log("blobObj:", blobObj);
return blobObj;
};