Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+49
-44
@@ -1,9 +1,11 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import { withTenantStorageWriteReservation } from "@hivegrid/db/storage-capacity.server";
|
||||
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 { withTenantRLS } from "~/utils/db/.server/tenantRls.server";
|
||||
import { enqueueFileScan } from "~/utils/integrations/fileScanQueue.server";
|
||||
import { putBufferAsFile, resolveStorageBucket } from "~/utils/integrations/storageService";
|
||||
import { deleteFile, putBufferAsFile, resolveStorageBucket } from "~/utils/integrations/storageService";
|
||||
|
||||
const FORM_FILE_MAX_BYTES = 25 * 1024 * 1024;
|
||||
|
||||
@@ -35,48 +37,51 @@ export async function prepareUploadedFiles(input: { tenantId: string; formBody:
|
||||
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;
|
||||
const bytes = new Uint8Array(await file.arrayBuffer());
|
||||
if (bytes.byteLength !== file.size) {
|
||||
throw new Error(`file-size-mismatch:${field.key}`);
|
||||
}
|
||||
const mimeType = file.type || "application/octet-stream";
|
||||
const displayName = file.name.slice(0, 255) || "upload.bin";
|
||||
const fileId = `file_${randomUUID()}`;
|
||||
const { original } = buildFileStorageKeys(input.tenantId, fileId);
|
||||
await withTenantStorageWriteReservation({
|
||||
reservation: {
|
||||
tenantId: input.tenantId,
|
||||
actorUserId: "system:form",
|
||||
targetFileId: fileId,
|
||||
originalFilename: displayName,
|
||||
mimeType,
|
||||
sizeBytes: bytes.byteLength,
|
||||
},
|
||||
runTransaction: (operation) => withTenantRLS(input.tenantId, operation),
|
||||
upload: () => putBufferAsFile({ bucket, key: original, body: bytes, contentType: mimeType }),
|
||||
commit: async (tx) => {
|
||||
await tx.file.create({
|
||||
data: {
|
||||
id: fileId,
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: "system:form",
|
||||
displayName,
|
||||
mimeType,
|
||||
sizeBytes: bytes.byteLength,
|
||||
storageKey: original,
|
||||
source: "form_submission",
|
||||
previewStatus: "pending",
|
||||
scanStatus: "pending",
|
||||
},
|
||||
});
|
||||
},
|
||||
rollbackUpload: () => deleteFile({ bucket, key: original }),
|
||||
});
|
||||
await enqueueFileScan({
|
||||
bucket,
|
||||
key: original,
|
||||
tenantId: input.tenantId,
|
||||
contentType: mimeType,
|
||||
fileId,
|
||||
}).catch(() => undefined);
|
||||
prepared.push({ fieldKey: field.key, fileId, kind: field.type });
|
||||
}
|
||||
return prepared;
|
||||
}
|
||||
Reference in new issue
Block a user