Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor(server): split the 1305-line file router into sub-domain modules (#21094)

Arvin Xu0e6d403e859b
diff --git a/apps/server/src/routers/lambda/file.ts b/apps/server/src/routers/lambda/file.ts
deleted file mode 100644
index 19b5dd59731..00000000000
--- a/apps/server/src/routers/lambda/file.ts
+++ /dev/null
@@ -1,1305 +0,0 @@
-import { randomUUID } from 'node:crypto';
-
-import {
- CUSTOM_FOLDER_FILE_TYPE,
- DERIVED_DOCUMENT_SOURCE_TYPE,
- MARKDOWN_MIME_TYPES,
- MAX_UPLOAD_FILE_SIZE,
- RESOURCE_CONTENT_PREVIEW_SOURCE_LENGTH,
- UPLOAD_FILE_SIZE_LIMIT_ERROR_MESSAGE,
-} from '@lobechat/const';
-import { type LobeChatDatabase } from '@lobechat/database';
-import { TRPCError } from '@trpc/server';
-import isEqual from 'fast-deep-equal';
-import pMap from 'p-map';
-import { z } from 'zod';
-
-import {
- businessFileTransferStorageCheck,
- businessFileUploadCheck,
-} from '@/business/server/lambda-routers/file';
-import { checkFileStorageUsage } from '@/business/server/trpc-middlewares/lambda';
-import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
-import { wsCompatProcedure } from '@/business/server/trpc-middlewares/workspaceAuth';
-import { serverDBEnv } from '@/config/db';
-import { AsyncTaskModel } from '@/database/models/asyncTask';
-import { ChunkModel } from '@/database/models/chunk';
-import { DOCUMENT_TRANSFER_FOREIGN_ROWS, DocumentModel } from '@/database/models/document';
-import { FileModel } from '@/database/models/file';
-import { KnowledgeBaseModel } from '@/database/models/knowledgeBase';
-import { KnowledgeRepo } from '@/database/repositories/knowledge';
-import { router } from '@/libs/trpc/lambda';
-import { serverDatabase } from '@/libs/trpc/lambda/middleware';
-import { DocumentService } from '@/server/services/document';
-import { FileService } from '@/server/services/file';
-import { downloadRemoteImage } from '@/server/services/file/downloadRemoteImage';
-import { FileUploadService } from '@/server/services/fileUpload';
-import { assertCanPerformResourceAction } from '@/server/services/resourcePermission';
-import { hasWorkspaceScopedPermission } from '@/server/services/workspacePermission';
-import { createResourceContentPreview } from '@/server/utils/resourceContentPreview';
-import { AsyncTaskStatus, AsyncTaskType, type IAsyncTaskError } from '@/types/asyncTask';
-import type { FileListItem, KnowledgeItemStatus } from '@/types/files';
-import {
- FileSource,
- QueryFileListSchema,
- stripAgentShareFileProvenance,
- toFileSource,
- UploadFileSchema,
-} from '@/types/files';
-import { TransferErrorCode } from '@/types/transferError';
-
-import {
- assertWorkspaceRowManageable,
- isWorkspaceNonOwner,
-} from './_helpers/assertWorkspaceRowManageable';
-import type { KnowledgeBaseAccessCtx } from './_helpers/knowledgeBaseAccess';
-import {
- assertContentsNotInRestrictedKnowledgeBase,
- assertFileNotInRestrictedKnowledgeBase,
- assertKnowledgeBaseBrowsable,
- getRestrictedKnowledgeBaseIds,
-} from './_helpers/knowledgeBaseAccess';
-
-const fileTransferEntityTypeSchema = z.enum(['document', 'file', 'folder']);
-const deleteKnowledgeItemsByQuerySchema = QueryFileListSchema.extend({
- excludedIds: z.array(z.string()).optional(),
-});
-const markdownPreviewTypes = new Set<string>(MARKDOWN_MIME_TYPES);
-const KNOWLEDGE_ITEM_RESOLUTION_CONCURRENCY = 8;
-
-const isMarkdownFile = (item: { fileType: string; name: string }) =>
- markdownPreviewTypes.has(item.fileType) || /\.md(?:arkdown)?$/i.test(item.name);
-
-const assertAllFilesAccessible = (requestedIds: string[], files: Array<{ id: string }>): void => {
- const accessibleIds = new Set(files.map((file) => file.id));
- if ([...new Set(requestedIds)].some((id) => !accessibleIds.has(id))) {
- throw new TRPCError({
- code: 'NOT_FOUND',
- message: 'One or more files were not found or are not accessible',
- });
- }
-};
-
-const resolveAccessibleParentDocument = async (
- ctx: KnowledgeBaseAccessCtx & {
- documentModel: Pick<DocumentModel, 'findById' | 'findBySlug'>;
- },
- parentId: string,
-) => {
- const parentDocument =
- (await ctx.documentModel.findBySlug(parentId)) ?? (await ctx.documentModel.findById(parentId));
-
- if (!parentDocument) {
- throw new TRPCError({ code: 'NOT_FOUND', message: 'Parent document not found' });
- }
-
- await assertContentsNotInRestrictedKnowledgeBase(ctx, [parentDocument.id]);
- return parentDocument;
-};
-
-const filterKnowledgeItems = <
- T extends {
- fileType: string;
- sourceType: string;
- },
->(
- items: T[],
- knowledgeBaseId?: string,
-) => {
- return !knowledgeBaseId
- ? items.filter(
- (item) =>
- !(
- item.sourceType === DERIVED_DOCUMENT_SOURCE_TYPE &&
- item.fileType === CUSTOM_FOLDER_FILE_TYPE
- ),
- )
- : items;
-};
-
-const getKnowledgeItemStatusMap = async (
- ctx: {
- asyncTaskModel: AsyncTaskModel;
- chunkModel: ChunkModel;
- },
- fileItems: Array<{
- chunkTaskId?: string | null;
- embeddingTaskId?: string | null;
- id: string;
- }>,
-): Promise<Map<string, KnowledgeItemStatus>> => {
- if (fileItems.length === 0) return new Map();
-
- const fileIds = fileItems.map((item) => item.id);
- const chunkTaskIds = [
- ...new Set(fileItems.map((item) => item.chunkTaskId).filter(Boolean)),
- ] as string[];
- const embeddingTaskIds = [
- ...new Set(fileItems.map((item) => item.embeddingTaskId).filter(Boolean)),
- ] as string[];
-
- const [chunks, chunkTasks, embeddingTasks] = await Promise.all([
- ctx.chunkModel.countByFileIds(fileIds),
- chunkTaskIds.length > 0
- ? ctx.asyncTaskModel.findByIds(chunkTaskIds, AsyncTaskType.Chunking)
- : Promise.resolve([]),
- embeddingTaskIds.length > 0
- ? ctx.asyncTaskModel.findByIds(embeddingTaskIds, AsyncTaskType.Embedding)
- : Promise.resolve([]),
- ]);
-
- const chunkRows = chunks ?? [];
- const chunkTaskRows = chunkTasks ?? [];
- const embeddingTaskRows = embeddingTasks ?? [];
-
- const chunkCountMap = new Map(
- chunkRows.filter((item) => item.id).map((item) => [item.id, item.count] as const),
- );
- const chunkTaskMap = new Map(chunkTaskRows.map((task) => [task.id, task] as const));
- const embeddingTaskMap = new Map(embeddingTaskRows.map((task) => [task.id, task] as const));
-
- return new Map(
- fileItems.map((item) => {
- const chunkTask = item.chunkTaskId ? chunkTaskMap.get(item.chunkTaskId) : null;
- const embeddingTask = item.embeddingTaskId
- ? embeddingTaskMap.get(item.embeddingTaskId)
- : null;
-
- return [
- item.id,
- {
- chunkCount: chunkCountMap.get(item.id) ?? null,
- chunkingError: (chunkTask?.error as IAsyncTaskError | null | undefined) ?? null,
- chunkingStatus: (chunkTask?.status as AsyncTaskStatus | null | undefined) ?? null,
- embeddingError: (embeddingTask?.error as IAsyncTaskError | null | undefined) ?? null,
- embeddingStatus: (embeddingTask?.status as AsyncTaskStatus | null | undefined) ?? null,
- finishEmbedding: embeddingTask?.status === AsyncTaskStatus.Success,
- id: item.id,
- },
- ] as const;
- }),
- );
-};
-
-const isStoredObjectAvailable = async (fileService: FileService, url: string): Promise<boolean> => {
- try {
- // Hash records can outlive their backing object, for example when generated
- // assets are cleaned up but the global hash row remains. Treat stale rows as
- // missing so the client uploads a fresh copy instead of reusing a dead key.
- await fileService.getFileMetadata(url);
- return true;
- } catch (error) {
- console.error('Failed to verify existing file hash storage object:', error);
- return false;
- }
-};
-
-const fileProcedure = wsCompatProcedure.use(serverDatabase).use(async (opts) => {
- const { ctx } = opts;
- const wsId = ctx.workspaceId ?? undefined;
-
- return opts.next({
- ctx: {
- asyncTaskModel: new AsyncTaskModel(ctx.serverDB, ctx.userId, wsId),
- chunkModel: new ChunkModel(ctx.serverDB, ctx.userId, wsId),
- documentModel: new DocumentModel(ctx.serverDB, ctx.userId, wsId),
- documentService: new DocumentService(ctx.serverDB, ctx.userId, wsId),
- fileModel: new FileModel(ctx.serverDB, ctx.userId, wsId),
- fileService: new FileService(ctx.serverDB, ctx.userId, wsId),
- fileUploadService: new FileUploadService(ctx.serverDB, ctx.userId, wsId),
- knowledgeBaseModel: new KnowledgeBaseModel(ctx.serverDB, ctx.userId, wsId),
- knowledgeRepo: new KnowledgeRepo(ctx.serverDB, ctx.userId, wsId),
- },
- });
-});
-
-export const fileRouter = router({
- checkFileHash: fileProcedure
- .use(withScopedPermission('file:upload'))
- .use(checkFileStorageUsage)
- .input(z.object({ hash: z.string() }))
- .mutation(async ({ ctx, input }) => {
- const existingFile = await ctx.fileModel.checkHash(input.hash);
- const existingHashUrl = existingFile?.isExist ? existingFile.url : undefined;
- if (!existingHashUrl) return existingFile;
-
- const isStorageAvailable = await isStoredObjectAvailable(ctx.fileService, existingHashUrl);
-
- return isStorageAvailable ? existingFile : { isExist: false };
- }),
-
- createFile: fileProcedure
- .use(withScopedPermission('file:upload'))
- .input(
- UploadFileSchema.omit({ url: true }).extend({
- parentId: z.string().optional(),
- url: z.string(),
- visibility: z.enum(['private', 'public']).optional(),
- }),
- )
- .mutation(async ({ ctx, input }) => {
- const metadata = stripAgentShareFileProvenance(input.metadata);
- const existingFile = await ctx.fileModel.checkHash(input.hash!);
- const { isExist } = existingFile;
- const latestUpload = await ctx.fileUploadService.findLatest(input.url);
- if (!latestUpload && (await ctx.fileUploadService.hasAnyLiveSession(input.url))) {
- throw new TRPCError({
- code: 'CONFLICT',
- message: 'Upload pathname belongs to another session',
- });
- }
-
- const parentDocument = input.parentId
- ? await resolveAccessibleParentDocument(ctx, input.parentId)
- : undefined;
- const resolvedParentId = parentDocument?.id;
- const parentVisibility = parentDocument?.visibility;
-
- let knowledgeBaseVisibility: 'private' | 'public' | undefined;
- if (ctx.workspaceId && input.knowledgeBaseId) {
- const knowledgeBase = await ctx.knowledgeBaseModel.findById(input.knowledgeBaseId);
- if (!knowledgeBase) {
- throw new TRPCError({ code: 'NOT_FOUND', message: 'Knowledge base not found' });
- }
- knowledgeBaseVisibility = knowledgeBase.visibility;
- }
-
- // Visibility precedence (workspace mode only — personal mode ignores the
- // column entirely):
- // 1. A library upload always uses the knowledge base visibility.
- // 2. Otherwise an explicit caller value wins.
- // 3. Otherwise inherit the parent document's visibility so a file
- // uploaded inside a private folder stays private.
- // 4. Agent-document uploads default to 'public' to match their document's
- // access contract; their source keeps them out of resource listings.
- // 5. Otherwise default top-level uploads to 'private' so new content
- // starts in the creator's private space (mirrors the Pages spec).
- const resolvedVisibility: 'private' | 'public' | undefined = ctx.workspaceId
- ? (knowledgeBaseVisibility ??
- input.visibility ??
- parentVisibility ??
- (input.source === FileSource.AgentDocument ? 'public' : 'private'))
- : undefined;
-
- if (latestUpload?.status === 'settled') {
- const settledFile = latestUpload.fileId
- ? await ctx.fileModel.findById(latestUpload.fileId)
- : undefined;
- const isRetry =
- !!settledFile &&
- !input.knowledgeBaseId &&
- isExist &&
- existingFile.url === input.url &&
- settledFile.fileHash === input.hash &&
- settledFile.fileType === input.fileType &&
- settledFile.name === input.name &&
- settledFile.parentId === (resolvedParentId ?? null) &&
- settledFile.size === input.size &&
- (settledFile.source ?? undefined) === toFileSource(input.source) &&
- settledFile.url === input.url &&
- (!ctx.workspaceId || settledFile.visibility === resolvedVisibility) &&
- isEqual(settledFile.metadata, metadata ?? null);
-
- if (isRetry) {
- return {
- id: settledFile.id,
- url: await ctx.fileService.getFileAccessUrl(settledFile),
- };
- }
-
- // The same global object can be referenced by multiple logical files.
- // A non-identical request is the normal dedup path, not a stale retry.
- } else if (latestUpload && latestUpload.status !== 'active') {
- throw new TRPCError({ code: 'CONFLICT', message: 'Upload session is no longer active' });
- }
-
- const activeUpload =
- latestUpload?.status === 'active'
- ? await ctx.fileUploadService.touchActive(input.url)
- : undefined;
- if (latestUpload?.status === 'active' && !activeUpload) {
- throw new TRPCError({ code: 'CONFLICT', message: 'Upload session is no longer active' });
- }
-
- let actualSize: number;
- if (activeUpload) {
- try {
- const { contentLength } = await ctx.fileService.getFileMetadata(input.url);
- actualSize = contentLength;
- } catch {
- await ctx.fileUploadService.releaseBestEffort(input.url);
- throw new TRPCError({ code: 'BAD_REQUEST', message: 'Uploaded file is unavailable' });
- }
-
- if (input.size !== activeUpload.size || actualSize !== activeUpload.size) {
- await ctx.fileUploadService.releaseBestEffort(input.url);
- throw new TRPCError({ code: 'BAD_REQUEST', message: 'Uploaded file size mismatch' });
- }
- } else {
- actualSize = input.size;
- try {
- const { contentLength } = await ctx.fileService.getFileMetadata(input.url);
- if (contentLength >= 1) actualSize = contentLength;
- } catch {
- // Compatibility path for clients that uploaded before reservations were introduced.
- }
- }
-
- if (actualSize < 0) {
- await businessFileUploadCheck({
- actualSize,
- clientIp: ctx.clientIp ?? undefined,
- inputSize: input.size,
- url: input.url,
- userId: ctx.userId,
- workspaceId: ctx.workspaceId,
- });
- throw new TRPCError({ code: 'BAD_REQUEST', message: 'File size cannot be negative' });
- }
-
- if (actualSize > MAX_UPLOAD_FILE_SIZE) {
- throw new TRPCError({
- code: 'PAYLOAD_TOO_LARGE',
- message: UPLOAD_FILE_SIZE_LIMIT_ERROR_MESSAGE,
- });
- }
-
- const { id } = await ctx.serverDB.transaction(async (trx) => {
- let shouldRefreshGlobalFile = false;
- if (isExist && existingFile.url && existingFile.url !== input.url) {
- shouldRefreshGlobalFile = !(await isStoredObjectAvailable(
- ctx.fileService,
- existingFile.url,
- ));
- }
-
- if (shouldRefreshGlobalFile) {
- // A user may re-upload the same bytes after the old object key was
- // removed. Keep the global hash pointer on the newly uploaded object so
- // future dedup checks do not resolve back to the stale key.
- await ctx.fileModel.updateGlobalFile(
- input.hash!,
- {
- metadata,
- url: input.url,
- },
- trx,
- );
- }
-
- const createFile = () =>
- ctx.fileModel.create(
- {
- fileHash: input.hash,
- fileType: input.fileType,
- knowledgeBaseId: input.knowledgeBaseId,
- metadata,
- name: input.name,
- parentId: resolvedParentId,
- size: actualSize,
- // Attribution the caller supplied (e.g. a page-editor paste). The
- // wire type is a loose string for older clients, so unknown values
- // are dropped rather than persisted — `source` drives the resource
- // library's origin filter and its hidden-source exclusion.
- source: toFileSource(input.source),
- url: input.url,
- ...(resolvedVisibility ? { visibility: resolvedVisibility } : {}),
- },
- // if the file is not exist in global file, create a new one
- !isExist,
- trx,
- );
-
- if (activeUpload) {
- const lockedUpload = await ctx.fileUploadService.model.findLatestByPathnameForUpdate(
- input.url,
- trx,
- );
- if (lockedUpload?.id !== activeUpload.id || lockedUpload.status !== 'active') {
- throw new TRPCError({
- code: 'CONFLICT',
- message: 'Upload session is no longer active',
- });
- }
-
- const file = await createFile();
- const settled = await ctx.fileUploadService.model.settle(lockedUpload.id, file.id, trx);
- if (!settled) {
- throw new TRPCError({ code: 'CONFLICT', message: 'Upload could not be settled' });
- }
- return file;
- }
-
- await businessFileUploadCheck({
- actualSize,
- clientIp: ctx.clientIp ?? undefined,
- inputSize: input.size,
- transaction: trx,
- url: input.url,
- userId: ctx.userId,
- workspaceId: ctx.workspaceId,
- });
-
- return createFile();
- });
-
- return { id, url: await ctx.fileService.getFileAccessUrl({ id, url: input.url }) };
- }),
- findById: fileProcedure
- .input(
- z.object({
- id: z.string(),
- }),
- )
- .query(async ({ ctx, input }) => {
- const item = await ctx.fileModel.findById(input.id);
- if (!item) throw new TRPCError({ code: 'BAD_REQUEST', message: 'File not found' });
-
- await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
-
- return {
- chunkTaskId: item.chunkTaskId,
- clientId: item.clientId,
- createdAt: item.createdAt,
- embeddingTaskId: item.embeddingTaskId,
- fileHash: item.fileHash,
- fileType: item.fileType,
- id: item.id,
- knowledgeBaseIds: await ctx.fileModel.findKnowledgeBaseIds(item.id),
- metadata: item.metadata,
- name: item.name,
- parentId: item.parentId,
- size: item.size,
- source: item.source,
- updatedAt: item.updatedAt,
- url: await ctx.fileService.getFileAccessUrl(item),
- userId: item.userId,
- visibility: item.visibility,
- };
- }),
-
- /**
- * Direct storage URL for reading a file's bytes in the browser (canvas
- * export). The `/f/:id` proxy answers with a cross-origin redirect, which
- * drops the request's Origin so bucket CORS can never allow it; fetching the
- * storage URL directly keeps the Origin the bucket already allows for uploads.
- */
- getReadableUrl: fileProcedure
- .input(
- z.object({
- id: z.string(),
- }),
- )
- .query(async ({ ctx, input }) => {
- const item = await ctx.fileModel.findById(input.id);
- if (!item) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
-
- await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
-
- return { url: await ctx.fileService.getFullFileUrl(item.url) };
- }),
-
- getFileItemById: fileProcedure
- .input(
- z.object({
- id: z.string(),
- }),
- )
- .query(async ({ ctx, input }): Promise<FileListItem | undefined> => {
- const item = await ctx.fileModel.findById(input.id);
-
- if (!item) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
-
- await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
-
- const statusMap = await getKnowledgeItemStatusMap(ctx, [item]);
- const status = statusMap.get(item.id)!;
-
- return {
- createdAt: item.createdAt,
- chunkCount: status.chunkCount ?? null,
- chunkingError: status.chunkingError,
- chunkingStatus: status.chunkingStatus,
- embeddingError: status.embeddingError,
- embeddingStatus: status.embeddingStatus,
- fileType: item.fileType,
- finishEmbedding: status.finishEmbedding ?? false,
- id: item.id,
- metadata: item.metadata as Record<string, any> | null | undefined,
- name: item.name,
- size: item.size,
- sourceType: 'file' as const,
- updatedAt: item.updatedAt,
- url: await ctx.fileService.getFileAccessUrl(item),
- userId: item.userId,
- visibility: item.visibility,
- };
- }),
-
- getFiles: fileProcedure.input(QueryFileListSchema).query(async ({ ctx, input }) => {
- if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
- const excludeKnowledgeBaseIds =
- !input.knowledgeBaseId && input.showFilesInKnowledgeBase
- ? await getRestrictedKnowledgeBaseIds(ctx)
- : undefined;
-
- const fileList = await ctx.fileModel.query({ ...input, excludeKnowledgeBaseIds });
- const statusMap = await getKnowledgeItemStatusMap(ctx, fileList);
-
- const resultFiles = [] as any[];
- for (const item of fileList as any[]) {
- const status = statusMap.get(item.id)!;
- const fileItem = {
- ...item,
- sourceType: 'file' as const,
- url: await ctx.fileService.getFileAccessUrl(item),
- ...status,
- } as FileListItem;
- resultFiles.push(fileItem);
- }
-
- return resultFiles;
- }),
-
- getKnowledgeItemStatusesByIds: fileProcedure
- .input(
- z.object({
- ids: z.array(z.string()),
- }),
- )
- .query(async ({ ctx, input }): Promise<KnowledgeItemStatus[]> => {
- const ids = [...new Set(input.ids)];
- if (ids.length === 0) return [];
-
- const fileItems = await ctx.fileModel.findByIds(ids);
- const statusMap = await getKnowledgeItemStatusMap(ctx, fileItems);
-
- return ids.flatMap((id) => {
- const status = statusMap.get(id);
- return status ? [status] : [];
- });
- }),
-
- getKnowledgeItems: fileProcedure.input(QueryFileListSchema).query(async ({ ctx, input }) => {
- if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
- const excludeKnowledgeBaseIds = input.knowledgeBaseId
- ? undefined
- : await getRestrictedKnowledgeBaseIds(ctx);
-
- // Request one more item than limit to check if there are more items
- const limit = input.limit ?? 50;
- // Absent preserves the legacy response for released clients. Current list
- // surfaces explicitly send false (metadata only) or true (bounded preview).
- const includeContent = input.includeContentPreview === undefined;
- const includeContentPreview = input.includeContentPreview === true;
- const knowledgeItems = await ctx.knowledgeRepo.query({
- ...input,
- excludeKnowledgeBaseIds,
- includeContent,
- includeContentPreview,
- limit: limit + 1,
- });
-
- // Check if there are more items
- const hasMore = knowledgeItems.length > limit;
-
- // Take only the requested number of items
- const itemsToProcess = hasMore ? knowledgeItems.slice(0, limit) : knowledgeItems;
-
- // Filter out folders from Documents category when in Inbox (no knowledgeBaseId)
- const filteredItems = filterKnowledgeItems(itemsToProcess, input.knowledgeBaseId);
-
- // Process files (add chunk info and async task status)
- const fileItems = filteredItems.filter((item) => item.sourceType === 'file');
- const statusMap = await getKnowledgeItemStatusMap(ctx, fileItems);
-
- // Resolve file access URLs and raw Markdown previews with bounded concurrency:
- // each item may perform Redis/S3 I/O, so serial work is slow while an unbounded
- // Promise.all lets a caller fan out arbitrary object-storage reads.
- const resultItems = await pMap(
- filteredItems,
- async (item) => {
- let contentPreviewSource = item.contentPreviewSource;
- if (
- includeContentPreview &&
- item.sourceType === 'file' &&
- !item.documentId &&
- !contentPreviewSource &&
- item.url &&
- isMarkdownFile(item)
- ) {
- try {
- contentPreviewSource = await ctx.fileService.getFileContent(
- item.url,
- RESOURCE_CONTENT_PREVIEW_SOURCE_LENGTH,
- );
- } catch {
- // Preview failure must not fail the resource list. The derived
- // document will become the normal source once parsing completes.
- }
- }
- const contentPreview = includeContentPreview
- ? createResourceContentPreview({
- content: contentPreviewSource,
- fileType: item.fileType,
- title: item.name,
- })
- : undefined;
-
- if (item.sourceType === 'file') {
- const status = statusMap.get(item.id)!;
- return {
- chunkCount: status.chunkCount,
- chunkingError: status.chunkingError,
- chunkingStatus: status.chunkingStatus,
- ...(includeContent ? { content: item.content } : {}),
- ...(includeContent ? { editorData: null } : {}),
- ...(includeContentPreview ? { contentPreview } : {}),
- createdAt: item.createdAt,
- embeddingError: status.embeddingError,
- embeddingStatus: status.embeddingStatus,
- fileId: item.fileId,
- fileType: item.fileType,
- finishEmbedding: status.finishEmbedding,
- id: item.id,
- metadata: item.metadata,
- name: item.name,
- size: item.size,
- slug: item.slug,
- sourceType: item.sourceType,
- updatedAt: item.updatedAt,
- uploader: item.uploader,
- url: await ctx.fileService.getFileAccessUrl(item),
- userId: item.userId,
- visibility: item.visibility,
- } as FileListItem;
- }
- return {
- chunkCount: null,
- chunkingError: null,
- chunkingStatus: null,
- ...(includeContent ? { content: item.content } : {}),
- ...(includeContent ? { editorData: item.editorData } : {}),
- ...(includeContentPreview ? { contentPreview } : {}),
- createdAt: item.createdAt,
- embeddingError: null,
- embeddingStatus: null,
- fileId: item.fileId,
- fileType: item.fileType,
- finishEmbedding: false,
- id: item.id,
- metadata: item.metadata,
- name: item.name,
- size: item.size,
- slug: item.slug,
- sourceType: item.sourceType,
- updatedAt: item.updatedAt,
- uploader: item.uploader,
- url: item.url ?? '',
- userId: item.userId,
- visibility: item.visibility,
- } as FileListItem;
- },
- { concurrency: KNOWLEDGE_ITEM_RESOLUTION_CONCURRENCY },
- );
-
- return {
- hasMore,
- items: resultItems,
- };
- }),
-
- resolveKnowledgeItemIds: fileProcedure
- .input(QueryFileListSchema)
- .query(async ({ ctx, input }): Promise<{ ids: string[]; total: number }> => {
- if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
- const excludeKnowledgeBaseIds = input.knowledgeBaseId
- ? undefined
- : await getRestrictedKnowledgeBaseIds(ctx);
-
- const ids: string[] = [];
- const batchSize = 500;
- let offset = 0;
- let hasMore = true;
-
- while (hasMore) {
- const knowledgeItems = await ctx.knowledgeRepo.query({
- ...input,
- excludeKnowledgeBaseIds,
- includeContent: false,
- includeContentPreview: false,
- limit: batchSize + 1,
- offset,
- });
-
- const currentHasMore = knowledgeItems.length > batchSize;
- const itemsToProcess = currentHasMore ? knowledgeItems.slice(0, batchSize) : knowledgeItems;
- const filteredItems = filterKnowledgeItems(itemsToProcess, input.knowledgeBaseId);
-
- ids.push(...filteredItems.map((item) => item.id));
-
- offset += itemsToProcess.length;
- hasMore = currentHasMore;
- }
-
- return { ids, total: ids.length };
- }),
-
- deleteKnowledgeItemsByQuery: fileProcedure
- .use(withScopedPermission('file:delete'))
- .input(deleteKnowledgeItemsByQuerySchema)
- .mutation(async ({ ctx, input }): Promise<{ count: number }> => {
- if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
- const excludeKnowledgeBaseIds = input.knowledgeBaseId
- ? undefined
- : await getRestrictedKnowledgeBaseIds(ctx);
- const { excludedIds = [], ...query } = input;
- const excludedIdSet = new Set(excludedIds);
-
- const fileIds: string[] = [];
- const documentIds: string[] = [];
- const batchSize = 500;
- let offset = 0;
- let hasMore = true;
-
- while (hasMore) {
- const knowledgeItems = await ctx.knowledgeRepo.query({
- ...query,
- excludeKnowledgeBaseIds,
- includeContent: false,
- includeContentPreview: false,
- limit: batchSize + 1,
- offset,
- });
-
- const currentHasMore = knowledgeItems.length > batchSize;
- const itemsToProcess = currentHasMore ? knowledgeItems.slice(0, batchSize) : knowledgeItems;
- const filteredItems = filterKnowledgeItems(itemsToProcess, query.knowledgeBaseId).filter(
- (item) => !excludedIdSet.has(item.id),
- );
-
- for (const item of filteredItems) {
- if (item.sourceType === DERIVED_DOCUMENT_SOURCE_TYPE) {
- documentIds.push(item.documentId ?? item.id);
- continue;
- }
-
- if (item.documentId) {
- documentIds.push(item.documentId);
- continue;
- }
-
- fileIds.push(item.fileId ?? item.id);
- }
-
- offset += itemsToProcess.length;
- hasMore = currentHasMore;
- }
-
- await assertContentsNotInRestrictedKnowledgeBase(ctx, [...documentIds, ...fileIds]);
-
- if (documentIds.length > 0) {
- await ctx.documentService.deleteDocuments(documentIds);
- }
-
- if (fileIds.length > 0) {
- const needToRemoveFileList = await ctx.fileModel.deleteMany(
- fileIds,
- serverDBEnv.REMOVE_GLOBAL_FILE,
- );
-
- if (needToRemoveFileList && needToRemoveFileList.length > 0) {
- await ctx.fileService.deleteFiles(needToRemoveFileList.map((file) => file.url!));
- }
- }
-
- return { count: fileIds.length + documentIds.length };
- }),
-
- recentFiles: fileProcedure
- .input(
- z
- .object({
- limit: z.number().max(50).optional(),
- visibility: z.enum(['private', 'public']).optional(),
- })
- .optional(),
- )
- .query(async ({ ctx, input }) => {
- const limit = input?.limit ?? 12;
- // Files only (pages are excluded in SQL, so `limit` can't be eaten by
- // page rows that are then filtered out).
- const fileItems = await ctx.knowledgeRepo.queryRecent(limit, 'file', input?.visibility);
-
- if (fileItems.length === 0) return [];
-
- // Get file IDs for batch processing
- const fileIds = fileItems.map((item) => item.id);
- const chunksArray = await ctx.chunkModel.countByFileIds(fileIds);
- const chunks: Record<string, number> = {};
- for (const item of chunksArray) {
- if (item.id) chunks[item.id] = item.count;
- }
-
- const chunkTaskIds = fileItems.map((item) => item.chunkTaskId).filter(Boolean) as string[];
- const embeddingTaskIds = fileItems
- .map((item) => item.embeddingTaskId)
- .filter(Boolean) as string[];
-
- const [chunkTasks, embeddingTasks] = await Promise.all([
- chunkTaskIds.length > 0
- ? ctx.asyncTaskModel.findByIds(chunkTaskIds, AsyncTaskType.Chunking)
- : Promise.resolve([]),
- embeddingTaskIds.length > 0
- ? ctx.asyncTaskModel.findByIds(embeddingTaskIds, AsyncTaskType.Embedding)
- : Promise.resolve([]),
- ]);
-
- // Build result with task status
- const resultFiles: FileListItem[] = [];
- for (const item of fileItems) {
- const chunkTask = item.chunkTaskId
- ? chunkTasks.find((task) => task.id === item.chunkTaskId)
- : null;
- const embeddingTask = item.embeddingTaskId
- ? embeddingTasks.find((task) => task.id === item.embeddingTaskId)
- : null;
-
- resultFiles.push({
- ...item,
- chunkCount: chunks[item.id] ?? 0,
- chunkingError: chunkTask?.error ?? null,
- chunkingStatus: chunkTask?.status as AsyncTaskStatus,
- embeddingError: embeddingTask?.error ?? null,
- embeddingStatus: embeddingTask?.status as AsyncTaskStatus,
- finishEmbedding: embeddingTask?.status === AsyncTaskStatus.Success,
- sourceType: 'file' as const,
- url: await ctx.fileService.getFileAccessUrl(item),
- } as FileListItem);
- }
-
- return resultFiles;
- }),
-
- recentPages: fileProcedure
- .input(
- z
- .object({
- limit: z.number().max(50).optional(),
- visibility: z.enum(['private', 'public']).optional(),
- })
- .optional(),
- )
- .query(async ({ ctx, input }) => {
- const limit = input?.limit ?? 12;
- // Pages only (folders and files are excluded in SQL, so `limit` can't be
- // eaten by rows that are then filtered out).
- return ctx.knowledgeRepo.queryRecent(limit, 'page', input?.visibility);
- }),
-
- rehostImage: fileProcedure
- .use(withScopedPermission('file:upload'))
- .use(checkFileStorageUsage)
- .input(z.object({ url: z.url() }))
- .mutation(async ({ ctx, input }) => {
- const { buffer, extension, mimeType } = await downloadRemoteImage(input.url);
- const pathname = `images/${ctx.userId}/${randomUUID()}.${extension}`;
- const result = await ctx.fileService.uploadFromBuffer(
- buffer,
- mimeType,
- pathname,
- (transaction) =>
- businessFileUploadCheck({
- actualSize: buffer.length,
- clientIp: ctx.clientIp ?? undefined,
- inputSize: buffer.length,
- transaction,
- url: pathname,
- userId: ctx.userId,
- workspaceId: ctx.workspaceId,
- }),
- { source: FileSource.PageEditor, visibility: 'private' },
- );
- return { fileId: result.fileId, url: result.url };
- }),
-
- removeFile: fileProcedure
- .use(withScopedPermission('file:delete'))
- .input(z.object({ id: z.string() }))
- .mutation(async ({ input, ctx }) => {
- const existing = await ctx.fileModel.findById(input.id);
- if (!existing) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
- await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
-
- const file = await ctx.fileModel.delete(input.id, {
- removeGlobalFile: serverDBEnv.REMOVE_GLOBAL_FILE,
- });
-
- if (!file) return;
-
- // delete the file from S3 if it is not used by other files
- await ctx.fileService.deleteFile(file.url!);
- }),
-
- removeUnreferencedFile: fileProcedure
- .use(withScopedPermission('file:delete'))
- .input(z.object({ id: z.string() }))
- .mutation(async ({ input, ctx }) => {
- const existing = await ctx.fileModel.findById(input.id);
- // Import failure cleanup can run on both server and client; retries are harmless.
- if (!existing) return;
- await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
-
- const file = await ctx.fileModel.deleteUnreferenced(input.id, {
- removeGlobalFile: serverDBEnv.REMOVE_GLOBAL_FILE,
- });
- if (!file) return;
-
- await ctx.fileService.deleteFile(file.url!);
- }),
-
- removeFileAsyncTask: fileProcedure
- .use(withScopedPermission('file:update'))
- .input(
- z.object({
- id: z.string(),
- type: z.enum(['embedding', 'chunk']),
- }),
- )
- .mutation(async ({ ctx, input }) => {
- const file = await ctx.fileModel.findById(input.id);
-
- if (!file) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
- await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
-
- const taskId = input.type === 'embedding' ? file.embeddingTaskId : file.chunkTaskId;
-
- if (!taskId) return;
-
- await ctx.asyncTaskModel.delete(taskId);
- }),
-
- removeFiles: fileProcedure
- .use(withScopedPermission('file:delete'))
- .input(z.object({ ids: z.array(z.string()) }))
- .mutation(async ({ input, ctx }) => {
- const ids = [...new Set(input.ids)];
- const targets = await ctx.fileModel.findByIds(ids);
- assertAllFilesAccessible(ids, targets);
- await Promise.all(
- targets.map((target) => assertFileNotInRestrictedKnowledgeBase(ctx, target.id)),
- );
-
- const needToRemoveFileList = await ctx.fileModel.deleteMany(
- ids,
- serverDBEnv.REMOVE_GLOBAL_FILE,
- );
-
- if (!needToRemoveFileList || needToRemoveFileList.length === 0) return;
-
- // remove from S3
- await ctx.fileService.deleteFiles(needToRemoveFileList.map((file) => file.url!));
- }),
-
- updateFile: fileProcedure
- .use(withScopedPermission('file:update'))
- .input(
- z.object({
- id: z.string(),
- metadata: z.record(z.string(), z.any()).optional(),
- name: z.string().optional(),
- parentId: z.string().nullish(),
- }),
- )
- .mutation(async ({ ctx, input }) => {
- const { id, metadata, name, parentId } = input;
-
- const existing = await ctx.fileModel.findById(id);
- if (!existing) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
- await assertFileNotInRestrictedKnowledgeBase(ctx, id);
-
- // Resolve parentId if it's a slug (otherwise use as-is)
- let resolvedParentId: string | null | undefined = parentId;
- if (parentId) {
- resolvedParentId = (await resolveAccessibleParentDocument(ctx, parentId)).id;
- }
-
- const updates: Parameters<typeof ctx.fileModel.update>[1] = {};
-
- if (metadata !== undefined) {
- updates.metadata = stripAgentShareFileProvenance(metadata);
- }
-
- if (name !== undefined) {
- updates.name = name;
- }
-
- if (parentId !== undefined) {
- updates.parentId = resolvedParentId;
- }
-
- if (Object.keys(updates).length > 0) {
- const wsId = ctx.workspaceId ?? undefined;
- await ctx.serverDB.transaction(async (tx) => {
- const trx = tx as unknown as LobeChatDatabase;
- // The knowledge-base tree reads `documents.parent_id`, so the file's
- // backing document row(s) must move (and rename) together with it.
- // Documents are written before the file, matching updateDocument's
- // lock order so concurrent moves cannot deadlock.
- if (updates.parentId !== undefined || updates.name !== undefined) {
- await new DocumentModel(trx, ctx.userId, wsId).syncFromFile(id, {
- name: updates.name,
- parentId: updates.parentId,
- });
- }
- await new FileModel(trx, ctx.userId, wsId).update(id, updates);
- });
- }
-
- return { success: true };
- }),
-
- publishFileToWorkspace: fileProcedure
- .use(withScopedPermission('file:update'))
- .input(z.object({ id: z.string() }))
- .mutation(async ({ ctx, input }) => {
- // Personal mode has no notion of workspace visibility — publish is only
- // meaningful inside a team workspace.
- if (!ctx.workspaceId) {
- throw new TRPCError({
- code: 'BAD_REQUEST',
- message: 'Cannot publish a file outside of a workspace',
- });
- }
-
- const file = await ctx.fileModel.findById(input.id);
- if (!file) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
-
- if (file.userId !== ctx.userId) {
- throw new TRPCError({
- code: 'FORBIDDEN',
- message: 'Only the creator can publish a private file to the workspace',
- });
- }
-
- if (file.visibility === 'public') return { success: true };
-
- await ctx.fileModel.publishToWorkspace(input.id);
- return { success: true };
- }),
-
- /**
- * Toggle a file's workspace visibility. Creator-only. Personal mode has no
- * workspace visibility concept, so the call is rejected there.
- */
- setFileVisibility: fileProcedure
- .use(withScopedPermission('file:update'))
- .input(
- z.object({
- id: z.string(),
- visibility: z.enum(['private', 'public']),
- }),
- )
- .mutation(async ({ ctx, input }) => {
- if (!ctx.workspaceId) {
- throw new TRPCError({
- code: 'BAD_REQUEST',
- message: 'File visibility only applies inside a workspace',
- });
- }
-
- const file = await ctx.fileModel.findById(input.id);
- if (!file) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
-
- if (file.userId !== ctx.userId) {
- throw new TRPCError({
- code: 'FORBIDDEN',
- message: 'Only the creator can change a file’s visibility',
- });
- }
-
- if (file.visibility === input.visibility) return { success: true };
-
- await ctx.fileModel.setVisibility(input.id, input.visibility);
- return { success: true };
- }),
-
- transferEntity: fileProcedure
- .use(withScopedPermission('file:upload'))
- .input(
- z.object({
- entityType: fileTransferEntityTypeSchema,
- id: z.string(),
- targetVisibility: z.enum(['private', 'public']).optional(),
- targetWorkspaceId: z.string().nullable(),
- }),
- )
- .mutation(async ({ ctx, input }) => {
- if (input.targetWorkspaceId === (ctx.workspaceId ?? null)) {
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.SameWorkspace } },
- code: 'BAD_REQUEST',
- message: 'Cannot transfer to the same workspace',
- });
- }
-
- if (input.targetWorkspaceId) {
- const canWriteTarget = await hasWorkspaceScopedPermission({
- action: 'FILE_UPLOAD',
- db: ctx.serverDB,
- userId: ctx.userId,
- workspaceId: input.targetWorkspaceId,
- });
- if (!canWriteTarget) {
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.TargetNoWriteAccess } },
- code: 'FORBIDDEN',
- message: 'No write access to target workspace',
- });
- }
- }
-
- if (input.entityType === 'folder' || input.entityType === 'document') {
- const document = await ctx.documentModel.findById(input.id);
- if (!document) {
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.ResourceNotFound } },
- code: 'NOT_FOUND',
- message: input.entityType === 'folder' ? 'Folder not found' : 'Document not found',
- });
- }
- // Transfer stays creator-only, mirroring `document.transferDocument`.
- if (ctx.workspaceId) {
- await assertCanPerformResourceAction({
- action: 'transfer',
- db: ctx.serverDB,
- resourceId: input.id,
- resourceType: 'document',
- userId: ctx.userId,
- workspaceId: ctx.workspaceId,
- });
- }
- const additionalSize = await ctx.documentModel.countFileUsageInSubtree(input.id);
- await businessFileTransferStorageCheck({
- additionalSize,
- targetUserId: ctx.userId,
- targetWorkspaceId: input.targetWorkspaceId,
- });
- // The transfer rehomes the entire subtree — a non-owner member must
- // not move teammates' documents/files/likes along with their own
- // folder. The guard runs INSIDE the transfer transaction (after the
- // subtree rows are locked) so content committed between any preflight
- // and the transfer cannot slip past it; see `document.transferDocument`.
- try {
- return await ctx.documentModel.transferTo(
- input.id,
- input.targetWorkspaceId,
- ctx.userId,
- input.targetVisibility,
- { forbidForeignRows: isWorkspaceNonOwner(ctx) },
- );
- } catch (error) {
- if (error instanceof Error && error.message === DOCUMENT_TRANSFER_FOREIGN_ROWS) {
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.OwnerOnly } },
- code: 'FORBIDDEN',
- message: "Only workspace owners can transfer a folder containing others' content",
- });
- }
- throw error;
- }
- }
-
- const file = await ctx.fileModel.findById(input.id);
- if (!file)
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.ResourceNotFound } },
- code: 'NOT_FOUND',
- message: 'File not found',
- });
- assertWorkspaceRowManageable(ctx, file.userId, 'file');
- await businessFileTransferStorageCheck({
- additionalSize: file.size,
- targetUserId: ctx.userId,
- targetWorkspaceId: input.targetWorkspaceId,
- });
- return ctx.fileModel.transferTo(
- input.id,
- input.targetWorkspaceId,
- ctx.userId,
- input.targetVisibility,
- );
- }),
-
- copyEntityToWorkspace: fileProcedure
- .use(withScopedPermission('file:upload'))
- .input(
- z.object({
- entityType: fileTransferEntityTypeSchema,
- id: z.string(),
- targetVisibility: z.enum(['private', 'public']).optional(),
- targetWorkspaceId: z.string().nullable(),
- }),
- )
- .mutation(async ({ ctx, input }) => {
- if (input.targetWorkspaceId) {
- const canWriteTarget = await hasWorkspaceScopedPermission({
- action: 'FILE_UPLOAD',
- db: ctx.serverDB,
- userId: ctx.userId,
- workspaceId: input.targetWorkspaceId,
- });
- if (!canWriteTarget) {
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.TargetNoWriteAccess } },
- code: 'FORBIDDEN',
- message: 'No write access to target workspace',
- });
- }
- }
-
- if (input.entityType === 'folder' || input.entityType === 'document') {
- const document = await ctx.documentModel.findById(input.id);
- if (!document) {
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.ResourceNotFound } },
- code: 'NOT_FOUND',
- message: input.entityType === 'folder' ? 'Folder not found' : 'Document not found',
- });
- }
- const additionalSize = await ctx.documentModel.countFileUsageInSubtree(input.id);
- await businessFileTransferStorageCheck({
- additionalSize,
- targetUserId: ctx.userId,
- targetWorkspaceId: input.targetWorkspaceId,
- });
- return ctx.documentModel.copyToWorkspace(
- input.id,
- input.targetWorkspaceId,
- ctx.userId,
- input.targetVisibility,
- );
- }
-
- const file = await ctx.fileModel.findById(input.id);
- if (!file)
- throw new TRPCError({
- cause: { data: { code: TransferErrorCode.ResourceNotFound } },
- code: 'NOT_FOUND',
- message: 'File not found',
- });
- await businessFileTransferStorageCheck({
- additionalSize: file.size,
- targetUserId: ctx.userId,
- targetWorkspaceId: input.targetWorkspaceId,
- });
- return ctx.fileModel.copyToWorkspace(
- input.id,
- input.targetWorkspaceId,
- ctx.userId,
- input.targetVisibility,
- );
- }),
-});
-
-export type FileRouter = typeof fileRouter;
diff --git a/apps/server/src/routers/lambda/file/index.procedures.test.ts b/apps/server/src/routers/lambda/file/index.procedures.test.ts
new file mode 100644
index 00000000000..cf680403a9e
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/index.procedures.test.ts
@@ -0,0 +1,51 @@
+// @vitest-environment node
+import { describe, expect, it } from 'vitest';
+
+import { fileRouter } from '.';
+
+/**
+ * Pins the router's public surface: every procedure path, its kind, and its
+ * middleware chain length (which encodes the procedure builder and its input
+ * parsers). Splitting the router across files must keep all three.
+ */
+describe('fileRouter procedures', () => {
+ const procedures = (fileRouter as any)._def.procedures as Record<string, any>;
+
+ it('exposes exactly the expected procedure paths', () => {
+ expect(Object.keys(procedures).sort()).toEqual(expected().map(([name]) => name));
+ });
+
+ it.each(expected())('%s is a %s with %i middlewares', (name, type, middlewares) => {
+ expect(procedures[name]._def.type).toBe(type);
+ expect(procedures[name]._def.middlewares).toHaveLength(middlewares);
+ });
+});
+
+function expected(): Array<
+ [name: string, type: 'mutation' | 'query' | 'subscription', middlewares: number]
+> {
+ return [
+ ['checkFileHash', 'mutation', 14],
+ ['copyEntityToWorkspace', 'mutation', 13],
+ ['createFile', 'mutation', 13],
+ ['deleteKnowledgeItemsByQuery', 'mutation', 13],
+ ['findById', 'query', 12],
+ ['getFileItemById', 'query', 12],
+ ['getFiles', 'query', 12],
+ ['getKnowledgeItemStatusesByIds', 'query', 12],
+ ['getKnowledgeItems', 'query', 12],
+ ['getReadableUrl', 'query', 12],
+ ['publishFileToWorkspace', 'mutation', 13],
+ ['recentFiles', 'query', 12],
+ ['recentPages', 'query', 12],
+ ['rehostImage', 'mutation', 14],
+ ['removeFile', 'mutation', 13],
+ ['removeFileAsyncTask', 'mutation', 13],
+ ['removeFiles', 'mutation', 13],
+ ['removeUnreferencedFile', 'mutation', 13],
+ ['resolveKnowledgeItemIds', 'query', 12],
+ ['setFileVisibility', 'mutation', 13],
+ ['transferEntity', 'mutation', 13],
+ ['updateFile', 'mutation', 13],
+ ];
+}
diff --git a/apps/server/src/routers/lambda/file/index.ts b/apps/server/src/routers/lambda/file/index.ts
new file mode 100644
index 00000000000..f1c9d36c6cc
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/index.ts
@@ -0,0 +1,19 @@
+import { mergeRouters } from '@/libs/trpc/lambda';
+
+import { knowledgeItemsRouter } from './knowledgeItems';
+import { queriesRouter } from './queries';
+import { removeRouter } from './remove';
+import { transferRouter } from './transfer';
+import { updateRouter } from './update';
+import { uploadRouter } from './upload';
+
+export const fileRouter = mergeRouters(
+ uploadRouter,
+ queriesRouter,
+ knowledgeItemsRouter,
+ removeRouter,
+ updateRouter,
+ transferRouter,
+);
+
+export type FileRouter = typeof fileRouter;
diff --git a/apps/server/src/routers/lambda/file/knowledgeItems.ts b/apps/server/src/routers/lambda/file/knowledgeItems.ts
new file mode 100644
index 00000000000..d69d07feba2
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/knowledgeItems.ts
@@ -0,0 +1,307 @@
+import {
+ CUSTOM_FOLDER_FILE_TYPE,
+ DERIVED_DOCUMENT_SOURCE_TYPE,
+ MARKDOWN_MIME_TYPES,
+ RESOURCE_CONTENT_PREVIEW_SOURCE_LENGTH,
+} from '@lobechat/const';
+import pMap from 'p-map';
+import { z } from 'zod';
+
+import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
+import { serverDBEnv } from '@/config/db';
+import { router } from '@/libs/trpc/lambda';
+import { createResourceContentPreview } from '@/server/utils/resourceContentPreview';
+import type { FileListItem, KnowledgeItemStatus } from '@/types/files';
+import { QueryFileListSchema } from '@/types/files';
+
+import {
+ assertContentsNotInRestrictedKnowledgeBase,
+ assertKnowledgeBaseBrowsable,
+ getRestrictedKnowledgeBaseIds,
+} from '../_helpers/knowledgeBaseAccess';
+import { fileProcedure, getKnowledgeItemStatusMap } from './procedures';
+
+const deleteKnowledgeItemsByQuerySchema = QueryFileListSchema.extend({
+ excludedIds: z.array(z.string()).optional(),
+});
+
+const markdownPreviewTypes = new Set<string>(MARKDOWN_MIME_TYPES);
+
+const KNOWLEDGE_ITEM_RESOLUTION_CONCURRENCY = 8;
+
+const isMarkdownFile = (item: { fileType: string; name: string }) =>
+ markdownPreviewTypes.has(item.fileType) || /\.md(?:arkdown)?$/i.test(item.name);
+
+const filterKnowledgeItems = <
+ T extends {
+ fileType: string;
+ sourceType: string;
+ },
+>(
+ items: T[],
+ knowledgeBaseId?: string,
+) => {
+ return !knowledgeBaseId
+ ? items.filter(
+ (item) =>
+ !(
+ item.sourceType === DERIVED_DOCUMENT_SOURCE_TYPE &&
+ item.fileType === CUSTOM_FOLDER_FILE_TYPE
+ ),
+ )
+ : items;
+};
+
+export const knowledgeItemsRouter = router({
+ getKnowledgeItemStatusesByIds: fileProcedure
+ .input(
+ z.object({
+ ids: z.array(z.string()),
+ }),
+ )
+ .query(async ({ ctx, input }): Promise<KnowledgeItemStatus[]> => {
+ const ids = [...new Set(input.ids)];
+ if (ids.length === 0) return [];
+
+ const fileItems = await ctx.fileModel.findByIds(ids);
+ const statusMap = await getKnowledgeItemStatusMap(ctx, fileItems);
+
+ return ids.flatMap((id) => {
+ const status = statusMap.get(id);
+ return status ? [status] : [];
+ });
+ }),
+ getKnowledgeItems: fileProcedure.input(QueryFileListSchema).query(async ({ ctx, input }) => {
+ if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
+ const excludeKnowledgeBaseIds = input.knowledgeBaseId
+ ? undefined
+ : await getRestrictedKnowledgeBaseIds(ctx);
+
+ // Request one more item than limit to check if there are more items
+ const limit = input.limit ?? 50;
+ // Absent preserves the legacy response for released clients. Current list
+ // surfaces explicitly send false (metadata only) or true (bounded preview).
+ const includeContent = input.includeContentPreview === undefined;
+ const includeContentPreview = input.includeContentPreview === true;
+ const knowledgeItems = await ctx.knowledgeRepo.query({
+ ...input,
+ excludeKnowledgeBaseIds,
+ includeContent,
+ includeContentPreview,
+ limit: limit + 1,
+ });
+
+ // Check if there are more items
+ const hasMore = knowledgeItems.length > limit;
+
+ // Take only the requested number of items
+ const itemsToProcess = hasMore ? knowledgeItems.slice(0, limit) : knowledgeItems;
+
+ // Filter out folders from Documents category when in Inbox (no knowledgeBaseId)
+ const filteredItems = filterKnowledgeItems(itemsToProcess, input.knowledgeBaseId);
+
+ // Process files (add chunk info and async task status)
+ const fileItems = filteredItems.filter((item) => item.sourceType === 'file');
+ const statusMap = await getKnowledgeItemStatusMap(ctx, fileItems);
+
+ // Resolve file access URLs and raw Markdown previews with bounded concurrency:
+ // each item may perform Redis/S3 I/O, so serial work is slow while an unbounded
+ // Promise.all lets a caller fan out arbitrary object-storage reads.
+ const resultItems = await pMap(
+ filteredItems,
+ async (item) => {
+ let contentPreviewSource = item.contentPreviewSource;
+ if (
+ includeContentPreview &&
+ item.sourceType === 'file' &&
+ !item.documentId &&
+ !contentPreviewSource &&
+ item.url &&
+ isMarkdownFile(item)
+ ) {
+ try {
+ contentPreviewSource = await ctx.fileService.getFileContent(
+ item.url,
+ RESOURCE_CONTENT_PREVIEW_SOURCE_LENGTH,
+ );
+ } catch {
+ // Preview failure must not fail the resource list. The derived
+ // document will become the normal source once parsing completes.
+ }
+ }
+ const contentPreview = includeContentPreview
+ ? createResourceContentPreview({
+ content: contentPreviewSource,
+ fileType: item.fileType,
+ title: item.name,
+ })
+ : undefined;
+
+ if (item.sourceType === 'file') {
+ const status = statusMap.get(item.id)!;
+ return {
+ chunkCount: status.chunkCount,
+ chunkingError: status.chunkingError,
+ chunkingStatus: status.chunkingStatus,
+ ...(includeContent ? { content: item.content } : {}),
+ ...(includeContent ? { editorData: null } : {}),
+ ...(includeContentPreview ? { contentPreview } : {}),
+ createdAt: item.createdAt,
+ embeddingError: status.embeddingError,
+ embeddingStatus: status.embeddingStatus,
+ fileId: item.fileId,
+ fileType: item.fileType,
+ finishEmbedding: status.finishEmbedding,
+ id: item.id,
+ metadata: item.metadata,
+ name: item.name,
+ size: item.size,
+ slug: item.slug,
+ sourceType: item.sourceType,
+ updatedAt: item.updatedAt,
+ uploader: item.uploader,
+ url: await ctx.fileService.getFileAccessUrl(item),
+ userId: item.userId,
+ visibility: item.visibility,
+ } as FileListItem;
+ }
+ return {
+ chunkCount: null,
+ chunkingError: null,
+ chunkingStatus: null,
+ ...(includeContent ? { content: item.content } : {}),
+ ...(includeContent ? { editorData: item.editorData } : {}),
+ ...(includeContentPreview ? { contentPreview } : {}),
+ createdAt: item.createdAt,
+ embeddingError: null,
+ embeddingStatus: null,
+ fileId: item.fileId,
+ fileType: item.fileType,
+ finishEmbedding: false,
+ id: item.id,
+ metadata: item.metadata,
+ name: item.name,
+ size: item.size,
+ slug: item.slug,
+ sourceType: item.sourceType,
+ updatedAt: item.updatedAt,
+ uploader: item.uploader,
+ url: item.url ?? '',
+ userId: item.userId,
+ visibility: item.visibility,
+ } as FileListItem;
+ },
+ { concurrency: KNOWLEDGE_ITEM_RESOLUTION_CONCURRENCY },
+ );
+
+ return {
+ hasMore,
+ items: resultItems,
+ };
+ }),
+ resolveKnowledgeItemIds: fileProcedure
+ .input(QueryFileListSchema)
+ .query(async ({ ctx, input }): Promise<{ ids: string[]; total: number }> => {
+ if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
+ const excludeKnowledgeBaseIds = input.knowledgeBaseId
+ ? undefined
+ : await getRestrictedKnowledgeBaseIds(ctx);
+
+ const ids: string[] = [];
+ const batchSize = 500;
+ let offset = 0;
+ let hasMore = true;
+
+ while (hasMore) {
+ const knowledgeItems = await ctx.knowledgeRepo.query({
+ ...input,
+ excludeKnowledgeBaseIds,
+ includeContent: false,
+ includeContentPreview: false,
+ limit: batchSize + 1,
+ offset,
+ });
+
+ const currentHasMore = knowledgeItems.length > batchSize;
+ const itemsToProcess = currentHasMore ? knowledgeItems.slice(0, batchSize) : knowledgeItems;
+ const filteredItems = filterKnowledgeItems(itemsToProcess, input.knowledgeBaseId);
+
+ ids.push(...filteredItems.map((item) => item.id));
+
+ offset += itemsToProcess.length;
+ hasMore = currentHasMore;
+ }
+
+ return { ids, total: ids.length };
+ }),
+ deleteKnowledgeItemsByQuery: fileProcedure
+ .use(withScopedPermission('file:delete'))
+ .input(deleteKnowledgeItemsByQuerySchema)
+ .mutation(async ({ ctx, input }): Promise<{ count: number }> => {
+ if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
+ const excludeKnowledgeBaseIds = input.knowledgeBaseId
+ ? undefined
+ : await getRestrictedKnowledgeBaseIds(ctx);
+ const { excludedIds = [], ...query } = input;
+ const excludedIdSet = new Set(excludedIds);
+
+ const fileIds: string[] = [];
+ const documentIds: string[] = [];
+ const batchSize = 500;
+ let offset = 0;
+ let hasMore = true;
+
+ while (hasMore) {
+ const knowledgeItems = await ctx.knowledgeRepo.query({
+ ...query,
+ excludeKnowledgeBaseIds,
+ includeContent: false,
+ includeContentPreview: false,
+ limit: batchSize + 1,
+ offset,
+ });
+
+ const currentHasMore = knowledgeItems.length > batchSize;
+ const itemsToProcess = currentHasMore ? knowledgeItems.slice(0, batchSize) : knowledgeItems;
+ const filteredItems = filterKnowledgeItems(itemsToProcess, query.knowledgeBaseId).filter(
+ (item) => !excludedIdSet.has(item.id),
+ );
+
+ for (const item of filteredItems) {
+ if (item.sourceType === DERIVED_DOCUMENT_SOURCE_TYPE) {
+ documentIds.push(item.documentId ?? item.id);
+ continue;
+ }
+
+ if (item.documentId) {
+ documentIds.push(item.documentId);
+ continue;
+ }
+
+ fileIds.push(item.fileId ?? item.id);
+ }
+
+ offset += itemsToProcess.length;
+ hasMore = currentHasMore;
+ }
+
+ await assertContentsNotInRestrictedKnowledgeBase(ctx, [...documentIds, ...fileIds]);
+
+ if (documentIds.length > 0) {
+ await ctx.documentService.deleteDocuments(documentIds);
+ }
+
+ if (fileIds.length > 0) {
+ const needToRemoveFileList = await ctx.fileModel.deleteMany(
+ fileIds,
+ serverDBEnv.REMOVE_GLOBAL_FILE,
+ );
+
+ if (needToRemoveFileList && needToRemoveFileList.length > 0) {
+ await ctx.fileService.deleteFiles(needToRemoveFileList.map((file) => file.url!));
+ }
+ }
+
+ return { count: fileIds.length + documentIds.length };
+ }),
+});
diff --git a/apps/server/src/routers/lambda/file/procedures.ts b/apps/server/src/routers/lambda/file/procedures.ts
new file mode 100644
index 00000000000..dd737c1d359
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/procedures.ts
@@ -0,0 +1,118 @@
+import { TRPCError } from '@trpc/server';
+
+import { wsCompatProcedure } from '@/business/server/trpc-middlewares/workspaceAuth';
+import { AsyncTaskModel } from '@/database/models/asyncTask';
+import { ChunkModel } from '@/database/models/chunk';
+import { DocumentModel } from '@/database/models/document';
+import { FileModel } from '@/database/models/file';
+import { KnowledgeBaseModel } from '@/database/models/knowledgeBase';
+import { KnowledgeRepo } from '@/database/repositories/knowledge';
+import { serverDatabase } from '@/libs/trpc/lambda/middleware';
+import { DocumentService } from '@/server/services/document';
+import { FileService } from '@/server/services/file';
+import { FileUploadService } from '@/server/services/fileUpload';
+import { AsyncTaskStatus, AsyncTaskType, type IAsyncTaskError } from '@/types/asyncTask';
+import type { KnowledgeItemStatus } from '@/types/files';
+
+import type { KnowledgeBaseAccessCtx } from '../_helpers/knowledgeBaseAccess';
+import { assertContentsNotInRestrictedKnowledgeBase } from '../_helpers/knowledgeBaseAccess';
+
+export const fileProcedure = wsCompatProcedure.use(serverDatabase).use(async (opts) => {
+ const { ctx } = opts;
+ const wsId = ctx.workspaceId ?? undefined;
+
+ return opts.next({
+ ctx: {
+ asyncTaskModel: new AsyncTaskModel(ctx.serverDB, ctx.userId, wsId),
+ chunkModel: new ChunkModel(ctx.serverDB, ctx.userId, wsId),
+ documentModel: new DocumentModel(ctx.serverDB, ctx.userId, wsId),
+ documentService: new DocumentService(ctx.serverDB, ctx.userId, wsId),
+ fileModel: new FileModel(ctx.serverDB, ctx.userId, wsId),
+ fileService: new FileService(ctx.serverDB, ctx.userId, wsId),
+ fileUploadService: new FileUploadService(ctx.serverDB, ctx.userId, wsId),
+ knowledgeBaseModel: new KnowledgeBaseModel(ctx.serverDB, ctx.userId, wsId),
+ knowledgeRepo: new KnowledgeRepo(ctx.serverDB, ctx.userId, wsId),
+ },
+ });
+});
+
+export const getKnowledgeItemStatusMap = async (
+ ctx: {
+ asyncTaskModel: AsyncTaskModel;
+ chunkModel: ChunkModel;
+ },
+ fileItems: Array<{
+ chunkTaskId?: string | null;
+ embeddingTaskId?: string | null;
+ id: string;
+ }>,
+): Promise<Map<string, KnowledgeItemStatus>> => {
+ if (fileItems.length === 0) return new Map();
+
+ const fileIds = fileItems.map((item) => item.id);
+ const chunkTaskIds = [
+ ...new Set(fileItems.map((item) => item.chunkTaskId).filter(Boolean)),
+ ] as string[];
+ const embeddingTaskIds = [
+ ...new Set(fileItems.map((item) => item.embeddingTaskId).filter(Boolean)),
+ ] as string[];
+
+ const [chunks, chunkTasks, embeddingTasks] = await Promise.all([
+ ctx.chunkModel.countByFileIds(fileIds),
+ chunkTaskIds.length > 0
+ ? ctx.asyncTaskModel.findByIds(chunkTaskIds, AsyncTaskType.Chunking)
+ : Promise.resolve([]),
+ embeddingTaskIds.length > 0
+ ? ctx.asyncTaskModel.findByIds(embeddingTaskIds, AsyncTaskType.Embedding)
+ : Promise.resolve([]),
+ ]);
+
+ const chunkRows = chunks ?? [];
+ const chunkTaskRows = chunkTasks ?? [];
+ const embeddingTaskRows = embeddingTasks ?? [];
+
+ const chunkCountMap = new Map(
+ chunkRows.filter((item) => item.id).map((item) => [item.id, item.count] as const),
+ );
+ const chunkTaskMap = new Map(chunkTaskRows.map((task) => [task.id, task] as const));
+ const embeddingTaskMap = new Map(embeddingTaskRows.map((task) => [task.id, task] as const));
+
+ return new Map(
+ fileItems.map((item) => {
+ const chunkTask = item.chunkTaskId ? chunkTaskMap.get(item.chunkTaskId) : null;
+ const embeddingTask = item.embeddingTaskId
+ ? embeddingTaskMap.get(item.embeddingTaskId)
+ : null;
+
+ return [
+ item.id,
+ {
+ chunkCount: chunkCountMap.get(item.id) ?? null,
+ chunkingError: (chunkTask?.error as IAsyncTaskError | null | undefined) ?? null,
+ chunkingStatus: (chunkTask?.status as AsyncTaskStatus | null | undefined) ?? null,
+ embeddingError: (embeddingTask?.error as IAsyncTaskError | null | undefined) ?? null,
+ embeddingStatus: (embeddingTask?.status as AsyncTaskStatus | null | undefined) ?? null,
+ finishEmbedding: embeddingTask?.status === AsyncTaskStatus.Success,
+ id: item.id,
+ },
+ ] as const;
+ }),
+ );
+};
+
+export const resolveAccessibleParentDocument = async (
+ ctx: KnowledgeBaseAccessCtx & {
+ documentModel: Pick<DocumentModel, 'findById' | 'findBySlug'>;
+ },
+ parentId: string,
+) => {
+ const parentDocument =
+ (await ctx.documentModel.findBySlug(parentId)) ?? (await ctx.documentModel.findById(parentId));
+
+ if (!parentDocument) {
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Parent document not found' });
+ }
+
+ await assertContentsNotInRestrictedKnowledgeBase(ctx, [parentDocument.id]);
+ return parentDocument;
+};
diff --git a/apps/server/src/routers/lambda/file/queries.ts b/apps/server/src/routers/lambda/file/queries.ts
new file mode 100644
index 00000000000..6cefddc8b22
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/queries.ts
@@ -0,0 +1,208 @@
+import { TRPCError } from '@trpc/server';
+import { z } from 'zod';
+
+import { router } from '@/libs/trpc/lambda';
+import { AsyncTaskStatus, AsyncTaskType } from '@/types/asyncTask';
+import type { FileListItem } from '@/types/files';
+import { QueryFileListSchema } from '@/types/files';
+
+import {
+ assertFileNotInRestrictedKnowledgeBase,
+ assertKnowledgeBaseBrowsable,
+ getRestrictedKnowledgeBaseIds,
+} from '../_helpers/knowledgeBaseAccess';
+import { fileProcedure, getKnowledgeItemStatusMap } from './procedures';
+
+export const queriesRouter = router({
+ findById: fileProcedure
+ .input(
+ z.object({
+ id: z.string(),
+ }),
+ )
+ .query(async ({ ctx, input }) => {
+ const item = await ctx.fileModel.findById(input.id);
+ if (!item) throw new TRPCError({ code: 'BAD_REQUEST', message: 'File not found' });
+
+ await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
+
+ return {
+ chunkTaskId: item.chunkTaskId,
+ clientId: item.clientId,
+ createdAt: item.createdAt,
+ embeddingTaskId: item.embeddingTaskId,
+ fileHash: item.fileHash,
+ fileType: item.fileType,
+ id: item.id,
+ knowledgeBaseIds: await ctx.fileModel.findKnowledgeBaseIds(item.id),
+ metadata: item.metadata,
+ name: item.name,
+ parentId: item.parentId,
+ size: item.size,
+ source: item.source,
+ updatedAt: item.updatedAt,
+ url: await ctx.fileService.getFileAccessUrl(item),
+ userId: item.userId,
+ visibility: item.visibility,
+ };
+ }),
+ /**
+ * Direct storage URL for reading a file's bytes in the browser (canvas
+ * export). The `/f/:id` proxy answers with a cross-origin redirect, which
+ * drops the request's Origin so bucket CORS can never allow it; fetching the
+ * storage URL directly keeps the Origin the bucket already allows for uploads.
+ */
+ getReadableUrl: fileProcedure
+ .input(
+ z.object({
+ id: z.string(),
+ }),
+ )
+ .query(async ({ ctx, input }) => {
+ const item = await ctx.fileModel.findById(input.id);
+ if (!item) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
+
+ await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
+
+ return { url: await ctx.fileService.getFullFileUrl(item.url) };
+ }),
+ getFileItemById: fileProcedure
+ .input(
+ z.object({
+ id: z.string(),
+ }),
+ )
+ .query(async ({ ctx, input }): Promise<FileListItem | undefined> => {
+ const item = await ctx.fileModel.findById(input.id);
+
+ if (!item) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
+
+ await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
+
+ const statusMap = await getKnowledgeItemStatusMap(ctx, [item]);
+ const status = statusMap.get(item.id)!;
+
+ return {
+ createdAt: item.createdAt,
+ chunkCount: status.chunkCount ?? null,
+ chunkingError: status.chunkingError,
+ chunkingStatus: status.chunkingStatus,
+ embeddingError: status.embeddingError,
+ embeddingStatus: status.embeddingStatus,
+ fileType: item.fileType,
+ finishEmbedding: status.finishEmbedding ?? false,
+ id: item.id,
+ metadata: item.metadata as Record<string, any> | null | undefined,
+ name: item.name,
+ size: item.size,
+ sourceType: 'file' as const,
+ updatedAt: item.updatedAt,
+ url: await ctx.fileService.getFileAccessUrl(item),
+ userId: item.userId,
+ visibility: item.visibility,
+ };
+ }),
+ getFiles: fileProcedure.input(QueryFileListSchema).query(async ({ ctx, input }) => {
+ if (input.knowledgeBaseId) await assertKnowledgeBaseBrowsable(ctx, input.knowledgeBaseId);
+ const excludeKnowledgeBaseIds =
+ !input.knowledgeBaseId && input.showFilesInKnowledgeBase
+ ? await getRestrictedKnowledgeBaseIds(ctx)
+ : undefined;
+
+ const fileList = await ctx.fileModel.query({ ...input, excludeKnowledgeBaseIds });
+ const statusMap = await getKnowledgeItemStatusMap(ctx, fileList);
+
+ const resultFiles = [] as any[];
+ for (const item of fileList as any[]) {
+ const status = statusMap.get(item.id)!;
+ const fileItem = {
+ ...item,
+ sourceType: 'file' as const,
+ url: await ctx.fileService.getFileAccessUrl(item),
+ ...status,
+ } as FileListItem;
+ resultFiles.push(fileItem);
+ }
+
+ return resultFiles;
+ }),
+ recentFiles: fileProcedure
+ .input(
+ z
+ .object({
+ limit: z.number().max(50).optional(),
+ visibility: z.enum(['private', 'public']).optional(),
+ })
+ .optional(),
+ )
+ .query(async ({ ctx, input }) => {
+ const limit = input?.limit ?? 12;
+ // Files only (pages are excluded in SQL, so `limit` can't be eaten by
+ // page rows that are then filtered out).
+ const fileItems = await ctx.knowledgeRepo.queryRecent(limit, 'file', input?.visibility);
+
+ if (fileItems.length === 0) return [];
+
+ // Get file IDs for batch processing
+ const fileIds = fileItems.map((item) => item.id);
+ const chunksArray = await ctx.chunkModel.countByFileIds(fileIds);
+ const chunks: Record<string, number> = {};
+ for (const item of chunksArray) {
+ if (item.id) chunks[item.id] = item.count;
+ }
+
+ const chunkTaskIds = fileItems.map((item) => item.chunkTaskId).filter(Boolean) as string[];
+ const embeddingTaskIds = fileItems
+ .map((item) => item.embeddingTaskId)
+ .filter(Boolean) as string[];
+
+ const [chunkTasks, embeddingTasks] = await Promise.all([
+ chunkTaskIds.length > 0
+ ? ctx.asyncTaskModel.findByIds(chunkTaskIds, AsyncTaskType.Chunking)
+ : Promise.resolve([]),
+ embeddingTaskIds.length > 0
+ ? ctx.asyncTaskModel.findByIds(embeddingTaskIds, AsyncTaskType.Embedding)
+ : Promise.resolve([]),
+ ]);
+
+ // Build result with task status
+ const resultFiles: FileListItem[] = [];
+ for (const item of fileItems) {
+ const chunkTask = item.chunkTaskId
+ ? chunkTasks.find((task) => task.id === item.chunkTaskId)
+ : null;
+ const embeddingTask = item.embeddingTaskId
+ ? embeddingTasks.find((task) => task.id === item.embeddingTaskId)
+ : null;
+
+ resultFiles.push({
+ ...item,
+ chunkCount: chunks[item.id] ?? 0,
+ chunkingError: chunkTask?.error ?? null,
+ chunkingStatus: chunkTask?.status as AsyncTaskStatus,
+ embeddingError: embeddingTask?.error ?? null,
+ embeddingStatus: embeddingTask?.status as AsyncTaskStatus,
+ finishEmbedding: embeddingTask?.status === AsyncTaskStatus.Success,
+ sourceType: 'file' as const,
+ url: await ctx.fileService.getFileAccessUrl(item),
+ } as FileListItem);
+ }
+
+ return resultFiles;
+ }),
+ recentPages: fileProcedure
+ .input(
+ z
+ .object({
+ limit: z.number().max(50).optional(),
+ visibility: z.enum(['private', 'public']).optional(),
+ })
+ .optional(),
+ )
+ .query(async ({ ctx, input }) => {
+ const limit = input?.limit ?? 12;
+ // Pages only (folders and files are excluded in SQL, so `limit` can't be
+ // eaten by rows that are then filtered out).
+ return ctx.knowledgeRepo.queryRecent(limit, 'page', input?.visibility);
+ }),
+});
diff --git a/apps/server/src/routers/lambda/file/remove.test.ts b/apps/server/src/routers/lambda/file/remove.test.ts
new file mode 100644
index 00000000000..54ac39bca13
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/remove.test.ts
@@ -0,0 +1,47 @@
+// @vitest-environment node
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { assertFileNotInRestrictedKnowledgeBase } from '../_helpers/knowledgeBaseAccess';
+import { assertFilesNotInRestrictedKnowledgeBase, FILE_ACCESS_CHECK_CONCURRENCY } from './remove';
+
+vi.mock('../_helpers/knowledgeBaseAccess', () => ({
+ assertFileNotInRestrictedKnowledgeBase: vi.fn(),
+}));
+
+const ctx = { workspaceId: 'ws-1' } as Parameters<
+ typeof assertFilesNotInRestrictedKnowledgeBase
+>[0];
+const files = Array.from({ length: 12 }, (_, i) => ({ id: `file-${i}` }));
+
+describe('assertFilesNotInRestrictedKnowledgeBase', () => {
+ beforeEach(() => {
+ vi.mocked(assertFileNotInRestrictedKnowledgeBase).mockReset();
+ });
+
+ it('checks every file and caps the checks in flight', async () => {
+ let inFlight = 0;
+ let maxInFlight = 0;
+ vi.mocked(assertFileNotInRestrictedKnowledgeBase).mockImplementation(async () => {
+ inFlight += 1;
+ maxInFlight = Math.max(maxInFlight, inFlight);
+ await new Promise((resolve) => setTimeout(resolve, 1));
+ inFlight -= 1;
+ });
+
+ await assertFilesNotInRestrictedKnowledgeBase(ctx, files);
+
+ expect(vi.mocked(assertFileNotInRestrictedKnowledgeBase).mock.calls).toEqual(
+ files.map((file) => [ctx, file.id]),
+ );
+ expect(maxInFlight).toBe(FILE_ACCESS_CHECK_CONCURRENCY);
+ });
+
+ it('rejects with the first restricted-file error', async () => {
+ const error = new Error('restricted');
+ vi.mocked(assertFileNotInRestrictedKnowledgeBase).mockImplementation(async (_ctx, id) => {
+ if (id === 'file-3') throw error;
+ });
+
+ await expect(assertFilesNotInRestrictedKnowledgeBase(ctx, files)).rejects.toBe(error);
+ });
+});
diff --git a/apps/server/src/routers/lambda/file/remove.ts b/apps/server/src/routers/lambda/file/remove.ts
new file mode 100644
index 00000000000..8a39388c97f
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/remove.ts
@@ -0,0 +1,107 @@
+import { TRPCError } from '@trpc/server';
+import pMap from 'p-map';
+import { z } from 'zod';
+
+import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
+import { serverDBEnv } from '@/config/db';
+import { router } from '@/libs/trpc/lambda';
+
+import { assertFileNotInRestrictedKnowledgeBase } from '../_helpers/knowledgeBaseAccess';
+import { fileProcedure } from './procedures';
+
+/** Caps the per-file knowledge-base access checks issued by a bulk delete. */
+export const FILE_ACCESS_CHECK_CONCURRENCY = 5;
+
+export const assertFilesNotInRestrictedKnowledgeBase = async (
+ ctx: Parameters<typeof assertFileNotInRestrictedKnowledgeBase>[0],
+ files: Array<{ id: string }>,
+): Promise<void> => {
+ await pMap(files, (file) => assertFileNotInRestrictedKnowledgeBase(ctx, file.id), {
+ concurrency: FILE_ACCESS_CHECK_CONCURRENCY,
+ });
+};
+
+const assertAllFilesAccessible = (requestedIds: string[], files: Array<{ id: string }>): void => {
+ const accessibleIds = new Set(files.map((file) => file.id));
+ if ([...new Set(requestedIds)].some((id) => !accessibleIds.has(id))) {
+ throw new TRPCError({
+ code: 'NOT_FOUND',
+ message: 'One or more files were not found or are not accessible',
+ });
+ }
+};
+
+export const removeRouter = router({
+ removeFile: fileProcedure
+ .use(withScopedPermission('file:delete'))
+ .input(z.object({ id: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ const existing = await ctx.fileModel.findById(input.id);
+ if (!existing) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
+ await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
+
+ const file = await ctx.fileModel.delete(input.id, {
+ removeGlobalFile: serverDBEnv.REMOVE_GLOBAL_FILE,
+ });
+
+ if (!file) return;
+
+ // delete the file from S3 if it is not used by other files
+ await ctx.fileService.deleteFile(file.url!);
+ }),
+ removeUnreferencedFile: fileProcedure
+ .use(withScopedPermission('file:delete'))
+ .input(z.object({ id: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ const existing = await ctx.fileModel.findById(input.id);
+ // Import failure cleanup can run on both server and client; retries are harmless.
+ if (!existing) return;
+ await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
+
+ const file = await ctx.fileModel.deleteUnreferenced(input.id, {
+ removeGlobalFile: serverDBEnv.REMOVE_GLOBAL_FILE,
+ });
+ if (!file) return;
+
+ await ctx.fileService.deleteFile(file.url!);
+ }),
+ removeFileAsyncTask: fileProcedure
+ .use(withScopedPermission('file:update'))
+ .input(
+ z.object({
+ id: z.string(),
+ type: z.enum(['embedding', 'chunk']),
+ }),
+ )
+ .mutation(async ({ ctx, input }) => {
+ const file = await ctx.fileModel.findById(input.id);
+
+ if (!file) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
+ await assertFileNotInRestrictedKnowledgeBase(ctx, input.id);
+
+ const taskId = input.type === 'embedding' ? file.embeddingTaskId : file.chunkTaskId;
+
+ if (!taskId) return;
+
+ await ctx.asyncTaskModel.delete(taskId);
+ }),
+ removeFiles: fileProcedure
+ .use(withScopedPermission('file:delete'))
+ .input(z.object({ ids: z.array(z.string()) }))
+ .mutation(async ({ input, ctx }) => {
+ const ids = [...new Set(input.ids)];
+ const targets = await ctx.fileModel.findByIds(ids);
+ assertAllFilesAccessible(ids, targets);
+ await assertFilesNotInRestrictedKnowledgeBase(ctx, targets);
+
+ const needToRemoveFileList = await ctx.fileModel.deleteMany(
+ ids,
+ serverDBEnv.REMOVE_GLOBAL_FILE,
+ );
+
+ if (!needToRemoveFileList || needToRemoveFileList.length === 0) return;
+
+ // remove from S3
+ await ctx.fileService.deleteFiles(needToRemoveFileList.map((file) => file.url!));
+ }),
+});
diff --git a/apps/server/src/routers/lambda/file/transfer.ts b/apps/server/src/routers/lambda/file/transfer.ts
new file mode 100644
index 00000000000..e8e3bbe807a
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/transfer.ts
@@ -0,0 +1,196 @@
+import { TRPCError } from '@trpc/server';
+import { z } from 'zod';
+
+import { businessFileTransferStorageCheck } from '@/business/server/lambda-routers/file';
+import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
+import { DOCUMENT_TRANSFER_FOREIGN_ROWS } from '@/database/models/document';
+import { router } from '@/libs/trpc/lambda';
+import { assertCanPerformResourceAction } from '@/server/services/resourcePermission';
+import { hasWorkspaceScopedPermission } from '@/server/services/workspacePermission';
+import { TransferErrorCode } from '@/types/transferError';
+
+import {
+ assertWorkspaceRowManageable,
+ isWorkspaceNonOwner,
+} from '../_helpers/assertWorkspaceRowManageable';
+import { fileProcedure } from './procedures';
+
+const fileTransferEntityTypeSchema = z.enum(['document', 'file', 'folder']);
+
+export const transferRouter = router({
+ transferEntity: fileProcedure
+ .use(withScopedPermission('file:upload'))
+ .input(
+ z.object({
+ entityType: fileTransferEntityTypeSchema,
+ id: z.string(),
+ targetVisibility: z.enum(['private', 'public']).optional(),
+ targetWorkspaceId: z.string().nullable(),
+ }),
+ )
+ .mutation(async ({ ctx, input }) => {
+ if (input.targetWorkspaceId === (ctx.workspaceId ?? null)) {
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.SameWorkspace } },
+ code: 'BAD_REQUEST',
+ message: 'Cannot transfer to the same workspace',
+ });
+ }
+
+ if (input.targetWorkspaceId) {
+ const canWriteTarget = await hasWorkspaceScopedPermission({
+ action: 'FILE_UPLOAD',
+ db: ctx.serverDB,
+ userId: ctx.userId,
+ workspaceId: input.targetWorkspaceId,
+ });
+ if (!canWriteTarget) {
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.TargetNoWriteAccess } },
+ code: 'FORBIDDEN',
+ message: 'No write access to target workspace',
+ });
+ }
+ }
+
+ if (input.entityType === 'folder' || input.entityType === 'document') {
+ const document = await ctx.documentModel.findById(input.id);
+ if (!document) {
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.ResourceNotFound } },
+ code: 'NOT_FOUND',
+ message: input.entityType === 'folder' ? 'Folder not found' : 'Document not found',
+ });
+ }
+ // Transfer stays creator-only, mirroring `document.transferDocument`.
+ if (ctx.workspaceId) {
+ await assertCanPerformResourceAction({
+ action: 'transfer',
+ db: ctx.serverDB,
+ resourceId: input.id,
+ resourceType: 'document',
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId,
+ });
+ }
+ const additionalSize = await ctx.documentModel.countFileUsageInSubtree(input.id);
+ await businessFileTransferStorageCheck({
+ additionalSize,
+ targetUserId: ctx.userId,
+ targetWorkspaceId: input.targetWorkspaceId,
+ });
+ // The transfer rehomes the entire subtree — a non-owner member must
+ // not move teammates' documents/files/likes along with their own
+ // folder. The guard runs INSIDE the transfer transaction (after the
+ // subtree rows are locked) so content committed between any preflight
+ // and the transfer cannot slip past it; see `document.transferDocument`.
+ try {
+ return await ctx.documentModel.transferTo(
+ input.id,
+ input.targetWorkspaceId,
+ ctx.userId,
+ input.targetVisibility,
+ { forbidForeignRows: isWorkspaceNonOwner(ctx) },
+ );
+ } catch (error) {
+ if (error instanceof Error && error.message === DOCUMENT_TRANSFER_FOREIGN_ROWS) {
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.OwnerOnly } },
+ code: 'FORBIDDEN',
+ message: "Only workspace owners can transfer a folder containing others' content",
+ });
+ }
+ throw error;
+ }
+ }
+
+ const file = await ctx.fileModel.findById(input.id);
+ if (!file)
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.ResourceNotFound } },
+ code: 'NOT_FOUND',
+ message: 'File not found',
+ });
+ assertWorkspaceRowManageable(ctx, file.userId, 'file');
+ await businessFileTransferStorageCheck({
+ additionalSize: file.size,
+ targetUserId: ctx.userId,
+ targetWorkspaceId: input.targetWorkspaceId,
+ });
+ return ctx.fileModel.transferTo(
+ input.id,
+ input.targetWorkspaceId,
+ ctx.userId,
+ input.targetVisibility,
+ );
+ }),
+ copyEntityToWorkspace: fileProcedure
+ .use(withScopedPermission('file:upload'))
+ .input(
+ z.object({
+ entityType: fileTransferEntityTypeSchema,
+ id: z.string(),
+ targetVisibility: z.enum(['private', 'public']).optional(),
+ targetWorkspaceId: z.string().nullable(),
+ }),
+ )
+ .mutation(async ({ ctx, input }) => {
+ if (input.targetWorkspaceId) {
+ const canWriteTarget = await hasWorkspaceScopedPermission({
+ action: 'FILE_UPLOAD',
+ db: ctx.serverDB,
+ userId: ctx.userId,
+ workspaceId: input.targetWorkspaceId,
+ });
+ if (!canWriteTarget) {
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.TargetNoWriteAccess } },
+ code: 'FORBIDDEN',
+ message: 'No write access to target workspace',
+ });
+ }
+ }
+
+ if (input.entityType === 'folder' || input.entityType === 'document') {
+ const document = await ctx.documentModel.findById(input.id);
+ if (!document) {
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.ResourceNotFound } },
+ code: 'NOT_FOUND',
+ message: input.entityType === 'folder' ? 'Folder not found' : 'Document not found',
+ });
+ }
+ const additionalSize = await ctx.documentModel.countFileUsageInSubtree(input.id);
+ await businessFileTransferStorageCheck({
+ additionalSize,
+ targetUserId: ctx.userId,
+ targetWorkspaceId: input.targetWorkspaceId,
+ });
+ return ctx.documentModel.copyToWorkspace(
+ input.id,
+ input.targetWorkspaceId,
+ ctx.userId,
+ input.targetVisibility,
+ );
+ }
+
+ const file = await ctx.fileModel.findById(input.id);
+ if (!file)
+ throw new TRPCError({
+ cause: { data: { code: TransferErrorCode.ResourceNotFound } },
+ code: 'NOT_FOUND',
+ message: 'File not found',
+ });
+ await businessFileTransferStorageCheck({
+ additionalSize: file.size,
+ targetUserId: ctx.userId,
+ targetWorkspaceId: input.targetWorkspaceId,
+ });
+ return ctx.fileModel.copyToWorkspace(
+ input.id,
+ input.targetWorkspaceId,
+ ctx.userId,
+ input.targetVisibility,
+ );
+ }),
+});
diff --git a/apps/server/src/routers/lambda/file/update.ts b/apps/server/src/routers/lambda/file/update.ts
new file mode 100644
index 00000000000..74d4cd21adc
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/update.ts
@@ -0,0 +1,135 @@
+import { type LobeChatDatabase } from '@lobechat/database';
+import { TRPCError } from '@trpc/server';
+import { z } from 'zod';
+
+import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
+import { DocumentModel } from '@/database/models/document';
+import { FileModel } from '@/database/models/file';
+import { router } from '@/libs/trpc/lambda';
+import { stripAgentShareFileProvenance } from '@/types/files';
+
+import { assertFileNotInRestrictedKnowledgeBase } from '../_helpers/knowledgeBaseAccess';
+import { fileProcedure, resolveAccessibleParentDocument } from './procedures';
+
+export const updateRouter = router({
+ updateFile: fileProcedure
+ .use(withScopedPermission('file:update'))
+ .input(
+ z.object({
+ id: z.string(),
+ metadata: z.record(z.string(), z.any()).optional(),
+ name: z.string().optional(),
+ parentId: z.string().nullish(),
+ }),
+ )
+ .mutation(async ({ ctx, input }) => {
+ const { id, metadata, name, parentId } = input;
+
+ const existing = await ctx.fileModel.findById(id);
+ if (!existing) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
+ await assertFileNotInRestrictedKnowledgeBase(ctx, id);
+
+ // Resolve parentId if it's a slug (otherwise use as-is)
+ let resolvedParentId: string | null | undefined = parentId;
+ if (parentId) {
+ resolvedParentId = (await resolveAccessibleParentDocument(ctx, parentId)).id;
+ }
+
+ const updates: Parameters<typeof ctx.fileModel.update>[1] = {};
+
+ if (metadata !== undefined) {
+ updates.metadata = stripAgentShareFileProvenance(metadata);
+ }
+
+ if (name !== undefined) {
+ updates.name = name;
+ }
+
+ if (parentId !== undefined) {
+ updates.parentId = resolvedParentId;
+ }
+
+ if (Object.keys(updates).length > 0) {
+ const wsId = ctx.workspaceId ?? undefined;
+ await ctx.serverDB.transaction(async (tx) => {
+ const trx = tx as unknown as LobeChatDatabase;
+ // The knowledge-base tree reads `documents.parent_id`, so the file's
+ // backing document row(s) must move (and rename) together with it.
+ // Documents are written before the file, matching updateDocument's
+ // lock order so concurrent moves cannot deadlock.
+ if (updates.parentId !== undefined || updates.name !== undefined) {
+ await new DocumentModel(trx, ctx.userId, wsId).syncFromFile(id, {
+ name: updates.name,
+ parentId: updates.parentId,
+ });
+ }
+ await new FileModel(trx, ctx.userId, wsId).update(id, updates);
+ });
+ }
+
+ return { success: true };
+ }),
+ publishFileToWorkspace: fileProcedure
+ .use(withScopedPermission('file:update'))
+ .input(z.object({ id: z.string() }))
+ .mutation(async ({ ctx, input }) => {
+ // Personal mode has no notion of workspace visibility — publish is only
+ // meaningful inside a team workspace.
+ if (!ctx.workspaceId) {
+ throw new TRPCError({
+ code: 'BAD_REQUEST',
+ message: 'Cannot publish a file outside of a workspace',
+ });
+ }
+
+ const file = await ctx.fileModel.findById(input.id);
+ if (!file) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
+
+ if (file.userId !== ctx.userId) {
+ throw new TRPCError({
+ code: 'FORBIDDEN',
+ message: 'Only the creator can publish a private file to the workspace',
+ });
+ }
+
+ if (file.visibility === 'public') return { success: true };
+
+ await ctx.fileModel.publishToWorkspace(input.id);
+ return { success: true };
+ }),
+ /**
+ * Toggle a file's workspace visibility. Creator-only. Personal mode has no
+ * workspace visibility concept, so the call is rejected there.
+ */
+ setFileVisibility: fileProcedure
+ .use(withScopedPermission('file:update'))
+ .input(
+ z.object({
+ id: z.string(),
+ visibility: z.enum(['private', 'public']),
+ }),
+ )
+ .mutation(async ({ ctx, input }) => {
+ if (!ctx.workspaceId) {
+ throw new TRPCError({
+ code: 'BAD_REQUEST',
+ message: 'File visibility only applies inside a workspace',
+ });
+ }
+
+ const file = await ctx.fileModel.findById(input.id);
+ if (!file) throw new TRPCError({ code: 'NOT_FOUND', message: 'File not found' });
+
+ if (file.userId !== ctx.userId) {
+ throw new TRPCError({
+ code: 'FORBIDDEN',
+ message: 'Only the creator can change a file’s visibility',
+ });
+ }
+
+ if (file.visibility === input.visibility) return { success: true };
+
+ await ctx.fileModel.setVisibility(input.id, input.visibility);
+ return { success: true };
+ }),
+});
diff --git a/apps/server/src/routers/lambda/file/upload.ts b/apps/server/src/routers/lambda/file/upload.ts
new file mode 100644
index 00000000000..5491868fc2b
--- /dev/null
+++ b/apps/server/src/routers/lambda/file/upload.ts
@@ -0,0 +1,295 @@
+import { randomUUID } from 'node:crypto';
+
+import { MAX_UPLOAD_FILE_SIZE, UPLOAD_FILE_SIZE_LIMIT_ERROR_MESSAGE } from '@lobechat/const';
+import { TRPCError } from '@trpc/server';
+import isEqual from 'fast-deep-equal';
+import { z } from 'zod';
+
+import { businessFileUploadCheck } from '@/business/server/lambda-routers/file';
+import { checkFileStorageUsage } from '@/business/server/trpc-middlewares/lambda';
+import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
+import { router } from '@/libs/trpc/lambda';
+import type { FileService } from '@/server/services/file';
+import { downloadRemoteImage } from '@/server/services/file/downloadRemoteImage';
+import {
+ FileSource,
+ stripAgentShareFileProvenance,
+ toFileSource,
+ UploadFileSchema,
+} from '@/types/files';
+
+import { fileProcedure, resolveAccessibleParentDocument } from './procedures';
+
+const isStoredObjectAvailable = async (fileService: FileService, url: string): Promise<boolean> => {
+ try {
+ // Hash records can outlive their backing object, for example when generated
+ // assets are cleaned up but the global hash row remains. Treat stale rows as
+ // missing so the client uploads a fresh copy instead of reusing a dead key.
+ await fileService.getFileMetadata(url);
+ return true;
+ } catch (error) {
+ console.error('Failed to verify existing file hash storage object:', error);
+ return false;
+ }
+};
+
+export const uploadRouter = router({
+ checkFileHash: fileProcedure
+ .use(withScopedPermission('file:upload'))
+ .use(checkFileStorageUsage)
+ .input(z.object({ hash: z.string() }))
+ .mutation(async ({ ctx, input }) => {
+ const existingFile = await ctx.fileModel.checkHash(input.hash);
+ const existingHashUrl = existingFile?.isExist ? existingFile.url : undefined;
+ if (!existingHashUrl) return existingFile;
+
+ const isStorageAvailable = await isStoredObjectAvailable(ctx.fileService, existingHashUrl);
+
+ return isStorageAvailable ? existingFile : { isExist: false };
+ }),
+ createFile: fileProcedure
+ .use(withScopedPermission('file:upload'))
+ .input(
+ UploadFileSchema.omit({ url: true }).extend({
+ parentId: z.string().optional(),
+ url: z.string(),
+ visibility: z.enum(['private', 'public']).optional(),
+ }),
+ )
+ .mutation(async ({ ctx, input }) => {
+ const metadata = stripAgentShareFileProvenance(input.metadata);
+ const existingFile = await ctx.fileModel.checkHash(input.hash!);
+ const { isExist } = existingFile;
+ const latestUpload = await ctx.fileUploadService.findLatest(input.url);
+ if (!latestUpload && (await ctx.fileUploadService.hasAnyLiveSession(input.url))) {
+ throw new TRPCError({
+ code: 'CONFLICT',
+ message: 'Upload pathname belongs to another session',
+ });
+ }
+
+ const parentDocument = input.parentId
+ ? await resolveAccessibleParentDocument(ctx, input.parentId)
+ : undefined;
+ const resolvedParentId = parentDocument?.id;
+ const parentVisibility = parentDocument?.visibility;
+
+ let knowledgeBaseVisibility: 'private' | 'public' | undefined;
+ if (ctx.workspaceId && input.knowledgeBaseId) {
+ const knowledgeBase = await ctx.knowledgeBaseModel.findById(input.knowledgeBaseId);
+ if (!knowledgeBase) {
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Knowledge base not found' });
+ }
+ knowledgeBaseVisibility = knowledgeBase.visibility;
+ }
+
+ // Visibility precedence (workspace mode only — personal mode ignores the
+ // column entirely):
+ // 1. A library upload always uses the knowledge base visibility.
+ // 2. Otherwise an explicit caller value wins.
+ // 3. Otherwise inherit the parent document's visibility so a file
+ // uploaded inside a private folder stays private.
+ // 4. Agent-document uploads default to 'public' to match their document's
+ // access contract; their source keeps them out of resource listings.
+ // 5. Otherwise default top-level uploads to 'private' so new content
+ // starts in the creator's private space (mirrors the Pages spec).
+ const resolvedVisibility: 'private' | 'public' | undefined = ctx.workspaceId
+ ? (knowledgeBaseVisibility ??
+ input.visibility ??
+ parentVisibility ??
+ (input.source === FileSource.AgentDocument ? 'public' : 'private'))
+ : undefined;
+
+ if (latestUpload?.status === 'settled') {
+ const settledFile = latestUpload.fileId
+ ? await ctx.fileModel.findById(latestUpload.fileId)
+ : undefined;
+ const isRetry =
+ !!settledFile &&
+ !input.knowledgeBaseId &&
+ isExist &&
+ existingFile.url === input.url &&
+ settledFile.fileHash === input.hash &&
+ settledFile.fileType === input.fileType &&
+ settledFile.name === input.name &&
+ settledFile.parentId === (resolvedParentId ?? null) &&
+ settledFile.size === input.size &&
+ (settledFile.source ?? undefined) === toFileSource(input.source) &&
+ settledFile.url === input.url &&
+ (!ctx.workspaceId || settledFile.visibility === resolvedVisibility) &&
+ isEqual(settledFile.metadata, metadata ?? null);
+
+ if (isRetry) {
+ return {
+ id: settledFile.id,
+ url: await ctx.fileService.getFileAccessUrl(settledFile),
+ };
+ }
+
+ // The same global object can be referenced by multiple logical files.
+ // A non-identical request is the normal dedup path, not a stale retry.
+ } else if (latestUpload && latestUpload.status !== 'active') {
+ throw new TRPCError({ code: 'CONFLICT', message: 'Upload session is no longer active' });
+ }
+
+ const activeUpload =
+ latestUpload?.status === 'active'
+ ? await ctx.fileUploadService.touchActive(input.url)
+ : undefined;
+ if (latestUpload?.status === 'active' && !activeUpload) {
+ throw new TRPCError({ code: 'CONFLICT', message: 'Upload session is no longer active' });
+ }
+
+ let actualSize: number;
+ if (activeUpload) {
+ try {
+ const { contentLength } = await ctx.fileService.getFileMetadata(input.url);
+ actualSize = contentLength;
+ } catch {
+ await ctx.fileUploadService.releaseBestEffort(input.url);
+ throw new TRPCError({ code: 'BAD_REQUEST', message: 'Uploaded file is unavailable' });
+ }
+
+ if (input.size !== activeUpload.size || actualSize !== activeUpload.size) {
+ await ctx.fileUploadService.releaseBestEffort(input.url);
+ throw new TRPCError({ code: 'BAD_REQUEST', message: 'Uploaded file size mismatch' });
+ }
+ } else {
+ actualSize = input.size;
+ try {
+ const { contentLength } = await ctx.fileService.getFileMetadata(input.url);
+ if (contentLength >= 1) actualSize = contentLength;
+ } catch {
+ // Compatibility path for clients that uploaded before reservations were introduced.
+ }
+ }
+
+ if (actualSize < 0) {
+ await businessFileUploadCheck({
+ actualSize,
+ clientIp: ctx.clientIp ?? undefined,
+ inputSize: input.size,
+ url: input.url,
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId,
+ });
+ throw new TRPCError({ code: 'BAD_REQUEST', message: 'File size cannot be negative' });
+ }
+
+ if (actualSize > MAX_UPLOAD_FILE_SIZE) {
+ throw new TRPCError({
+ code: 'PAYLOAD_TOO_LARGE',
+ message: UPLOAD_FILE_SIZE_LIMIT_ERROR_MESSAGE,
+ });
+ }
+
+ const { id } = await ctx.serverDB.transaction(async (trx) => {
+ let shouldRefreshGlobalFile = false;
+ if (isExist && existingFile.url && existingFile.url !== input.url) {
+ shouldRefreshGlobalFile = !(await isStoredObjectAvailable(
+ ctx.fileService,
+ existingFile.url,
+ ));
+ }
+
+ if (shouldRefreshGlobalFile) {
+ // A user may re-upload the same bytes after the old object key was
+ // removed. Keep the global hash pointer on the newly uploaded object so
+ // future dedup checks do not resolve back to the stale key.
+ await ctx.fileModel.updateGlobalFile(
+ input.hash!,
+ {
+ metadata,
+ url: input.url,
+ },
+ trx,
+ );
+ }
+
+ const createFile = () =>
+ ctx.fileModel.create(
+ {
+ fileHash: input.hash,
+ fileType: input.fileType,
+ knowledgeBaseId: input.knowledgeBaseId,
+ metadata,
+ name: input.name,
+ parentId: resolvedParentId,
+ size: actualSize,
+ // Attribution the caller supplied (e.g. a page-editor paste). The
+ // wire type is a loose string for older clients, so unknown values
+ // are dropped rather than persisted — `source` drives the resource
+ // library's origin filter and its hidden-source exclusion.
+ source: toFileSource(input.source),
+ url: input.url,
+ ...(resolvedVisibility ? { visibility: resolvedVisibility } : {}),
+ },
+ // if the file is not exist in global file, create a new one
+ !isExist,
+ trx,
+ );
+
+ if (activeUpload) {
+ const lockedUpload = await ctx.fileUploadService.model.findLatestByPathnameForUpdate(
+ input.url,
+ trx,
+ );
+ if (lockedUpload?.id !== activeUpload.id || lockedUpload.status !== 'active') {
+ throw new TRPCError({
+ code: 'CONFLICT',
+ message: 'Upload session is no longer active',
+ });
+ }
+
+ const file = await createFile();
+ const settled = await ctx.fileUploadService.model.settle(lockedUpload.id, file.id, trx);
+ if (!settled) {
+ throw new TRPCError({ code: 'CONFLICT', message: 'Upload could not be settled' });
+ }
+ return file;
+ }
+
+ await businessFileUploadCheck({
+ actualSize,
+ clientIp: ctx.clientIp ?? undefined,
+ inputSize: input.size,
+ transaction: trx,
+ url: input.url,
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId,
+ });
+
+ return createFile();
+ });
+
+ return { id, url: await ctx.fileService.getFileAccessUrl({ id, url: input.url }) };
+ }),
+ rehostImage: fileProcedure
+ .use(withScopedPermission('file:upload'))
+ .use(checkFileStorageUsage)
+ .input(z.object({ url: z.url() }))
+ .mutation(async ({ ctx, input }) => {
+ // SSRF-safe: `downloadRemoteImage` accepts only credential-free http(s) URLs and
+ // fetches through `ssrfSafeFetch`, which validates resolved addresses at connect
+ // time and on every redirect hop.
+ const { buffer, extension, mimeType } = await downloadRemoteImage(input.url);
+ const pathname = `images/${ctx.userId}/${randomUUID()}.${extension}`;
+ const result = await ctx.fileService.uploadFromBuffer(
+ buffer,
+ mimeType,
+ pathname,
+ (transaction) =>
+ businessFileUploadCheck({
+ actualSize: buffer.length,
+ clientIp: ctx.clientIp ?? undefined,
+ inputSize: buffer.length,
+ transaction,
+ url: pathname,
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId,
+ }),
+ { source: FileSource.PageEditor, visibility: 'private' },
+ );
+ return { fileId: result.fileId, url: result.url };
+ }),
+});