Skip to content

Commit ae4152e

Browse files
committed
refactor(uploads): rename server fn to fetchWorkspaceFileBuffer, move client download to uploads/client/download.ts as triggerFileDownload
1 parent 97d94b9 commit ae4152e

19 files changed

Lines changed: 62 additions & 77 deletions

File tree

apps/sim/app/api/tools/file/manage/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { acquireLock, releaseLock } from '@/lib/core/config/redis'
77
import { ensureAbsoluteUrl } from '@/lib/core/utils/urls'
88
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
99
import {
10-
downloadWorkspaceFile,
10+
fetchWorkspaceFileBuffer,
1111
getWorkspaceFileByName,
1212
updateWorkspaceFileContent,
1313
uploadWorkspaceFile,
@@ -91,7 +91,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
9191
}
9292

9393
try {
94-
const existingBuffer = await downloadWorkspaceFile(existing)
94+
const existingBuffer = await fetchWorkspaceFileBuffer(existing)
9595
const finalContent = existingBuffer.toString('utf-8') + content
9696
const fileBuffer = Buffer.from(finalContent, 'utf-8')
9797
await updateWorkspaceFileContent(workspaceId, existing.id, userId, fileBuffer)

apps/sim/app/api/v1/files/[fileId]/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { generateRequestId } from '@/lib/core/utils/request'
77
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
88
import {
99
deleteWorkspaceFile,
10-
downloadWorkspaceFile,
10+
fetchWorkspaceFileBuffer,
1111
getWorkspaceFile,
1212
} from '@/lib/uploads/contexts/workspace'
1313
import {
@@ -50,7 +50,7 @@ export const GET = withRouteHandler(async (request: NextRequest, context: FileRo
5050
return NextResponse.json({ error: 'File not found' }, { status: 404 })
5151
}
5252

53-
const buffer = await downloadWorkspaceFile(fileRecord)
53+
const buffer = await fetchWorkspaceFileBuffer(fileRecord)
5454

5555
return new Response(new Uint8Array(buffer), {
5656
status: 200,

apps/sim/app/api/workspaces/[id]/files/[fileId]/compiled-check/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
88
import { BINARY_DOC_TASKS, MAX_DOCUMENT_PREVIEW_CODE_BYTES } from '@/lib/execution/constants'
99
import { runSandboxTask, SandboxUserCodeError } from '@/lib/execution/sandbox/run-task'
1010
import { validateMermaidSource } from '@/lib/mermaid/validate'
11-
import { downloadWorkspaceFile, getWorkspaceFile } from '@/lib/uploads/contexts/workspace'
11+
import { fetchWorkspaceFileBuffer, getWorkspaceFile } from '@/lib/uploads/contexts/workspace'
1212
import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
1313

1414
export const dynamic = 'force-dynamic'
@@ -62,7 +62,7 @@ export const GET = withRouteHandler(
6262

6363
let buffer: Buffer
6464
try {
65-
buffer = await downloadWorkspaceFile(fileRecord)
65+
buffer = await fetchWorkspaceFileBuffer(fileRecord)
6666
} catch (err) {
6767
logger.error('Failed to download file for compiled check', {
6868
fileId,

apps/sim/app/api/workspaces/[id]/files/[fileId]/style/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { parseRequest } from '@/lib/api/server'
66
import { getSession } from '@/lib/auth'
77
import { extractDocumentStyle } from '@/lib/copilot/vfs/document-style'
88
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
9-
import { downloadWorkspaceFile, getWorkspaceFile } from '@/lib/uploads/contexts/workspace'
9+
import { fetchWorkspaceFileBuffer, getWorkspaceFile } from '@/lib/uploads/contexts/workspace'
1010
import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
1111

1212
export const dynamic = 'force-dynamic'
@@ -52,7 +52,7 @@ export const GET = withRouteHandler(
5252

5353
let buffer: Buffer
5454
try {
55-
buffer = await downloadWorkspaceFile(fileRecord)
55+
buffer = await fetchWorkspaceFileBuffer(fileRecord)
5656
} catch (err) {
5757
logger.error('Failed to download file for style extraction', {
5858
fileId,

apps/sim/app/workspace/[workspaceId]/files/files.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import {
2828
} from '@/components/emcn'
2929
import { File as FilesIcon } from '@/components/emcn/icons'
3030
import { getDocumentIcon } from '@/components/icons/document-icons'
31+
import { triggerFileDownload } from '@/lib/uploads/client/download'
3132
import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace'
3233
import { MAX_WORKSPACE_FILE_SIZE } from '@/lib/uploads/shared/types'
3334
import {
34-
downloadWorkspaceFile,
3535
formatFileSize,
3636
getFileExtension,
3737
getMimeTypeFromExtension,
@@ -475,7 +475,7 @@ export function Files() {
475475

476476
const handleDownload = useCallback(async (file: WorkspaceFileRecord) => {
477477
try {
478-
await downloadWorkspaceFile(file)
478+
await triggerFileDownload(file)
479479
} catch (err) {
480480
logger.error('Failed to download file:', err)
481481
}

apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/resource-content.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ import {
1919
markRunToolManuallyStopped,
2020
reportManualRunToolStop,
2121
} from '@/lib/copilot/tools/client/run-tool-execution'
22-
import {
23-
downloadWorkspaceFile,
24-
getFileExtension,
25-
getMimeTypeFromExtension,
26-
} from '@/lib/uploads/utils/file-utils'
22+
import { triggerFileDownload } from '@/lib/uploads/client/download'
23+
import { getFileExtension, getMimeTypeFromExtension } from '@/lib/uploads/utils/file-utils'
2724
import { workflowBorderColor } from '@/lib/workspaces/colors'
2825
import {
2926
FileViewer,
@@ -422,7 +419,7 @@ function EmbeddedFileActions({ workspaceId, fileId }: EmbeddedFileActionsProps)
422419
const handleDownload = async () => {
423420
if (!file) return
424421
try {
425-
await downloadWorkspaceFile(file)
422+
await triggerFileDownload(file)
426423
} catch (err) {
427424
fileLogger.error('Failed to download file:', err)
428425
}

apps/sim/lib/copilot/tools/handlers/function-execute.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { getTableById, queryRows } from '@/lib/table/service'
33
import {
4-
downloadWorkspaceFile,
4+
fetchWorkspaceFileBuffer,
55
findWorkspaceFileRecord,
66
getSandboxWorkspaceFilePath,
77
listWorkspaceFiles,
@@ -44,7 +44,7 @@ async function resolveInputFiles(
4444
logger.warn('Total input size limit reached')
4545
break
4646
}
47-
const buffer = await downloadWorkspaceFile(record)
47+
const buffer = await fetchWorkspaceFileBuffer(record)
4848
totalSize += buffer.length
4949
const isText = /^text\/|application\/json|application\/xml|application\/csv/.test(
5050
record.type || ''

apps/sim/lib/copilot/tools/handlers/materialize-file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { and, eq, isNull } from 'drizzle-orm'
88
import type { ExecutionContext, ToolCallResult } from '@/lib/copilot/request/types'
99
import { findMothershipUploadRowByChatAndName } from '@/lib/copilot/tools/handlers/upload-file-reader'
1010
import { getServePathPrefix } from '@/lib/uploads'
11-
import { downloadWorkspaceFile } from '@/lib/uploads/contexts/workspace/workspace-file-manager'
11+
import { fetchWorkspaceFileBuffer } from '@/lib/uploads/contexts/workspace/workspace-file-manager'
1212
import { parseWorkflowJson } from '@/lib/workflows/operations/import-export'
1313
import { saveWorkflowToNormalizedTables } from '@/lib/workflows/persistence/utils'
1414
import { deduplicateWorkflowName } from '@/lib/workflows/utils'
@@ -83,7 +83,7 @@ async function executeImport(
8383
}
8484
}
8585

86-
const buffer = await downloadWorkspaceFile(toFileRecord(row))
86+
const buffer = await fetchWorkspaceFileBuffer(toFileRecord(row))
8787
const content = buffer.toString('utf-8')
8888

8989
let parsed: unknown

apps/sim/lib/copilot/tools/server/files/file-preview.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { toError } from '@sim/utils/errors'
33
import {
4-
downloadWorkspaceFile,
4+
fetchWorkspaceFileBuffer,
55
getWorkspaceFile,
66
} from '@/lib/uploads/contexts/workspace/workspace-file-manager'
77

@@ -148,7 +148,7 @@ export async function loadWorkspaceFileTextForPreview(
148148
try {
149149
const record = await getWorkspaceFile(workspaceId, fileId)
150150
if (!record) return undefined
151-
const buffer = await downloadWorkspaceFile(record)
151+
const buffer = await fetchWorkspaceFileBuffer(record)
152152
return buffer.toString('utf-8')
153153
} catch (error) {
154154
logger.warn('Failed to load workspace file text for preview', {

apps/sim/lib/copilot/tools/server/files/workspace-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { runSandboxTask } from '@/lib/execution/sandbox/run-task'
1010
import {
1111
deleteWorkspaceFile,
12-
downloadWorkspaceFile as downloadWsFile,
12+
fetchWorkspaceFileBuffer as downloadWsFile,
1313
getWorkspaceFile,
1414
getWorkspaceFileByName,
1515
renameWorkspaceFile,

0 commit comments

Comments
 (0)