Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,151 @@
|
||||
import { Entity } from "@toolctrl/db/prisma-browser";
|
||||
import { RowWithDetails, updateRowMedia } from "~/utils/db/entities/rows.db.server";
|
||||
import { deleteFile, hasStorageCredentials, putBufferAsFile } from "~/utils/integrations/storageService";
|
||||
import { fetchOutboundHttpWithRedirects } from "~/utils/outboundHttp.server";
|
||||
|
||||
// Media sources come from client-supplied row JSON. A bare fetch() of a
|
||||
// caller-controlled URL is an SSRF vector (cloud metadata, internal services),
|
||||
// so only inline data: URIs are fetched directly; any remote URL is routed
|
||||
// through the SSRF-safe helper that validates the scheme and rejects
|
||||
// private/loopback/link-local/metadata addresses (incl. across redirects).
|
||||
async function fetchMediaSource(source: string): Promise<Blob> {
|
||||
const trimmed = (source ?? "").trim();
|
||||
if (trimmed.toLowerCase().startsWith("data:")) {
|
||||
return await (await fetch(trimmed)).blob();
|
||||
}
|
||||
const response = await fetchOutboundHttpWithRedirects(trimmed, {});
|
||||
return await response.blob();
|
||||
}
|
||||
|
||||
export async function storeRowMediaInStorageProvider(entity: Entity, row: RowWithDetails | null) {
|
||||
if (!row) {
|
||||
return;
|
||||
}
|
||||
const mediaRowValues = row.values.filter((f) => f.media && f.media.length > 0);
|
||||
return await Promise.all(
|
||||
mediaRowValues.map(async (mediaRowValue) => {
|
||||
if (mediaRowValue.media) {
|
||||
return await Promise.all(
|
||||
mediaRowValue.media
|
||||
.filter((f) => !f.publicUrl)
|
||||
.map(async (media) => {
|
||||
if (hasStorageCredentials()) {
|
||||
try {
|
||||
media.name = sanitizeFileName(media.name);
|
||||
const blob = await fetchMediaSource(media.publicUrl || media.file);
|
||||
const body = new Uint8Array(await blob.arrayBuffer());
|
||||
const key = media.id + "-" + media.name;
|
||||
const stored = await putBufferAsFile({ bucket: entity.name, key, body, contentType: blob.type || undefined });
|
||||
if (stored.publicUrl) {
|
||||
media.file = "";
|
||||
media.publicUrl = stored.publicUrl;
|
||||
media.storageBucket = entity.name;
|
||||
media.storageProvider = "s3";
|
||||
return await updateRowMedia({
|
||||
id: media.id,
|
||||
rowId: mediaRowValue.rowId,
|
||||
entityId: row.entityId,
|
||||
tenantId: row.tenantId,
|
||||
}, {
|
||||
file: "",
|
||||
publicUrl: stored.publicUrl,
|
||||
storageBucket: entity.name,
|
||||
storageProvider: "s3",
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("Could not store row media: " + e);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteRowMediaFromStorageProvider(row: RowWithDetails | null) {
|
||||
if (!row) {
|
||||
return;
|
||||
}
|
||||
const mediaRowValues = row.values.filter((f) => f.media && f.media.length > 0);
|
||||
return await Promise.all(
|
||||
mediaRowValues.map(async (mediaRowValue) => {
|
||||
if (mediaRowValue.media) {
|
||||
return await Promise.all(
|
||||
mediaRowValue.media.map(async (media) => {
|
||||
if (media.publicUrl && media.storageBucket && hasStorageCredentials()) {
|
||||
try {
|
||||
await deleteFile({ bucket: media.storageBucket, key: media.id + "-" + media.name });
|
||||
return await updateRowMedia({
|
||||
id: media.id,
|
||||
rowId: mediaRowValue.rowId,
|
||||
entityId: row.entityId,
|
||||
tenantId: row.tenantId,
|
||||
}, {
|
||||
file: "",
|
||||
publicUrl: "",
|
||||
storageBucket: "",
|
||||
storageProvider: "",
|
||||
});
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("Could not delete stored media: " + e);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// export async function getMediaFromZipFiles(zipFiles: MediaDto[]) {
|
||||
// let createdFiles: MediaDto[] = [];
|
||||
// await Promise.all(
|
||||
// zipFiles.map(async (item) => {
|
||||
// const filePath = "zip-uploads/" + item.name.replace(" ", "");
|
||||
// deleteFile(filePath);
|
||||
// const path = await saveZip(filePath, { type: item.type, content: item.file.split(",")[1] });
|
||||
// if (path) {
|
||||
// const folderPath = path + "-unzip";
|
||||
// deleteFolder(folderPath, { recursive: true, force: true });
|
||||
// const entries = await extractZip(path);
|
||||
// entries.forEach((entry) => {
|
||||
// if (entry.name.startsWith("._")) {
|
||||
// return;
|
||||
// }
|
||||
// var file = entry.getData().toString("base64");
|
||||
// let type = "";
|
||||
// if (entry.name.toLowerCase().endsWith("xml")) {
|
||||
// type = "text/xml";
|
||||
// } else if (entry.name.toLowerCase().endsWith("pdf")) {
|
||||
// type = "application/pdf";
|
||||
// }
|
||||
// createdFiles.push({
|
||||
// title: entry.name,
|
||||
// name: entry.name,
|
||||
// file: `data:${type};base64,${file}`,
|
||||
// type,
|
||||
// });
|
||||
// });
|
||||
// deleteFile(path);
|
||||
// }
|
||||
// deleteFile(filePath);
|
||||
// })
|
||||
// );
|
||||
// return createdFiles;
|
||||
// }
|
||||
|
||||
function sanitizeFileName(fileName: string): string {
|
||||
// Replace spaces with underscores
|
||||
const sanitized = fileName
|
||||
.replace(/\s+/g, "_")
|
||||
// Remove or replace other invalid characters as needed, e.g., commas, semicolons, etc.
|
||||
.replace(/[^a-zA-Z0-9._-]/g, "");
|
||||
// Optionally, you could add more specific replacements for known bad characters
|
||||
// .replace(/specific-bad-character/g, 'replacement')
|
||||
|
||||
return sanitized;
|
||||
}
|
||||
Reference in new issue
Block a user