@@ -29,6 +29,7 @@ import { MAX_WORKSPACE_FILE_SIZE } from '@/lib/uploads/shared/types'
2929import { getWorkspaceWithOwner } from '@/lib/workspaces/permissions/utils'
3030import { isUuid , sanitizeFileName } from '@/executor/constants'
3131import type { UserFile } from '@/executor/types'
32+ import type { WorkspaceFileFolderRecord } from './workspace-file-folder-manager'
3233import {
3334 assertWorkspaceFileFolderTarget ,
3435 buildWorkspaceFileFolderPathMap ,
@@ -69,6 +70,12 @@ export interface WorkspaceFileRecord {
6970 storageContext ?: 'workspace' | 'mothership'
7071}
7172
73+ interface ListWorkspaceFilesOptions {
74+ scope ?: WorkspaceFileScope
75+ folders ?: WorkspaceFileFolderRecord [ ]
76+ hydrateFolderPaths ?: boolean
77+ }
78+
7279/**
7380 * Workspace file key pattern: workspace/{workspaceId}/{timestamp}-{random}-{filename}
7481 */
@@ -620,10 +627,10 @@ export async function getWorkspaceFileByName(
620627 */
621628export async function listWorkspaceFiles (
622629 workspaceId : string ,
623- options ?: { scope ?: WorkspaceFileScope }
630+ options ?: ListWorkspaceFilesOptions
624631) : Promise < WorkspaceFileRecord [ ] > {
625632 try {
626- const { scope = 'active' } = options ?? { }
633+ const { scope = 'active' , hydrateFolderPaths = true } = options ?? { }
627634 const files = await db
628635 . select ( )
629636 . from ( workspaceFiles )
@@ -647,8 +654,11 @@ export async function listWorkspaceFiles(
647654 )
648655 . orderBy ( workspaceFiles . uploadedAt )
649656
650- const folders = await listWorkspaceFileFolders ( workspaceId , { scope : 'all' } )
651- const folderPaths = buildWorkspaceFileFolderPathMap ( folders )
657+ const needsFolderPaths = hydrateFolderPaths && files . some ( ( file ) => file . folderId )
658+ const folders = needsFolderPaths
659+ ? ( options ?. folders ?? ( await listWorkspaceFileFolders ( workspaceId , { scope : 'all' } ) ) )
660+ : [ ]
661+ const folderPaths = needsFolderPaths ? buildWorkspaceFileFolderPathMap ( folders ) : new Map ( )
652662
653663 return files . map ( ( file ) => mapWorkspaceFileRecord ( file , workspaceId , folderPaths ) )
654664 } catch ( error ) {
0 commit comments