Repository navigation
fix(desktop,host-service): send attachments through storage, not host-service bodies #8704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
saddlepaddle
wants to merge
3
commits into
main
from
satya/super-2658-dragging-images-into-cloud-workspaces-fails-to-upload
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
30d8796
fix(desktop,host-service): send attachments through storage, not host…
saddlepaddle 10a2b74
fix(api): scope attachment.resolve to visible cloud workspaces and re…
harshithmullapudi 05165d4
Merge main into #8704 to resolve the cloud-workspace.ts import conflict
harshithmullapudi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 6 additions & 24 deletions
30
...onents/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,33 @@ | ||
| import { useLingui } from "@lingui/react/macro"; | ||
| import { errorMessage } from "@superset/i18n/errors"; | ||
| import { toast } from "@superset/ui/sonner"; | ||
| import { workspaceTrpc } from "@superset/workspace-client"; | ||
| import { useCallback } from "react"; | ||
| import { | ||
| PasteUploadLimitError, | ||
| uploadPastedFiles, | ||
| } from "../../../../../../utils/uploadPastedFiles"; | ||
| import { useSendAttachments } from "../../../../../../hooks/useSendAttachments"; | ||
| import { AttachmentLimitError } from "../../../../../../utils/sendAttachmentsToWorkspace"; | ||
| import { formatAttachmentTag } from "../../../../utils/attachmentTags"; | ||
|
|
||
| export function useUploadAttachments(workspaceId: string) { | ||
| const { t } = useLingui(); | ||
| const { data: workspace } = workspaceTrpc.workspace.get.useQuery({ | ||
| id: workspaceId, | ||
| }); | ||
| const createDirectory = | ||
| workspaceTrpc.filesystem.createDirectory.useMutation(); | ||
| const writeFile = workspaceTrpc.filesystem.writeFile.useMutation(); | ||
| const worktreePath = workspace?.worktreePath; | ||
| const sendAttachments = useSendAttachments(workspaceId); | ||
|
|
||
| return useCallback( | ||
| async (files: File[]): Promise<string[] | null> => { | ||
| if (files.length === 0) return []; | ||
| try { | ||
| if (!worktreePath) throw new Error("Workspace is not ready"); | ||
| const paths = await uploadPastedFiles({ | ||
| deps: { | ||
| createDirectory: (input) => createDirectory.mutateAsync(input), | ||
| writeFile: (input) => writeFile.mutateAsync(input), | ||
| }, | ||
| workspaceId, | ||
| worktreePath, | ||
| files, | ||
| }); | ||
| const paths = await sendAttachments(files); | ||
| return paths.map((path, index) => | ||
| formatAttachmentTag({ path, type: files[index]?.type ?? "" }), | ||
| ); | ||
| } catch (error) { | ||
| toast.error(t({ message: "Couldn't attach files" }), { | ||
| description: | ||
| error instanceof PasteUploadLimitError | ||
| error instanceof AttachmentLimitError | ||
| ? error.message | ||
| : errorMessage(error, t({ message: "Unknown error" })), | ||
| }); | ||
| return null; | ||
| } | ||
| }, | ||
| [worktreePath, workspaceId, createDirectory, writeFile, t], | ||
| [sendAttachments, t], | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...shboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { useSendAttachments } from "./useSendAttachments"; |
54 changes: 54 additions & 0 deletions
54
...rkspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { workspaceTrpc } from "@superset/workspace-client"; | ||
| import { useCallback, useRef } from "react"; | ||
| import { useWorkspaceHostTarget } from "renderer/hooks/host-service/useWorkspaceHostUrl"; | ||
| import { apiTrpcClient } from "renderer/lib/api-trpc-client"; | ||
| import { sendAttachmentsToWorkspace } from "../../utils/sendAttachmentsToWorkspace"; | ||
|
|
||
| async function putObject( | ||
| target: { url: string; headers: Record<string, string> }, | ||
| file: File, | ||
| ): Promise<Response> { | ||
| return await fetch(target.url, { | ||
| method: "PUT", | ||
| headers: target.headers, | ||
| body: file, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * A stable function that gets files into the workspace's worktree and | ||
| * returns their worktree-relative paths. Stable so a terminal's paste | ||
| * override, registered once, always sees the current host target. | ||
| */ | ||
| export function useSendAttachments( | ||
| workspaceId: string, | ||
| ): (files: File[]) => Promise<string[]> { | ||
| const target = useWorkspaceHostTarget(workspaceId); | ||
| const isCloudWorkspace = | ||
| target.status === "ready" && target.kind === "sandbox"; | ||
| const isCloudRef = useRef(isCloudWorkspace); | ||
| isCloudRef.current = isCloudWorkspace; | ||
|
|
||
| const materialize = | ||
| workspaceTrpc.attachments.materializeIntoWorkspace.useMutation(); | ||
| const materializeRef = useRef(materialize.mutateAsync); | ||
| materializeRef.current = materialize.mutateAsync; | ||
|
|
||
| return useCallback( | ||
| (files: File[]) => | ||
| sendAttachmentsToWorkspace({ | ||
| deps: { | ||
| createUpload: (input) => | ||
| apiTrpcClient.attachment.createUpload.mutate(input), | ||
| putObject, | ||
| attachFiles: (input) => | ||
| apiTrpcClient.cloudWorkspace.attachFiles.mutate(input), | ||
| materialize: (input) => materializeRef.current(input), | ||
| }, | ||
| workspaceId, | ||
| isCloudWorkspace: isCloudRef.current, | ||
| files, | ||
| }), | ||
| [workspaceId], | ||
| ); | ||
| } |
5 changes: 5 additions & 0 deletions
5
...v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export { | ||
| AttachmentLimitError, | ||
| type SendAttachmentsDeps, | ||
| sendAttachmentsToWorkspace, | ||
| } from "./sendAttachmentsToWorkspace"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: superset-sh/superset
Length of output: 11774
Script executed:
Repository: superset-sh/superset
Length of output: 8282
Make pasted attachment paths absolute before they reach the terminal.
The host returns worktree-relative paths such as
.superset/attachments/file-1.png.TerminalPane.tsxpasses them toshellEscapePathsand then toterminalRuntimeRegistry.paste.shellEscapePathsonly shell-quotes its input and does not resolve or rebase paths. A relative path resolves against the shell's current directory. If the user has changed directory in that terminal, the pasted path does not point to the attachment file.Do not change the shared
materializeIntoWorkspaceresponse.useUploadAttachmentsandTerminalRichInputalso use these relative paths. Have the host return an absolute path for each attachment, and paste that value only in the terminal path inTerminalPane.tsx.TerminalRichInputalso passes these paths throughshellEscapePaths, so check that path too.