feat(app): support local screenshot actions
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
|
||||
)
|
||||
|
||||
// CaptureActionsService handles non-upload actions on already-produced PNG
|
||||
// screenshots. Keeping these actions here makes save/copy available across
|
||||
// overlay implementations without binding them to Wails or a specific OS.
|
||||
type CaptureActionsService struct {
|
||||
Clipboard clipboard.Writer
|
||||
Notifier Notifier
|
||||
}
|
||||
|
||||
func (s *CaptureActionsService) CopyImage(_ context.Context, pngBytes []byte) error {
|
||||
if len(pngBytes) == 0 {
|
||||
err := fmt.Errorf("empty screenshot")
|
||||
s.notifyFailure(err.Error())
|
||||
return err
|
||||
}
|
||||
if s.Clipboard == nil {
|
||||
err := fmt.Errorf("clipboard is not configured")
|
||||
s.notifyFailure(err.Error())
|
||||
return err
|
||||
}
|
||||
if err := s.Clipboard.WriteImage(pngBytes); err != nil {
|
||||
s.notifyFailure("clipboard write failed: " + err.Error())
|
||||
return err
|
||||
}
|
||||
if s.Notifier != nil {
|
||||
s.Notifier.NotifySuccess("image copied to clipboard")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *CaptureActionsService) SaveImage(_ context.Context, pngBytes []byte, dir string) (string, error) {
|
||||
if len(pngBytes) == 0 {
|
||||
err := fmt.Errorf("empty screenshot")
|
||||
s.notifyFailure(err.Error())
|
||||
return "", err
|
||||
}
|
||||
dir = strings.TrimSpace(dir)
|
||||
if dir == "" {
|
||||
err := fmt.Errorf("save directory is empty")
|
||||
s.notifyFailure(err.Error())
|
||||
return "", err
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
s.notifyFailure("create save directory failed: " + err.Error())
|
||||
return "", err
|
||||
}
|
||||
path := uniquePNGPath(dir, time.Now())
|
||||
if err := os.WriteFile(path, pngBytes, 0o644); err != nil {
|
||||
s.notifyFailure("save failed: " + err.Error())
|
||||
return "", err
|
||||
}
|
||||
if s.Notifier != nil {
|
||||
s.Notifier.NotifySuccess(path)
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (s *CaptureActionsService) notifyFailure(reason string) {
|
||||
if s.Notifier != nil {
|
||||
s.Notifier.NotifyFailure(reason)
|
||||
}
|
||||
}
|
||||
|
||||
func uniquePNGPath(dir string, now time.Time) string {
|
||||
base := now.Format("20060102-150405")
|
||||
path := filepath.Join(dir, base+".png")
|
||||
for i := 1; fileExists(path); i++ {
|
||||
path = filepath.Join(dir, fmt.Sprintf("%s-%02d.png", base, i))
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeClipboard struct {
|
||||
text string
|
||||
image []byte
|
||||
}
|
||||
|
||||
func (f *fakeClipboard) WriteText(s string) error {
|
||||
f.text = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeClipboard) WriteImage(pngBytes []byte) error {
|
||||
f.image = append([]byte(nil), pngBytes...)
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeNotifier struct {
|
||||
success string
|
||||
failure string
|
||||
}
|
||||
|
||||
func (f *fakeNotifier) NotifySuccess(value string) { f.success = value }
|
||||
func (f *fakeNotifier) NotifyFailure(reason string) { f.failure = reason }
|
||||
|
||||
func TestCaptureActionsCopyImageWritesPNGToClipboard(t *testing.T) {
|
||||
clip := &fakeClipboard{}
|
||||
notifier := &fakeNotifier{}
|
||||
svc := &CaptureActionsService{Clipboard: clip, Notifier: notifier}
|
||||
|
||||
err := svc.CopyImage(context.Background(), []byte("png"))
|
||||
if err != nil {
|
||||
t.Fatalf("copy image: %v", err)
|
||||
}
|
||||
if string(clip.image) != "png" {
|
||||
t.Fatalf("expected image bytes copied, got %q", string(clip.image))
|
||||
}
|
||||
if notifier.success != "image copied to clipboard" {
|
||||
t.Fatalf("expected copy success notification, got %q", notifier.success)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureActionsSaveImageWritesUniquePNG(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
svc := &CaptureActionsService{Notifier: &fakeNotifier{}}
|
||||
|
||||
path, err := svc.SaveImage(context.Background(), []byte("png"), dir)
|
||||
if err != nil {
|
||||
t.Fatalf("save image: %v", err)
|
||||
}
|
||||
if filepath.Dir(path) != dir {
|
||||
t.Fatalf("expected save in %q, got %q", dir, path)
|
||||
}
|
||||
if data, err := os.ReadFile(path); err != nil || string(data) != "png" {
|
||||
t.Fatalf("expected saved bytes, data=%q err=%v", string(data), err)
|
||||
}
|
||||
|
||||
next, err := svc.SaveImage(context.Background(), []byte("png2"), dir)
|
||||
if err != nil {
|
||||
t.Fatalf("save second image: %v", err)
|
||||
}
|
||||
if next == path {
|
||||
t.Fatalf("expected unique path for second save")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user