chore: change dir (prepare to merge)
This commit is contained in:
parent
f3078c47ea
commit
7c55806956
31 changed files with 0 additions and 0 deletions
74
Services/server/src/utils/minio.ts
Normal file
74
Services/server/src/utils/minio.ts
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import { EhrFolder } from "../interfaces/ehr-fs";
|
||||
import minioClient from "../storage";
|
||||
|
||||
/**
|
||||
* Remove slash at the start and ensure slash at the end of the path
|
||||
* @param path - path to be check and ensure
|
||||
* @returns path without / at start and end with trailing slash
|
||||
*/
|
||||
function safePath(path: string) {
|
||||
return path.replace(/^\/|\/$/g, "") + "/";
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace illegal character eg. ? % < > / \ : | that can't be in path with "-".
|
||||
* @param path - string to check and replace
|
||||
* @returns path with illegal character replaced with "-"
|
||||
*/
|
||||
export function replaceIllegalChars(path: string, replaceChar = "-") {
|
||||
return path.replace(/[/\\?%*:|"<>]/g, replaceChar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to check for .keep file if it is exist or not.
|
||||
* @returns true if .keep exist, false otherwise
|
||||
*/
|
||||
export async function pathExist(path: string): Promise<boolean> {
|
||||
return await minioClient
|
||||
.statObject("ehr", `${safePath(path)}.keep`)
|
||||
.then((_) => true)
|
||||
.catch((e) => {
|
||||
if (e.code === "NotFound") return false;
|
||||
throw new Error("Object Storage Error");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to list folder by using .keep file with prefix in minio.
|
||||
* @param path - path to list
|
||||
* @return list of folder with metadata
|
||||
*/
|
||||
export function listFolder(path?: string): Promise<EhrFolder[]> {
|
||||
if (path) path = safePath(path);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const folder: EhrFolder[] = [];
|
||||
|
||||
const stream = minioClient.listObjectsV2("ehr", path ?? "");
|
||||
|
||||
stream.on("data", (v) => {
|
||||
if (!(v && v.prefix)) return;
|
||||
|
||||
folder.push({
|
||||
pathname: v.prefix,
|
||||
name: v.prefix.slice(path?.length).split("/")[0],
|
||||
createdAt: "N/A",
|
||||
createdBy: "N/A",
|
||||
});
|
||||
});
|
||||
|
||||
stream.on("end", async () => {
|
||||
for (let i = 0; i < folder.length; i++) {
|
||||
const stat = await minioClient.statObject("ehr", `${folder[i].pathname}.keep`);
|
||||
folder[i] = {
|
||||
...folder[i],
|
||||
createdAt: stat.metaData.createdat ?? "N/A",
|
||||
createdBy: stat.metaData.createdby ?? "N/A",
|
||||
};
|
||||
}
|
||||
resolve(folder);
|
||||
});
|
||||
|
||||
stream.on("error", () => reject(new Error("Object storage error occured.")));
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue