Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+82
@@ -0,0 +1,82 @@
|
||||
import { buildFileStorageKeys } from "@hivegrid/storage";
|
||||
import type { FormDefinition } from "@hivegrid/form-widget";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { enqueueFilePreview } from "~/utils/integrations/filePreviewQueue.server";
|
||||
import { enqueueFileScan } from "~/utils/integrations/fileScanQueue.server";
|
||||
import { putBufferAsFile, resolveStorageBucket } from "~/utils/integrations/storageService";
|
||||
|
||||
const FORM_FILE_MAX_BYTES = 25 * 1024 * 1024;
|
||||
|
||||
export type PreparedFormFile = {
|
||||
fieldKey: string;
|
||||
fileId: string;
|
||||
kind: "file" | "photo";
|
||||
};
|
||||
|
||||
function isSubmittedFile(value: FormDataEntryValue | null): value is File {
|
||||
return typeof File !== "undefined" && value instanceof File && value.size > 0;
|
||||
}
|
||||
|
||||
export async function prepareUploadedFiles(input: { tenantId: string; formBody: FormData; fields: FormDefinition["fields"] }): Promise<PreparedFormFile[]> {
|
||||
const prepared: PreparedFormFile[] = [];
|
||||
for (const field of input.fields) {
|
||||
if (field.type !== "file" && field.type !== "photo") continue;
|
||||
const file = input.formBody.get(field.key);
|
||||
if (!isSubmittedFile(file)) continue;
|
||||
const bucket = resolveStorageBucket();
|
||||
const maxBytes = Math.min((field.validationRules?.maxFileSizeMb ?? 25) * 1024 * 1024, FORM_FILE_MAX_BYTES);
|
||||
if (file.size > maxBytes) {
|
||||
throw new Error(`file-too-large:${field.key}`);
|
||||
}
|
||||
const accepted = field.type === "photo" ? ["image/"] : field.validationRules?.acceptedFileTypes;
|
||||
if (accepted && accepted.length > 0) {
|
||||
const allowed = accepted.some((mime) => (mime.endsWith("/") ? file.type.startsWith(mime) : file.type === mime));
|
||||
if (!allowed) {
|
||||
throw new Error(`file-type-blocked:${field.key}`);
|
||||
}
|
||||
}
|
||||
const created = await db.file.create({
|
||||
data: {
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: "system:form",
|
||||
displayName: file.name.slice(0, 255),
|
||||
mimeType: file.type || "application/octet-stream",
|
||||
sizeBytes: file.size,
|
||||
storageKey: "",
|
||||
source: "form_submission",
|
||||
previewStatus: "pending",
|
||||
scanStatus: "pending",
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
const { original } = buildFileStorageKeys(input.tenantId, created.id);
|
||||
try {
|
||||
await putBufferAsFile({
|
||||
bucket,
|
||||
key: original,
|
||||
body: new Uint8Array(await file.arrayBuffer()),
|
||||
contentType: file.type || "application/octet-stream",
|
||||
});
|
||||
await db.file.update({ where: { id: created.id }, data: { storageKey: original } });
|
||||
await enqueueFileScan({
|
||||
bucket,
|
||||
key: original,
|
||||
tenantId: input.tenantId,
|
||||
contentType: file.type || "application/octet-stream",
|
||||
fileId: created.id,
|
||||
}).catch(() => undefined);
|
||||
await enqueueFilePreview({
|
||||
fileId: created.id,
|
||||
tenantId: input.tenantId,
|
||||
bucket,
|
||||
storageKey: original,
|
||||
mimeType: file.type || "application/octet-stream",
|
||||
}).catch(() => undefined);
|
||||
prepared.push({ fieldKey: field.key, fileId: created.id, kind: field.type });
|
||||
} catch (error) {
|
||||
await db.file.delete({ where: { id: created.id } }).catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return prepared;
|
||||
}
|
||||
Reference in new issue
Block a user