Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 66 additions & 35 deletions desktop/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3742,38 +3742,59 @@ func (a *App) currentProviderEntryForTab(tabID string) (*config.ProviderEntry, e
return entry, nil
}

// SavePastedImage stores a browser clipboard image data URL under the active
// tab's workspace .reasonix/attachments and returns the relative @-reference path.
func (a *App) SavePastedImage(dataURL string) (string, error) {
func (a *App) withActiveWorkspace(fn func() (string, error)) (string, error) {
var result string
err := a.withActiveWorkspaceDo(func() error {
var err error
result, err = fn()
return err
})
return result, err
}

func (a *App) withActiveWorkspaceDo(fn func() error) error {
root := a.activeWorkspaceRoot()
if root != "" && root != "." {
if prev, err := os.Getwd(); err == nil {
if err := os.Chdir(root); err == nil {
defer func() { _ = os.Chdir(prev) }()
}
prev, err := os.Getwd()
if err != nil {
return err
}
if err := os.Chdir(root); err != nil {
return err
}
defer func() { _ = os.Chdir(prev) }()
}
return control.SaveImageDataURL(dataURL)
return fn()
}

// SavePastedImage stores a browser clipboard image data URL under the active
// tab's workspace .reasonix/attachments and returns the relative @-reference path.
func (a *App) SavePastedImage(dataURL string) (string, error) {
return a.withActiveWorkspace(func() (string, error) {
return control.SaveImageDataURL(dataURL)
})
}

// SaveClipboardImage reads the native OS clipboard image under the active tab's
// workspace .reasonix/attachments and returns the relative @-reference path.
func (a *App) SaveClipboardImage() (string, error) {
return a.withActiveWorkspace(control.SaveClipboardImage)
}

// SavePastedFile stores a dropped non-image file (the browser exposes its bytes
// as a data URL but not a real path) under the active tab's workspace
// .reasonix/attachments and returns the relative @-reference path.
func (a *App) SavePastedFile(name, dataURL string) (string, error) {
root := a.activeWorkspaceRoot()
if root != "" && root != "." {
if prev, err := os.Getwd(); err == nil {
if err := os.Chdir(root); err == nil {
defer func() { _ = os.Chdir(prev) }()
}
}
}
return control.SaveAttachmentDataURL(name, dataURL)
return a.withActiveWorkspace(func() (string, error) {
return control.SaveAttachmentDataURL(name, dataURL)
})
}

// AttachmentDataURL returns a safe data URL for a stored image attachment.
func (a *App) AttachmentDataURL(path string) (string, error) {
return control.ImageDataURL(path)
return a.withActiveWorkspace(func() (string, error) {
return control.ImageDataURL(path)
})
}

// DroppedItem is one OS-dropped file resolved into a composer context entry: an
Expand All @@ -3791,27 +3812,37 @@ type DroppedItem struct {
// thumbnail; other in-workspace files are referenced relatively (no copy); files
// outside the workspace are copied into .reasonix/attachments.
func (a *App) AttachDropped(path string) (DroppedItem, error) {
info, err := os.Lstat(path)
if err != nil {
return DroppedItem{}, err
}
if isImageExt(path) {
if rel, err := control.SaveImageFile(path); err == nil {
preview, _ := control.ImageDataURL(rel)
return DroppedItem{Kind: "attachment", Path: rel, PreviewURL: preview}, nil
var item DroppedItem
err := a.withActiveWorkspaceDo(func() error {
info, err := os.Lstat(path)
if err != nil {
return err
}
}
if rel, ok := workspaceRelative(path); ok {
return DroppedItem{Kind: "workspace", Path: rel, IsDir: info.IsDir()}, nil
}
if info.IsDir() {
return DroppedItem{}, fmt.Errorf("can only attach files from outside the workspace")
}
rel, err := control.SaveAttachmentFile(path)
if isImageExt(path) {
if rel, err := control.SaveImageFile(path); err == nil {
preview, _ := control.ImageDataURL(rel)
item = DroppedItem{Kind: "attachment", Path: rel, PreviewURL: preview}
return nil
}
}
if rel, ok := workspaceRelative(path); ok {
item = DroppedItem{Kind: "workspace", Path: rel, IsDir: info.IsDir()}
return nil
}
if info.IsDir() {
return fmt.Errorf("can only attach files from outside the workspace")
}
rel, err := control.SaveAttachmentFile(path)
if err != nil {
return err
}
item = DroppedItem{Kind: "attachment", Path: rel}
return nil
})
if err != nil {
return DroppedItem{}, err
}
return DroppedItem{Kind: "attachment", Path: rel}, nil
return item, nil
}

func isImageExt(path string) bool {
Expand Down
120 changes: 120 additions & 0 deletions desktop/attach_dropped_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"encoding/base64"
"os"
"path/filepath"
"strings"
"testing"
)

const desktopTinyPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="

func TestWorkspaceRelative(t *testing.T) {
orig, _ := os.Getwd()
defer os.Chdir(orig)
Expand Down Expand Up @@ -38,6 +41,123 @@ func TestIsImageExt(t *testing.T) {
}
}

func TestSavePastedImageUsesActiveWorkspaceRoot(t *testing.T) {
orig, _ := os.Getwd()
defer os.Chdir(orig)

launchRoot := t.TempDir()
projectRoot := t.TempDir()
if err := os.Chdir(projectRoot); err != nil {
t.Fatal(err)
}
projectRoot, _ = os.Getwd()
if err := os.Chdir(launchRoot); err != nil {
t.Fatal(err)
}
app := &App{
tabs: map[string]*WorkspaceTab{
"project": {ID: "project", WorkspaceRoot: projectRoot},
},
activeTabID: "project",
}

got, err := app.SavePastedImage("data:image/png;base64," + desktopTinyPNG)
if err != nil {
t.Fatalf("SavePastedImage: %v", err)
}
if _, err := os.Stat(filepath.Join(projectRoot, filepath.FromSlash(got))); err != nil {
t.Fatalf("pasted image should be saved under active workspace: %v", err)
}
if _, err := os.Stat(filepath.Join(launchRoot, filepath.FromSlash(got))); !os.IsNotExist(err) {
t.Fatalf("pasted image should not be saved under launch root, stat err=%v", err)
}
preview, err := app.AttachmentDataURL(got)
if err != nil {
t.Fatalf("AttachmentDataURL: %v", err)
}
if !strings.HasPrefix(preview, "data:image/png;base64,") {
t.Fatalf("preview = %q, want png data URL", preview)
}
}

func TestAttachDroppedUsesActiveWorkspaceRoot(t *testing.T) {
orig, _ := os.Getwd()
defer os.Chdir(orig)

launchRoot := t.TempDir()
projectRoot := t.TempDir()
if err := os.Chdir(projectRoot); err != nil {
t.Fatal(err)
}
projectRoot, _ = os.Getwd()
if err := os.Chdir(launchRoot); err != nil {
t.Fatal(err)
}
app := &App{
tabs: map[string]*WorkspaceTab{
"project": {ID: "project", WorkspaceRoot: projectRoot},
},
activeTabID: "project",
}
if err := os.MkdirAll(filepath.Join(projectRoot, "sub"), 0o755); err != nil {
t.Fatal(err)
}
target := filepath.Join(projectRoot, "sub", "notes.txt")
if err := os.WriteFile(target, []byte("body"), 0o644); err != nil {
t.Fatal(err)
}

got, err := app.AttachDropped(target)
if err != nil {
t.Fatalf("AttachDropped: %v", err)
}
if got.Kind != "workspace" || got.Path != "sub/notes.txt" {
t.Fatalf("got %+v, want workspace ref sub/notes.txt", got)
}
}

func TestAttachDroppedImageUsesActiveWorkspaceRoot(t *testing.T) {
orig, _ := os.Getwd()
defer os.Chdir(orig)

launchRoot := t.TempDir()
projectRoot := t.TempDir()
if err := os.Chdir(launchRoot); err != nil {
t.Fatal(err)
}
app := &App{
tabs: map[string]*WorkspaceTab{
"project": {ID: "project", WorkspaceRoot: projectRoot},
},
activeTabID: "project",
}
raw, err := base64.StdEncoding.DecodeString(desktopTinyPNG)
if err != nil {
t.Fatal(err)
}
outside := filepath.Join(t.TempDir(), "shot.png")
if err := os.WriteFile(outside, raw, 0o644); err != nil {
t.Fatal(err)
}

got, err := app.AttachDropped(outside)
if err != nil {
t.Fatalf("AttachDropped: %v", err)
}
if got.Kind != "attachment" || !strings.HasSuffix(got.Path, ".png") {
t.Fatalf("got %+v, want png attachment", got)
}
if _, err := os.Stat(filepath.Join(projectRoot, filepath.FromSlash(got.Path))); err != nil {
t.Fatalf("dropped image should be saved under active workspace: %v", err)
}
if _, err := os.Stat(filepath.Join(launchRoot, filepath.FromSlash(got.Path))); !os.IsNotExist(err) {
t.Fatalf("dropped image should not be saved under launch root, stat err=%v", err)
}
if !strings.HasPrefix(got.PreviewURL, "data:image/png;base64,") {
t.Fatalf("preview = %q, want png data URL", got.PreviewURL)
}
}

func TestAttachDroppedInWorkspaceReferencesInPlace(t *testing.T) {
orig, _ := os.Getwd()
defer os.Chdir(orig)
Expand Down
Loading
Loading