82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
// components/admin/utils/adminStorage.js
|
|
|
|
/**
|
|
* Get the folder path up to the last slash.
|
|
* Used for grouping blobs by "case folder".
|
|
*/
|
|
export function getUpToLastSlash(url) {
|
|
let lastSlash = url.lastIndexOf("/");
|
|
|
|
url = url.substring(0, lastSlash);
|
|
|
|
url =
|
|
url.lastIndexOf("/files") > 0
|
|
? url.substring(0, url.lastIndexOf("/files"))
|
|
: url;
|
|
return url;
|
|
}
|
|
|
|
/**
|
|
* Convert a stream (Azure blob download) to a string.
|
|
*/
|
|
export async function streamToString(readableStream) {
|
|
return new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
readableStream.on("data", (data) => {
|
|
chunks.push(data.toString());
|
|
});
|
|
readableStream.on("end", () => {
|
|
resolve(chunks.join(""));
|
|
});
|
|
readableStream.on("error", reject);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Format how long ago a date occurred.
|
|
*/
|
|
export function formatTimeSince(date) {
|
|
if (!date) return "Unknown date";
|
|
|
|
const now = new Date();
|
|
const diffMs = now - date;
|
|
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
|
|
if (diffDays < 7) {
|
|
return `${diffDays} day${diffDays !== 1 ? "s" : ""} ago`;
|
|
} else {
|
|
const diffWeeks = Math.floor(diffDays / 7);
|
|
return `${diffWeeks} week${diffWeeks !== 1 ? "s" : ""} ago`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Format Azure blob created/modified dates into a readable string.
|
|
*/
|
|
export function formatBlobDates(blob) {
|
|
const created = blob.createdOn ? new Date(blob.createdOn) : null;
|
|
const modified = blob.lastModified ? new Date(blob.lastModified) : null;
|
|
|
|
if (!created && !modified) return "Unknown";
|
|
|
|
// If both exist and are the same
|
|
if (created && modified && created.getTime() === modified.getTime()) {
|
|
return `${created.toLocaleString("en-GB")} (${formatTimeSince(
|
|
created
|
|
)})`;
|
|
}
|
|
|
|
// If modified exists and is newer than created
|
|
if (created && modified && modified.getTime() > created.getTime()) {
|
|
return `Created: ${created.toLocaleString("en-GB")} (${formatTimeSince(
|
|
created
|
|
)}), Modified: ${modified.toLocaleString("en-GB")} (${formatTimeSince(
|
|
modified
|
|
)})`;
|
|
}
|
|
|
|
// Only one date exists
|
|
const date = modified || created;
|
|
return `${date.toLocaleString("en-GB")} (${formatTimeSince(date)})`;
|
|
}
|