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
+288
-35
@@ -18,16 +18,23 @@
|
||||
// transaction, and functions never nest withTenantRLS() — internal composition
|
||||
// goes through *WithTx(tx, ...) helpers that take the active transaction client.
|
||||
|
||||
import { CatalogError, foundationErrors } from "@toolctrl/foundation";
|
||||
import { buildFileStorageKeys, buildUploadChunkKey, uploadChunkPrefix } from "@toolctrl/storage";
|
||||
import { CatalogError, foundationErrors } from "@hivegrid/foundation";
|
||||
import { filesIndex } from "@hivegrid/search";
|
||||
import { buildFileStorageKeys, buildUploadChunkKey, uploadChunkPrefix } from "@hivegrid/storage";
|
||||
import {
|
||||
FILE_UPLOAD_CHUNK_SIZE_BYTES,
|
||||
FILE_UPLOAD_MAX_FILE_SIZE_BYTES,
|
||||
} from "@toolctrl/view-models";
|
||||
} from "@hivegrid/view-models";
|
||||
import { publishFileSearchUpsert, publishSearchProjectionDelete } from "~/services/search/searchIndexProjection.server";
|
||||
import { withTenantRLS, type TenantRlsClient } from "~/utils/db/.server/tenantRls.server";
|
||||
import { assembleChunkedUpload, deleteFile, putBufferAsFile, resolveStorageBucket } from "./storageService";
|
||||
import { getTenantUsers } from "~/utils/db/tenants.db.server";
|
||||
import { fullName } from "~/utils/app/UserDisplayUtils";
|
||||
import { createMediaDownloadUrl } from "./mediaSigning.server";
|
||||
import type { Prisma } from "@hivegrid/db/prisma-browser";
|
||||
import { assembleChunkedUpload, deleteFile, putBufferAsFile, resolveStorageBucket, storeFile } from "./storageService";
|
||||
import { enqueueFileScan } from "./fileScanQueue.server";
|
||||
import { enqueueFilePreview } from "./filePreviewQueue.server";
|
||||
import type { ScanContext } from "@hivegrid/storage";
|
||||
|
||||
/**
|
||||
* Domain error carrying an i18n message key (M002 Klartext). Routes map
|
||||
@@ -52,7 +59,8 @@ export type FileEntityType =
|
||||
| "form_submission"
|
||||
| "order"
|
||||
| "task"
|
||||
| "appointment";
|
||||
| "appointment"
|
||||
| "signature_document";
|
||||
|
||||
export type FileSource = "manual_upload" | "customer_portal" | "form_submission" | "email_attachment" | "automation" | "api";
|
||||
|
||||
@@ -286,7 +294,10 @@ export async function completeUpload(input: CompleteUploadInput): Promise<Comple
|
||||
throw error instanceof FileServiceError ? error : new FileServiceError("files.error.assemblyFailed");
|
||||
}
|
||||
|
||||
await withTenantRLS(input.tenantId, (tx) => tx.file.update({ where: { id: file.id }, data: { storageKey: original } }));
|
||||
await withTenantRLS(input.tenantId, async (tx) => {
|
||||
const updated = await tx.file.update({ where: { id: file.id }, data: { storageKey: original } });
|
||||
await publishFileSearchUpsert(tx, updated);
|
||||
});
|
||||
|
||||
// Clean up temp chunk objects (best-effort, S3 outside the tx) + mark the
|
||||
// session done in one tenant-scoped transaction.
|
||||
@@ -312,6 +323,89 @@ export async function completeUpload(input: CompleteUploadInput): Promise<Comple
|
||||
return { id: file.id, displayName: file.displayName, mimeType: file.mimeType, sizeBytes: file.sizeBytes, storageKey: original };
|
||||
}
|
||||
|
||||
export type CreateFileFromBytesInput = {
|
||||
tenantId: string;
|
||||
userId: string;
|
||||
filename: string;
|
||||
mimeType: string;
|
||||
bytes: Uint8Array;
|
||||
/** Pre-upload scan context (magic-bytes allow-list); defaults to mediaLibrary. */
|
||||
scanContext?: ScanContext;
|
||||
isDemoData?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Single-request upload: create a File from an in-memory byte buffer (no chunked
|
||||
* session). Mirrors the finalize tail of {@link completeUpload} — pending row →
|
||||
* fail-closed magic-bytes scan + storage PUT → finalize storageKey + search
|
||||
* upsert → fan out ClamAV (layer 2) + preview. Callers gate on their OWN
|
||||
* permission (e.g. HR self-upload gates on hr.read_own, NOT files.write). A
|
||||
* rejected scan throws {@link StorageScanRejectedError} and leaves no orphan row.
|
||||
*/
|
||||
export async function createFileFromBytes(input: CreateFileFromBytesInput): Promise<{ id: string; displayName: string; mimeType: string; sizeBytes: number }> {
|
||||
const displayName = (input.filename || "upload").slice(0, 255);
|
||||
const mimeType = input.mimeType || "application/octet-stream";
|
||||
const sizeBytes = input.bytes.byteLength;
|
||||
|
||||
// 1. Pending File row (id needed for the storage key).
|
||||
const file = await withTenantRLS(input.tenantId, (tx) =>
|
||||
tx.file.create({
|
||||
data: {
|
||||
tenantId: input.tenantId,
|
||||
createdByUserId: input.userId,
|
||||
displayName,
|
||||
mimeType,
|
||||
sizeBytes,
|
||||
storageKey: "",
|
||||
source: "manual_upload",
|
||||
isDemoData: input.isDemoData ?? false,
|
||||
previewStatus: "pending",
|
||||
scanStatus: "pending",
|
||||
},
|
||||
select: { id: true, displayName: true, mimeType: true, sizeBytes: true },
|
||||
}),
|
||||
);
|
||||
|
||||
const bucket = resolveStorageBucket();
|
||||
const { original } = buildFileStorageKeys(input.tenantId, file.id);
|
||||
|
||||
// 2. Scan (fail-closed) + store the bytes. storeFile fetches the data: URI,
|
||||
// runs the magic-bytes allow-list and PUTs to `original`; a rejected verdict
|
||||
// throws StorageScanRejectedError. Roll the pending row back on any failure.
|
||||
const dataUri = `data:${mimeType};base64,${Buffer.from(input.bytes).toString("base64")}`;
|
||||
try {
|
||||
await storeFile({
|
||||
bucket,
|
||||
id: original,
|
||||
content: dataUri,
|
||||
replace: true,
|
||||
throwOnFailure: true,
|
||||
scan: { context: input.scanContext ?? "mediaLibrary", declaredMimeType: mimeType, filename: displayName, tenantId: input.tenantId },
|
||||
});
|
||||
} catch (error) {
|
||||
await withTenantRLS(input.tenantId, (tx) => tx.file.delete({ where: { id: file.id } }).catch(() => {})).catch(() => {});
|
||||
throw error;
|
||||
}
|
||||
|
||||
// 3. Finalize the storage key + publish to the search index.
|
||||
await withTenantRLS(input.tenantId, async (tx) => {
|
||||
const updated = await tx.file.update({ where: { id: file.id }, data: { storageKey: original } });
|
||||
await publishFileSearchUpsert(tx, updated);
|
||||
});
|
||||
|
||||
// 4. Fan out ClamAV (layer 2) + preview. When scanning is disabled, mark the
|
||||
// file scanStatus=skipped so the fail-closed media proxy still serves it.
|
||||
const scanResult = await enqueueFileScan({ bucket, key: original, tenantId: input.tenantId, contentType: mimeType, fileId: file.id }).catch(
|
||||
() => ({ enqueued: false, reason: "queue-unavailable" }) as const,
|
||||
);
|
||||
if (!scanResult.enqueued && scanResult.reason === "scan-disabled") {
|
||||
await withTenantRLS(input.tenantId, (tx) => tx.file.update({ where: { id: file.id }, data: { scanStatus: "skipped" } })).catch(() => {});
|
||||
}
|
||||
await enqueueFilePreview({ fileId: file.id, tenantId: input.tenantId, bucket, storageKey: original, mimeType }).catch(() => {});
|
||||
|
||||
return { id: file.id, displayName: file.displayName, mimeType: file.mimeType, sizeBytes: file.sizeBytes };
|
||||
}
|
||||
|
||||
// --- Visibility / read ----------------------------------------------------
|
||||
|
||||
export type FileRow = {
|
||||
@@ -405,6 +499,7 @@ export async function getVisibleFileWithLinks(
|
||||
}
|
||||
|
||||
export type ListFilesFilter = {
|
||||
entityType?: string | null;
|
||||
mimeGroup?: string | null;
|
||||
tags?: readonly string[];
|
||||
assignedToUserId?: string | null;
|
||||
@@ -427,24 +522,24 @@ export async function listFilesForBrowser(input: {
|
||||
}): Promise<{ rows: FileRow[]; nextCursor: string | null }> {
|
||||
const limit = Math.min(Math.max(input.limit ?? 50, 1), 100);
|
||||
const filter = input.filter ?? {};
|
||||
const visibility = hasPermission(input.ctx, "files.read_all")
|
||||
? {}
|
||||
: {
|
||||
OR: [
|
||||
{ createdByUserId: input.ctx.userId },
|
||||
{ assignedToUserId: input.ctx.userId },
|
||||
{ links: { some: { tenantId: input.tenantId } } },
|
||||
],
|
||||
};
|
||||
const mimePrefix = filter.mimeGroup ? MIME_GROUP_PREFIX[filter.mimeGroup] : undefined;
|
||||
const where = {
|
||||
tenantId: input.tenantId,
|
||||
...(filter.includeArchived ? {} : { archivedAt: null }),
|
||||
...(filter.assignedToUserId ? { assignedToUserId: filter.assignedToUserId } : {}),
|
||||
...(filter.tags && filter.tags.length > 0 ? { tags: { hasEvery: [...filter.tags] } } : {}),
|
||||
...(mimePrefix ? { mimeType: { startsWith: mimePrefix } } : {}),
|
||||
...visibility,
|
||||
};
|
||||
// Build the where clause on a typed mutable object (not spread-of-union) so the
|
||||
// optional filters + visibility OR type-check cleanly against FileWhereInput.
|
||||
const where: Prisma.FileWhereInput = { tenantId: input.tenantId };
|
||||
if (!filter.includeArchived) where.archivedAt = null;
|
||||
if (filter.assignedToUserId) where.assignedToUserId = filter.assignedToUserId;
|
||||
if (filter.tags && filter.tags.length > 0) where.tags = { hasEvery: [...filter.tags] };
|
||||
if (mimePrefix) where.mimeType = { startsWith: mimePrefix };
|
||||
if (filter.entityType) {
|
||||
where.links = { some: { tenantId: input.tenantId, entityType: filter.entityType as Prisma.FileEntityLinkWhereInput["entityType"] } };
|
||||
}
|
||||
if (!hasPermission(input.ctx, "files.read_all")) {
|
||||
where.OR = [
|
||||
{ createdByUserId: input.ctx.userId },
|
||||
{ assignedToUserId: input.ctx.userId },
|
||||
{ links: { some: { tenantId: input.tenantId } } },
|
||||
];
|
||||
}
|
||||
const rows = await withTenantRLS(input.tenantId, (tx) =>
|
||||
tx.file.findMany({
|
||||
where,
|
||||
@@ -457,6 +552,146 @@ export async function listFilesForBrowser(input: {
|
||||
return { rows: rows.slice(0, limit) as FileRow[], nextCursor };
|
||||
}
|
||||
|
||||
export type FileFilterOptions = {
|
||||
/** Distinct entity types that are actually linked to files in this tenant. */
|
||||
entityTypes: string[];
|
||||
/** Distinct tags across (recent) tenant files, sorted. */
|
||||
tags: string[];
|
||||
/** Active tenant members, usable as assignee (Verantwortlicher) filter. */
|
||||
assignees: { id: string; name: string }[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Build the option lists for the browser filter dropdowns (tenant-scoped).
|
||||
* Tags are scanned over the most recent files (capped) so the dropdown stays
|
||||
* bounded; entity types come from the FileEntityLink distinct set; assignees are
|
||||
* the tenant's active members.
|
||||
*/
|
||||
export async function listFileFilterOptions(input: { tenantId: string }): Promise<FileFilterOptions> {
|
||||
const TAG_SCAN_LIMIT = 500;
|
||||
const { tagRows, entityTypeRows } = await withTenantRLS(input.tenantId, async (tx) => {
|
||||
const tagRows = await tx.file.findMany({
|
||||
where: { tenantId: input.tenantId },
|
||||
select: { tags: true },
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: TAG_SCAN_LIMIT,
|
||||
});
|
||||
const entityTypeRows = await tx.fileEntityLink.findMany({
|
||||
where: { tenantId: input.tenantId },
|
||||
select: { entityType: true },
|
||||
distinct: ["entityType"],
|
||||
orderBy: { entityType: "asc" },
|
||||
});
|
||||
return { tagRows, entityTypeRows };
|
||||
});
|
||||
const tags = Array.from(new Set(tagRows.flatMap((row) => row.tags))).sort((a, b) => a.localeCompare(b));
|
||||
const entityTypes = entityTypeRows.map((row) => row.entityType);
|
||||
|
||||
const members = await getTenantUsers(input.tenantId);
|
||||
const assignees = members
|
||||
.map((member) => ({ id: member.user.id, name: fullName(member.user) }))
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
|
||||
return { entityTypes, tags, assignees };
|
||||
}
|
||||
|
||||
// --- Bulk ZIP -------------------------------------------------------------
|
||||
|
||||
/** Max files a single bulk-ZIP may contain (defensive bound for the in-memory zip). */
|
||||
export const BULK_ZIP_MAX_FILES = 50;
|
||||
/** Max total original bytes a single bulk-ZIP may pull (in-memory safety). */
|
||||
export const BULK_ZIP_MAX_TOTAL_BYTES = 200 * 1024 * 1024;
|
||||
|
||||
export type BulkZipEntry = { name: string; bytes: Uint8Array };
|
||||
export type BulkZipResult = {
|
||||
/** Zip entries for the buildable (visible + clean) files. */
|
||||
entries: BulkZipEntry[];
|
||||
/** Files that were requested but skipped (not visible / not clean / over the cap). */
|
||||
skippedCount: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Collect the byte payloads for a bulk-ZIP download (tenant-scoped, fail-closed).
|
||||
* Only files that are visible to the user AND scan-clean/skipped are included; an
|
||||
* infected/pending/invisible/over-cap file is silently dropped (never leaked into
|
||||
* the archive). Bytes are pulled via short-lived signed URLs (AD-23, no public
|
||||
* bucket). Entry names are de-duplicated so the zip stays valid.
|
||||
*/
|
||||
export async function collectFilesForZip(input: {
|
||||
tenantId: string;
|
||||
ctx: FileVisibilityContext;
|
||||
fileIds: readonly string[];
|
||||
}): Promise<BulkZipResult> {
|
||||
const ids = Array.from(new Set(input.fileIds)).slice(0, BULK_ZIP_MAX_FILES);
|
||||
if (ids.length === 0) {
|
||||
return { entries: [], skippedCount: 0 };
|
||||
}
|
||||
|
||||
const where: Prisma.FileWhereInput = {
|
||||
tenantId: input.tenantId,
|
||||
id: { in: ids },
|
||||
scanStatus: { in: ["clean", "skipped"] },
|
||||
};
|
||||
if (!hasPermission(input.ctx, "files.read_all")) {
|
||||
where.OR = [
|
||||
{ createdByUserId: input.ctx.userId },
|
||||
{ assignedToUserId: input.ctx.userId },
|
||||
{ links: { some: { tenantId: input.tenantId } } },
|
||||
];
|
||||
}
|
||||
|
||||
const files = await withTenantRLS(input.tenantId, (tx) =>
|
||||
tx.file.findMany({ where, select: { id: true, displayName: true, storageKey: true, sizeBytes: true } }),
|
||||
);
|
||||
|
||||
const usedNames = new Set<string>();
|
||||
const entries: BulkZipEntry[] = [];
|
||||
let totalBytes = 0;
|
||||
let skippedCount = ids.length - files.length;
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.storageKey || totalBytes + file.sizeBytes > BULK_ZIP_MAX_TOTAL_BYTES) {
|
||||
skippedCount += 1;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const url = await createMediaDownloadUrl({ key: file.storageKey });
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
skippedCount += 1;
|
||||
continue;
|
||||
}
|
||||
const bytes = new Uint8Array(await response.arrayBuffer());
|
||||
totalBytes += bytes.byteLength;
|
||||
entries.push({ name: uniqueZipName(file.displayName, usedNames), bytes });
|
||||
} catch {
|
||||
skippedCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return { entries, skippedCount };
|
||||
}
|
||||
|
||||
/** Ensure a zip entry name is unique (append " (n)" before the extension). */
|
||||
function uniqueZipName(rawName: string, used: Set<string>): string {
|
||||
const safe = (rawName || "datei").replace(/[/\\]/g, "_");
|
||||
if (!used.has(safe)) {
|
||||
used.add(safe);
|
||||
return safe;
|
||||
}
|
||||
const dot = safe.lastIndexOf(".");
|
||||
const base = dot > 0 ? safe.slice(0, dot) : safe;
|
||||
const ext = dot > 0 ? safe.slice(dot) : "";
|
||||
let index = 2;
|
||||
let candidate = `${base} (${index})${ext}`;
|
||||
while (used.has(candidate)) {
|
||||
index += 1;
|
||||
candidate = `${base} (${index})${ext}`;
|
||||
}
|
||||
used.add(candidate);
|
||||
return candidate;
|
||||
}
|
||||
|
||||
// --- Mutations ------------------------------------------------------------
|
||||
|
||||
export async function linkFile(input: {
|
||||
@@ -497,15 +732,25 @@ export async function unlinkFile(input: { tenantId: string; fileId: string; enti
|
||||
}
|
||||
|
||||
export async function archiveFile(input: { tenantId: string; fileId: string }): Promise<void> {
|
||||
await withTenantRLS(input.tenantId, (tx) =>
|
||||
tx.file.updateMany({ where: { id: input.fileId, tenantId: input.tenantId }, data: { archivedAt: new Date() } }),
|
||||
);
|
||||
await withTenantRLS(input.tenantId, async (tx) => {
|
||||
const file = await tx.file.findFirst({ where: { id: input.fileId, tenantId: input.tenantId }, select: { id: true } });
|
||||
if (!file) {
|
||||
throw new CatalogError(foundationErrors.notFound, { fileId: input.fileId });
|
||||
}
|
||||
await tx.file.update({ where: { id: file.id }, data: { archivedAt: new Date() } });
|
||||
await publishSearchProjectionDelete(tx, { schema: filesIndex, tenantId: input.tenantId, entityType: "file", entityId: file.id });
|
||||
});
|
||||
}
|
||||
|
||||
export async function reactivateFile(input: { tenantId: string; fileId: string }): Promise<void> {
|
||||
await withTenantRLS(input.tenantId, (tx) =>
|
||||
tx.file.updateMany({ where: { id: input.fileId, tenantId: input.tenantId }, data: { archivedAt: null } }),
|
||||
);
|
||||
await withTenantRLS(input.tenantId, async (tx) => {
|
||||
const file = await tx.file.findFirst({ where: { id: input.fileId, tenantId: input.tenantId }, select: { id: true } });
|
||||
if (!file) {
|
||||
throw new CatalogError(foundationErrors.notFound, { fileId: input.fileId });
|
||||
}
|
||||
const updated = await tx.file.update({ where: { id: file.id }, data: { archivedAt: null } });
|
||||
await publishFileSearchUpsert(tx, updated);
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateFileMeta(input: {
|
||||
@@ -516,17 +761,22 @@ export async function updateFileMeta(input: {
|
||||
assignedToUserId?: string | null;
|
||||
displayName?: string;
|
||||
}): Promise<void> {
|
||||
await withTenantRLS(input.tenantId, (tx) =>
|
||||
tx.file.updateMany({
|
||||
where: { id: input.fileId, tenantId: input.tenantId },
|
||||
await withTenantRLS(input.tenantId, async (tx) => {
|
||||
const file = await tx.file.findFirst({ where: { id: input.fileId, tenantId: input.tenantId }, select: { id: true } });
|
||||
if (!file) {
|
||||
throw new CatalogError(foundationErrors.notFound, { fileId: input.fileId });
|
||||
}
|
||||
const updated = await tx.file.update({
|
||||
where: { id: file.id },
|
||||
data: {
|
||||
...(input.tags !== undefined ? { tags: input.tags } : {}),
|
||||
...(input.notes !== undefined ? { notes: input.notes } : {}),
|
||||
...(input.assignedToUserId !== undefined ? { assignedToUserId: input.assignedToUserId } : {}),
|
||||
...(input.displayName !== undefined ? { displayName: input.displayName.slice(0, 255) } : {}),
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
await publishFileSearchUpsert(tx, updated);
|
||||
});
|
||||
}
|
||||
|
||||
export async function regenerateFilePreview(input: { tenantId: string; fileId: string }): Promise<void> {
|
||||
@@ -567,7 +817,10 @@ export async function hardDeleteFile(input: { tenantId: string; fileId: string }
|
||||
}
|
||||
}
|
||||
// FileEntityLink rows cascade on File delete (schema onDelete: Cascade).
|
||||
await withTenantRLS(input.tenantId, (tx) => tx.file.delete({ where: { id: file.id } }));
|
||||
await withTenantRLS(input.tenantId, async (tx) => {
|
||||
await tx.file.delete({ where: { id: file.id } });
|
||||
await publishSearchProjectionDelete(tx, { schema: filesIndex, tenantId: input.tenantId, entityType: "file", entityId: file.id });
|
||||
});
|
||||
}
|
||||
|
||||
/** Best-effort cleanup of all temp chunk objects of an upload session. */
|
||||
|
||||
Reference in new issue
Block a user