Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 250ba269f5 |
@@ -1,67 +0,0 @@
|
|||||||
# AGENTS.md
|
|
||||||
|
|
||||||
## 项目概述
|
|
||||||
|
|
||||||
SnapGo 是一个常驻菜单栏的轻量截图工具:全局快捷键唤起选区截图,确认后自动上传到 S3 兼容对象存储(或通过 SSH/SCP 保存到远端),并把可分享链接复制到剪贴板。目前主要打磨 macOS 体验。
|
|
||||||
|
|
||||||
技术栈:**Wails v2**(Go 后端 + WebView 前端)+ **Vue 3 / TypeScript / Vite** 前端。Go 后端遵循分层架构(domain → application → infrastructure),macOS 上截图选区使用原生 AppKit overlay,其它平台回退到 Wails 透明窗口 overlay。
|
|
||||||
|
|
||||||
### 分层架构(`internal/`)
|
|
||||||
|
|
||||||
- `domain/`:核心业务类型与接口,**不依赖任何第三方 SDK 或 OS API**(仅标准库)。如 `OSSProvider` 接口、`AppConfig`/`S3Config`/`SSHConfig`、`Screenshot`/`UploadResult`。
|
|
||||||
- `application/`:用例编排(capture → upload/save → clipboard → notify)。**只依赖 domain 接口**,具体适配器从 `main.go`/`app.go` 注入,便于单测。如 `CaptureAndUploadService`、`CaptureActionsService`、`CaptureAndSSHService`、`ApplyAnnotations`。
|
|
||||||
- `infrastructure/`:对接外部世界的适配器实现 —— `oss/`(S3)、`ssh/`(内置 Go client + Kerberos 委托系统 ssh)、`clipboard/`、`config/`(JSON 文件存储)、`screencapture/`、`display/`、`cursor/`、`hotkey/`、`tray/`(菜单栏)、`logging/`。
|
|
||||||
|
|
||||||
### 顶层文件(`main` 包,根目录)
|
|
||||||
|
|
||||||
- `main.go`:Wails 启动入口,embed `frontend/dist`,装配 tray 与 `App`。
|
|
||||||
- `app.go`:`App` 结构体,通过 Wails `Bind` 暴露给前端的方法(RPC),编排截图/上传/保存流程与 overlay 窗口生命周期。
|
|
||||||
- `*_darwin.go` / `*_other.go`:平台分离的 overlay、dock 图标、激活策略、帧刷新等实现。`native_overlay_darwin.go` 使用 cgo + AppKit。
|
|
||||||
|
|
||||||
### 关键约定
|
|
||||||
|
|
||||||
- 前端通过 Wails 自动生成的 `frontend/wailsjs/go/main/App.js` 调用后端 `App` 的导出方法;后端通过 `wruntime.EventsEmit` 向前端推事件(如 `upload:success`、`upload:failure`、`hotkey:ready`、`capture:overlay`)。
|
|
||||||
- 新增 OSS provider 只需在 `infrastructure/oss/` 加适配器并实现 `domain.OSSProvider`,无需改 application 层。
|
|
||||||
- S3 与 SSH 走独立 pipeline(`runUploadPipeline` vs `runSaveRemotePipeline`),避免 provider 分支泄漏进彼此。
|
|
||||||
|
|
||||||
## 构建与命令
|
|
||||||
|
|
||||||
依赖前置:Go 1.23+、Node/npm、`wails` CLI(`go install github.com/wailsapp/wails/v2/cmd/wails@latest`)。
|
|
||||||
|
|
||||||
- `go mod tidy`:同步 Go 依赖。
|
|
||||||
- `wails dev`:开发模式(热重载前端 + 后端),日志可直接在终端看到。
|
|
||||||
- `wails build -platform darwin/arm64 -clean`:构建 `.app`(产物在 `build/bin/`)。
|
|
||||||
- `./scripts/create-dev-cert.sh`:首次创建本地自签开发证书(保证 macOS TCC 权限不被反复重置)。
|
|
||||||
- `./scripts/dev-build.sh`:本地迭代用 —— 构建 + 稳定证书重签名 + 同步到 `/Applications` + 打开。优先用它而非裸 `wails build`。
|
|
||||||
- `./scripts/release.sh`:一键 build → sign → DMG → notarize(notarize 需 `NOTARIZE=1` 与凭证,默认跳过)。`ARCH=universal` 可出 fat binary。
|
|
||||||
- 前端单独命令(`frontend/`):`npm run dev` / `npm run build`(`vue-tsc --noEmit && vite build`)/ `npm run preview`。
|
|
||||||
|
|
||||||
## 代码风格
|
|
||||||
|
|
||||||
- **Go**:标准 `gofmt`(tab 缩进)。包级 doc 注释说明“设计动机/rationale”是本仓库的强约定,注释解释“为什么”而非“是什么”。导出标识符必须有注释。错误用 `fmt.Errorf("...: %w", err)` 包装。日志统一用 `log/slog`(结构化 kv,如 `slog.Info("...", "host", h)`)。
|
|
||||||
- **平台分离**:用 `_darwin.go` / `_other.go` 文件名后缀或 `//go:build` 标签隔离平台代码,保持跨平台编译通过。
|
|
||||||
- **TypeScript/Vue**:Vue 3 `<script setup>` + Composition API;2 空格缩进。视图在 `frontend/src/views/`,组件在 `components/`。
|
|
||||||
- 注释中可使用中文(部分 infra 包如 logging 已用中文 rationale),与现有文件保持一致即可。
|
|
||||||
- 不要手改 `frontend/wailsjs/` 下文件,它们由 Wails 生成。
|
|
||||||
|
|
||||||
## 测试
|
|
||||||
|
|
||||||
- 框架:Go 标准 `testing` 包(无第三方断言库)。运行:`go test ./...`。
|
|
||||||
- 现有测试集中在 `internal/application/`(如 `capture_actions_test.go`、`annotation_test.go`)。
|
|
||||||
- 约定:application 层依赖 domain 接口,测试用手写 fake 实现(如 `fakeClipboard`、`fakeNotifier`)注入,不触碰真实 OS/网络。文件 IO 测试使用 `t.TempDir()`。
|
|
||||||
- infrastructure 适配器(S3/SSH/screencapture 等)依赖真实环境,通常不写单测;SSH 连通性可借助 `cmd/sshdiag/` 诊断工具与设置页的 “Test connection” 按钮验证。
|
|
||||||
|
|
||||||
## 安全
|
|
||||||
|
|
||||||
- **凭证落盘**:配置(含 S3 access key、SSH 密码)以 JSON 存于 `os.UserConfigDir()/SnapGo/config.json`,文件权限 `0600`、目录 `0700`。尚未接入 OS keychain(后续计划),改动配置存储时务必保持严格权限并避免把密钥写入日志。
|
|
||||||
- **日志**:写入 `~/Library/Logs/SnapGo/snapgo.log`(大小滚动,5MiB×2 备份)。记录连接信息时只记 host/user/port 等非敏感字段,**切勿记录密码/密钥**(参考 `app.go` 中的 `slog` 用法)。
|
|
||||||
- **SSH**:`StrictHostKey=false` 时使用 `InsecureIgnoreHostKey()`(牺牲安全换首启可用性,仅限个人 LAN);Kerberos 模式委托系统 `/usr/bin/ssh` 复用 `kinit` 凭证缓存。`PathPrefix` 相对远端 `$HOME`,会剥离前导 `/` 和 `~` 防止逃逸。
|
|
||||||
- **上传兜底**:上传失败时把 PNG 落地到 `~/Pictures/SnapGo` 并复制本地路径,保证不丢截图。
|
|
||||||
- macOS 需 Screen Recording / Accessibility(TCC)权限;稳定的代码签名身份与安装路径是权限保持的关键(见 `dev-build.sh` 注释)。
|
|
||||||
|
|
||||||
## 配置
|
|
||||||
|
|
||||||
- Wails 项目配置:`wails.json`(产物名、frontend install/build/dev 命令、应用元信息)。
|
|
||||||
- 运行时配置:首启写入默认值(`domain.DefaultAppConfig()`:快捷键 `cmd+shift+a`、S3 `pathPrefix=snapgo/`、SSH port 22 / timeout 10s)。原子写入(temp + rename)。
|
|
||||||
- 环境变量:`SNAPGO_LOG_LEVEL=debug|info|warn|error`(默认 debug)。Release 相关:`ARCH`、`NOTARIZE`、`SIGN_MODE`、`DEVELOPER_ID_APPLICATION`、`KEYCHAIN_PROFILE`、`APPLE_ID`/`APPLE_TEAM_ID`/`APPLE_APP_SPECIFIC_PASSWORD`。
|
|
||||||
- S3 兼容性:`UsePathStyle` 默认 true(适配 MinIO/R2);`PublicURLBase` 可选用于 CDN/自定义域名,否则回退 `{Endpoint}/{Bucket}/{Key}`。
|
|
||||||
@@ -21,7 +21,6 @@ SnapGo 是一个为“截完图马上发链接”而做的轻量截图工具。
|
|||||||
- 全局快捷键触发:无论当前在什么应用里,都能快速发起截图
|
- 全局快捷键触发:无论当前在什么应用里,都能快速发起截图
|
||||||
- 菜单栏常驻:随用随取,不打断主流程
|
- 菜单栏常驻:随用随取,不打断主流程
|
||||||
- 支持 S3 兼容存储:可接入 AWS S3、MinIO、Cloudflare R2、Backblaze B2 等
|
- 支持 S3 兼容存储:可接入 AWS S3、MinIO、Cloudflare R2、Backblaze B2 等
|
||||||
- 支持 FTP / SFTP:可通过独立截图操作上传到文件服务器,并复制远端路径
|
|
||||||
- 支持自定义公开地址:可配合 CDN 或自定义域名使用
|
- 支持自定义公开地址:可配合 CDN 或自定义域名使用
|
||||||
- 上传失败自动兜底:至少保住截图文件,不会白截
|
- 上传失败自动兜底:至少保住截图文件,不会白截
|
||||||
|
|
||||||
@@ -58,11 +57,11 @@ SnapGo 是一个为“截完图马上发链接”而做的轻量截图工具。
|
|||||||
|
|
||||||
## 使用方式
|
## 使用方式
|
||||||
|
|
||||||
1. 打开应用,填写 S3、FTP / SFTP 或 SSH 远端配置
|
1. 打开应用,填写你的 S3 兼容对象存储配置
|
||||||
2. 保存并测试连接
|
2. 保存并测试连接
|
||||||
3. 按下默认快捷键 `cmd+shift+a`
|
3. 按下默认快捷键 `cmd+shift+a`
|
||||||
4. 框选截图区域
|
4. 框选截图区域
|
||||||
5. 点击目标上传按钮,直接粘贴自动复制的图片链接或远端路径
|
5. 直接粘贴刚刚自动复制好的图片链接
|
||||||
|
|
||||||
## 当前体验
|
## 当前体验
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,6 @@ package main
|
|||||||
/*
|
/*
|
||||||
#cgo CFLAGS: -x objective-c -fobjc-arc
|
#cgo CFLAGS: -x objective-c -fobjc-arc
|
||||||
#cgo LDFLAGS: -framework AppKit
|
#cgo LDFLAGS: -framework AppKit
|
||||||
// Silence the "ignoring duplicate libraries: '-lobjc'" note emitted by the
|
|
||||||
// Xcode 15+ linker (ld-prime): cgo links -lobjc for our Objective-C code and
|
|
||||||
// the Go darwin runtime pulls it in as well, which the new linker flags as a
|
|
||||||
// duplicate. Suppressing it here keeps the build output clean; the flag is a
|
|
||||||
// no-op on the legacy ld64 path.
|
|
||||||
#cgo LDFLAGS: -Wl,-no_warn_duplicate_libraries
|
|
||||||
#include <dispatch/dispatch.h>
|
#include <dispatch/dispatch.h>
|
||||||
#import <AppKit/AppKit.h>
|
#import <AppKit/AppKit.h>
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
"image/png"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@@ -24,10 +21,7 @@ import (
|
|||||||
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
|
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/config"
|
"github.com/mmmy/snapgo/internal/infrastructure/config"
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/display"
|
"github.com/mmmy/snapgo/internal/infrastructure/display"
|
||||||
ftppkg "github.com/mmmy/snapgo/internal/infrastructure/ftp"
|
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/hotkey"
|
"github.com/mmmy/snapgo/internal/infrastructure/hotkey"
|
||||||
llmpkg "github.com/mmmy/snapgo/internal/infrastructure/llm"
|
|
||||||
ocrpkg "github.com/mmmy/snapgo/internal/infrastructure/ocr"
|
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/oss"
|
"github.com/mmmy/snapgo/internal/infrastructure/oss"
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/screencapture"
|
"github.com/mmmy/snapgo/internal/infrastructure/screencapture"
|
||||||
sshpkg "github.com/mmmy/snapgo/internal/infrastructure/ssh"
|
sshpkg "github.com/mmmy/snapgo/internal/infrastructure/ssh"
|
||||||
@@ -37,8 +31,8 @@ import (
|
|||||||
//
|
//
|
||||||
// We deliberately keep this struct small: it owns long-lived collaborators
|
// We deliberately keep this struct small: it owns long-lived collaborators
|
||||||
// (config store, hotkey manager, capturer, clipboard) but delegates the
|
// (config store, hotkey manager, capturer, clipboard) but delegates the
|
||||||
// real work to application services constructed on demand for the selected
|
// real work to the application service constructed on demand once an OSS
|
||||||
// storage destination.
|
// provider is configured.
|
||||||
type App struct {
|
type App struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
|
||||||
@@ -102,15 +96,6 @@ type CaptureActionResult struct {
|
|||||||
Path string `json:"path,omitempty"`
|
Path string `json:"path,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// OperationStatusPayload drives the frontend status HUD and mirrors the
|
|
||||||
// native macOS HUD state.
|
|
||||||
type OperationStatusPayload struct {
|
|
||||||
Operation string `json:"operation"`
|
|
||||||
Phase string `json:"phase"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
State string `json:"state"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewApp creates a new App with collaborators already initialised.
|
// NewApp creates a new App with collaborators already initialised.
|
||||||
func NewApp() *App {
|
func NewApp() *App {
|
||||||
store, err := config.NewFileStore()
|
store, err := config.NewFileStore()
|
||||||
@@ -125,7 +110,6 @@ func NewApp() *App {
|
|||||||
slog.Warn("config load failed, using defaults", "err", lerr)
|
slog.Warn("config load failed, using defaults", "err", lerr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cfg.Normalize()
|
|
||||||
return &App{
|
return &App{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
configFile: store,
|
configFile: store,
|
||||||
@@ -243,8 +227,8 @@ func (a *App) runInteractiveCapture() {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
const (
|
const (
|
||||||
settingsWidth = 1000
|
settingsWidth = 1080
|
||||||
settingsHeight = 820
|
settingsHeight = 720
|
||||||
)
|
)
|
||||||
|
|
||||||
// showOverlayWindow prepares the hidden main window as a borderless,
|
// showOverlayWindow prepares the hidden main window as a borderless,
|
||||||
@@ -313,44 +297,23 @@ func (a *App) runUploadPipeline(provider domain.OSSProvider, pngBytes []byte) er
|
|||||||
FallbackDir: filepath.Join(userPicturesDir(), "SnapGo"),
|
FallbackDir: filepath.Join(userPicturesDir(), "SnapGo"),
|
||||||
PathPrefix: cfg.S3.PathPrefix,
|
PathPrefix: cfg.S3.PathPrefix,
|
||||||
}
|
}
|
||||||
a.emitOperationStatus("upload", "上传中", "正在上传截图到 S3", "running")
|
return svc.ExecuteWithBytes(a.ctx, pngBytes)
|
||||||
if err := svc.ExecuteWithBytes(a.ctx, pngBytes); err != nil {
|
|
||||||
a.emitOperationStatus("upload", "上传失败", err.Error(), "error")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("upload", "上传完成", "链接已复制到剪贴板", "success")
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) runCopyImagePipeline(pngBytes []byte) error {
|
func (a *App) runCopyImagePipeline(pngBytes []byte) error {
|
||||||
svc := &application.CaptureActionsService{
|
svc := &application.CaptureActionsService{
|
||||||
Clipboard: a.clip,
|
Clipboard: a.clip,
|
||||||
|
Notifier: &runtimeNotifier{ctx: a.ctx},
|
||||||
}
|
}
|
||||||
a.emitOperationStatus("copy", "复制中", "正在把截图复制到剪贴板", "running")
|
return svc.CopyImage(a.ctx, pngBytes)
|
||||||
if err := svc.CopyImage(a.ctx, pngBytes); err != nil {
|
|
||||||
a.emitOperationStatus("copy", "复制失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("copy", "复制完成", "截图已复制到剪贴板", "success")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:success", "image copied to clipboard")
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) runSaveImagePipeline(pngBytes []byte, dir string) (string, error) {
|
func (a *App) runSaveImagePipeline(pngBytes []byte, dir string) (string, error) {
|
||||||
svc := &application.CaptureActionsService{
|
svc := &application.CaptureActionsService{
|
||||||
Clipboard: a.clip,
|
Clipboard: a.clip,
|
||||||
|
Notifier: &runtimeNotifier{ctx: a.ctx},
|
||||||
}
|
}
|
||||||
a.emitOperationStatus("save", "保存中", "正在保存截图到本地", "running")
|
return svc.SaveImage(a.ctx, pngBytes, dir)
|
||||||
path, err := svc.SaveImage(a.ctx, pngBytes, dir)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("save", "保存失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("save", "保存完成", "文件路径已复制到剪贴板:"+path, "success")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:success", path)
|
|
||||||
return path, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// runSaveRemotePipeline uploads the captured PNG to the configured SSH host
|
// runSaveRemotePipeline uploads the captured PNG to the configured SSH host
|
||||||
@@ -369,7 +332,6 @@ func (a *App) runSaveRemotePipeline(pngBytes []byte) error {
|
|||||||
err := fmt.Errorf("SSH host/user is not configured")
|
err := fmt.Errorf("SSH host/user is not configured")
|
||||||
slog.Warn("save-remote rejected: ssh not configured",
|
slog.Warn("save-remote rejected: ssh not configured",
|
||||||
"host", cfg.SSH.Host, "user", cfg.SSH.User)
|
"host", cfg.SSH.Host, "user", cfg.SSH.User)
|
||||||
a.emitOperationStatus("save-remote", "需要配置 SSH", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -378,12 +340,16 @@ func (a *App) runSaveRemotePipeline(pngBytes []byte) error {
|
|||||||
"auth_method", cfg.SSH.AuthMethod, "png_size", len(pngBytes))
|
"auth_method", cfg.SSH.AuthMethod, "png_size", len(pngBytes))
|
||||||
|
|
||||||
// Select the uploader by auth method: Kerberos delegates to the system
|
// Select the uploader by auth method: Kerberos delegates to the system
|
||||||
// ssh/scp binaries (reusing the kinit credential cache), while builtin
|
// ssh/scp binaries (reusing the kinit credential cache), bgo delegates to
|
||||||
// uses the in-process Go SSH client.
|
// the internal `bgo scp` client (under a PTY), while builtin uses the
|
||||||
|
// in-process Go SSH client.
|
||||||
var uploader application.SSHUploader
|
var uploader application.SSHUploader
|
||||||
if cfg.SSH.IsKerberos() {
|
switch {
|
||||||
|
case cfg.SSH.IsKerberos():
|
||||||
uploader = sshpkg.NewKerberosUploader(cfg.SSH)
|
uploader = sshpkg.NewKerberosUploader(cfg.SSH)
|
||||||
} else {
|
case cfg.SSH.IsBgo():
|
||||||
|
uploader = sshpkg.NewBgoUploader(cfg.SSH)
|
||||||
|
default:
|
||||||
uploader = sshpkg.NewUploader(cfg.SSH)
|
uploader = sshpkg.NewUploader(cfg.SSH)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,207 +359,7 @@ func (a *App) runSaveRemotePipeline(pngBytes []byte) error {
|
|||||||
Notifier: &runtimeNotifier{ctx: a.ctx},
|
Notifier: &runtimeNotifier{ctx: a.ctx},
|
||||||
Cfg: cfg.SSH,
|
Cfg: cfg.SSH,
|
||||||
}
|
}
|
||||||
a.emitOperationStatus("save-remote", "保存中", "正在保存截图到远端 SSH", "running")
|
return svc.ExecuteWithBytes(a.ctx, pngBytes)
|
||||||
if err := svc.ExecuteWithBytes(a.ctx, pngBytes); err != nil {
|
|
||||||
a.emitOperationStatus("save-remote", "保存失败", err.Error(), "error")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("save-remote", "保存完成", "远端路径已复制到剪贴板", "success")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// runFTPUploadPipeline uploads through the protocol selected in FTPConfig and
|
|
||||||
// copies the resulting remote path. FTP/SFTP remains separate from both S3
|
|
||||||
// (public URL semantics) and SSH/SCP (a distinct toolbar destination).
|
|
||||||
func (a *App) runFTPUploadPipeline(pngBytes []byte) error {
|
|
||||||
a.mu.RLock()
|
|
||||||
cfg := a.cfg
|
|
||||||
a.mu.RUnlock()
|
|
||||||
|
|
||||||
if !cfg.IsFTPConfigured() {
|
|
||||||
err := fmt.Errorf("FTP/SFTP host/user is not configured")
|
|
||||||
a.emitOperationStatus("ftp-upload", "需要配置 FTP/SFTP", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
uploader, err := ftppkg.NewUploader(cfg.FTP)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("ftp-upload", "FTP/SFTP 配置错误", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
slog.Info("FTP/SFTP upload dispatch",
|
|
||||||
"protocol", cfg.FTP.Protocol,
|
|
||||||
"host", cfg.FTP.Host,
|
|
||||||
"user", cfg.FTP.User,
|
|
||||||
"port", cfg.FTP.Port,
|
|
||||||
"auth_method", cfg.FTP.AuthMethod,
|
|
||||||
"strict_host_key", cfg.FTP.StrictHostKey,
|
|
||||||
"has_password", cfg.FTP.Password != "",
|
|
||||||
"png_size", len(pngBytes))
|
|
||||||
|
|
||||||
svc := &application.CaptureAndFTPService{
|
|
||||||
Uploader: uploader,
|
|
||||||
Clipboard: a.clip,
|
|
||||||
Notifier: &runtimeNotifier{ctx: a.ctx},
|
|
||||||
Cfg: cfg.FTP,
|
|
||||||
}
|
|
||||||
protocolLabel := strings.ToUpper(cfg.FTP.Protocol)
|
|
||||||
if protocolLabel == "" {
|
|
||||||
protocolLabel = "FTP"
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("ftp-upload", "上传中", "正在上传截图到 "+protocolLabel, "running")
|
|
||||||
if err := svc.ExecuteWithBytes(a.ctx, pngBytes); err != nil {
|
|
||||||
a.emitOperationStatus("ftp-upload", "上传失败", err.Error(), "error")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("ftp-upload", "上传完成", "远端路径已复制到剪贴板", "success")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *App) runSummaryPipeline(pngBytes []byte) error {
|
|
||||||
a.mu.RLock()
|
|
||||||
cfg := a.cfg
|
|
||||||
a.mu.RUnlock()
|
|
||||||
|
|
||||||
if !cfg.IsLLMConfigured() {
|
|
||||||
err := fmt.Errorf("请先在 LLM 配置页填写 API Key 和模型")
|
|
||||||
a.emitOperationStatus("summary", "需要配置 LLM", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
providerID, llmCfg, _ := cfg.ActiveLLMProvider()
|
|
||||||
visionClient, err := llmpkg.NewVisionClient(providerID, llmCfg)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("summary", "LLM 配置错误", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
svc := &application.CaptureSummaryService{
|
|
||||||
Summarizer: visionClient,
|
|
||||||
Clipboard: a.clip,
|
|
||||||
Prompt: cfg.LLM.Prompt,
|
|
||||||
PathPrefix: cfg.S3.PathPrefix,
|
|
||||||
MaxInlineBytes: llmCfg.MaxInlineBytes,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Preferred path: send the screenshot inline as a base64 data URL so the
|
|
||||||
// summary never depends on S3 being configured or publicly reachable.
|
|
||||||
if svc.CanInline(pngBytes) {
|
|
||||||
a.emitOperationStatus("summary", "识别中", "正在请求多模态模型(本地内联图片)", "running")
|
|
||||||
summary, err := svc.Summarize(a.ctx, svc.InlineDataURL(pngBytes))
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("summary", "识别失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.finishSummary(svc, summary)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Oversized image: fall back to uploading to S3 and passing a public URL.
|
|
||||||
if !cfg.IsS3Configured() {
|
|
||||||
err := fmt.Errorf("截图尺寸超过内联上限(%d 字节),请缩小截图区域,或在 S3 配置页填写公网可达的对象存储后重试", llmCfg.MaxInlineBytes)
|
|
||||||
a.emitOperationStatus("summary", "尺寸超限", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s3Provider, err := oss.NewS3Provider(cfg.S3)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("summary", "S3 配置错误", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
svc.Provider = s3Provider
|
|
||||||
|
|
||||||
a.emitOperationStatus("summary", "上传中", "截图较大,正在上传供模型读取", "running")
|
|
||||||
uploaded, err := svc.UploadImage(a.ctx, pngBytes)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("summary", "上传失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// The remote model fetches the URL itself, so verify it is reachable and
|
|
||||||
// clean up the temporary object once we are done with it either way.
|
|
||||||
defer func() {
|
|
||||||
if derr := svc.DeleteUploaded(a.ctx, uploaded.Key); derr != nil {
|
|
||||||
slog.Warn("summary: failed to delete temporary object", "key", uploaded.Key, "err", derr)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := svc.EnsureReachable(a.ctx, uploaded.URL); err != nil {
|
|
||||||
a.emitOperationStatus("summary", "链接不可达", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
a.emitOperationStatus("summary", "识别中", "图片已上传,正在请求多模态模型", "running")
|
|
||||||
summary, err := svc.Summarize(a.ctx, uploaded.URL)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("summary", "识别失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.finishSummary(svc, summary)
|
|
||||||
}
|
|
||||||
|
|
||||||
// finishSummary copies the summary to the clipboard and emits the terminal
|
|
||||||
// success/failure UI state shared by the inline and S3 paths.
|
|
||||||
func (a *App) finishSummary(svc *application.CaptureSummaryService, summary string) error {
|
|
||||||
if err := svc.CopySummary(a.ctx, summary); err != nil {
|
|
||||||
a.emitOperationStatus("summary", "复制失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("summary", "识别完成", "总结已复制到剪贴板", "success")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:success", "summary copied to clipboard")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *App) runOCRPipeline(pngBytes []byte) error {
|
|
||||||
a.mu.RLock()
|
|
||||||
cfg := a.cfg
|
|
||||||
a.mu.RUnlock()
|
|
||||||
|
|
||||||
if !cfg.IsOCRConfigured() {
|
|
||||||
err := fmt.Errorf("请先在文字提取配置页填写 OCR Provider 的 AccessKey ID 和 AccessKey Secret")
|
|
||||||
a.emitOperationStatus("ocr", "需要配置 OCR", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
providerID, ocrCfg, _ := cfg.ActiveOCRProvider()
|
|
||||||
recognizer, err := ocrpkg.NewClient(providerID, ocrCfg)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("ocr", "OCR 配置错误", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
svc := &application.CaptureOCRService{
|
|
||||||
Recognizer: recognizer,
|
|
||||||
Clipboard: a.clip,
|
|
||||||
}
|
|
||||||
|
|
||||||
a.emitOperationStatus("ocr", "识别中", "正在提取截图文字", "running")
|
|
||||||
text, err := svc.Recognize(a.ctx, pngBytes)
|
|
||||||
if err != nil {
|
|
||||||
a.emitOperationStatus("ocr", "识别失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := svc.CopyText(a.ctx, text); err != nil {
|
|
||||||
a.emitOperationStatus("ocr", "复制失败", err.Error(), "error")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
a.emitOperationStatus("ocr", "提取完成", "文字已复制到剪贴板", "success")
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:success", "ocr text copied to clipboard")
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) consumePendingCapture() (*pendingCapture, error) {
|
func (a *App) consumePendingCapture() (*pendingCapture, error) {
|
||||||
@@ -616,8 +382,7 @@ func (a *App) captureSelectedPNG(result CaptureResult, pc *pendingCapture) ([]by
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(result.Annotations) > 0 {
|
if len(result.Annotations) > 0 {
|
||||||
scaleX, scaleY := annotationScalesForCapture(cropped, rect, pc.Display.Scale)
|
cropped, err = application.ApplyAnnotations(cropped, result.Annotations, pc.Display.Scale)
|
||||||
cropped, err = application.ApplyAnnotationsWithScale(cropped, result.Annotations, scaleX, scaleY)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -625,33 +390,6 @@ func (a *App) captureSelectedPNG(result CaptureResult, pc *pendingCapture) ([]by
|
|||||||
return cropped, nil
|
return cropped, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func annotationScalesForCapture(pngBytes []byte, rect RegionRect, fallback float64) (float64, float64) {
|
|
||||||
if fallback <= 0 {
|
|
||||||
fallback = 1
|
|
||||||
}
|
|
||||||
scaleX := fallback
|
|
||||||
scaleY := fallback
|
|
||||||
|
|
||||||
cfg, err := png.DecodeConfig(bytes.NewReader(pngBytes))
|
|
||||||
if err != nil {
|
|
||||||
return scaleX, scaleY
|
|
||||||
}
|
|
||||||
if rect.W > 0 && cfg.Width > 0 {
|
|
||||||
scaleX = saneAnnotationScale(float64(cfg.Width)/float64(rect.W), fallback)
|
|
||||||
}
|
|
||||||
if rect.H > 0 && cfg.Height > 0 {
|
|
||||||
scaleY = saneAnnotationScale(float64(cfg.Height)/float64(rect.H), fallback)
|
|
||||||
}
|
|
||||||
return scaleX, scaleY
|
|
||||||
}
|
|
||||||
|
|
||||||
func saneAnnotationScale(scale, fallback float64) float64 {
|
|
||||||
if scale >= 0.25 && scale <= 8 {
|
|
||||||
return scale
|
|
||||||
}
|
|
||||||
return fallback
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *App) chooseSaveDirectory() (string, error) {
|
func (a *App) chooseSaveDirectory() (string, error) {
|
||||||
return wruntime.OpenDirectoryDialog(a.ctx, wruntime.OpenDialogOptions{
|
return wruntime.OpenDirectoryDialog(a.ctx, wruntime.OpenDialogOptions{
|
||||||
Title: "Save screenshot to folder",
|
Title: "Save screenshot to folder",
|
||||||
@@ -674,7 +412,6 @@ func (a *App) GetConfig() domain.AppConfig {
|
|||||||
// SaveConfig persists the supplied configuration and re-registers the hotkey
|
// SaveConfig persists the supplied configuration and re-registers the hotkey
|
||||||
// if it changed.
|
// if it changed.
|
||||||
func (a *App) SaveConfig(cfg domain.AppConfig) error {
|
func (a *App) SaveConfig(cfg domain.AppConfig) error {
|
||||||
cfg.Normalize()
|
|
||||||
a.mu.Lock()
|
a.mu.Lock()
|
||||||
prev := a.cfg
|
prev := a.cfg
|
||||||
a.cfg = cfg
|
a.cfg = cfg
|
||||||
@@ -734,6 +471,15 @@ func (a *App) TestSSHConnection(cfg domain.SSHConfig) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
// bgo manages its own auth; we can only confirm the binary is installed
|
||||||
|
// and resolvable (a real transfer would need a live remote + PTY).
|
||||||
|
if cfg.IsBgo() {
|
||||||
|
if err := sshpkg.TestBgoConnection(a.ctx, cfg); err != nil {
|
||||||
|
slog.Error("RPC TestSSHConnection (bgo) failed", "err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if err := sshpkg.TestConnection(a.ctx, cfg); err != nil {
|
if err := sshpkg.TestConnection(a.ctx, cfg); err != nil {
|
||||||
slog.Error("RPC TestSSHConnection failed", "err", err)
|
slog.Error("RPC TestSSHConnection failed", "err", err)
|
||||||
return err
|
return err
|
||||||
@@ -741,33 +487,6 @@ func (a *App) TestSSHConnection(cfg domain.SSHConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestFTPConnection performs a write/delete probe with the supplied FTP or
|
|
||||||
// SFTP configuration so Settings can verify both authentication and remote
|
|
||||||
// directory permissions before saving.
|
|
||||||
func (a *App) TestFTPConnection(cfg domain.FTPConfig) error {
|
|
||||||
slog.Info("RPC TestFTPConnection",
|
|
||||||
"protocol", cfg.Protocol,
|
|
||||||
"host", cfg.Host,
|
|
||||||
"user", cfg.User,
|
|
||||||
"port", cfg.Port,
|
|
||||||
"auth_method", cfg.AuthMethod,
|
|
||||||
"strict_host_key", cfg.StrictHostKey,
|
|
||||||
"has_password", cfg.Password != "")
|
|
||||||
uploader, err := ftppkg.NewUploader(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := uploader.TestConnection(a.ctx); err != nil {
|
|
||||||
slog.Error("RPC TestFTPConnection failed",
|
|
||||||
"protocol", cfg.Protocol,
|
|
||||||
"host", cfg.Host,
|
|
||||||
"user", cfg.User,
|
|
||||||
"err", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureNow is the in-app trigger.
|
// CaptureNow is the in-app trigger.
|
||||||
func (a *App) CaptureNow() {
|
func (a *App) CaptureNow() {
|
||||||
go a.runInteractiveCapture()
|
go a.runInteractiveCapture()
|
||||||
@@ -999,88 +718,6 @@ func (a *App) SaveRegionToRemote(result CaptureResult) error {
|
|||||||
return a.runSaveRemotePipeline(cropped)
|
return a.runSaveRemotePipeline(cropped)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadRegionToFTP uploads the selected region through the configured FTP or
|
|
||||||
// SFTP destination. This is the non-macOS Wails overlay action.
|
|
||||||
func (a *App) UploadRegionToFTP(result CaptureResult) error {
|
|
||||||
pc, err := a.consumePendingCapture()
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("UploadRegionToFTP: no pending capture", "err", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
a.capturing.Store(false)
|
|
||||||
a.dismissOverlay()
|
|
||||||
}()
|
|
||||||
|
|
||||||
a.dismissOverlay()
|
|
||||||
flushFrame()
|
|
||||||
cropped, err := a.captureSelectedPNG(result, pc)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("UploadRegionToFTP: capture failed", "err", err)
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.runFTPUploadPipeline(cropped)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SummarizeRegion uploads the selected screenshot to S3, sends the public URL
|
|
||||||
// to the configured multimodal LLM, and copies the resulting summary.
|
|
||||||
func (a *App) SummarizeRegion(result CaptureResult) error {
|
|
||||||
pc, err := a.consumePendingCapture()
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("SummarizeRegion: no pending capture", "err", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
a.capturing.Store(false)
|
|
||||||
a.dismissOverlay()
|
|
||||||
}()
|
|
||||||
|
|
||||||
a.dismissOverlay()
|
|
||||||
flushFrame()
|
|
||||||
|
|
||||||
slog.Info("SummarizeRegion: capturing region",
|
|
||||||
"x", result.Rect.X, "y", result.Rect.Y,
|
|
||||||
"w", result.Rect.W, "h", result.Rect.H,
|
|
||||||
"annotations", len(result.Annotations))
|
|
||||||
cropped, err := a.captureSelectedPNG(result, pc)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("SummarizeRegion: capture failed", "err", err)
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.runSummaryPipeline(cropped)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExtractTextRegion sends the selected screenshot to the configured OCR
|
|
||||||
// provider and copies the extracted text to the clipboard.
|
|
||||||
func (a *App) ExtractTextRegion(result CaptureResult) error {
|
|
||||||
pc, err := a.consumePendingCapture()
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("ExtractTextRegion: no pending capture", "err", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
a.capturing.Store(false)
|
|
||||||
a.dismissOverlay()
|
|
||||||
}()
|
|
||||||
|
|
||||||
a.dismissOverlay()
|
|
||||||
flushFrame()
|
|
||||||
|
|
||||||
slog.Info("ExtractTextRegion: capturing region",
|
|
||||||
"x", result.Rect.X, "y", result.Rect.Y,
|
|
||||||
"w", result.Rect.W, "h", result.Rect.H,
|
|
||||||
"annotations", len(result.Annotations))
|
|
||||||
cropped, err := a.captureSelectedPNG(result, pc)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("ExtractTextRegion: capture failed", "err", err)
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.runOCRPipeline(cropped)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SaveNativeRegionToRemote is the macOS-native overlay equivalent of
|
// SaveNativeRegionToRemote is the macOS-native overlay equivalent of
|
||||||
// SaveRegionToRemote. The AppKit panel is already closed by the time this
|
// SaveRegionToRemote. The AppKit panel is already closed by the time this
|
||||||
// runs (see saveRemoteSelection in native_overlay_darwin.go), so we only
|
// runs (see saveRemoteSelection in native_overlay_darwin.go), so we only
|
||||||
@@ -1111,83 +748,6 @@ func (a *App) SaveNativeRegionToRemote(result CaptureResult) error {
|
|||||||
return a.runSaveRemotePipeline(cropped)
|
return a.runSaveRemotePipeline(cropped)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadNativeRegionToFTP is the macOS AppKit overlay equivalent of
|
|
||||||
// UploadRegionToFTP. The native panel has already closed when this runs.
|
|
||||||
func (a *App) UploadNativeRegionToFTP(result CaptureResult) error {
|
|
||||||
pc, err := a.consumePendingCapture()
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("UploadNativeRegionToFTP: no pending capture", "err", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
a.capturing.Store(false)
|
|
||||||
hideDockIcon()
|
|
||||||
}()
|
|
||||||
|
|
||||||
flushFrame()
|
|
||||||
cropped, err := a.captureSelectedPNG(result, pc)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("UploadNativeRegionToFTP: capture failed", "err", err)
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.runFTPUploadPipeline(cropped)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SummarizeNativeRegion is the macOS-native overlay equivalent of
|
|
||||||
// SummarizeRegion. The AppKit panel is already closed before this runs.
|
|
||||||
func (a *App) SummarizeNativeRegion(result CaptureResult) error {
|
|
||||||
pc, err := a.consumePendingCapture()
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("SummarizeNativeRegion: no pending capture", "err", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
a.capturing.Store(false)
|
|
||||||
hideDockIcon()
|
|
||||||
}()
|
|
||||||
|
|
||||||
flushFrame()
|
|
||||||
slog.Info("SummarizeNativeRegion: capturing region",
|
|
||||||
"x", result.Rect.X, "y", result.Rect.Y,
|
|
||||||
"w", result.Rect.W, "h", result.Rect.H,
|
|
||||||
"annotations", len(result.Annotations))
|
|
||||||
cropped, err := a.captureSelectedPNG(result, pc)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("SummarizeNativeRegion: capture failed", "err", err)
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.runSummaryPipeline(cropped)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExtractTextNativeRegion is the macOS-native overlay equivalent of
|
|
||||||
// ExtractTextRegion. The AppKit panel is already closed before this runs.
|
|
||||||
func (a *App) ExtractTextNativeRegion(result CaptureResult) error {
|
|
||||||
pc, err := a.consumePendingCapture()
|
|
||||||
if err != nil {
|
|
||||||
slog.Warn("ExtractTextNativeRegion: no pending capture", "err", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
a.capturing.Store(false)
|
|
||||||
hideDockIcon()
|
|
||||||
}()
|
|
||||||
|
|
||||||
flushFrame()
|
|
||||||
slog.Info("ExtractTextNativeRegion: capturing region",
|
|
||||||
"x", result.Rect.X, "y", result.Rect.Y,
|
|
||||||
"w", result.Rect.W, "h", result.Rect.H,
|
|
||||||
"annotations", len(result.Annotations))
|
|
||||||
cropped, err := a.captureSelectedPNG(result, pc)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("ExtractTextNativeRegion: capture failed", "err", err)
|
|
||||||
wruntime.EventsEmit(a.ctx, "upload:failure", err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return a.runOCRPipeline(cropped)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseNativeAnnotations(raw string) []application.Annotation {
|
func parseNativeAnnotations(raw string) []application.Annotation {
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
return nil
|
return nil
|
||||||
@@ -1209,7 +769,6 @@ func (a *App) CancelRegion() {
|
|||||||
a.pendingMu.Unlock()
|
a.pendingMu.Unlock()
|
||||||
a.capturing.Store(false)
|
a.capturing.Store(false)
|
||||||
a.dismissOverlay()
|
a.dismissOverlay()
|
||||||
a.emitOperationStatus("capture", "已取消", "本次截图已取消", "success")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CancelNativeRegion releases native-overlay state without touching the
|
// CancelNativeRegion releases native-overlay state without touching the
|
||||||
@@ -1220,7 +779,6 @@ func (a *App) CancelNativeRegion() {
|
|||||||
a.pendingMu.Unlock()
|
a.pendingMu.Unlock()
|
||||||
a.capturing.Store(false)
|
a.capturing.Store(false)
|
||||||
hideDockIcon()
|
hideDockIcon()
|
||||||
a.emitOperationStatus("capture", "已取消", "本次截图已取消", "success")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShowWindow brings the main window back to the foreground in its normal
|
// ShowWindow brings the main window back to the foreground in its normal
|
||||||
@@ -1239,27 +797,6 @@ func (a *App) QuitApp() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) emitOperationStatus(operation, phase, message, state string) {
|
|
||||||
if a.ctx != nil {
|
|
||||||
wruntime.EventsEmit(a.ctx, "operation:status", OperationStatusPayload{
|
|
||||||
Operation: operation,
|
|
||||||
Phase: phase,
|
|
||||||
Message: message,
|
|
||||||
State: state,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
switch state {
|
|
||||||
case "success":
|
|
||||||
showOperationStatus(phase, message, operationStatusSuccess)
|
|
||||||
hideOperationStatusAfter(1400 * time.Millisecond)
|
|
||||||
case "error":
|
|
||||||
showOperationStatus(phase, message, operationStatusError)
|
|
||||||
hideOperationStatusAfter(2600 * time.Millisecond)
|
|
||||||
default:
|
|
||||||
showOperationStatus(phase, message, operationStatusRunning)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// runtimeNotifier emits success / failure events via the Wails runtime.
|
// runtimeNotifier emits success / failure events via the Wails runtime.
|
||||||
type runtimeNotifier struct{ ctx context.Context }
|
type runtimeNotifier struct{ ctx context.Context }
|
||||||
|
|
||||||
|
|||||||
+71
-250
@@ -3,7 +3,9 @@
|
|||||||
* App shell — switches between two distinct UI modes that share the same
|
* App shell — switches between two distinct UI modes that share the same
|
||||||
* Wails window:
|
* Wails window:
|
||||||
*
|
*
|
||||||
* • "settings" : full settings UI (native title bar + sidebar + form).
|
* • "settings" : full settings UI (self-drawn title bar + sidebar + form).
|
||||||
|
* Self-drawn because the window is now Frameless to make
|
||||||
|
* the overlay paint edge-to-edge.
|
||||||
* • "overlay" : Snipaste-style region picker that fills the whole
|
* • "overlay" : Snipaste-style region picker that fills the whole
|
||||||
* primary display.
|
* primary display.
|
||||||
*
|
*
|
||||||
@@ -11,7 +13,7 @@
|
|||||||
* emits a `capture:overlay` event with the screenshot payload so the
|
* emits a `capture:overlay` event with the screenshot payload so the
|
||||||
* frontend knows when (and what) to render.
|
* frontend knows when (and what) to render.
|
||||||
*/
|
*/
|
||||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
import { onMounted, onUnmounted, ref } from 'vue'
|
||||||
import SettingsView from './views/SettingsView.vue'
|
import SettingsView from './views/SettingsView.vue'
|
||||||
import Toast from './components/Toast.vue'
|
import Toast from './components/Toast.vue'
|
||||||
import CaptureOverlay from './views/CaptureOverlay.vue'
|
import CaptureOverlay from './views/CaptureOverlay.vue'
|
||||||
@@ -22,11 +24,7 @@ import {
|
|||||||
CopyRegionImage,
|
CopyRegionImage,
|
||||||
SaveRegionImage,
|
SaveRegionImage,
|
||||||
SaveRegionToRemote,
|
SaveRegionToRemote,
|
||||||
UploadRegionToFTP,
|
|
||||||
SummarizeRegion,
|
|
||||||
ExtractTextRegion,
|
|
||||||
CancelRegion,
|
CancelRegion,
|
||||||
GetConfig,
|
|
||||||
} from '../wailsjs/go/main/App'
|
} from '../wailsjs/go/main/App'
|
||||||
|
|
||||||
type HotkeyStatus =
|
type HotkeyStatus =
|
||||||
@@ -38,40 +36,18 @@ const hotkeyStatus = ref<HotkeyStatus>({ state: 'unknown' })
|
|||||||
type Mode = 'settings' | 'overlay'
|
type Mode = 'settings' | 'overlay'
|
||||||
const mode = ref<Mode>('settings')
|
const mode = ref<Mode>('settings')
|
||||||
|
|
||||||
type ThemeMode = 'auto' | 'light' | 'dark'
|
|
||||||
const themeMode = ref<ThemeMode>('auto')
|
|
||||||
const appThemeClass = computed(() => `theme-${themeMode.value}`)
|
|
||||||
|
|
||||||
function normalizeTheme(theme: unknown): ThemeMode {
|
|
||||||
return theme === 'light' || theme === 'dark' || theme === 'auto'
|
|
||||||
? theme
|
|
||||||
: 'auto'
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadThemePreference() {
|
|
||||||
try {
|
|
||||||
const cfg = await GetConfig()
|
|
||||||
themeMode.value = normalizeTheme((cfg as any)?.theme)
|
|
||||||
} catch {
|
|
||||||
themeMode.value = 'auto'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// `SettingsTab` is hoisted to the App shell so the sidebar (which lives
|
// `SettingsTab` is hoisted to the App shell so the sidebar (which lives
|
||||||
// here) and the inner SettingsView (which renders the matching card) can
|
// here) and the inner SettingsView (which renders the matching card) can
|
||||||
// share a single source of truth without an event bus.
|
// share a single source of truth without an event bus.
|
||||||
type SettingsTab = 'general' | 's3' | 'ftp' | 'ssh' | 'llm' | 'ocr'
|
type SettingsTab = 'general' | 's3' | 'ssh'
|
||||||
const activeTab = ref<SettingsTab>('general')
|
const activeTab = ref<SettingsTab>('general')
|
||||||
|
|
||||||
// Sidebar entries are declarative so adding a destination type later is
|
// Sidebar entries are declarative so adding a destination type later is
|
||||||
// a one-line change. The label values match the user-facing tab names.
|
// a one-line change. The label values match the user-facing tab names.
|
||||||
const sidebarItems: Array<{ id: SettingsTab; label: string }> = [
|
const sidebarItems: Array<{ id: SettingsTab; label: string }> = [
|
||||||
{ id: 'general', label: '通用设置' },
|
{ id: 'general', label: 'General' },
|
||||||
{ id: 's3', label: '对象存储' },
|
{ id: 's3', label: 'S3' },
|
||||||
{ id: 'ftp', label: 'FTP / SFTP' },
|
{ id: 'ssh', label: 'SSH' },
|
||||||
{ id: 'ssh', label: '远程主机' },
|
|
||||||
{ id: 'llm', label: '智能识图' },
|
|
||||||
{ id: 'ocr', label: '文字提取' },
|
|
||||||
]
|
]
|
||||||
|
|
||||||
interface OverlayPayload {
|
interface OverlayPayload {
|
||||||
@@ -81,38 +57,11 @@ interface OverlayPayload {
|
|||||||
}
|
}
|
||||||
const overlayPayload = ref<OverlayPayload | null>(null)
|
const overlayPayload = ref<OverlayPayload | null>(null)
|
||||||
|
|
||||||
interface OverlayAnnotation {
|
|
||||||
tool: string
|
|
||||||
color: string
|
|
||||||
points: Array<{ x: number; y: number }>
|
|
||||||
text?: string
|
|
||||||
strokeWidth?: number
|
|
||||||
fontSize?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OverlayResult {
|
|
||||||
rect: {
|
|
||||||
x: number
|
|
||||||
y: number
|
|
||||||
w: number
|
|
||||||
h: number
|
|
||||||
}
|
|
||||||
annotations: OverlayAnnotation[]
|
|
||||||
}
|
|
||||||
|
|
||||||
const capturing = ref(false)
|
const capturing = ref(false)
|
||||||
|
|
||||||
const toast = ref<{ kind: 'success' | 'error'; text: string } | null>(null)
|
const toast = ref<{ kind: 'success' | 'error'; text: string } | null>(null)
|
||||||
let toastTimer: number | undefined
|
let toastTimer: number | undefined
|
||||||
|
|
||||||
const operationStatus = ref<{
|
|
||||||
operation: string
|
|
||||||
phase: string
|
|
||||||
message: string
|
|
||||||
state: 'running' | 'success' | 'error'
|
|
||||||
} | null>(null)
|
|
||||||
let operationTimer: number | undefined
|
|
||||||
|
|
||||||
function showToast(kind: 'success' | 'error', text: string) {
|
function showToast(kind: 'success' | 'error', text: string) {
|
||||||
toast.value = { kind, text }
|
toast.value = { kind, text }
|
||||||
window.clearTimeout(toastTimer)
|
window.clearTimeout(toastTimer)
|
||||||
@@ -121,21 +70,6 @@ function showToast(kind: 'success' | 'error', text: string) {
|
|||||||
}, 3000)
|
}, 3000)
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateOperationStatus(payload: {
|
|
||||||
operation: string
|
|
||||||
phase: string
|
|
||||||
message: string
|
|
||||||
state: 'running' | 'success' | 'error'
|
|
||||||
}) {
|
|
||||||
operationStatus.value = payload
|
|
||||||
window.clearTimeout(operationTimer)
|
|
||||||
if (payload.state !== 'running') {
|
|
||||||
operationTimer = window.setTimeout(() => {
|
|
||||||
operationStatus.value = null
|
|
||||||
}, payload.state === 'success' ? 1600 : 3200)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function retryHotkey() {
|
async function retryHotkey() {
|
||||||
try {
|
try {
|
||||||
await RetryRegisterHotkey()
|
await RetryRegisterHotkey()
|
||||||
@@ -144,7 +78,19 @@ async function retryHotkey() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onOverlayConfirm(rect: OverlayResult) {
|
async function onOverlayConfirm(rect: {
|
||||||
|
rect: {
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
w: number
|
||||||
|
h: number
|
||||||
|
}
|
||||||
|
annotations: Array<{
|
||||||
|
tool: string
|
||||||
|
color: string
|
||||||
|
points: Array<{ x: number; y: number }>
|
||||||
|
}>
|
||||||
|
}) {
|
||||||
// Optimistically swap back so the window does not visually lag the
|
// Optimistically swap back so the window does not visually lag the
|
||||||
// Go-side hide. If upload fails, the toast surfaces the reason.
|
// Go-side hide. If upload fails, the toast surfaces the reason.
|
||||||
mode.value = 'settings'
|
mode.value = 'settings'
|
||||||
@@ -156,7 +102,19 @@ async function onOverlayConfirm(rect: OverlayResult) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onOverlayCopy(rect: OverlayResult) {
|
async function onOverlayCopy(rect: {
|
||||||
|
rect: {
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
w: number
|
||||||
|
h: number
|
||||||
|
}
|
||||||
|
annotations: Array<{
|
||||||
|
tool: string
|
||||||
|
color: string
|
||||||
|
points: Array<{ x: number; y: number }>
|
||||||
|
}>
|
||||||
|
}) {
|
||||||
mode.value = 'settings'
|
mode.value = 'settings'
|
||||||
overlayPayload.value = null
|
overlayPayload.value = null
|
||||||
try {
|
try {
|
||||||
@@ -166,7 +124,19 @@ async function onOverlayCopy(rect: OverlayResult) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onOverlaySave(rect: OverlayResult) {
|
async function onOverlaySave(rect: {
|
||||||
|
rect: {
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
w: number
|
||||||
|
h: number
|
||||||
|
}
|
||||||
|
annotations: Array<{
|
||||||
|
tool: string
|
||||||
|
color: string
|
||||||
|
points: Array<{ x: number; y: number }>
|
||||||
|
}>
|
||||||
|
}) {
|
||||||
mode.value = 'settings'
|
mode.value = 'settings'
|
||||||
overlayPayload.value = null
|
overlayPayload.value = null
|
||||||
try {
|
try {
|
||||||
@@ -176,7 +146,19 @@ async function onOverlaySave(rect: OverlayResult) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onOverlaySaveRemote(rect: OverlayResult) {
|
async function onOverlaySaveRemote(rect: {
|
||||||
|
rect: {
|
||||||
|
x: number
|
||||||
|
y: number
|
||||||
|
w: number
|
||||||
|
h: number
|
||||||
|
}
|
||||||
|
annotations: Array<{
|
||||||
|
tool: string
|
||||||
|
color: string
|
||||||
|
points: Array<{ x: number; y: number }>
|
||||||
|
}>
|
||||||
|
}) {
|
||||||
mode.value = 'settings'
|
mode.value = 'settings'
|
||||||
overlayPayload.value = null
|
overlayPayload.value = null
|
||||||
try {
|
try {
|
||||||
@@ -186,36 +168,6 @@ async function onOverlaySaveRemote(rect: OverlayResult) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onOverlayUploadFTP(rect: OverlayResult) {
|
|
||||||
mode.value = 'settings'
|
|
||||||
overlayPayload.value = null
|
|
||||||
try {
|
|
||||||
await UploadRegionToFTP(rect as any)
|
|
||||||
} catch {
|
|
||||||
/* Surfaced via upload:failure */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function onOverlaySummarize(rect: OverlayResult) {
|
|
||||||
mode.value = 'settings'
|
|
||||||
overlayPayload.value = null
|
|
||||||
try {
|
|
||||||
await SummarizeRegion(rect as any)
|
|
||||||
} catch {
|
|
||||||
/* Surfaced via upload:failure */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function onOverlayOCR(rect: OverlayResult) {
|
|
||||||
mode.value = 'settings'
|
|
||||||
overlayPayload.value = null
|
|
||||||
try {
|
|
||||||
await ExtractTextRegion(rect as any)
|
|
||||||
} catch {
|
|
||||||
/* Surfaced via upload:failure */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function onOverlayCancel() {
|
async function onOverlayCancel() {
|
||||||
mode.value = 'settings'
|
mode.value = 'settings'
|
||||||
overlayPayload.value = null
|
overlayPayload.value = null
|
||||||
@@ -227,7 +179,6 @@ async function onOverlayCancel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
void loadThemePreference()
|
|
||||||
EventsOn('capture:start', () => {
|
EventsOn('capture:start', () => {
|
||||||
capturing.value = true
|
capturing.value = true
|
||||||
})
|
})
|
||||||
@@ -243,14 +194,7 @@ onMounted(() => {
|
|||||||
capturing.value = false
|
capturing.value = false
|
||||||
})
|
})
|
||||||
EventsOn('upload:success', (url: string) => {
|
EventsOn('upload:success', (url: string) => {
|
||||||
showToast(
|
showToast('success', `Copied: ${url}`)
|
||||||
'success',
|
|
||||||
url === 'summary copied to clipboard'
|
|
||||||
? 'Summary copied to clipboard'
|
|
||||||
: url === 'ocr text copied to clipboard'
|
|
||||||
? 'OCR text copied to clipboard'
|
|
||||||
: `Copied: ${url}`
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
EventsOn('upload:failure', (reason: string) => {
|
EventsOn('upload:failure', (reason: string) => {
|
||||||
showToast('error', reason)
|
showToast('error', reason)
|
||||||
@@ -261,7 +205,6 @@ onMounted(() => {
|
|||||||
EventsOn('hotkey:error', (reason: string) => {
|
EventsOn('hotkey:error', (reason: string) => {
|
||||||
hotkeyStatus.value = { state: 'error', reason }
|
hotkeyStatus.value = { state: 'error', reason }
|
||||||
})
|
})
|
||||||
EventsOn('operation:status', updateOperationStatus)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
@@ -273,7 +216,6 @@ onUnmounted(() => {
|
|||||||
EventsOff('upload:failure')
|
EventsOff('upload:failure')
|
||||||
EventsOff('hotkey:ready')
|
EventsOff('hotkey:ready')
|
||||||
EventsOff('hotkey:error')
|
EventsOff('hotkey:error')
|
||||||
EventsOff('operation:status')
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -287,14 +229,11 @@ onUnmounted(() => {
|
|||||||
@copy="onOverlayCopy"
|
@copy="onOverlayCopy"
|
||||||
@save="onOverlaySave"
|
@save="onOverlaySave"
|
||||||
@save-remote="onOverlaySaveRemote"
|
@save-remote="onOverlaySaveRemote"
|
||||||
@upload-ftp="onOverlayUploadFTP"
|
|
||||||
@summarize="onOverlaySummarize"
|
|
||||||
@ocr="onOverlayOCR"
|
|
||||||
@cancel="onOverlayCancel"
|
@cancel="onOverlayCancel"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<!-- Settings mode: standard desktop window; macOS title bar is native. -->
|
<!-- Settings mode: standard desktop window; macOS title bar is native. -->
|
||||||
<div v-else class="app-root" :class="appThemeClass">
|
<div v-else class="app-root">
|
||||||
<div v-if="hotkeyStatus.state === 'error'" class="permission-banner">
|
<div v-if="hotkeyStatus.state === 'error'" class="permission-banner">
|
||||||
<div>
|
<div>
|
||||||
<strong>Global hotkey is not active.</strong>
|
<strong>Global hotkey is not active.</strong>
|
||||||
@@ -320,29 +259,11 @@ onUnmounted(() => {
|
|||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
<section class="content">
|
<section class="content">
|
||||||
<SettingsView
|
<SettingsView :tab="activeTab" />
|
||||||
:tab="activeTab"
|
|
||||||
:theme="themeMode"
|
|
||||||
@theme-change="themeMode = normalizeTheme($event)"
|
|
||||||
/>
|
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<Toast v-if="toast" :kind="toast.kind" :text="toast.text" />
|
<Toast v-if="toast" :kind="toast.kind" :text="toast.text" />
|
||||||
<div
|
|
||||||
v-if="operationStatus"
|
|
||||||
class="operation-hud"
|
|
||||||
:class="operationStatus.state"
|
|
||||||
>
|
|
||||||
<span v-if="operationStatus.state === 'running'" class="spinner" />
|
|
||||||
<span v-else class="status-mark">
|
|
||||||
{{ operationStatus.state === 'success' ? 'OK' : '!' }}
|
|
||||||
</span>
|
|
||||||
<div>
|
|
||||||
<strong>{{ operationStatus.phase }}</strong>
|
|
||||||
<p>{{ operationStatus.message }}</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -356,15 +277,6 @@ onUnmounted(() => {
|
|||||||
color: #111827;
|
color: #111827;
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
}
|
}
|
||||||
.app-root.theme-light {
|
|
||||||
color-scheme: light;
|
|
||||||
}
|
|
||||||
.app-root.theme-dark {
|
|
||||||
color-scheme: dark;
|
|
||||||
}
|
|
||||||
.app-root.theme-auto {
|
|
||||||
color-scheme: light dark;
|
|
||||||
}
|
|
||||||
|
|
||||||
.status-pill {
|
.status-pill {
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
@@ -459,124 +371,33 @@ onUnmounted(() => {
|
|||||||
min-width: 0;
|
min-width: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.operation-hud {
|
|
||||||
position: fixed;
|
|
||||||
right: 18px;
|
|
||||||
bottom: 18px;
|
|
||||||
z-index: 20;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 12px;
|
|
||||||
min-width: 260px;
|
|
||||||
max-width: 360px;
|
|
||||||
padding: 12px 14px;
|
|
||||||
color: #f9fafb;
|
|
||||||
background: rgba(17, 24, 39, 0.94);
|
|
||||||
border-radius: 8px;
|
|
||||||
box-shadow: 0 12px 34px rgba(15, 23, 42, 0.28);
|
|
||||||
}
|
|
||||||
.operation-hud strong {
|
|
||||||
display: block;
|
|
||||||
margin-bottom: 2px;
|
|
||||||
font-size: 13px;
|
|
||||||
}
|
|
||||||
.operation-hud p {
|
|
||||||
margin: 0;
|
|
||||||
color: #d1d5db;
|
|
||||||
font-size: 12px;
|
|
||||||
line-height: 1.35;
|
|
||||||
}
|
|
||||||
.spinner {
|
|
||||||
width: 20px;
|
|
||||||
height: 20px;
|
|
||||||
border: 2px solid rgba(255, 255, 255, 0.25);
|
|
||||||
border-top-color: #60a5fa;
|
|
||||||
border-radius: 50%;
|
|
||||||
animation: spin 0.75s linear infinite;
|
|
||||||
flex: 0 0 auto;
|
|
||||||
}
|
|
||||||
.status-mark {
|
|
||||||
display: grid;
|
|
||||||
place-items: center;
|
|
||||||
width: 24px;
|
|
||||||
height: 24px;
|
|
||||||
border-radius: 50%;
|
|
||||||
font-size: 11px;
|
|
||||||
font-weight: 700;
|
|
||||||
flex: 0 0 auto;
|
|
||||||
}
|
|
||||||
.operation-hud.success .status-mark {
|
|
||||||
color: #052e16;
|
|
||||||
background: #86efac;
|
|
||||||
}
|
|
||||||
.operation-hud.error .status-mark {
|
|
||||||
color: #450a0a;
|
|
||||||
background: #fca5a5;
|
|
||||||
}
|
|
||||||
@keyframes spin {
|
|
||||||
to {
|
|
||||||
transform: rotate(360deg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.app-root.theme-dark {
|
|
||||||
background: #1c1d22;
|
|
||||||
color: #e5e7eb;
|
|
||||||
}
|
|
||||||
.app-root.theme-dark .titlebar {
|
|
||||||
background: rgba(28, 29, 34, 0.7);
|
|
||||||
border-bottom-color: #2c2f36;
|
|
||||||
}
|
|
||||||
.app-root.theme-dark .titlebar-title {
|
|
||||||
color: #f3f4f6;
|
|
||||||
}
|
|
||||||
.app-root.theme-dark .sidebar {
|
|
||||||
background: rgba(0, 0, 0, 0.15);
|
|
||||||
border-right-color: #2c2f36;
|
|
||||||
}
|
|
||||||
.app-root.theme-dark .sidebar-item {
|
|
||||||
color: #d1d5db;
|
|
||||||
}
|
|
||||||
.app-root.theme-dark .sidebar-item:hover:not(.active) {
|
|
||||||
background: rgba(255, 255, 255, 0.05);
|
|
||||||
}
|
|
||||||
.app-root.theme-dark .sidebar-item.active {
|
|
||||||
background: rgba(59, 130, 246, 0.18);
|
|
||||||
color: #93c5fd;
|
|
||||||
}
|
|
||||||
.app-root.theme-dark .permission-banner {
|
|
||||||
background: rgba(120, 53, 15, 0.3);
|
|
||||||
border-color: rgba(253, 186, 116, 0.4);
|
|
||||||
color: #fed7aa;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
.app-root.theme-auto {
|
.app-root {
|
||||||
background: #1c1d22;
|
background: #1c1d22;
|
||||||
color: #e5e7eb;
|
color: #e5e7eb;
|
||||||
}
|
}
|
||||||
.app-root.theme-auto .titlebar {
|
.titlebar {
|
||||||
background: rgba(28, 29, 34, 0.7);
|
background: rgba(28, 29, 34, 0.7);
|
||||||
border-bottom-color: #2c2f36;
|
border-bottom-color: #2c2f36;
|
||||||
}
|
}
|
||||||
.app-root.theme-auto .titlebar-title {
|
.titlebar-title {
|
||||||
color: #f3f4f6;
|
color: #f3f4f6;
|
||||||
}
|
}
|
||||||
.app-root.theme-auto .sidebar {
|
.sidebar {
|
||||||
background: rgba(0, 0, 0, 0.15);
|
background: rgba(0, 0, 0, 0.15);
|
||||||
border-right-color: #2c2f36;
|
border-right-color: #2c2f36;
|
||||||
}
|
}
|
||||||
.app-root.theme-auto .sidebar-item {
|
.sidebar-item {
|
||||||
color: #d1d5db;
|
color: #d1d5db;
|
||||||
}
|
}
|
||||||
.app-root.theme-auto .sidebar-item:hover:not(.active) {
|
.sidebar-item:hover:not(.active) {
|
||||||
background: rgba(255, 255, 255, 0.05);
|
background: rgba(255, 255, 255, 0.05);
|
||||||
}
|
}
|
||||||
.app-root.theme-auto .sidebar-item.active {
|
.sidebar-item.active {
|
||||||
background: rgba(59, 130, 246, 0.18);
|
background: rgba(59, 130, 246, 0.18);
|
||||||
color: #93c5fd;
|
color: #93c5fd;
|
||||||
}
|
}
|
||||||
.app-root.theme-auto .permission-banner {
|
.permission-banner {
|
||||||
background: rgba(120, 53, 15, 0.3);
|
background: rgba(120, 53, 15, 0.3);
|
||||||
border-color: rgba(253, 186, 116, 0.4);
|
border-color: rgba(253, 186, 116, 0.4);
|
||||||
color: #fed7aa;
|
color: #fed7aa;
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M128 128h768v256H128V128zm64 64v128h640V192H192zm64 48h64v32h-64v-32zM128 448h416v64H192v192h352v64H128V448zm560-32 160 160-45.248 45.248L720 538.496V832h-64V538.496l-82.752 82.752L528 576l160-160z" fill="currentColor"></path></svg>
|
|
||||||
|
Before Width: | Height: | Size: 306 B |
File diff suppressed because it is too large
Load Diff
@@ -7,36 +7,25 @@
|
|||||||
*
|
*
|
||||||
* • "general" — the global hotkey / capture parameters.
|
* • "general" — the global hotkey / capture parameters.
|
||||||
* • "s3" — S3-compatible object-storage credentials.
|
* • "s3" — S3-compatible object-storage credentials.
|
||||||
* • "ftp" — FTP/SFTP file-transfer destination.
|
|
||||||
* • "ssh" — SSH/SCP destination for the "save remote" button.
|
* • "ssh" — SSH/SCP destination for the "save remote" button.
|
||||||
* • "llm" — multimodal screenshot summary provider settings.
|
|
||||||
* • "ocr" — cloud OCR provider settings for text extraction.
|
|
||||||
*
|
*
|
||||||
* State flow: load() pulls config from Go on mount, save() pushes back.
|
* State flow: load() pulls config from Go on mount, save() pushes back.
|
||||||
*/
|
*/
|
||||||
import { computed, onMounted, ref, watch } from 'vue'
|
import { computed, onMounted, ref } from 'vue'
|
||||||
import {
|
import {
|
||||||
GetConfig,
|
GetConfig,
|
||||||
SaveConfig,
|
SaveConfig,
|
||||||
TestConnection,
|
TestConnection,
|
||||||
TestFTPConnection,
|
|
||||||
TestSSHConnection,
|
TestSSHConnection,
|
||||||
CaptureNow,
|
CaptureNow,
|
||||||
} from '../../wailsjs/go/main/App'
|
} from '../../wailsjs/go/main/App'
|
||||||
|
|
||||||
// Tab discriminator shared with the App shell. Kept as a string union so
|
// Tab discriminator shared with the App shell. Kept as a string union so
|
||||||
// the parent can pass the value without importing a type from this view.
|
// the parent can pass the value without importing a type from this view.
|
||||||
type TabId = 'general' | 's3' | 'ftp' | 'ssh' | 'llm' | 'ocr'
|
type TabId = 'general' | 's3' | 'ssh'
|
||||||
type ThemeMode = 'auto' | 'light' | 'dark'
|
|
||||||
type FTPProtocol = 'ftp' | 'sftp'
|
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
tab: TabId
|
tab: TabId
|
||||||
theme: ThemeMode
|
|
||||||
}>()
|
|
||||||
|
|
||||||
const emit = defineEmits<{
|
|
||||||
(event: 'theme-change', theme: ThemeMode): void
|
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
interface S3Config {
|
interface S3Config {
|
||||||
@@ -62,88 +51,12 @@ interface SSHConfig {
|
|||||||
connectTimeoutSecs: number
|
connectTimeoutSecs: number
|
||||||
}
|
}
|
||||||
|
|
||||||
interface FTPConfig {
|
|
||||||
protocol: FTPProtocol
|
|
||||||
host: string
|
|
||||||
port: number
|
|
||||||
user: string
|
|
||||||
authMethod: 'password' | 'key'
|
|
||||||
password: string
|
|
||||||
pathPrefix: string
|
|
||||||
strictHostKey: boolean
|
|
||||||
knownHostsPath: string
|
|
||||||
connectTimeoutSecs: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LLMProviderConfig {
|
|
||||||
label: string
|
|
||||||
baseUrl: string
|
|
||||||
apiKey: string
|
|
||||||
model: string
|
|
||||||
maxTokens: number
|
|
||||||
temperature: number
|
|
||||||
timeoutSecs: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface LLMConfig {
|
|
||||||
activeProvider: string
|
|
||||||
prompt: string
|
|
||||||
providers: Record<string, LLMProviderConfig>
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OCRProviderConfig {
|
|
||||||
label: string
|
|
||||||
endpoint: string
|
|
||||||
accessKeyId: string
|
|
||||||
accessKeySecret: string
|
|
||||||
region: string
|
|
||||||
service: string
|
|
||||||
action: string
|
|
||||||
version: string
|
|
||||||
timeoutSecs: number
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OCRConfig {
|
|
||||||
activeProvider: string
|
|
||||||
providers: Record<string, OCRProviderConfig>
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AppConfig {
|
interface AppConfig {
|
||||||
hotkey: string
|
hotkey: string
|
||||||
theme: ThemeMode
|
|
||||||
s3: S3Config
|
s3: S3Config
|
||||||
ssh: SSHConfig
|
ssh: SSHConfig
|
||||||
ftp: FTPConfig
|
|
||||||
llm: LLMConfig
|
|
||||||
ocr: OCRConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const llmProviderOptions = [
|
|
||||||
{ id: 'qwen', label: '阿里千问多模态' },
|
|
||||||
{ id: 'doubao', label: '火山方舟豆包多模态' },
|
|
||||||
{ id: 'openai', label: 'ChatGPT / OpenAI-compatible' },
|
|
||||||
]
|
|
||||||
|
|
||||||
const ocrProviderOptions = [
|
|
||||||
{ id: 'aliyun', label: '阿里云文字识别' },
|
|
||||||
{ id: 'volcengine', label: '火山引擎文字识别' },
|
|
||||||
]
|
|
||||||
|
|
||||||
const themeOptions: Array<{ id: ThemeMode; label: string }> = [
|
|
||||||
{ id: 'light', label: '浅色' },
|
|
||||||
{ id: 'dark', label: '深色' },
|
|
||||||
{ id: 'auto', label: '自动' },
|
|
||||||
]
|
|
||||||
|
|
||||||
const DEFAULT_SUMMARY_PROMPT = `你是一个截图内容总结助手。请阅读截图,并用中文输出一段适合直接粘贴到聊天、工单、Issue 或 PR 中的说明。
|
|
||||||
|
|
||||||
要求:
|
|
||||||
- 先说明截图中最重要的信息和用户可能想表达的意图。
|
|
||||||
- 如果截图包含报错、异常状态、表格、代码、界面控件或关键数字,请准确提取。
|
|
||||||
- 如果截图内容不足以判断,不要编造;直接说明不确定点。
|
|
||||||
- 输出尽量简洁,默认 3 到 6 条要点。
|
|
||||||
- 不要输出 Markdown 标题,不要寒暄。`
|
|
||||||
|
|
||||||
// `defaultConfig` keeps the initial render in sync with the Go-side
|
// `defaultConfig` keeps the initial render in sync with the Go-side
|
||||||
// DefaultAppConfig so the UI never flashes empty fields before load()
|
// DefaultAppConfig so the UI never flashes empty fields before load()
|
||||||
// completes. Centralising the defaults also avoids subtle drift between
|
// completes. Centralising the defaults also avoids subtle drift between
|
||||||
@@ -151,7 +64,6 @@ const DEFAULT_SUMMARY_PROMPT = `你是一个截图内容总结助手。请阅读
|
|||||||
function defaultConfig(): AppConfig {
|
function defaultConfig(): AppConfig {
|
||||||
return {
|
return {
|
||||||
hotkey: 'cmd+shift+a',
|
hotkey: 'cmd+shift+a',
|
||||||
theme: 'auto',
|
|
||||||
s3: {
|
s3: {
|
||||||
endpoint: '',
|
endpoint: '',
|
||||||
region: 'us-east-1',
|
region: 'us-east-1',
|
||||||
@@ -162,18 +74,6 @@ function defaultConfig(): AppConfig {
|
|||||||
publicUrlBase: '',
|
publicUrlBase: '',
|
||||||
usePathStyle: true,
|
usePathStyle: true,
|
||||||
},
|
},
|
||||||
ftp: {
|
|
||||||
protocol: 'ftp',
|
|
||||||
host: '',
|
|
||||||
port: 21,
|
|
||||||
user: '',
|
|
||||||
authMethod: 'password',
|
|
||||||
password: '',
|
|
||||||
pathPrefix: 'snapgo/',
|
|
||||||
strictHostKey: false,
|
|
||||||
knownHostsPath: '',
|
|
||||||
connectTimeoutSecs: 10,
|
|
||||||
},
|
|
||||||
ssh: {
|
ssh: {
|
||||||
host: '',
|
host: '',
|
||||||
port: 22,
|
port: 22,
|
||||||
@@ -185,66 +85,6 @@ function defaultConfig(): AppConfig {
|
|||||||
knownHostsPath: '',
|
knownHostsPath: '',
|
||||||
connectTimeoutSecs: 10,
|
connectTimeoutSecs: 10,
|
||||||
},
|
},
|
||||||
llm: {
|
|
||||||
activeProvider: 'openai',
|
|
||||||
prompt: DEFAULT_SUMMARY_PROMPT,
|
|
||||||
providers: {
|
|
||||||
qwen: {
|
|
||||||
label: '阿里千问多模态',
|
|
||||||
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
|
||||||
apiKey: '',
|
|
||||||
model: 'qwen-vl-plus',
|
|
||||||
maxTokens: 600,
|
|
||||||
temperature: 0.2,
|
|
||||||
timeoutSecs: 60,
|
|
||||||
},
|
|
||||||
doubao: {
|
|
||||||
label: '火山方舟豆包多模态',
|
|
||||||
baseUrl: 'https://ark.cn-beijing.volces.com/api/v3',
|
|
||||||
apiKey: '',
|
|
||||||
model: '',
|
|
||||||
maxTokens: 600,
|
|
||||||
temperature: 0.2,
|
|
||||||
timeoutSecs: 60,
|
|
||||||
},
|
|
||||||
openai: {
|
|
||||||
label: 'ChatGPT / OpenAI-compatible',
|
|
||||||
baseUrl: 'https://api.openai.com/v1',
|
|
||||||
apiKey: '',
|
|
||||||
model: 'gpt-5.5',
|
|
||||||
maxTokens: 600,
|
|
||||||
temperature: 0.2,
|
|
||||||
timeoutSecs: 60,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
ocr: {
|
|
||||||
activeProvider: 'aliyun',
|
|
||||||
providers: {
|
|
||||||
aliyun: {
|
|
||||||
label: '阿里云文字识别',
|
|
||||||
endpoint: 'https://ocr-api.cn-hangzhou.aliyuncs.com',
|
|
||||||
accessKeyId: '',
|
|
||||||
accessKeySecret: '',
|
|
||||||
region: '',
|
|
||||||
service: '',
|
|
||||||
action: 'RecognizeGeneral',
|
|
||||||
version: '2021-07-07',
|
|
||||||
timeoutSecs: 30,
|
|
||||||
},
|
|
||||||
volcengine: {
|
|
||||||
label: '火山引擎文字识别',
|
|
||||||
endpoint: 'https://visual.volcengineapi.com',
|
|
||||||
accessKeyId: '',
|
|
||||||
accessKeySecret: '',
|
|
||||||
region: 'cn-north-1',
|
|
||||||
service: 'cv',
|
|
||||||
action: 'OCRNormal',
|
|
||||||
version: '2020-08-26',
|
|
||||||
timeoutSecs: 30,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,7 +92,6 @@ const config = ref<AppConfig>(defaultConfig())
|
|||||||
|
|
||||||
const saving = ref(false)
|
const saving = ref(false)
|
||||||
const testing = ref(false)
|
const testing = ref(false)
|
||||||
const testingFTP = ref(false)
|
|
||||||
const testingSSH = ref(false)
|
const testingSSH = ref(false)
|
||||||
const message = ref<{ kind: 'ok' | 'err'; text: string } | null>(null)
|
const message = ref<{ kind: 'ok' | 'err'; text: string } | null>(null)
|
||||||
|
|
||||||
@@ -270,63 +109,6 @@ const sshPathDisplay = computed({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// FTP and SFTP both upload relative to the authenticated login directory.
|
|
||||||
// The visible prefix communicates how that location will be copied: FTP uses
|
|
||||||
// its virtual root, while SFTP uses the SSH user's home directory.
|
|
||||||
const ftpPathDisplay = computed({
|
|
||||||
get: () => config.value.ftp.pathPrefix,
|
|
||||||
set: (raw: string) => {
|
|
||||||
let cleaned = raw.trim()
|
|
||||||
cleaned = cleaned.replace(/^~/, '')
|
|
||||||
cleaned = cleaned.replace(/^\/+/, '')
|
|
||||||
config.value.ftp.pathPrefix = cleaned
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const ftpPathRootLabel = computed(() =>
|
|
||||||
config.value.ftp.protocol === 'sftp' ? '~/' : '/'
|
|
||||||
)
|
|
||||||
|
|
||||||
function changeFTPProtocol(event: Event) {
|
|
||||||
const next = (event.target as HTMLSelectElement).value as FTPProtocol
|
|
||||||
const previous = config.value.ftp.protocol
|
|
||||||
const previousDefault = previous === 'sftp' ? 22 : 21
|
|
||||||
if (config.value.ftp.port === previousDefault) {
|
|
||||||
config.value.ftp.port = next === 'sftp' ? 22 : 21
|
|
||||||
}
|
|
||||||
config.value.ftp.protocol = next
|
|
||||||
}
|
|
||||||
|
|
||||||
function changeFTPAuthMethod(event: Event) {
|
|
||||||
config.value.ftp.authMethod = (event.target as HTMLSelectElement).value as
|
|
||||||
| 'password'
|
|
||||||
| 'key'
|
|
||||||
}
|
|
||||||
|
|
||||||
const activeLLMProvider = computed(() => {
|
|
||||||
const id = config.value.llm.activeProvider || 'openai'
|
|
||||||
if (!config.value.llm.providers[id]) {
|
|
||||||
config.value.llm.activeProvider = 'openai'
|
|
||||||
return config.value.llm.providers.openai
|
|
||||||
}
|
|
||||||
return config.value.llm.providers[id]
|
|
||||||
})
|
|
||||||
|
|
||||||
const activeOCRProvider = computed(() => {
|
|
||||||
const id = config.value.ocr.activeProvider || 'aliyun'
|
|
||||||
if (!config.value.ocr.providers[id]) {
|
|
||||||
config.value.ocr.activeProvider = 'aliyun'
|
|
||||||
return config.value.ocr.providers.aliyun
|
|
||||||
}
|
|
||||||
return config.value.ocr.providers[id]
|
|
||||||
})
|
|
||||||
|
|
||||||
function normalizeTheme(theme: unknown): ThemeMode {
|
|
||||||
return theme === 'light' || theme === 'dark' || theme === 'auto'
|
|
||||||
? theme
|
|
||||||
: 'auto'
|
|
||||||
}
|
|
||||||
|
|
||||||
function flash(kind: 'ok' | 'err', text: string) {
|
function flash(kind: 'ok' | 'err', text: string) {
|
||||||
message.value = { kind, text }
|
message.value = { kind, text }
|
||||||
window.setTimeout(() => {
|
window.setTimeout(() => {
|
||||||
@@ -343,20 +125,7 @@ async function load() {
|
|||||||
Object.assign(merged, cfg)
|
Object.assign(merged, cfg)
|
||||||
merged.s3 = { ...merged.s3, ...(cfg as any).s3 }
|
merged.s3 = { ...merged.s3, ...(cfg as any).s3 }
|
||||||
merged.ssh = { ...merged.ssh, ...(cfg as any).ssh }
|
merged.ssh = { ...merged.ssh, ...(cfg as any).ssh }
|
||||||
merged.ftp = { ...merged.ftp, ...(cfg as any).ftp }
|
|
||||||
merged.llm = { ...merged.llm, ...(cfg as any).llm }
|
|
||||||
merged.llm.providers = {
|
|
||||||
...defaultConfig().llm.providers,
|
|
||||||
...((cfg as any).llm?.providers ?? {}),
|
|
||||||
}
|
|
||||||
merged.ocr = { ...merged.ocr, ...(cfg as any).ocr }
|
|
||||||
merged.ocr.providers = {
|
|
||||||
...defaultConfig().ocr.providers,
|
|
||||||
...((cfg as any).ocr?.providers ?? {}),
|
|
||||||
}
|
|
||||||
merged.theme = normalizeTheme((cfg as any).theme)
|
|
||||||
config.value = merged
|
config.value = merged
|
||||||
emit('theme-change', config.value.theme)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -384,21 +153,6 @@ async function test() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function testFTP() {
|
|
||||||
testingFTP.value = true
|
|
||||||
try {
|
|
||||||
await TestFTPConnection(config.value.ftp as any)
|
|
||||||
flash(
|
|
||||||
'ok',
|
|
||||||
`${config.value.ftp.protocol.toUpperCase()} connection OK — remote directory is writable`
|
|
||||||
)
|
|
||||||
} catch (err: any) {
|
|
||||||
flash('err', `FTP/SFTP test failed: ${err?.message ?? err}`)
|
|
||||||
} finally {
|
|
||||||
testingFTP.value = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function testSSH() {
|
async function testSSH() {
|
||||||
testingSSH.value = true
|
testingSSH.value = true
|
||||||
try {
|
try {
|
||||||
@@ -415,16 +169,11 @@ async function manualCapture() {
|
|||||||
await CaptureNow()
|
await CaptureNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(
|
|
||||||
() => config.value.theme,
|
|
||||||
(theme) => emit('theme-change', normalizeTheme(theme))
|
|
||||||
)
|
|
||||||
|
|
||||||
onMounted(load)
|
onMounted(load)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="settings" :class="`theme-${props.theme}`">
|
<div class="settings">
|
||||||
<header class="hero">
|
<header class="hero">
|
||||||
<div>
|
<div>
|
||||||
<h1>SnapGo</h1>
|
<h1>SnapGo</h1>
|
||||||
@@ -436,31 +185,17 @@ onMounted(load)
|
|||||||
<button class="btn primary" @click="manualCapture">Capture now</button>
|
<button class="btn primary" @click="manualCapture">Capture now</button>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<!-- General tab — shortcut and appearance configuration. -->
|
<!-- General tab — only the shortcut configuration lives here. -->
|
||||||
<section v-if="props.tab === 'general'" class="card">
|
<section v-if="props.tab === 'general'" class="card">
|
||||||
<h2>General</h2>
|
<h2>Shortcut</h2>
|
||||||
<div class="grid">
|
<label class="field">
|
||||||
<label class="field">
|
<span>Global hotkey</span>
|
||||||
<span>Global hotkey</span>
|
<input
|
||||||
<input
|
v-model="config.hotkey"
|
||||||
v-model="config.hotkey"
|
placeholder="cmd+shift+a"
|
||||||
placeholder="cmd+shift+a"
|
spellcheck="false"
|
||||||
spellcheck="false"
|
/>
|
||||||
/>
|
</label>
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>主题选择</span>
|
|
||||||
<select v-model="config.theme">
|
|
||||||
<option
|
|
||||||
v-for="themeOption in themeOptions"
|
|
||||||
:key="themeOption.id"
|
|
||||||
:value="themeOption.id"
|
|
||||||
>
|
|
||||||
{{ themeOption.label }}
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<p class="hint">
|
<p class="hint">
|
||||||
Tokens (case-insensitive, "+"-separated): cmd / ctrl / option / shift +
|
Tokens (case-insensitive, "+"-separated): cmd / ctrl / option / shift +
|
||||||
a–z or 0–9. Example: <code>cmd+shift+a</code>.
|
a–z or 0–9. Example: <code>cmd+shift+a</code>.
|
||||||
@@ -529,121 +264,6 @@ onMounted(load)
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<!-- FTP/SFTP tab — file-transfer destination for its toolbar action. -->
|
|
||||||
<section v-if="props.tab === 'ftp'" class="card">
|
|
||||||
<h2>FTP / SFTP destination</h2>
|
|
||||||
<div class="grid">
|
|
||||||
<label class="field">
|
|
||||||
<span>Protocol</span>
|
|
||||||
<select :value="config.ftp.protocol" @change="changeFTPProtocol">
|
|
||||||
<option value="ftp">FTP</option>
|
|
||||||
<option value="sftp">SFTP (SSH File Transfer Protocol)</option>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Port</span>
|
|
||||||
<input
|
|
||||||
v-model.number="config.ftp.port"
|
|
||||||
type="number"
|
|
||||||
min="1"
|
|
||||||
max="65535"
|
|
||||||
:placeholder="config.ftp.protocol === 'sftp' ? '22' : '21'"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Host (IP or domain) *</span>
|
|
||||||
<input v-model.trim="config.ftp.host" placeholder="files.example.com" />
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>User *</span>
|
|
||||||
<input v-model.trim="config.ftp.user" placeholder="snapgo" />
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
v-if="config.ftp.protocol === 'sftp'"
|
|
||||||
key="sftp-auth-method"
|
|
||||||
class="field"
|
|
||||||
>
|
|
||||||
<span>Auth method</span>
|
|
||||||
<select
|
|
||||||
:value="config.ftp.authMethod"
|
|
||||||
@change="changeFTPAuthMethod"
|
|
||||||
>
|
|
||||||
<option value="password">Password</option>
|
|
||||||
<option value="key">SSH-Key</option>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
v-if="config.ftp.protocol === 'ftp' || config.ftp.authMethod === 'password'"
|
|
||||||
:key="`${config.ftp.protocol}-password`"
|
|
||||||
class="field"
|
|
||||||
>
|
|
||||||
<span>Password</span>
|
|
||||||
<input v-model="config.ftp.password" type="password" />
|
|
||||||
</label>
|
|
||||||
<label class="field full">
|
|
||||||
<span>Remote directory (under login root)</span>
|
|
||||||
<div class="prefixed-input">
|
|
||||||
<span class="input-prefix">{{ ftpPathRootLabel }}</span>
|
|
||||||
<input
|
|
||||||
v-model="ftpPathDisplay"
|
|
||||||
placeholder="snapgo/"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Connect timeout (sec)</span>
|
|
||||||
<input
|
|
||||||
v-model.number="config.ftp.connectTimeoutSecs"
|
|
||||||
type="number"
|
|
||||||
min="1"
|
|
||||||
max="120"
|
|
||||||
placeholder="10"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label v-if="config.ftp.protocol === 'sftp'" class="field">
|
|
||||||
<span>Known hosts path</span>
|
|
||||||
<input
|
|
||||||
v-model="config.ftp.knownHostsPath"
|
|
||||||
placeholder="~/.ssh/known_hosts"
|
|
||||||
:disabled="!config.ftp.strictHostKey"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label v-if="config.ftp.protocol === 'sftp'" class="field checkbox">
|
|
||||||
<input type="checkbox" v-model="config.ftp.strictHostKey" />
|
|
||||||
<span>Strict host key checking (recommended)</span>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<p v-if="config.ftp.protocol === 'ftp'" class="hint warn">
|
|
||||||
FTP sends the username, password, and screenshot without encryption.
|
|
||||||
Prefer SFTP on networks you do not fully trust. SFTP is not FTPS.
|
|
||||||
</p>
|
|
||||||
<p
|
|
||||||
v-else-if="config.ftp.authMethod === 'key'"
|
|
||||||
class="hint"
|
|
||||||
>
|
|
||||||
SSH-Key mode uses your <code>ssh-agent</code> and
|
|
||||||
<code>~/.ssh/id_*</code> keys.
|
|
||||||
</p>
|
|
||||||
<p
|
|
||||||
v-else-if="!config.ftp.strictHostKey"
|
|
||||||
class="hint warn"
|
|
||||||
>
|
|
||||||
Host key checking is disabled. Enable it for production servers to
|
|
||||||
detect server impersonation.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<div class="actions">
|
|
||||||
<button class="btn" :disabled="testingFTP" @click="testFTP">
|
|
||||||
{{ testingFTP ? 'Testing…' : 'Test connection' }}
|
|
||||||
</button>
|
|
||||||
<button class="btn primary" :disabled="saving" @click="save">
|
|
||||||
{{ saving ? 'Saving…' : 'Save' }}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<!-- SSH-Conf tab — destination for the save-remote action. -->
|
<!-- SSH-Conf tab — destination for the save-remote action. -->
|
||||||
<section v-if="props.tab === 'ssh'" class="card">
|
<section v-if="props.tab === 'ssh'" class="card">
|
||||||
<h2>SSH / SCP destination</h2>
|
<h2>SSH / SCP destination</h2>
|
||||||
@@ -669,6 +289,7 @@ onMounted(load)
|
|||||||
<option value="password">Password</option>
|
<option value="password">Password</option>
|
||||||
<option value="key">SSH-Key</option>
|
<option value="key">SSH-Key</option>
|
||||||
<option value="kerberos">Kerberos</option>
|
<option value="kerberos">Kerberos</option>
|
||||||
|
<option value="bgo">bgo</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
<label class="field">
|
<label class="field">
|
||||||
@@ -730,6 +351,18 @@ onMounted(load)
|
|||||||
keys. Make sure password-less login already works (e.g. via
|
keys. Make sure password-less login already works (e.g. via
|
||||||
<code>ssh-copy-id</code>).
|
<code>ssh-copy-id</code>).
|
||||||
</p>
|
</p>
|
||||||
|
<p v-else-if="config.ssh.authMethod === 'bgo'" class="hint">
|
||||||
|
bgo mode delegates uploads to the internal <code>bgo scp</code> client,
|
||||||
|
which handles authentication itself. Install bgo and add it to your
|
||||||
|
<code>PATH</code> first — see the
|
||||||
|
<a
|
||||||
|
href="https://bytedance.larkoffice.com/wiki/HpUCw7qRDiW66YkzKdwcXYrTnLf"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener"
|
||||||
|
>setup guide</a>.
|
||||||
|
Note: bgo cannot create remote directories, so screenshots are saved
|
||||||
|
flat into the remote home directory (the Remote path setting is ignored).
|
||||||
|
</p>
|
||||||
<p
|
<p
|
||||||
v-else-if="config.ssh.authMethod === 'password' && !config.ssh.password"
|
v-else-if="config.ssh.authMethod === 'password' && !config.ssh.password"
|
||||||
class="hint warn"
|
class="hint warn"
|
||||||
@@ -748,200 +381,6 @@ onMounted(load)
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<!-- LLM tab — multimodal screenshot summary provider settings. -->
|
|
||||||
<section v-if="props.tab === 'llm'" class="card">
|
|
||||||
<h2>LLM screenshot summary</h2>
|
|
||||||
<div class="grid">
|
|
||||||
<label class="field full">
|
|
||||||
<span>Provider</span>
|
|
||||||
<select v-model="config.llm.activeProvider">
|
|
||||||
<option
|
|
||||||
v-for="provider in llmProviderOptions"
|
|
||||||
:key="provider.id"
|
|
||||||
:value="provider.id"
|
|
||||||
>
|
|
||||||
{{ provider.label }}
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
<label class="field full">
|
|
||||||
<span>Base URL *</span>
|
|
||||||
<input
|
|
||||||
v-model="activeLLMProvider.baseUrl"
|
|
||||||
placeholder="https://api.openai.com/v1"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>API Key *</span>
|
|
||||||
<input v-model="activeLLMProvider.apiKey" type="password" />
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Model *</span>
|
|
||||||
<input
|
|
||||||
v-model="activeLLMProvider.model"
|
|
||||||
placeholder="gpt-5.5 / qwen-vl-plus / Ark endpoint ID"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Max tokens</span>
|
|
||||||
<input
|
|
||||||
v-model.number="activeLLMProvider.maxTokens"
|
|
||||||
type="number"
|
|
||||||
min="64"
|
|
||||||
max="4096"
|
|
||||||
placeholder="600"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Temperature</span>
|
|
||||||
<input
|
|
||||||
v-model.number="activeLLMProvider.temperature"
|
|
||||||
type="number"
|
|
||||||
min="0"
|
|
||||||
max="2"
|
|
||||||
step="0.1"
|
|
||||||
placeholder="0.2"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Timeout (sec)</span>
|
|
||||||
<input
|
|
||||||
v-model.number="activeLLMProvider.timeoutSecs"
|
|
||||||
type="number"
|
|
||||||
min="10"
|
|
||||||
max="300"
|
|
||||||
placeholder="60"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field full">
|
|
||||||
<span>Prompt</span>
|
|
||||||
<textarea
|
|
||||||
v-model="config.llm.prompt"
|
|
||||||
class="prompt-textarea"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<p class="hint">
|
|
||||||
The "copy summary" screenshot action first uploads the image through the
|
|
||||||
S3 configuration, then sends the public image URL to this multimodal
|
|
||||||
provider. Keep S3 configured before using it.
|
|
||||||
</p>
|
|
||||||
<p v-if="config.llm.activeProvider === 'doubao'" class="hint">
|
|
||||||
Volcengine Ark usually uses the inference endpoint ID as the model
|
|
||||||
value; paste the endpoint/model identifier from your Ark console.
|
|
||||||
</p>
|
|
||||||
<p v-else-if="config.llm.activeProvider === 'qwen'" class="hint">
|
|
||||||
DashScope OpenAI-compatible mode is used here. If your workspace uses a
|
|
||||||
regional endpoint, replace Base URL with that compatible-mode URL.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<div class="actions">
|
|
||||||
<button class="btn primary" :disabled="saving" @click="save">
|
|
||||||
{{ saving ? 'Saving…' : 'Save' }}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<!-- OCR tab — cloud OCR provider settings for screenshot text extraction. -->
|
|
||||||
<section v-if="props.tab === 'ocr'" class="card">
|
|
||||||
<h2>文字提取</h2>
|
|
||||||
<div class="grid">
|
|
||||||
<label class="field full">
|
|
||||||
<span>Provider</span>
|
|
||||||
<select v-model="config.ocr.activeProvider">
|
|
||||||
<option
|
|
||||||
v-for="provider in ocrProviderOptions"
|
|
||||||
:key="provider.id"
|
|
||||||
:value="provider.id"
|
|
||||||
>
|
|
||||||
{{ provider.label }}
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
</label>
|
|
||||||
<label class="field full">
|
|
||||||
<span>Endpoint *</span>
|
|
||||||
<input
|
|
||||||
v-model="activeOCRProvider.endpoint"
|
|
||||||
placeholder="https://ocr-api.cn-hangzhou.aliyuncs.com"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>AccessKey ID *</span>
|
|
||||||
<input v-model="activeOCRProvider.accessKeyId" spellcheck="false" />
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>AccessKey Secret *</span>
|
|
||||||
<input v-model="activeOCRProvider.accessKeySecret" type="password" />
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
v-if="config.ocr.activeProvider === 'volcengine'"
|
|
||||||
class="field"
|
|
||||||
>
|
|
||||||
<span>Region *</span>
|
|
||||||
<input
|
|
||||||
v-model="activeOCRProvider.region"
|
|
||||||
placeholder="cn-north-1"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
v-if="config.ocr.activeProvider === 'volcengine'"
|
|
||||||
class="field"
|
|
||||||
>
|
|
||||||
<span>Service *</span>
|
|
||||||
<input
|
|
||||||
v-model="activeOCRProvider.service"
|
|
||||||
placeholder="cv"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Action *</span>
|
|
||||||
<input
|
|
||||||
v-model="activeOCRProvider.action"
|
|
||||||
placeholder="RecognizeGeneral / OCRNormal"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Version *</span>
|
|
||||||
<input
|
|
||||||
v-model="activeOCRProvider.version"
|
|
||||||
placeholder="2021-07-07 / 2020-08-26"
|
|
||||||
spellcheck="false"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label class="field">
|
|
||||||
<span>Timeout (sec)</span>
|
|
||||||
<input
|
|
||||||
v-model.number="activeOCRProvider.timeoutSecs"
|
|
||||||
type="number"
|
|
||||||
min="5"
|
|
||||||
max="120"
|
|
||||||
placeholder="30"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<p v-if="config.ocr.activeProvider === 'aliyun'" class="hint">
|
|
||||||
阿里云默认使用 OCR API 的 <code>RecognizeGeneral</code>,请求体为截图
|
|
||||||
PNG 原始数据,并使用 ACS3-HMAC-SHA256 签名。
|
|
||||||
</p>
|
|
||||||
<p v-else class="hint">
|
|
||||||
火山引擎默认使用视觉智能的 <code>OCRNormal</code>,截图以
|
|
||||||
<code>image_base64</code> 发送,并使用 Region/Service 参与签名。
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<div class="actions">
|
|
||||||
<button class="btn primary" :disabled="saving" @click="save">
|
|
||||||
{{ saving ? 'Saving…' : 'Save' }}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<transition name="fade">
|
<transition name="fade">
|
||||||
<div
|
<div
|
||||||
v-if="message"
|
v-if="message"
|
||||||
@@ -961,15 +400,6 @@ onMounted(load)
|
|||||||
padding: 28px 32px 60px;
|
padding: 28px 32px 60px;
|
||||||
color: #1f2937;
|
color: #1f2937;
|
||||||
}
|
}
|
||||||
.settings.theme-light {
|
|
||||||
color-scheme: light;
|
|
||||||
}
|
|
||||||
.settings.theme-dark {
|
|
||||||
color-scheme: dark;
|
|
||||||
}
|
|
||||||
.settings.theme-auto {
|
|
||||||
color-scheme: light dark;
|
|
||||||
}
|
|
||||||
.hero {
|
.hero {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
@@ -1043,13 +473,13 @@ kbd {
|
|||||||
.field input[type='text'],
|
.field input[type='text'],
|
||||||
.field input[type='number'],
|
.field input[type='number'],
|
||||||
.field input:not([type='checkbox']),
|
.field input:not([type='checkbox']),
|
||||||
.field select,
|
.field select {
|
||||||
.field textarea {
|
|
||||||
border: 1px solid #d1d5db;
|
border: 1px solid #d1d5db;
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
padding: 7px 10px;
|
padding: 7px 10px;
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
line-height: 1.4;
|
line-height: 1.4;
|
||||||
|
height: 36px;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
color: inherit;
|
color: inherit;
|
||||||
outline: none;
|
outline: none;
|
||||||
@@ -1057,20 +487,8 @@ kbd {
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
.field input[type='text'],
|
|
||||||
.field input[type='number'],
|
|
||||||
.field input:not([type='checkbox']),
|
|
||||||
.field select {
|
|
||||||
height: 36px;
|
|
||||||
}
|
|
||||||
.field textarea {
|
|
||||||
min-height: 168px;
|
|
||||||
resize: vertical;
|
|
||||||
font-family: inherit;
|
|
||||||
}
|
|
||||||
.field input:focus,
|
.field input:focus,
|
||||||
.field select:focus,
|
.field select:focus {
|
||||||
.field textarea:focus {
|
|
||||||
border-color: #3b82f6;
|
border-color: #3b82f6;
|
||||||
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
|
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
|
||||||
}
|
}
|
||||||
@@ -1185,100 +603,46 @@ kbd {
|
|||||||
.fade-leave-to {
|
.fade-leave-to {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
}
|
}
|
||||||
.settings.theme-dark {
|
|
||||||
color: #e5e7eb;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .card {
|
|
||||||
background: #1f2937;
|
|
||||||
border-color: #374151;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .field span {
|
|
||||||
color: #d1d5db;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .field input[type='text'],
|
|
||||||
.settings.theme-dark .field input[type='number'],
|
|
||||||
.settings.theme-dark .field input:not([type='checkbox']),
|
|
||||||
.settings.theme-dark .field select,
|
|
||||||
.settings.theme-dark .field textarea {
|
|
||||||
background: #111827;
|
|
||||||
border-color: #374151;
|
|
||||||
color: #fff;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .field input::placeholder,
|
|
||||||
.settings.theme-dark .field textarea::placeholder {
|
|
||||||
color: #9ca3af;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .field input:disabled {
|
|
||||||
background: #1f2937;
|
|
||||||
color: #6b7280;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .prefixed-input {
|
|
||||||
background: #111827;
|
|
||||||
border-color: #374151;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .input-prefix {
|
|
||||||
background: #1f2937;
|
|
||||||
border-right-color: #374151;
|
|
||||||
color: #d1d5db;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .btn:not(.primary) {
|
|
||||||
background: #1f2937;
|
|
||||||
border-color: #374151;
|
|
||||||
color: #e5e7eb;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .btn:not(.primary):hover:not(:disabled) {
|
|
||||||
background: #374151;
|
|
||||||
}
|
|
||||||
.settings.theme-dark .subtitle {
|
|
||||||
color: #9ca3af;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
.settings.theme-auto {
|
.settings {
|
||||||
color: #e5e7eb;
|
color: #e5e7eb;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .card {
|
.card {
|
||||||
background: #1f2937;
|
background: #1f2937;
|
||||||
border-color: #374151;
|
border-color: #374151;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .field span {
|
.field input {
|
||||||
color: #d1d5db;
|
|
||||||
}
|
|
||||||
.settings.theme-auto .field input[type='text'],
|
|
||||||
.settings.theme-auto .field input[type='number'],
|
|
||||||
.settings.theme-auto .field input:not([type='checkbox']),
|
|
||||||
.settings.theme-auto .field select,
|
|
||||||
.settings.theme-auto .field textarea {
|
|
||||||
background: #111827;
|
background: #111827;
|
||||||
border-color: #374151;
|
border-color: #374151;
|
||||||
color: #fff;
|
color: #e5e7eb;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .field input::placeholder,
|
.field select {
|
||||||
.settings.theme-auto .field textarea::placeholder {
|
background: #111827;
|
||||||
color: #9ca3af;
|
border-color: #374151;
|
||||||
|
color: #e5e7eb;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .field input:disabled {
|
.field input:disabled {
|
||||||
background: #1f2937;
|
background: #1f2937;
|
||||||
color: #6b7280;
|
color: #6b7280;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .prefixed-input {
|
.prefixed-input {
|
||||||
background: #111827;
|
background: #111827;
|
||||||
border-color: #374151;
|
border-color: #374151;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .input-prefix {
|
.input-prefix {
|
||||||
background: #1f2937;
|
background: #1f2937;
|
||||||
border-right-color: #374151;
|
border-right-color: #374151;
|
||||||
color: #d1d5db;
|
color: #d1d5db;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .btn:not(.primary) {
|
.btn {
|
||||||
background: #1f2937;
|
background: #1f2937;
|
||||||
border-color: #374151;
|
border-color: #374151;
|
||||||
color: #e5e7eb;
|
color: #e5e7eb;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .btn:not(.primary):hover:not(:disabled) {
|
.btn:hover:not(:disabled) {
|
||||||
background: #374151;
|
background: #374151;
|
||||||
}
|
}
|
||||||
.settings.theme-auto .subtitle {
|
.subtitle {
|
||||||
color: #9ca3af;
|
color: #9ca3af;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
-14
@@ -17,10 +17,6 @@ export function CopyNativeRegionImage(arg1:main.CaptureResult):Promise<void>;
|
|||||||
|
|
||||||
export function CopyRegionImage(arg1:main.CaptureResult):Promise<void>;
|
export function CopyRegionImage(arg1:main.CaptureResult):Promise<void>;
|
||||||
|
|
||||||
export function ExtractTextNativeRegion(arg1:main.CaptureResult):Promise<void>;
|
|
||||||
|
|
||||||
export function ExtractTextRegion(arg1:main.CaptureResult):Promise<void>;
|
|
||||||
|
|
||||||
export function GetConfig():Promise<domain.AppConfig>;
|
export function GetConfig():Promise<domain.AppConfig>;
|
||||||
|
|
||||||
export function QuitApp():Promise<void>;
|
export function QuitApp():Promise<void>;
|
||||||
@@ -41,16 +37,6 @@ export function SaveRegionToRemote(arg1:main.CaptureResult):Promise<void>;
|
|||||||
|
|
||||||
export function ShowWindow():Promise<void>;
|
export function ShowWindow():Promise<void>;
|
||||||
|
|
||||||
export function SummarizeNativeRegion(arg1:main.CaptureResult):Promise<void>;
|
|
||||||
|
|
||||||
export function SummarizeRegion(arg1:main.CaptureResult):Promise<void>;
|
|
||||||
|
|
||||||
export function TestConnection(arg1:domain.S3Config):Promise<void>;
|
export function TestConnection(arg1:domain.S3Config):Promise<void>;
|
||||||
|
|
||||||
export function TestFTPConnection(arg1:domain.FTPConfig):Promise<void>;
|
|
||||||
|
|
||||||
export function TestSSHConnection(arg1:domain.SSHConfig):Promise<void>;
|
export function TestSSHConnection(arg1:domain.SSHConfig):Promise<void>;
|
||||||
|
|
||||||
export function UploadNativeRegionToFTP(arg1:main.CaptureResult):Promise<void>;
|
|
||||||
|
|
||||||
export function UploadRegionToFTP(arg1:main.CaptureResult):Promise<void>;
|
|
||||||
|
|||||||
@@ -30,14 +30,6 @@ export function CopyRegionImage(arg1) {
|
|||||||
return window['go']['main']['App']['CopyRegionImage'](arg1);
|
return window['go']['main']['App']['CopyRegionImage'](arg1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ExtractTextNativeRegion(arg1) {
|
|
||||||
return window['go']['main']['App']['ExtractTextNativeRegion'](arg1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function ExtractTextRegion(arg1) {
|
|
||||||
return window['go']['main']['App']['ExtractTextRegion'](arg1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function GetConfig() {
|
export function GetConfig() {
|
||||||
return window['go']['main']['App']['GetConfig']();
|
return window['go']['main']['App']['GetConfig']();
|
||||||
}
|
}
|
||||||
@@ -78,30 +70,10 @@ export function ShowWindow() {
|
|||||||
return window['go']['main']['App']['ShowWindow']();
|
return window['go']['main']['App']['ShowWindow']();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SummarizeNativeRegion(arg1) {
|
|
||||||
return window['go']['main']['App']['SummarizeNativeRegion'](arg1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function SummarizeRegion(arg1) {
|
|
||||||
return window['go']['main']['App']['SummarizeRegion'](arg1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function TestConnection(arg1) {
|
export function TestConnection(arg1) {
|
||||||
return window['go']['main']['App']['TestConnection'](arg1);
|
return window['go']['main']['App']['TestConnection'](arg1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TestFTPConnection(arg1) {
|
|
||||||
return window['go']['main']['App']['TestFTPConnection'](arg1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function TestSSHConnection(arg1) {
|
export function TestSSHConnection(arg1) {
|
||||||
return window['go']['main']['App']['TestSSHConnection'](arg1);
|
return window['go']['main']['App']['TestSSHConnection'](arg1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function UploadNativeRegionToFTP(arg1) {
|
|
||||||
return window['go']['main']['App']['UploadNativeRegionToFTP'](arg1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function UploadRegionToFTP(arg1) {
|
|
||||||
return window['go']['main']['App']['UploadRegionToFTP'](arg1);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,9 +18,6 @@ export namespace application {
|
|||||||
tool: string;
|
tool: string;
|
||||||
color: string;
|
color: string;
|
||||||
points: Point[];
|
points: Point[];
|
||||||
text?: string;
|
|
||||||
strokeWidth?: number;
|
|
||||||
fontSize?: number;
|
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
static createFrom(source: any = {}) {
|
||||||
return new Annotation(source);
|
return new Annotation(source);
|
||||||
@@ -31,9 +28,6 @@ export namespace application {
|
|||||||
this.tool = source["tool"];
|
this.tool = source["tool"];
|
||||||
this.color = source["color"];
|
this.color = source["color"];
|
||||||
this.points = this.convertValues(source["points"], Point);
|
this.points = this.convertValues(source["points"], Point);
|
||||||
this.text = source["text"];
|
|
||||||
this.strokeWidth = source["strokeWidth"];
|
|
||||||
this.fontSize = source["fontSize"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
||||||
@@ -59,156 +53,6 @@ export namespace application {
|
|||||||
|
|
||||||
export namespace domain {
|
export namespace domain {
|
||||||
|
|
||||||
export class OCRProviderConfig {
|
|
||||||
label: string;
|
|
||||||
endpoint: string;
|
|
||||||
accessKeyId: string;
|
|
||||||
accessKeySecret: string;
|
|
||||||
region: string;
|
|
||||||
service: string;
|
|
||||||
action: string;
|
|
||||||
version: string;
|
|
||||||
timeoutSecs: number;
|
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
|
||||||
return new OCRProviderConfig(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(source: any = {}) {
|
|
||||||
if ('string' === typeof source) source = JSON.parse(source);
|
|
||||||
this.label = source["label"];
|
|
||||||
this.endpoint = source["endpoint"];
|
|
||||||
this.accessKeyId = source["accessKeyId"];
|
|
||||||
this.accessKeySecret = source["accessKeySecret"];
|
|
||||||
this.region = source["region"];
|
|
||||||
this.service = source["service"];
|
|
||||||
this.action = source["action"];
|
|
||||||
this.version = source["version"];
|
|
||||||
this.timeoutSecs = source["timeoutSecs"];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class OCRConfig {
|
|
||||||
activeProvider: string;
|
|
||||||
providers: Record<string, OCRProviderConfig>;
|
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
|
||||||
return new OCRConfig(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(source: any = {}) {
|
|
||||||
if ('string' === typeof source) source = JSON.parse(source);
|
|
||||||
this.activeProvider = source["activeProvider"];
|
|
||||||
this.providers = this.convertValues(source["providers"], OCRProviderConfig, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
|
||||||
if (!a) {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
if (a.slice && a.map) {
|
|
||||||
return (a as any[]).map(elem => this.convertValues(elem, classs));
|
|
||||||
} else if ("object" === typeof a) {
|
|
||||||
if (asMap) {
|
|
||||||
for (const key of Object.keys(a)) {
|
|
||||||
a[key] = new classs(a[key]);
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
return new classs(a);
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class LLMProviderConfig {
|
|
||||||
label: string;
|
|
||||||
baseUrl: string;
|
|
||||||
apiKey: string;
|
|
||||||
model: string;
|
|
||||||
maxTokens: number;
|
|
||||||
temperature: number;
|
|
||||||
timeoutSecs: number;
|
|
||||||
maxInlineBytes: number;
|
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
|
||||||
return new LLMProviderConfig(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(source: any = {}) {
|
|
||||||
if ('string' === typeof source) source = JSON.parse(source);
|
|
||||||
this.label = source["label"];
|
|
||||||
this.baseUrl = source["baseUrl"];
|
|
||||||
this.apiKey = source["apiKey"];
|
|
||||||
this.model = source["model"];
|
|
||||||
this.maxTokens = source["maxTokens"];
|
|
||||||
this.temperature = source["temperature"];
|
|
||||||
this.timeoutSecs = source["timeoutSecs"];
|
|
||||||
this.maxInlineBytes = source["maxInlineBytes"];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class LLMConfig {
|
|
||||||
activeProvider: string;
|
|
||||||
prompt: string;
|
|
||||||
providers: Record<string, LLMProviderConfig>;
|
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
|
||||||
return new LLMConfig(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(source: any = {}) {
|
|
||||||
if ('string' === typeof source) source = JSON.parse(source);
|
|
||||||
this.activeProvider = source["activeProvider"];
|
|
||||||
this.prompt = source["prompt"];
|
|
||||||
this.providers = this.convertValues(source["providers"], LLMProviderConfig, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
|
||||||
if (!a) {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
if (a.slice && a.map) {
|
|
||||||
return (a as any[]).map(elem => this.convertValues(elem, classs));
|
|
||||||
} else if ("object" === typeof a) {
|
|
||||||
if (asMap) {
|
|
||||||
for (const key of Object.keys(a)) {
|
|
||||||
a[key] = new classs(a[key]);
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
return new classs(a);
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class FTPConfig {
|
|
||||||
protocol: string;
|
|
||||||
host: string;
|
|
||||||
port: number;
|
|
||||||
user: string;
|
|
||||||
authMethod: string;
|
|
||||||
password: string;
|
|
||||||
pathPrefix: string;
|
|
||||||
strictHostKey: boolean;
|
|
||||||
knownHostsPath: string;
|
|
||||||
connectTimeoutSecs: number;
|
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
|
||||||
return new FTPConfig(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(source: any = {}) {
|
|
||||||
if ('string' === typeof source) source = JSON.parse(source);
|
|
||||||
this.protocol = source["protocol"];
|
|
||||||
this.host = source["host"];
|
|
||||||
this.port = source["port"];
|
|
||||||
this.user = source["user"];
|
|
||||||
this.authMethod = source["authMethod"];
|
|
||||||
this.password = source["password"];
|
|
||||||
this.pathPrefix = source["pathPrefix"];
|
|
||||||
this.strictHostKey = source["strictHostKey"];
|
|
||||||
this.knownHostsPath = source["knownHostsPath"];
|
|
||||||
this.connectTimeoutSecs = source["connectTimeoutSecs"];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class SSHConfig {
|
export class SSHConfig {
|
||||||
host: string;
|
host: string;
|
||||||
port: number;
|
port: number;
|
||||||
@@ -265,12 +109,8 @@ export namespace domain {
|
|||||||
}
|
}
|
||||||
export class AppConfig {
|
export class AppConfig {
|
||||||
hotkey: string;
|
hotkey: string;
|
||||||
theme: string;
|
|
||||||
s3: S3Config;
|
s3: S3Config;
|
||||||
ssh: SSHConfig;
|
ssh: SSHConfig;
|
||||||
ftp: FTPConfig;
|
|
||||||
llm: LLMConfig;
|
|
||||||
ocr: OCRConfig;
|
|
||||||
|
|
||||||
static createFrom(source: any = {}) {
|
static createFrom(source: any = {}) {
|
||||||
return new AppConfig(source);
|
return new AppConfig(source);
|
||||||
@@ -279,12 +119,8 @@ export namespace domain {
|
|||||||
constructor(source: any = {}) {
|
constructor(source: any = {}) {
|
||||||
if ('string' === typeof source) source = JSON.parse(source);
|
if ('string' === typeof source) source = JSON.parse(source);
|
||||||
this.hotkey = source["hotkey"];
|
this.hotkey = source["hotkey"];
|
||||||
this.theme = source["theme"];
|
|
||||||
this.s3 = this.convertValues(source["s3"], S3Config);
|
this.s3 = this.convertValues(source["s3"], S3Config);
|
||||||
this.ssh = this.convertValues(source["ssh"], SSHConfig);
|
this.ssh = this.convertValues(source["ssh"], SSHConfig);
|
||||||
this.ftp = this.convertValues(source["ftp"], FTPConfig);
|
|
||||||
this.llm = this.convertValues(source["llm"], LLMConfig);
|
|
||||||
this.ocr = this.convertValues(source["ocr"], OCRConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
convertValues(a: any, classs: any, asMap: boolean = false): any {
|
||||||
@@ -306,11 +142,6 @@ export namespace domain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,11 @@ require (
|
|||||||
github.com/aws/aws-sdk-go-v2 v1.30.3
|
github.com/aws/aws-sdk-go-v2 v1.30.3
|
||||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
|
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
|
||||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2
|
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.2
|
||||||
github.com/jlaffaye/ftp v0.2.0
|
|
||||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237
|
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237
|
||||||
github.com/pkg/sftp v1.13.10
|
|
||||||
github.com/wailsapp/wails/v2 v2.12.0
|
github.com/wailsapp/wails/v2 v2.12.0
|
||||||
golang.design/x/clipboard v0.7.0
|
golang.design/x/clipboard v0.7.0
|
||||||
golang.design/x/hotkey v0.4.1
|
golang.design/x/hotkey v0.4.1
|
||||||
golang.org/x/crypto v0.41.0
|
golang.org/x/crypto v0.33.0
|
||||||
golang.org/x/image v0.12.0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -34,11 +31,8 @@ require (
|
|||||||
github.com/godbus/dbus/v5 v5.1.0 // indirect
|
github.com/godbus/dbus/v5 v5.1.0 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/gorilla/websocket v1.5.3 // indirect
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
github.com/hashicorp/errwrap v1.0.0 // indirect
|
|
||||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
|
||||||
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect
|
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect
|
||||||
github.com/jezek/xgb v1.1.0 // indirect
|
github.com/jezek/xgb v1.1.0 // indirect
|
||||||
github.com/kr/fs v0.1.0 // indirect
|
|
||||||
github.com/labstack/echo/v4 v4.13.3 // indirect
|
github.com/labstack/echo/v4 v4.13.3 // indirect
|
||||||
github.com/labstack/gommon v0.4.2 // indirect
|
github.com/labstack/gommon v0.4.2 // indirect
|
||||||
github.com/leaanthony/go-ansi-parser v1.6.1 // indirect
|
github.com/leaanthony/go-ansi-parser v1.6.1 // indirect
|
||||||
@@ -58,8 +52,9 @@ require (
|
|||||||
github.com/wailsapp/go-webview2 v1.0.22 // indirect
|
github.com/wailsapp/go-webview2 v1.0.22 // indirect
|
||||||
github.com/wailsapp/mimetype v1.4.1 // indirect
|
github.com/wailsapp/mimetype v1.4.1 // indirect
|
||||||
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
|
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
|
||||||
|
golang.org/x/image v0.12.0 // indirect
|
||||||
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
|
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
|
||||||
golang.org/x/net v0.42.0 // indirect
|
golang.org/x/net v0.35.0 // indirect
|
||||||
golang.org/x/sys v0.35.0 // indirect
|
golang.org/x/sys v0.30.0 // indirect
|
||||||
golang.org/x/text v0.28.0 // indirect
|
golang.org/x/text v0.22.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -41,20 +41,12 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
|
||||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
|
||||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
|
||||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
|
||||||
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck=
|
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck=
|
||||||
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs=
|
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs=
|
||||||
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||||
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||||
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
|
|
||||||
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
|
|
||||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 h1:YOp8St+CM/AQ9Vp4XYm4272E77MptJDHkwypQHIRl9Q=
|
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237 h1:YOp8St+CM/AQ9Vp4XYm4272E77MptJDHkwypQHIRl9Q=
|
||||||
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237/go.mod h1:e7qQlOY68wOz4b82D7n+DdaptZAi+SHW0+yKiWZzEYE=
|
github.com/kbinani/screenshot v0.0.0-20230812210009-b87d31814237/go.mod h1:e7qQlOY68wOz4b82D7n+DdaptZAi+SHW0+yKiWZzEYE=
|
||||||
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
|
|
||||||
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
|
||||||
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
|
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
|
||||||
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
|
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
|
||||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||||
@@ -83,8 +75,6 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmd
|
|||||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
|
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pkg/sftp v1.13.10 h1:+5FbKNTe5Z9aspU88DPIKJ9z2KZoaGCu6Sr6kKR/5mU=
|
|
||||||
github.com/pkg/sftp v1.13.10/go.mod h1:bJ1a7uDhrX/4OII+agvy28lzRvQrmIQuaHrcI1HbeGA=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
@@ -116,8 +106,8 @@ golang.design/x/mainthread v0.3.0/go.mod h1:vYX7cF2b3pTJMGM/hc13NmN6kblKnf4/IyvH
|
|||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
|
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||||
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
|
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||||
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
|
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
|
||||||
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
|
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
|
||||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||||
@@ -136,8 +126,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
|
|||||||
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
|
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||||
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
|
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
@@ -154,21 +144,21 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||||
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
golang.org/x/term v0.34.0 h1:O/2T7POpk0ZZ7MAzMeWFSg6S5IpWd/RXDlM9hgM3DR4=
|
golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU=
|
||||||
golang.org/x/term v0.34.0/go.mod h1:5jC53AEywhIVebHgPVeg0mj8OD3VO9OzclacVrqpaAw=
|
golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
|
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||||
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
|
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
|||||||
@@ -8,26 +8,16 @@ import (
|
|||||||
"image/draw"
|
"image/draw"
|
||||||
"image/png"
|
"image/png"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
xfont "golang.org/x/image/font"
|
|
||||||
"golang.org/x/image/font/basicfont"
|
|
||||||
"golang.org/x/image/font/opentype"
|
|
||||||
"golang.org/x/image/math/fixed"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Annotation describes a user-drawn mark relative to the selected screenshot.
|
// Annotation describes a user-drawn mark relative to the selected screenshot.
|
||||||
// Coordinates are logical pixels in the same coordinate system as the overlay.
|
// Coordinates are logical pixels in the same coordinate system as the overlay.
|
||||||
type Annotation struct {
|
type Annotation struct {
|
||||||
Tool string `json:"tool"`
|
Tool string `json:"tool"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
Points []Point `json:"points"`
|
Points []Point `json:"points"`
|
||||||
Text string `json:"text,omitempty"`
|
|
||||||
StrokeWidth float64 `json:"strokeWidth,omitempty"`
|
|
||||||
FontSize float64 `json:"fontSize,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Point struct {
|
type Point struct {
|
||||||
@@ -37,22 +27,12 @@ type Point struct {
|
|||||||
|
|
||||||
// ApplyAnnotations decodes a PNG, draws all annotations, and re-encodes it.
|
// ApplyAnnotations decodes a PNG, draws all annotations, and re-encodes it.
|
||||||
func ApplyAnnotations(pngBytes []byte, annotations []Annotation, scale float64) ([]byte, error) {
|
func ApplyAnnotations(pngBytes []byte, annotations []Annotation, scale float64) ([]byte, error) {
|
||||||
return ApplyAnnotationsWithScale(pngBytes, annotations, scale, scale)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyAnnotationsWithScale decodes a PNG, draws all annotations using the
|
|
||||||
// actual device-pixel scale of the captured image, and re-encodes it.
|
|
||||||
func ApplyAnnotationsWithScale(pngBytes []byte, annotations []Annotation, scaleX, scaleY float64) ([]byte, error) {
|
|
||||||
if len(annotations) == 0 {
|
if len(annotations) == 0 {
|
||||||
return pngBytes, nil
|
return pngBytes, nil
|
||||||
}
|
}
|
||||||
if scaleX <= 0 {
|
if scale <= 0 {
|
||||||
scaleX = 1
|
scale = 1
|
||||||
}
|
}
|
||||||
if scaleY <= 0 {
|
|
||||||
scaleY = scaleX
|
|
||||||
}
|
|
||||||
strokeScale := math.Max(scaleX, scaleY)
|
|
||||||
|
|
||||||
src, err := png.Decode(bytes.NewReader(pngBytes))
|
src, err := png.Decode(bytes.NewReader(pngBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -67,28 +47,14 @@ func ApplyAnnotationsWithScale(pngBytes []byte, annotations []Annotation, scaleX
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
c = color.RGBA{R: 59, G: 130, B: 246, A: 255}
|
c = color.RGBA{R: 59, G: 130, B: 246, A: 255}
|
||||||
}
|
}
|
||||||
strokeWidth := ann.StrokeWidth
|
width := int(math.Max(2, math.Round(3*scale)))
|
||||||
if strokeWidth <= 0 {
|
|
||||||
strokeWidth = 3
|
|
||||||
}
|
|
||||||
width := int(math.Max(1, math.Round(strokeWidth*strokeScale)))
|
|
||||||
switch ann.Tool {
|
switch ann.Tool {
|
||||||
case "pen":
|
case "pen":
|
||||||
drawPolyline(dst, ann.Points, scaleX, scaleY, width, c)
|
drawPolyline(dst, ann.Points, scale, width, c)
|
||||||
case "line":
|
|
||||||
drawStraightLine(dst, ann.Points, scaleX, scaleY, width, c)
|
|
||||||
case "arrow":
|
|
||||||
drawArrow(dst, ann.Points, scaleX, scaleY, width, c)
|
|
||||||
case "rect":
|
case "rect":
|
||||||
drawRectOutline(dst, ann.Points, scaleX, scaleY, width, c)
|
drawRectOutline(dst, ann.Points, scale, width, c)
|
||||||
case "ellipse":
|
case "ellipse":
|
||||||
drawEllipseOutline(dst, ann.Points, scaleX, scaleY, width, c)
|
drawEllipseOutline(dst, ann.Points, scale, width, c)
|
||||||
case "text":
|
|
||||||
drawTextAnnotation(dst, ann, scaleX, scaleY, c)
|
|
||||||
case "mosaic-brush":
|
|
||||||
applyMosaicBrush(dst, ann.Points, scaleX, scaleY, width)
|
|
||||||
case "mosaic-rect":
|
|
||||||
applyMosaicRect(dst, ann.Points, scaleX, scaleY)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,121 +65,6 @@ func ApplyAnnotationsWithScale(pngBytes []byte, annotations []Annotation, scaleX
|
|||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawStraightLine(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
|
||||||
if len(points) < 2 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
drawLine(img, scalePoint(points[0], scaleX, scaleY), scalePoint(points[len(points)-1], scaleX, scaleY), width, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawArrow(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
|
||||||
if len(points) < 2 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
start := scalePoint(points[0], scaleX, scaleY)
|
|
||||||
end := scalePoint(points[len(points)-1], scaleX, scaleY)
|
|
||||||
dx, dy := end.X-start.X, end.Y-start.Y
|
|
||||||
length := math.Hypot(dx, dy)
|
|
||||||
if length < 1 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
drawLine(img, start, end, width, c)
|
|
||||||
headLength := math.Min(length*0.45, math.Max(10*math.Max(scaleX, scaleY), float64(width)*4))
|
|
||||||
angle := math.Atan2(dy, dx)
|
|
||||||
spread := math.Pi / 6
|
|
||||||
left := Point{X: end.X - headLength*math.Cos(angle-spread), Y: end.Y - headLength*math.Sin(angle-spread)}
|
|
||||||
right := Point{X: end.X - headLength*math.Cos(angle+spread), Y: end.Y - headLength*math.Sin(angle+spread)}
|
|
||||||
drawLine(img, end, left, width, c)
|
|
||||||
drawLine(img, end, right, width, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
const mosaicBlockSize = 10
|
|
||||||
|
|
||||||
func applyMosaicBrush(img *image.RGBA, points []Point, scaleX, scaleY float64, width int) {
|
|
||||||
if len(points) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mask := image.NewAlpha(img.Bounds())
|
|
||||||
maskColor := color.RGBA{A: 255}
|
|
||||||
if len(points) == 1 {
|
|
||||||
drawDotMask(mask, scalePoint(points[0], scaleX, scaleY), width, maskColor)
|
|
||||||
} else {
|
|
||||||
for i := 1; i < len(points); i++ {
|
|
||||||
drawMaskLine(mask, scalePoint(points[i-1], scaleX, scaleY), scalePoint(points[i], scaleX, scaleY), width, maskColor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
applyPixelation(img, img.Bounds(), mask)
|
|
||||||
}
|
|
||||||
|
|
||||||
func applyMosaicRect(img *image.RGBA, points []Point, scaleX, scaleY float64) {
|
|
||||||
if len(points) < 2 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
a := scalePoint(points[0], scaleX, scaleY)
|
|
||||||
b := scalePoint(points[len(points)-1], scaleX, scaleY)
|
|
||||||
x1, x2 := ordered(a.X, b.X)
|
|
||||||
y1, y2 := ordered(a.Y, b.Y)
|
|
||||||
r := image.Rect(int(math.Floor(x1)), int(math.Floor(y1)), int(math.Ceil(x2)), int(math.Ceil(y2))).Intersect(img.Bounds())
|
|
||||||
applyPixelation(img, r, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func applyPixelation(img *image.RGBA, region image.Rectangle, mask *image.Alpha) {
|
|
||||||
if region.Empty() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for y := region.Min.Y; y < region.Max.Y; y += mosaicBlockSize {
|
|
||||||
for x := region.Min.X; x < region.Max.X; x += mosaicBlockSize {
|
|
||||||
block := image.Rect(x, y, min(x+mosaicBlockSize, region.Max.X), min(y+mosaicBlockSize, region.Max.Y))
|
|
||||||
var r, g, b, a, count uint64
|
|
||||||
for py := block.Min.Y; py < block.Max.Y; py++ {
|
|
||||||
for px := block.Min.X; px < block.Max.X; px++ {
|
|
||||||
c := img.RGBAAt(px, py)
|
|
||||||
r += uint64(c.R)
|
|
||||||
g += uint64(c.G)
|
|
||||||
b += uint64(c.B)
|
|
||||||
a += uint64(c.A)
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if count == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
avg := color.RGBA{uint8(r / count), uint8(g / count), uint8(b / count), uint8(a / count)}
|
|
||||||
for py := block.Min.Y; py < block.Max.Y; py++ {
|
|
||||||
for px := block.Min.X; px < block.Max.X; px++ {
|
|
||||||
if mask == nil || mask.AlphaAt(px, py).A > 0 {
|
|
||||||
img.SetRGBA(px, py, avg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawMaskLine(mask *image.Alpha, a, b Point, width int, c color.RGBA) {
|
|
||||||
dx, dy := b.X-a.X, b.Y-a.Y
|
|
||||||
steps := int(math.Max(math.Abs(dx), math.Abs(dy)))
|
|
||||||
if steps == 0 {
|
|
||||||
drawDotMask(mask, a, width, c)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for i := 0; i <= steps; i++ {
|
|
||||||
t := float64(i) / float64(steps)
|
|
||||||
drawDotMask(mask, Point{X: a.X + dx*t, Y: a.Y + dy*t}, width, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawDotMask(mask *image.Alpha, p Point, width int, c color.RGBA) {
|
|
||||||
radius := float64(width) / 2
|
|
||||||
for y := int(math.Floor(p.Y - radius)); y <= int(math.Ceil(p.Y+radius)); y++ {
|
|
||||||
for x := int(math.Floor(p.X - radius)); x <= int(math.Ceil(p.X+radius)); x++ {
|
|
||||||
if image.Pt(x, y).In(mask.Bounds()) && math.Hypot(float64(x)-p.X, float64(y)-p.Y) <= radius {
|
|
||||||
mask.SetAlpha(x, y, color.Alpha{A: c.A})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseHexColor(hex string) (color.RGBA, error) {
|
func parseHexColor(hex string) (color.RGBA, error) {
|
||||||
value := strings.TrimPrefix(strings.TrimSpace(hex), "#")
|
value := strings.TrimPrefix(strings.TrimSpace(hex), "#")
|
||||||
if len(value) != 6 {
|
if len(value) != 6 {
|
||||||
@@ -231,22 +82,22 @@ func parseHexColor(hex string) (color.RGBA, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawPolyline(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
func drawPolyline(img *image.RGBA, points []Point, scale float64, width int, c color.RGBA) {
|
||||||
if len(points) == 1 {
|
if len(points) == 1 {
|
||||||
drawDot(img, scalePoint(points[0], scaleX, scaleY), width, c)
|
drawDot(img, scalePoint(points[0], scale), width, c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i := 1; i < len(points); i++ {
|
for i := 1; i < len(points); i++ {
|
||||||
drawLine(img, scalePoint(points[i-1], scaleX, scaleY), scalePoint(points[i], scaleX, scaleY), width, c)
|
drawLine(img, scalePoint(points[i-1], scale), scalePoint(points[i], scale), width, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawRectOutline(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
func drawRectOutline(img *image.RGBA, points []Point, scale float64, width int, c color.RGBA) {
|
||||||
if len(points) < 2 {
|
if len(points) < 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
a := scalePoint(points[0], scaleX, scaleY)
|
a := scalePoint(points[0], scale)
|
||||||
b := scalePoint(points[len(points)-1], scaleX, scaleY)
|
b := scalePoint(points[len(points)-1], scale)
|
||||||
x1, x2 := ordered(a.X, b.X)
|
x1, x2 := ordered(a.X, b.X)
|
||||||
y1, y2 := ordered(a.Y, b.Y)
|
y1, y2 := ordered(a.Y, b.Y)
|
||||||
drawLine(img, Point{X: x1, Y: y1}, Point{X: x2, Y: y1}, width, c)
|
drawLine(img, Point{X: x1, Y: y1}, Point{X: x2, Y: y1}, width, c)
|
||||||
@@ -255,12 +106,12 @@ func drawRectOutline(img *image.RGBA, points []Point, scaleX, scaleY float64, wi
|
|||||||
drawLine(img, Point{X: x1, Y: y2}, Point{X: x1, Y: y1}, width, c)
|
drawLine(img, Point{X: x1, Y: y2}, Point{X: x1, Y: y1}, width, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawEllipseOutline(img *image.RGBA, points []Point, scaleX, scaleY float64, width int, c color.RGBA) {
|
func drawEllipseOutline(img *image.RGBA, points []Point, scale float64, width int, c color.RGBA) {
|
||||||
if len(points) < 2 {
|
if len(points) < 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
a := scalePoint(points[0], scaleX, scaleY)
|
a := scalePoint(points[0], scale)
|
||||||
b := scalePoint(points[len(points)-1], scaleX, scaleY)
|
b := scalePoint(points[len(points)-1], scale)
|
||||||
x1, x2 := ordered(a.X, b.X)
|
x1, x2 := ordered(a.X, b.X)
|
||||||
y1, y2 := ordered(a.Y, b.Y)
|
y1, y2 := ordered(a.Y, b.Y)
|
||||||
rx := (x2 - x1) / 2
|
rx := (x2 - x1) / 2
|
||||||
@@ -319,8 +170,8 @@ func drawDot(img *image.RGBA, p Point, width int, c color.RGBA) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func scalePoint(p Point, scaleX, scaleY float64) Point {
|
func scalePoint(p Point, scale float64) Point {
|
||||||
return Point{X: p.X * scaleX, Y: p.Y * scaleY}
|
return Point{X: p.X * scale, Y: p.Y * scale}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ordered(a, b float64) (float64, float64) {
|
func ordered(a, b float64) (float64, float64) {
|
||||||
@@ -329,94 +180,3 @@ func ordered(a, b float64) (float64, float64) {
|
|||||||
}
|
}
|
||||||
return b, a
|
return b, a
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawTextAnnotation(img *image.RGBA, ann Annotation, scaleX, scaleY float64, c color.RGBA) {
|
|
||||||
if len(ann.Points) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
text := strings.TrimSpace(ann.Text)
|
|
||||||
if text == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fontSize := ann.FontSize
|
|
||||||
if fontSize <= 0 {
|
|
||||||
fontSize = 20
|
|
||||||
}
|
|
||||||
face := annotationFontFace(fontSize * scaleY)
|
|
||||||
if face == nil {
|
|
||||||
face = basicfont.Face7x13
|
|
||||||
}
|
|
||||||
|
|
||||||
origin := scalePoint(ann.Points[0], scaleX, scaleY)
|
|
||||||
metrics := face.Metrics()
|
|
||||||
lineHeight := metrics.Height
|
|
||||||
if lineHeight <= 0 {
|
|
||||||
lineHeight = fixed.I(int(math.Ceil(fontSize * 1.2 * scaleY)))
|
|
||||||
}
|
|
||||||
d := &xfont.Drawer{
|
|
||||||
Dst: img,
|
|
||||||
Src: image.NewUniform(c),
|
|
||||||
Face: face,
|
|
||||||
}
|
|
||||||
baselineY := fixed.I(int(math.Round(origin.Y))) + metrics.Ascent
|
|
||||||
x := fixed.I(int(math.Round(origin.X)))
|
|
||||||
for _, line := range strings.Split(text, "\n") {
|
|
||||||
line = strings.TrimRight(line, "\r")
|
|
||||||
if line != "" {
|
|
||||||
d.Dot = fixed.Point26_6{X: x, Y: baselineY}
|
|
||||||
d.DrawString(line)
|
|
||||||
}
|
|
||||||
baselineY += lineHeight
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
annotationFontOnce sync.Once
|
|
||||||
annotationFont *opentype.Font
|
|
||||||
)
|
|
||||||
|
|
||||||
func annotationFontFace(size float64) xfont.Face {
|
|
||||||
if size <= 0 {
|
|
||||||
size = 20
|
|
||||||
}
|
|
||||||
annotationFontOnce.Do(func() {
|
|
||||||
annotationFont = loadAnnotationFont()
|
|
||||||
})
|
|
||||||
if annotationFont == nil {
|
|
||||||
return basicfont.Face7x13
|
|
||||||
}
|
|
||||||
face, err := opentype.NewFace(annotationFont, &opentype.FaceOptions{
|
|
||||||
Size: size,
|
|
||||||
DPI: 72,
|
|
||||||
Hinting: xfont.HintingFull,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return basicfont.Face7x13
|
|
||||||
}
|
|
||||||
return face
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadAnnotationFont() *opentype.Font {
|
|
||||||
paths := []string{
|
|
||||||
"/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
|
|
||||||
"/Library/Fonts/Arial Unicode.ttf",
|
|
||||||
"/System/Library/Fonts/Hiragino Sans GB.ttc",
|
|
||||||
"/System/Library/Fonts/PingFang.ttc",
|
|
||||||
"/System/Library/Fonts/Helvetica.ttc",
|
|
||||||
}
|
|
||||||
for _, path := range paths {
|
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if collection, err := opentype.ParseCollection(data); err == nil && collection.NumFonts() > 0 {
|
|
||||||
if font, err := collection.Font(0); err == nil {
|
|
||||||
return font
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if font, err := opentype.Parse(data); err == nil {
|
|
||||||
return font
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -51,230 +51,3 @@ func TestApplyAnnotationsNoAnnotationsReturnsOriginalBytes(t *testing.T) {
|
|||||||
t.Fatalf("expected original bytes")
|
t.Fatalf("expected original bytes")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApplyAnnotationsDrawsTextIntoPNG(t *testing.T) {
|
|
||||||
src := image.NewRGBA(image.Rect(0, 0, 120, 80))
|
|
||||||
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := png.Encode(&buf, src); err != nil {
|
|
||||||
t.Fatalf("encode source: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{
|
|
||||||
{
|
|
||||||
Tool: "text",
|
|
||||||
Color: "#ef4444",
|
|
||||||
Points: []Point{
|
|
||||||
{X: 10, Y: 10},
|
|
||||||
},
|
|
||||||
Text: "T",
|
|
||||||
},
|
|
||||||
}, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("apply annotations: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
img, err := png.Decode(bytes.NewReader(out))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("decode output: %v", err)
|
|
||||||
}
|
|
||||||
found := false
|
|
||||||
for y := 8; y < 40 && !found; y++ {
|
|
||||||
for x := 8; x < 40; x++ {
|
|
||||||
got := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
|
|
||||||
if got.R > 180 && got.G < 180 && got.B < 180 {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Fatalf("expected red text pixels near annotation point")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApplyAnnotationsWithScaleDrawsTextAtDevicePosition(t *testing.T) {
|
|
||||||
src := image.NewRGBA(image.Rect(0, 0, 220, 120))
|
|
||||||
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := png.Encode(&buf, src); err != nil {
|
|
||||||
t.Fatalf("encode source: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := ApplyAnnotationsWithScale(buf.Bytes(), []Annotation{
|
|
||||||
{
|
|
||||||
Tool: "text",
|
|
||||||
Color: "#ef4444",
|
|
||||||
Points: []Point{{X: 50, Y: 20}},
|
|
||||||
Text: "T",
|
|
||||||
FontSize: 20,
|
|
||||||
},
|
|
||||||
}, 2, 2)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("apply annotations: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
img, err := png.Decode(bytes.NewReader(out))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("decode output: %v", err)
|
|
||||||
}
|
|
||||||
minX := 999
|
|
||||||
found := false
|
|
||||||
for y := 0; y < img.Bounds().Dy(); y++ {
|
|
||||||
for x := 0; x < img.Bounds().Dx(); x++ {
|
|
||||||
got := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
|
|
||||||
if got.R > 180 && got.G < 180 && got.B < 180 {
|
|
||||||
found = true
|
|
||||||
if x < minX {
|
|
||||||
minX = x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Fatalf("expected red text pixels")
|
|
||||||
}
|
|
||||||
if minX < 90 {
|
|
||||||
t.Fatalf("expected text to be drawn near scaled x=100, min red x=%d", minX)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApplyAnnotationsUsesStrokeWidth(t *testing.T) {
|
|
||||||
src := image.NewRGBA(image.Rect(0, 0, 80, 80))
|
|
||||||
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := png.Encode(&buf, src); err != nil {
|
|
||||||
t.Fatalf("encode source: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{
|
|
||||||
{
|
|
||||||
Tool: "rect",
|
|
||||||
Color: "#ef4444",
|
|
||||||
Points: []Point{{X: 20, Y: 20}, {X: 60, Y: 60}},
|
|
||||||
StrokeWidth: 8,
|
|
||||||
},
|
|
||||||
}, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("apply annotations: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
img, err := png.Decode(bytes.NewReader(out))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("decode output: %v", err)
|
|
||||||
}
|
|
||||||
got := color.RGBAModel.Convert(img.At(20, 24)).(color.RGBA)
|
|
||||||
if got.R < 180 || got.G > 180 || got.B > 180 {
|
|
||||||
t.Fatalf("expected thick rectangle stroke to cover y=24, got %#v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApplyAnnotationsPixelatesMosaicRectangle(t *testing.T) {
|
|
||||||
src := image.NewRGBA(image.Rect(0, 0, 40, 30))
|
|
||||||
for y := 0; y < 30; y++ {
|
|
||||||
for x := 0; x < 40; x++ {
|
|
||||||
src.SetRGBA(x, y, color.RGBA{R: uint8(x * 6), G: uint8(y * 8), B: uint8((x + y) * 3), A: 255})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := png.Encode(&buf, src); err != nil {
|
|
||||||
t.Fatalf("encode source: %v", err)
|
|
||||||
}
|
|
||||||
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{{
|
|
||||||
Tool: "mosaic-rect", Points: []Point{{X: 10, Y: 5}, {X: 30, Y: 25}},
|
|
||||||
}}, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("apply annotations: %v", err)
|
|
||||||
}
|
|
||||||
img, err := png.Decode(bytes.NewReader(out))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("decode output: %v", err)
|
|
||||||
}
|
|
||||||
if img.At(12, 7) != img.At(18, 13) {
|
|
||||||
t.Fatalf("expected pixels in one mosaic block to share a color")
|
|
||||||
}
|
|
||||||
if img.At(5, 5) != src.At(5, 5) {
|
|
||||||
t.Fatalf("expected pixels outside mosaic rectangle to remain unchanged")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApplyAnnotationsPixelatesOnlyMosaicBrushStroke(t *testing.T) {
|
|
||||||
src := image.NewRGBA(image.Rect(0, 0, 50, 30))
|
|
||||||
for y := 0; y < 30; y++ {
|
|
||||||
for x := 0; x < 50; x++ {
|
|
||||||
src.SetRGBA(x, y, color.RGBA{R: uint8(x * 5), G: uint8(y * 8), A: 255})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := png.Encode(&buf, src); err != nil {
|
|
||||||
t.Fatalf("encode source: %v", err)
|
|
||||||
}
|
|
||||||
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{{
|
|
||||||
Tool: "mosaic-brush", StrokeWidth: 12, Points: []Point{{X: 5, Y: 15}, {X: 45, Y: 15}},
|
|
||||||
}}, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("apply annotations: %v", err)
|
|
||||||
}
|
|
||||||
img, err := png.Decode(bytes.NewReader(out))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("decode output: %v", err)
|
|
||||||
}
|
|
||||||
if img.At(12, 15) == src.At(12, 15) {
|
|
||||||
t.Fatalf("expected brush path to be pixelated")
|
|
||||||
}
|
|
||||||
if img.At(12, 2) != src.At(12, 2) {
|
|
||||||
t.Fatalf("expected pixels outside brush path to remain unchanged")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApplyAnnotationsDrawsStraightLine(t *testing.T) {
|
|
||||||
src := image.NewRGBA(image.Rect(0, 0, 60, 40))
|
|
||||||
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := png.Encode(&buf, src); err != nil {
|
|
||||||
t.Fatalf("encode source: %v", err)
|
|
||||||
}
|
|
||||||
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{{
|
|
||||||
Tool: "line", Color: "#ef4444", StrokeWidth: 4,
|
|
||||||
Points: []Point{{X: 5, Y: 20}, {X: 50, Y: 20}},
|
|
||||||
}}, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("apply annotations: %v", err)
|
|
||||||
}
|
|
||||||
img, err := png.Decode(bytes.NewReader(out))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("decode output: %v", err)
|
|
||||||
}
|
|
||||||
got := color.RGBAModel.Convert(img.At(30, 20)).(color.RGBA)
|
|
||||||
if got.R < 180 || got.G > 180 || got.B > 180 {
|
|
||||||
t.Fatalf("expected red line at center, got %#v", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestApplyAnnotationsDrawsArrowHead(t *testing.T) {
|
|
||||||
src := image.NewRGBA(image.Rect(0, 0, 80, 60))
|
|
||||||
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := png.Encode(&buf, src); err != nil {
|
|
||||||
t.Fatalf("encode source: %v", err)
|
|
||||||
}
|
|
||||||
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{{
|
|
||||||
Tool: "arrow", Color: "#3b82f6", StrokeWidth: 3,
|
|
||||||
Points: []Point{{X: 10, Y: 30}, {X: 60, Y: 30}},
|
|
||||||
}}, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("apply annotations: %v", err)
|
|
||||||
}
|
|
||||||
img, err := png.Decode(bytes.NewReader(out))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("decode output: %v", err)
|
|
||||||
}
|
|
||||||
shaft := color.RGBAModel.Convert(img.At(35, 30)).(color.RGBA)
|
|
||||||
head := color.RGBAModel.Convert(img.At(52, 25)).(color.RGBA)
|
|
||||||
if shaft.B < 180 || shaft.R > 140 || head.B < 180 || head.R > 140 {
|
|
||||||
t.Fatalf("expected blue arrow shaft and head, got shaft=%#v head=%#v", shaft, head)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -61,14 +61,6 @@ func (s *CaptureActionsService) SaveImage(_ context.Context, pngBytes []byte, di
|
|||||||
s.notifyFailure("save failed: " + err.Error())
|
s.notifyFailure("save failed: " + err.Error())
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// Copy the saved path to the clipboard so the user can paste it right
|
|
||||||
// away, mirroring how the upload/summary flows copy their result. A
|
|
||||||
// clipboard failure must not fail the save itself.
|
|
||||||
if s.Clipboard != nil {
|
|
||||||
if err := s.Clipboard.WriteText(path); err != nil {
|
|
||||||
s.notifyFailure("clipboard write failed: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if s.Notifier != nil {
|
if s.Notifier != nil {
|
||||||
s.Notifier.NotifySuccess(path)
|
s.Notifier.NotifySuccess(path)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,145 +0,0 @@
|
|||||||
// capture_ftp.go — application-level FTP/SFTP upload orchestration.
|
|
||||||
//
|
|
||||||
// Design rationale:
|
|
||||||
// - FTP/SFTP returns a remote filesystem path, not necessarily a public URL,
|
|
||||||
// so this flow stays separate from the OSSProvider-based S3 pipeline.
|
|
||||||
// - A tiny uploader interface keeps protocol libraries out of the
|
|
||||||
// application layer and makes the upload/copy/notify sequence unit-testable.
|
|
||||||
// - The dated filename layout is shared with SSH/SCP while FTP-specific path
|
|
||||||
// validation prevents an invalid prefix from escaping the login root.
|
|
||||||
package application
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ftpSvcLog returns an application-layer FTP logger bound to the current
|
|
||||||
// default slog handler.
|
|
||||||
func ftpSvcLog() *slog.Logger { return slog.Default().With("component", "application.ftp") }
|
|
||||||
|
|
||||||
// FTPUploader abstracts both FTP and SFTP adapters.
|
|
||||||
type FTPUploader interface {
|
|
||||||
// Upload stores data at remoteRelPath, relative to the authenticated
|
|
||||||
// account's login root.
|
|
||||||
Upload(ctx context.Context, remoteRelPath string, data []byte) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureAndFTPService wires captured PNG bytes → FTP/SFTP → clipboard →
|
|
||||||
// notification.
|
|
||||||
type CaptureAndFTPService struct {
|
|
||||||
Uploader FTPUploader
|
|
||||||
Clipboard clipboard.Writer
|
|
||||||
Notifier Notifier
|
|
||||||
Cfg domain.FTPConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
// ExecuteWithBytes uploads previously captured PNG bytes and copies the exact
|
|
||||||
// remote path semantics shown in Settings: FTP paths are rooted at "/", while
|
|
||||||
// SFTP paths are relative to the authenticated user's home directory.
|
|
||||||
func (s *CaptureAndFTPService) ExecuteWithBytes(ctx context.Context, pngBytes []byte) error {
|
|
||||||
if s.Uploader == nil {
|
|
||||||
s.notifyFailure("FTP/SFTP not configured")
|
|
||||||
return fmt.Errorf("ftp uploader is nil")
|
|
||||||
}
|
|
||||||
if len(pngBytes) == 0 {
|
|
||||||
s.notifyFailure("empty screenshot")
|
|
||||||
return fmt.Errorf("empty screenshot")
|
|
||||||
}
|
|
||||||
|
|
||||||
relPath, err := buildFTPRemotePath(s.Cfg.PathPrefix)
|
|
||||||
if err != nil {
|
|
||||||
s.notifyFailure(err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
protocol := normalizedFTPProtocol(s.Cfg.Protocol)
|
|
||||||
start := time.Now()
|
|
||||||
ftpSvcLog().Info("file-transfer pipeline start",
|
|
||||||
"protocol", protocol,
|
|
||||||
"host", s.Cfg.Host,
|
|
||||||
"user", s.Cfg.User,
|
|
||||||
"port", s.Cfg.Port,
|
|
||||||
"size", len(pngBytes),
|
|
||||||
"remote_path", relPath,
|
|
||||||
"strict_host_key", s.Cfg.StrictHostKey)
|
|
||||||
|
|
||||||
if err := s.Uploader.Upload(ctx, relPath, pngBytes); err != nil {
|
|
||||||
ftpSvcLog().Error("file-transfer upload failed",
|
|
||||||
"protocol", protocol,
|
|
||||||
"remote_path", relPath,
|
|
||||||
"elapsed", time.Since(start),
|
|
||||||
"err", err)
|
|
||||||
s.notifyFailure(strings.ToUpper(protocol) + " upload failed: " + err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
clipText := ftpShareText(protocol, relPath)
|
|
||||||
if s.Clipboard != nil {
|
|
||||||
if err := s.Clipboard.WriteText(clipText); err != nil {
|
|
||||||
s.notifyFailure("clipboard write failed: " + err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if s.Notifier != nil {
|
|
||||||
s.Notifier.NotifySuccess(clipText)
|
|
||||||
}
|
|
||||||
ftpSvcLog().Info("file-transfer pipeline done",
|
|
||||||
"protocol", protocol,
|
|
||||||
"remote_path", relPath,
|
|
||||||
"total_elapsed", time.Since(start))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildFTPRemotePath validates the user-controlled prefix before reusing the
|
|
||||||
// existing dated remote-path generator shared with SSH/SCP.
|
|
||||||
func buildFTPRemotePath(prefix string) (string, error) {
|
|
||||||
if err := validateFTPPathPrefix(prefix); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
prefix = strings.TrimSpace(prefix)
|
|
||||||
prefix = strings.TrimPrefix(prefix, "~")
|
|
||||||
prefix = strings.TrimLeft(prefix, "/")
|
|
||||||
return buildRemoteRelPath(prefix), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateFTPPathPrefix(prefix string) error {
|
|
||||||
if strings.ContainsAny(prefix, "\x00\r\n\\") {
|
|
||||||
return fmt.Errorf("FTP/SFTP path contains an invalid character")
|
|
||||||
}
|
|
||||||
prefix = strings.TrimSpace(prefix)
|
|
||||||
prefix = strings.TrimPrefix(prefix, "~")
|
|
||||||
prefix = strings.TrimLeft(prefix, "/")
|
|
||||||
for _, segment := range strings.Split(prefix, "/") {
|
|
||||||
if segment == ".." {
|
|
||||||
return fmt.Errorf("FTP/SFTP path must stay inside the login directory")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizedFTPProtocol(protocol string) string {
|
|
||||||
if protocol == domain.FTPProtocolSFTP {
|
|
||||||
return domain.FTPProtocolSFTP
|
|
||||||
}
|
|
||||||
return domain.FTPProtocolFTP
|
|
||||||
}
|
|
||||||
|
|
||||||
func ftpShareText(protocol, relPath string) string {
|
|
||||||
if protocol == domain.FTPProtocolSFTP {
|
|
||||||
return "~/" + relPath
|
|
||||||
}
|
|
||||||
return "/" + relPath
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *CaptureAndFTPService) notifyFailure(reason string) {
|
|
||||||
if s.Notifier != nil {
|
|
||||||
s.Notifier.NotifyFailure(reason)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
package application
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
type fakeFTPUploader struct {
|
|
||||||
path string
|
|
||||||
data []byte
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeFTPUploader) Upload(_ context.Context, remoteRelPath string, data []byte) error {
|
|
||||||
f.path = remoteRelPath
|
|
||||||
f.data = append([]byte(nil), data...)
|
|
||||||
return f.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureAndFTPServiceUploadsAndCopiesFTPPath(t *testing.T) {
|
|
||||||
uploader := &fakeFTPUploader{}
|
|
||||||
clip := &fakeClipboard{}
|
|
||||||
notifier := &fakeNotifier{}
|
|
||||||
svc := &CaptureAndFTPService{
|
|
||||||
Uploader: uploader,
|
|
||||||
Clipboard: clip,
|
|
||||||
Notifier: notifier,
|
|
||||||
Cfg: domain.FTPConfig{
|
|
||||||
Protocol: domain.FTPProtocolFTP,
|
|
||||||
PathPrefix: "snapgo/",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := svc.ExecuteWithBytes(context.Background(), []byte("png")); err != nil {
|
|
||||||
t.Fatalf("upload FTP screenshot: %v", err)
|
|
||||||
}
|
|
||||||
if string(uploader.data) != "png" {
|
|
||||||
t.Fatalf("expected PNG bytes, got %q", string(uploader.data))
|
|
||||||
}
|
|
||||||
if !strings.HasPrefix(uploader.path, "snapgo/") || !strings.HasSuffix(uploader.path, ".png") {
|
|
||||||
t.Fatalf("unexpected remote path %q", uploader.path)
|
|
||||||
}
|
|
||||||
if clip.text != "/"+uploader.path {
|
|
||||||
t.Fatalf("expected FTP root path copied, got %q", clip.text)
|
|
||||||
}
|
|
||||||
if notifier.success != clip.text {
|
|
||||||
t.Fatalf("expected success notification %q, got %q", clip.text, notifier.success)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureAndFTPServiceCopiesSFTPHomePath(t *testing.T) {
|
|
||||||
uploader := &fakeFTPUploader{}
|
|
||||||
clip := &fakeClipboard{}
|
|
||||||
svc := &CaptureAndFTPService{
|
|
||||||
Uploader: uploader,
|
|
||||||
Clipboard: clip,
|
|
||||||
Cfg: domain.FTPConfig{
|
|
||||||
Protocol: domain.FTPProtocolSFTP,
|
|
||||||
PathPrefix: "images",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := svc.ExecuteWithBytes(context.Background(), []byte("png")); err != nil {
|
|
||||||
t.Fatalf("upload SFTP screenshot: %v", err)
|
|
||||||
}
|
|
||||||
if clip.text != "~/"+uploader.path {
|
|
||||||
t.Fatalf("expected SFTP home path copied, got %q", clip.text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureAndFTPServiceRejectsTraversalBeforeUpload(t *testing.T) {
|
|
||||||
uploader := &fakeFTPUploader{}
|
|
||||||
notifier := &fakeNotifier{}
|
|
||||||
svc := &CaptureAndFTPService{
|
|
||||||
Uploader: uploader,
|
|
||||||
Notifier: notifier,
|
|
||||||
Cfg: domain.FTPConfig{
|
|
||||||
Protocol: domain.FTPProtocolSFTP,
|
|
||||||
PathPrefix: "../../outside",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := svc.ExecuteWithBytes(context.Background(), []byte("png"))
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected traversal prefix to fail")
|
|
||||||
}
|
|
||||||
if uploader.path != "" {
|
|
||||||
t.Fatalf("uploader should not run, got path %q", uploader.path)
|
|
||||||
}
|
|
||||||
if notifier.failure == "" {
|
|
||||||
t.Fatal("expected failure notification")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureAndFTPServiceSurfacesUploadFailure(t *testing.T) {
|
|
||||||
want := errors.New("server unavailable")
|
|
||||||
uploader := &fakeFTPUploader{err: want}
|
|
||||||
clip := &fakeClipboard{}
|
|
||||||
notifier := &fakeNotifier{}
|
|
||||||
svc := &CaptureAndFTPService{
|
|
||||||
Uploader: uploader,
|
|
||||||
Clipboard: clip,
|
|
||||||
Notifier: notifier,
|
|
||||||
Cfg: domain.FTPConfig{Protocol: domain.FTPProtocolFTP},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := svc.ExecuteWithBytes(context.Background(), []byte("png"))
|
|
||||||
if !errors.Is(err, want) {
|
|
||||||
t.Fatalf("expected %v, got %v", want, err)
|
|
||||||
}
|
|
||||||
if clip.text != "" {
|
|
||||||
t.Fatalf("clipboard should remain empty, got %q", clip.text)
|
|
||||||
}
|
|
||||||
if !strings.Contains(notifier.failure, "server unavailable") {
|
|
||||||
t.Fatalf("unexpected failure notification %q", notifier.failure)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBuildFTPRemotePathNormalizesLoginRootMarkers(t *testing.T) {
|
|
||||||
remotePath, err := buildFTPRemotePath(" ~////snapgo/ ")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("build remote path: %v", err)
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(remotePath, "/") || !strings.HasPrefix(remotePath, "snapgo/") {
|
|
||||||
t.Fatalf("expected relative snapgo path, got %q", remotePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
package application
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
|
|
||||||
)
|
|
||||||
|
|
||||||
// OCRRecognizer describes a provider capable of extracting text from a PNG
|
|
||||||
// screenshot.
|
|
||||||
type OCRRecognizer interface {
|
|
||||||
RecognizeText(ctx context.Context, pngBytes []byte) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureOCRService wires screenshot bytes -> OCR provider -> clipboard. It
|
|
||||||
// leaves progress reporting to the caller so native and web overlays can share
|
|
||||||
// the same status HUD.
|
|
||||||
type CaptureOCRService struct {
|
|
||||||
Recognizer OCRRecognizer
|
|
||||||
Clipboard clipboard.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recognize asks the configured OCR provider to extract text from the PNG.
|
|
||||||
func (s *CaptureOCRService) Recognize(ctx context.Context, pngBytes []byte) (string, error) {
|
|
||||||
if s.Recognizer == nil {
|
|
||||||
return "", fmt.Errorf("ocr is not configured")
|
|
||||||
}
|
|
||||||
if len(pngBytes) == 0 {
|
|
||||||
return "", fmt.Errorf("empty screenshot")
|
|
||||||
}
|
|
||||||
text, err := s.Recognizer.RecognizeText(ctx, pngBytes)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
text = strings.TrimSpace(text)
|
|
||||||
if text == "" {
|
|
||||||
return "", fmt.Errorf("ocr result is empty")
|
|
||||||
}
|
|
||||||
return text, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CopyText writes the extracted text to the clipboard.
|
|
||||||
func (s *CaptureOCRService) CopyText(_ context.Context, text string) error {
|
|
||||||
text = strings.TrimSpace(text)
|
|
||||||
if text == "" {
|
|
||||||
return fmt.Errorf("empty ocr text")
|
|
||||||
}
|
|
||||||
if s.Clipboard == nil {
|
|
||||||
return fmt.Errorf("clipboard is not configured")
|
|
||||||
}
|
|
||||||
if err := s.Clipboard.WriteText(text); err != nil {
|
|
||||||
return fmt.Errorf("clipboard write failed: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package application
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type fakeOCRRecognizer struct {
|
|
||||||
image []byte
|
|
||||||
text string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeOCRRecognizer) RecognizeText(_ context.Context, pngBytes []byte) (string, error) {
|
|
||||||
f.image = append([]byte(nil), pngBytes...)
|
|
||||||
if f.text == "" {
|
|
||||||
return "extracted text", nil
|
|
||||||
}
|
|
||||||
return f.text, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureOCRServiceRecognizesAndCopies(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
recognizer := &fakeOCRRecognizer{}
|
|
||||||
clip := &fakeClipboard{}
|
|
||||||
svc := &CaptureOCRService{Recognizer: recognizer, Clipboard: clip}
|
|
||||||
|
|
||||||
text, err := svc.Recognize(ctx, []byte("png"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("recognize: %v", err)
|
|
||||||
}
|
|
||||||
if string(recognizer.image) != "png" {
|
|
||||||
t.Fatalf("expected screenshot bytes forwarded, got %q", string(recognizer.image))
|
|
||||||
}
|
|
||||||
if text != "extracted text" {
|
|
||||||
t.Fatalf("expected extracted text, got %q", text)
|
|
||||||
}
|
|
||||||
if err := svc.CopyText(ctx, text); err != nil {
|
|
||||||
t.Fatalf("copy text: %v", err)
|
|
||||||
}
|
|
||||||
if clip.text != "extracted text" {
|
|
||||||
t.Fatalf("expected ocr text copied, got %q", clip.text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureOCRServiceRejectsEmptyResult(t *testing.T) {
|
|
||||||
svc := &CaptureOCRService{Recognizer: &fakeOCRRecognizer{text: " \n "}}
|
|
||||||
if _, err := svc.Recognize(context.Background(), []byte("png")); err == nil {
|
|
||||||
t.Fatalf("expected empty OCR result to fail")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -69,6 +69,12 @@ func (s *CaptureAndSSHService) ExecuteWithBytes(ctx context.Context, pngBytes []
|
|||||||
return fmt.Errorf("empty screenshot")
|
return fmt.Errorf("empty screenshot")
|
||||||
}
|
}
|
||||||
relPath := buildRemoteRelPath(s.Cfg.PathPrefix)
|
relPath := buildRemoteRelPath(s.Cfg.PathPrefix)
|
||||||
|
// bgo cannot create remote directories, so it uploads flat into the
|
||||||
|
// remote $HOME with a timestamped filename rather than a date-grouped
|
||||||
|
// subdirectory tree (see BgoUploader for the rationale).
|
||||||
|
if s.Cfg.IsBgo() {
|
||||||
|
relPath = buildRemoteFlatName()
|
||||||
|
}
|
||||||
sshSvcLog().Info("save-remote pipeline start",
|
sshSvcLog().Info("save-remote pipeline start",
|
||||||
"host", s.Cfg.Host, "user", s.Cfg.User, "port", s.Cfg.Port,
|
"host", s.Cfg.Host, "user", s.Cfg.User, "port", s.Cfg.Port,
|
||||||
"size", len(pngBytes), "remote_path", relPath,
|
"size", len(pngBytes), "remote_path", relPath,
|
||||||
@@ -128,6 +134,17 @@ func buildRemoteRelPath(prefix string) string {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// buildRemoteFlatName produces a flat, timestamped filename (no directory
|
||||||
|
// components) for destinations that cannot create remote directories, such
|
||||||
|
// as the bgo client. The file lands directly in the remote $HOME.
|
||||||
|
func buildRemoteFlatName() string {
|
||||||
|
now := time.Now()
|
||||||
|
return fmt.Sprintf("%s-%s.png",
|
||||||
|
now.Format("20060102-150405"),
|
||||||
|
randomSuffix(6),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *CaptureAndSSHService) notifyFailure(reason string) {
|
func (s *CaptureAndSSHService) notifyFailure(reason string) {
|
||||||
if s.Notifier != nil {
|
if s.Notifier != nil {
|
||||||
s.Notifier.NotifyFailure(reason)
|
s.Notifier.NotifyFailure(reason)
|
||||||
|
|||||||
@@ -1,158 +0,0 @@
|
|||||||
package application
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
|
|
||||||
)
|
|
||||||
|
|
||||||
// VisionSummarizer describes a multimodal model capable of reading an image
|
|
||||||
// reference (either a public https URL or an inline base64 data URL) and
|
|
||||||
// returning a textual summary.
|
|
||||||
type VisionSummarizer interface {
|
|
||||||
SummarizeImage(ctx context.Context, prompt string, imageURL string) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ObjectDeleter is an optional capability an OSSProvider may implement so the
|
|
||||||
// summary pipeline can delete the temporary screenshot it uploaded purely so a
|
|
||||||
// remote model could fetch it. It is kept separate from domain.OSSProvider to
|
|
||||||
// avoid forcing every provider (or test fake) to implement deletion.
|
|
||||||
type ObjectDeleter interface {
|
|
||||||
Delete(ctx context.Context, key string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureSummaryService wires screenshot bytes -> multimodal LLM summary ->
|
|
||||||
// clipboard. It prefers sending the image inline as a base64 data URL and only
|
|
||||||
// falls back to an S3 upload (public URL) when the payload exceeds the
|
|
||||||
// provider's inline size limit. It intentionally does not notify UI state
|
|
||||||
// itself: the caller emits fine-grained "uploading / recognizing / done"
|
|
||||||
// progress.
|
|
||||||
type CaptureSummaryService struct {
|
|
||||||
Provider domain.OSSProvider
|
|
||||||
Summarizer VisionSummarizer
|
|
||||||
Clipboard clipboard.Writer
|
|
||||||
Prompt string
|
|
||||||
PathPrefix string
|
|
||||||
// MaxInlineBytes caps the PNG size eligible for inline base64. Payloads
|
|
||||||
// larger than this must go through S3. A value <= 0 disables the inline
|
|
||||||
// path entirely (always upload).
|
|
||||||
MaxInlineBytes int
|
|
||||||
// HTTPClient is used for the public-URL reachability probe. Defaults to a
|
|
||||||
// short-timeout client when nil.
|
|
||||||
HTTPClient *http.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
// CanInline reports whether the screenshot is small enough to be embedded
|
|
||||||
// directly in the request as a base64 data URL.
|
|
||||||
func (s *CaptureSummaryService) CanInline(pngBytes []byte) bool {
|
|
||||||
return s.MaxInlineBytes > 0 && len(pngBytes) <= s.MaxInlineBytes
|
|
||||||
}
|
|
||||||
|
|
||||||
// InlineDataURL encodes the PNG as an RFC 2397 data URL suitable for the
|
|
||||||
// image_url field of an OpenAI-compatible request.
|
|
||||||
func (s *CaptureSummaryService) InlineDataURL(pngBytes []byte) string {
|
|
||||||
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UploadImage uploads the screenshot and returns the public URL visible to
|
|
||||||
// the multimodal provider. Used only when the image is too large to inline.
|
|
||||||
func (s *CaptureSummaryService) UploadImage(ctx context.Context, pngBytes []byte) (*domain.UploadResult, error) {
|
|
||||||
if s.Provider == nil {
|
|
||||||
return nil, fmt.Errorf("s3 is not configured")
|
|
||||||
}
|
|
||||||
if len(pngBytes) == 0 {
|
|
||||||
return nil, fmt.Errorf("empty screenshot")
|
|
||||||
}
|
|
||||||
key := buildObjectKey(s.PathPrefix)
|
|
||||||
start := time.Now()
|
|
||||||
url, err := s.Provider.Upload(ctx, key, pngBytes, "image/png")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("s3 put object: %w", err)
|
|
||||||
}
|
|
||||||
return &domain.UploadResult{
|
|
||||||
URL: url,
|
|
||||||
Key: key,
|
|
||||||
Provider: s.Provider.Name(),
|
|
||||||
Elapsed: time.Since(start),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnsureReachable verifies the public URL can actually be fetched, so we can
|
|
||||||
// surface a clear "link not reachable" message instead of an opaque provider
|
|
||||||
// error when the bucket is private or fronted by an inaccessible CDN.
|
|
||||||
func (s *CaptureSummaryService) EnsureReachable(ctx context.Context, imageURL string) error {
|
|
||||||
client := s.HTTPClient
|
|
||||||
if client == nil {
|
|
||||||
client = &http.Client{Timeout: 10 * time.Second}
|
|
||||||
}
|
|
||||||
probe := func(method string) (int, error) {
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, imageURL, nil)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
resp.Body.Close()
|
|
||||||
return resp.StatusCode, nil
|
|
||||||
}
|
|
||||||
status, err := probe(http.MethodHead)
|
|
||||||
// Some S3-compatible endpoints reject HEAD; retry with GET before failing.
|
|
||||||
if err != nil || status == http.StatusMethodNotAllowed || status == http.StatusForbidden {
|
|
||||||
if s, gerr := probe(http.MethodGet); gerr == nil {
|
|
||||||
status, err = s, nil
|
|
||||||
} else if err == nil {
|
|
||||||
err = gerr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("图片链接不可达(模型无法读取上传的截图):%w", err)
|
|
||||||
}
|
|
||||||
if status < 200 || status >= 300 {
|
|
||||||
return fmt.Errorf("图片链接不可达:HTTP %d,请确认对象存储 bucket 公网可读或已正确配置 PublicURLBase", status)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteUploaded removes the temporary object from S3 if the provider supports
|
|
||||||
// deletion. It is best-effort: a nil return means either success or that the
|
|
||||||
// provider cannot delete.
|
|
||||||
func (s *CaptureSummaryService) DeleteUploaded(ctx context.Context, key string) error {
|
|
||||||
if key == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
deleter, ok := s.Provider.(ObjectDeleter)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return deleter.Delete(ctx, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Summarize asks the configured model to summarize the image reference, which
|
|
||||||
// may be a public URL or an inline base64 data URL.
|
|
||||||
func (s *CaptureSummaryService) Summarize(ctx context.Context, imageURL string) (string, error) {
|
|
||||||
if s.Summarizer == nil {
|
|
||||||
return "", fmt.Errorf("llm is not configured")
|
|
||||||
}
|
|
||||||
return s.Summarizer.SummarizeImage(ctx, s.Prompt, imageURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CopySummary writes the final summary text to the clipboard.
|
|
||||||
func (s *CaptureSummaryService) CopySummary(_ context.Context, summary string) error {
|
|
||||||
if summary == "" {
|
|
||||||
return fmt.Errorf("empty summary")
|
|
||||||
}
|
|
||||||
if s.Clipboard == nil {
|
|
||||||
return fmt.Errorf("clipboard is not configured")
|
|
||||||
}
|
|
||||||
if err := s.Clipboard.WriteText(summary); err != nil {
|
|
||||||
return fmt.Errorf("clipboard write failed: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
package application
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type fakeOSSProvider struct {
|
|
||||||
key string
|
|
||||||
data []byte
|
|
||||||
contentType string
|
|
||||||
url string
|
|
||||||
deletedKey string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeOSSProvider) Upload(_ context.Context, key string, data []byte, contentType string) (string, error) {
|
|
||||||
f.key = key
|
|
||||||
f.data = append([]byte(nil), data...)
|
|
||||||
f.contentType = contentType
|
|
||||||
if f.url == "" {
|
|
||||||
return "https://cdn.example.com/" + key, nil
|
|
||||||
}
|
|
||||||
return f.url, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeOSSProvider) Name() string { return "fake" }
|
|
||||||
|
|
||||||
func (f *fakeOSSProvider) Delete(_ context.Context, key string) error {
|
|
||||||
f.deletedKey = key
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeSummarizer struct {
|
|
||||||
prompt string
|
|
||||||
url string
|
|
||||||
text string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeSummarizer) SummarizeImage(_ context.Context, prompt string, imageURL string) (string, error) {
|
|
||||||
f.prompt = prompt
|
|
||||||
f.url = imageURL
|
|
||||||
if f.text == "" {
|
|
||||||
return "summary text", nil
|
|
||||||
}
|
|
||||||
return f.text, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureSummaryServiceUploadsSummarizesAndCopies(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
provider := &fakeOSSProvider{}
|
|
||||||
summarizer := &fakeSummarizer{}
|
|
||||||
clip := &fakeClipboard{}
|
|
||||||
svc := &CaptureSummaryService{
|
|
||||||
Provider: provider,
|
|
||||||
Summarizer: summarizer,
|
|
||||||
Clipboard: clip,
|
|
||||||
Prompt: "describe screenshot",
|
|
||||||
PathPrefix: "snapgo/",
|
|
||||||
}
|
|
||||||
|
|
||||||
uploaded, err := svc.UploadImage(ctx, []byte("png"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("upload image: %v", err)
|
|
||||||
}
|
|
||||||
if provider.contentType != "image/png" {
|
|
||||||
t.Fatalf("expected image/png content type, got %q", provider.contentType)
|
|
||||||
}
|
|
||||||
if string(provider.data) != "png" {
|
|
||||||
t.Fatalf("expected uploaded png bytes, got %q", string(provider.data))
|
|
||||||
}
|
|
||||||
|
|
||||||
summary, err := svc.Summarize(ctx, uploaded.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("summarize: %v", err)
|
|
||||||
}
|
|
||||||
if summarizer.prompt != "describe screenshot" {
|
|
||||||
t.Fatalf("expected prompt forwarded, got %q", summarizer.prompt)
|
|
||||||
}
|
|
||||||
if summarizer.url != uploaded.URL {
|
|
||||||
t.Fatalf("expected uploaded url forwarded, got %q", summarizer.url)
|
|
||||||
}
|
|
||||||
if summary != "summary text" {
|
|
||||||
t.Fatalf("expected summary text, got %q", summary)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := svc.CopySummary(ctx, summary); err != nil {
|
|
||||||
t.Fatalf("copy summary: %v", err)
|
|
||||||
}
|
|
||||||
if clip.text != "summary text" {
|
|
||||||
t.Fatalf("expected summary copied, got %q", clip.text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureSummaryServiceInlineDataURL(t *testing.T) {
|
|
||||||
svc := &CaptureSummaryService{MaxInlineBytes: 10}
|
|
||||||
|
|
||||||
if !svc.CanInline([]byte("small")) {
|
|
||||||
t.Fatalf("expected 5-byte payload to be inlinable under 10-byte cap")
|
|
||||||
}
|
|
||||||
if svc.CanInline([]byte("this is definitely too big")) {
|
|
||||||
t.Fatalf("expected oversized payload to be rejected for inlining")
|
|
||||||
}
|
|
||||||
|
|
||||||
url := svc.InlineDataURL([]byte("png"))
|
|
||||||
if !strings.HasPrefix(url, "data:image/png;base64,") {
|
|
||||||
t.Fatalf("expected base64 data URL prefix, got %q", url)
|
|
||||||
}
|
|
||||||
if strings.Contains(url, "https://") {
|
|
||||||
t.Fatalf("inline URL must not be a remote URL, got %q", url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureSummaryServiceDeleteUploadedUsesDeleter(t *testing.T) {
|
|
||||||
provider := &fakeOSSProvider{}
|
|
||||||
svc := &CaptureSummaryService{Provider: provider}
|
|
||||||
|
|
||||||
if err := svc.DeleteUploaded(context.Background(), "snapgo/a.png"); err != nil {
|
|
||||||
t.Fatalf("delete uploaded: %v", err)
|
|
||||||
}
|
|
||||||
if provider.deletedKey != "snapgo/a.png" {
|
|
||||||
t.Fatalf("expected object deleted, got %q", provider.deletedKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCaptureSummaryServiceDeleteUploadedNoDeleterIsNoop(t *testing.T) {
|
|
||||||
// A provider without Delete must not error when cleanup is requested.
|
|
||||||
svc := &CaptureSummaryService{Provider: nonDeletingProvider{}}
|
|
||||||
if err := svc.DeleteUploaded(context.Background(), "snapgo/a.png"); err != nil {
|
|
||||||
t.Fatalf("expected no-op delete, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type nonDeletingProvider struct{}
|
|
||||||
|
|
||||||
func (nonDeletingProvider) Upload(_ context.Context, _ string, _ []byte, _ string) (string, error) {
|
|
||||||
return "https://cdn.example.com/x.png", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (nonDeletingProvider) Name() string { return "no-delete" }
|
|
||||||
+20
-390
@@ -1,8 +1,8 @@
|
|||||||
// Package domain — configuration types.
|
// Package domain — configuration types.
|
||||||
//
|
//
|
||||||
// S3Config, SSHConfig, and FTPConfig are intentionally split into their own
|
// S3Config and SSHConfig are intentionally split into their own structs so
|
||||||
// structs so future providers can introduce configuration types side-by-side
|
// that future providers can introduce their own configuration types side-
|
||||||
// without polluting the core domain types file.
|
// by-side without polluting the core domain types file.
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
// S3Config describes the connection parameters for any S3-compatible
|
// S3Config describes the connection parameters for any S3-compatible
|
||||||
@@ -42,6 +42,11 @@ type S3Config struct {
|
|||||||
// `kinit` is reused. Native GSSAPI is required here
|
// `kinit` is reused. Native GSSAPI is required here
|
||||||
// because macOS stores tickets in an API: ccache
|
// because macOS stores tickets in an API: ccache
|
||||||
// that pure-Go SSH libraries cannot read.
|
// that pure-Go SSH libraries cannot read.
|
||||||
|
// "bgo" → delegate to the internal `bgo scp` client which
|
||||||
|
// handles its own auth/credential management. bgo
|
||||||
|
// must run under a PTY and cannot create remote
|
||||||
|
// directories, so this mode uploads to the user's
|
||||||
|
// remote $HOME with a flat, timestamped filename.
|
||||||
// "" / "builtin" → legacy combined behaviour (password → agent → key
|
// "" / "builtin" → legacy combined behaviour (password → agent → key
|
||||||
// files) kept only for backward compatibility with
|
// files) kept only for backward compatibility with
|
||||||
// configs written before the method was split.
|
// configs written before the method was split.
|
||||||
@@ -72,90 +77,6 @@ type SSHConfig struct {
|
|||||||
ConnectTimeoutSecs int `json:"connectTimeoutSecs"`
|
ConnectTimeoutSecs int `json:"connectTimeoutSecs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// FTPConfig describes a file-transfer destination reached through FTP or
|
|
||||||
// SFTP. It is intentionally separate from SSHConfig: SFTP uses the SSH
|
|
||||||
// transport, but it is a different file-transfer protocol from the existing
|
|
||||||
// SCP action and has its own remote-root semantics.
|
|
||||||
//
|
|
||||||
// Field design notes:
|
|
||||||
// - Protocol is either "ftp" or "sftp". Their default ports are 21 and 22.
|
|
||||||
// - AuthMethod is used only by SFTP. "password" uses Password, while "key"
|
|
||||||
// reuses the local ssh-agent and ~/.ssh/id_* discovery used by SSH/SCP.
|
|
||||||
// - PathPrefix is relative to the account's login root. The application
|
|
||||||
// rejects absolute paths and traversal segments before uploading.
|
|
||||||
// - StrictHostKey and KnownHostsPath apply only to SFTP. Plain FTP has no
|
|
||||||
// host-key mechanism and sends credentials and data without encryption.
|
|
||||||
type FTPConfig struct {
|
|
||||||
Protocol string `json:"protocol"`
|
|
||||||
Host string `json:"host"`
|
|
||||||
Port int `json:"port"`
|
|
||||||
User string `json:"user"`
|
|
||||||
AuthMethod string `json:"authMethod"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
PathPrefix string `json:"pathPrefix"`
|
|
||||||
StrictHostKey bool `json:"strictHostKey"`
|
|
||||||
KnownHostsPath string `json:"knownHostsPath"`
|
|
||||||
ConnectTimeoutSecs int `json:"connectTimeoutSecs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// LLMProviderConfig stores one OpenAI-compatible multimodal chat endpoint.
|
|
||||||
//
|
|
||||||
// The three built-in providers (Qwen, Doubao/Ark, OpenAI-compatible) all use
|
|
||||||
// the same request shape: chat/completions with a user content array that
|
|
||||||
// contains text plus an image_url item. Keeping them as provider records lets
|
|
||||||
// users switch between credentials/models without retyping every field.
|
|
||||||
type LLMProviderConfig struct {
|
|
||||||
Label string `json:"label"`
|
|
||||||
BaseURL string `json:"baseUrl"`
|
|
||||||
APIKey string `json:"apiKey"`
|
|
||||||
Model string `json:"model"`
|
|
||||||
MaxTokens int `json:"maxTokens"`
|
|
||||||
Temperature float64 `json:"temperature"`
|
|
||||||
TimeoutSecs int `json:"timeoutSecs"`
|
|
||||||
// MaxInlineBytes caps the screenshot size (in bytes of the PNG payload)
|
|
||||||
// that may be sent inline as a base64 data URL. Above this threshold the
|
|
||||||
// summary pipeline falls back to uploading the image to S3 and passing a
|
|
||||||
// public URL instead, because most providers reject oversized inline
|
|
||||||
// images. A value <= 0 means "use the built-in default".
|
|
||||||
MaxInlineBytes int `json:"maxInlineBytes"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultMaxInlineBytes is the fallback inline-image cap (~4 MiB) used when a
|
|
||||||
// provider config does not specify MaxInlineBytes. It is a conservative bound
|
|
||||||
// that keeps a single base64 data URL within the request-size limits accepted
|
|
||||||
// by the common OpenAI-compatible multimodal endpoints.
|
|
||||||
const DefaultMaxInlineBytes = 4 << 20
|
|
||||||
|
|
||||||
// LLMConfig controls screenshot summarisation.
|
|
||||||
type LLMConfig struct {
|
|
||||||
ActiveProvider string `json:"activeProvider"`
|
|
||||||
Prompt string `json:"prompt"`
|
|
||||||
Providers map[string]LLMProviderConfig `json:"providers"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// OCRProviderConfig stores one cloud OCR endpoint that accepts a screenshot
|
|
||||||
// image and returns extracted text. Aliyun and Volcengine both authenticate
|
|
||||||
// with an AccessKey pair, but their signing schemes use slightly different
|
|
||||||
// endpoint metadata; keeping Region/Service/Action/Version configurable lets
|
|
||||||
// the presets track official API changes without a schema rewrite.
|
|
||||||
type OCRProviderConfig struct {
|
|
||||||
Label string `json:"label"`
|
|
||||||
Endpoint string `json:"endpoint"`
|
|
||||||
AccessKeyID string `json:"accessKeyId"`
|
|
||||||
AccessKeySecret string `json:"accessKeySecret"`
|
|
||||||
Region string `json:"region"`
|
|
||||||
Service string `json:"service"`
|
|
||||||
Action string `json:"action"`
|
|
||||||
Version string `json:"version"`
|
|
||||||
TimeoutSecs int `json:"timeoutSecs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// OCRConfig controls screenshot text extraction.
|
|
||||||
type OCRConfig struct {
|
|
||||||
ActiveProvider string `json:"activeProvider"`
|
|
||||||
Providers map[string]OCRProviderConfig `json:"providers"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// SSH authentication method identifiers stored in SSHConfig.AuthMethod.
|
// SSH authentication method identifiers stored in SSHConfig.AuthMethod.
|
||||||
const (
|
const (
|
||||||
// SSHAuthBuiltin is the legacy combined method (password → agent → key
|
// SSHAuthBuiltin is the legacy combined method (password → agent → key
|
||||||
@@ -169,83 +90,44 @@ const (
|
|||||||
// SSHAuthKerberos delegates to the system ssh binary so an existing
|
// SSHAuthKerberos delegates to the system ssh binary so an existing
|
||||||
// Kerberos credential cache (populated by `kinit`) is reused via GSSAPI.
|
// Kerberos credential cache (populated by `kinit`) is reused via GSSAPI.
|
||||||
SSHAuthKerberos = "kerberos"
|
SSHAuthKerberos = "kerberos"
|
||||||
|
// SSHAuthBgo delegates to the internal `bgo scp` client, which manages
|
||||||
|
// its own authentication. bgo requires a PTY and cannot create remote
|
||||||
|
// directories, so uploads land flat in the remote $HOME.
|
||||||
|
SSHAuthBgo = "bgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// File-transfer protocol identifiers stored in FTPConfig.Protocol.
|
|
||||||
const (
|
|
||||||
FTPProtocolFTP = "ftp"
|
|
||||||
FTPProtocolSFTP = "sftp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Built-in LLM provider identifiers.
|
|
||||||
const (
|
|
||||||
LLMProviderQwen = "qwen"
|
|
||||||
LLMProviderDoubao = "doubao"
|
|
||||||
LLMProviderOpenAI = "openai"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Built-in OCR provider identifiers.
|
|
||||||
const (
|
|
||||||
OCRProviderAliyun = "aliyun"
|
|
||||||
OCRProviderVolcengine = "volcengine"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Theme preference identifiers stored in AppConfig.Theme.
|
|
||||||
const (
|
|
||||||
ThemeAuto = "auto"
|
|
||||||
ThemeLight = "light"
|
|
||||||
ThemeDark = "dark"
|
|
||||||
)
|
|
||||||
|
|
||||||
const DefaultSummaryPrompt = `你是一个截图内容总结助手。请阅读截图,并用中文输出一段适合直接粘贴到聊天、工单、Issue 或 PR 中的说明。
|
|
||||||
|
|
||||||
要求:
|
|
||||||
- 先说明截图中最重要的信息和用户可能想表达的意图。
|
|
||||||
- 如果截图包含报错、异常状态、表格、代码、界面控件或关键数字,请准确提取。
|
|
||||||
- 如果截图内容不足以判断,不要编造;直接说明不确定点。
|
|
||||||
- 输出尽量简洁,默认 3 到 6 条要点。
|
|
||||||
- 不要输出 Markdown 标题,不要寒暄。`
|
|
||||||
|
|
||||||
// IsKerberos reports whether this config requests Kerberos/GSSAPI auth.
|
// IsKerberos reports whether this config requests Kerberos/GSSAPI auth.
|
||||||
func (c SSHConfig) IsKerberos() bool {
|
func (c SSHConfig) IsKerberos() bool {
|
||||||
return c.AuthMethod == SSHAuthKerberos
|
return c.AuthMethod == SSHAuthKerberos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsBgo reports whether this config requests the internal bgo client.
|
||||||
|
func (c SSHConfig) IsBgo() bool {
|
||||||
|
return c.AuthMethod == SSHAuthBgo
|
||||||
|
}
|
||||||
|
|
||||||
// AppConfig is the top-level on-disk configuration document.
|
// AppConfig is the top-level on-disk configuration document.
|
||||||
//
|
//
|
||||||
// We keep S3 / SSH / FTP nested so adding another provider later only requires
|
// We keep S3 / SSH nested so that adding more providers later (e.g. AliyunOSS,
|
||||||
// a new sibling field rather than a schema rewrite.
|
// COS, FTP) only requires a new sibling field rather than a schema rewrite.
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
// Hotkey describes the global shortcut that triggers a capture.
|
// Hotkey describes the global shortcut that triggers a capture.
|
||||||
// Stored as a human-readable string like "cmd+shift+a"; parsing happens
|
// Stored as a human-readable string like "cmd+shift+a"; parsing happens
|
||||||
// in the infrastructure hotkey adapter.
|
// in the infrastructure hotkey adapter.
|
||||||
Hotkey string `json:"hotkey"`
|
Hotkey string `json:"hotkey"`
|
||||||
|
|
||||||
// Theme controls the settings UI appearance: "auto", "light", or "dark".
|
|
||||||
Theme string `json:"theme"`
|
|
||||||
|
|
||||||
// S3 holds the active S3-compatible storage configuration.
|
// S3 holds the active S3-compatible storage configuration.
|
||||||
S3 S3Config `json:"s3"`
|
S3 S3Config `json:"s3"`
|
||||||
|
|
||||||
// SSH holds the configuration for the optional "save to remote via scp"
|
// SSH holds the configuration for the optional "save to remote via scp"
|
||||||
// destination triggered by the save-remote toolbar button.
|
// destination triggered by the save-remote toolbar button.
|
||||||
SSH SSHConfig `json:"ssh"`
|
SSH SSHConfig `json:"ssh"`
|
||||||
|
|
||||||
// FTP holds the destination used by the dedicated FTP/SFTP toolbar action.
|
|
||||||
FTP FTPConfig `json:"ftp"`
|
|
||||||
|
|
||||||
// LLM holds provider settings for the "copy summary" screenshot action.
|
|
||||||
LLM LLMConfig `json:"llm"`
|
|
||||||
|
|
||||||
// OCR holds provider settings for the "extract text" screenshot action.
|
|
||||||
OCR OCRConfig `json:"ocr"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultAppConfig returns sane zero-value defaults used on first launch.
|
// DefaultAppConfig returns sane zero-value defaults used on first launch.
|
||||||
func DefaultAppConfig() AppConfig {
|
func DefaultAppConfig() AppConfig {
|
||||||
cfg := AppConfig{
|
return AppConfig{
|
||||||
Hotkey: "cmd+shift+a",
|
Hotkey: "cmd+shift+a",
|
||||||
Theme: ThemeAuto,
|
|
||||||
S3: S3Config{
|
S3: S3Config{
|
||||||
PathPrefix: "snapgo/",
|
PathPrefix: "snapgo/",
|
||||||
UsePathStyle: true,
|
UsePathStyle: true,
|
||||||
@@ -256,203 +138,6 @@ func DefaultAppConfig() AppConfig {
|
|||||||
ConnectTimeoutSecs: 10,
|
ConnectTimeoutSecs: 10,
|
||||||
StrictHostKey: false,
|
StrictHostKey: false,
|
||||||
},
|
},
|
||||||
FTP: FTPConfig{
|
|
||||||
Protocol: FTPProtocolFTP,
|
|
||||||
Port: 21,
|
|
||||||
AuthMethod: SSHAuthPassword,
|
|
||||||
PathPrefix: "snapgo/",
|
|
||||||
ConnectTimeoutSecs: 10,
|
|
||||||
StrictHostKey: false,
|
|
||||||
},
|
|
||||||
LLM: DefaultLLMConfig(),
|
|
||||||
OCR: DefaultOCRConfig(),
|
|
||||||
}
|
|
||||||
cfg.Normalize()
|
|
||||||
return cfg
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultLLMConfig returns the built-in provider presets. API keys and exact
|
|
||||||
// models remain user-editable because each provider account may expose
|
|
||||||
// different model names / endpoint IDs.
|
|
||||||
func DefaultLLMConfig() LLMConfig {
|
|
||||||
return LLMConfig{
|
|
||||||
ActiveProvider: LLMProviderOpenAI,
|
|
||||||
Prompt: DefaultSummaryPrompt,
|
|
||||||
Providers: map[string]LLMProviderConfig{
|
|
||||||
LLMProviderQwen: {
|
|
||||||
Label: "阿里千问多模态",
|
|
||||||
BaseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
||||||
Model: "qwen-vl-plus",
|
|
||||||
MaxTokens: 600,
|
|
||||||
Temperature: 0.2,
|
|
||||||
TimeoutSecs: 60,
|
|
||||||
MaxInlineBytes: DefaultMaxInlineBytes,
|
|
||||||
},
|
|
||||||
LLMProviderDoubao: {
|
|
||||||
Label: "火山方舟豆包多模态",
|
|
||||||
BaseURL: "https://ark.cn-beijing.volces.com/api/v3",
|
|
||||||
Model: "",
|
|
||||||
MaxTokens: 600,
|
|
||||||
Temperature: 0.2,
|
|
||||||
TimeoutSecs: 60,
|
|
||||||
MaxInlineBytes: DefaultMaxInlineBytes,
|
|
||||||
},
|
|
||||||
LLMProviderOpenAI: {
|
|
||||||
Label: "ChatGPT / OpenAI-compatible",
|
|
||||||
BaseURL: "https://api.openai.com/v1",
|
|
||||||
Model: "gpt-5.5",
|
|
||||||
MaxTokens: 600,
|
|
||||||
Temperature: 0.2,
|
|
||||||
TimeoutSecs: 60,
|
|
||||||
MaxInlineBytes: DefaultMaxInlineBytes,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultOCRConfig returns the built-in OCR provider presets. Users only need
|
|
||||||
// to fill AccessKey credentials for the common regions/actions, while advanced
|
|
||||||
// accounts can still override endpoint metadata from the settings UI.
|
|
||||||
func DefaultOCRConfig() OCRConfig {
|
|
||||||
return OCRConfig{
|
|
||||||
ActiveProvider: OCRProviderAliyun,
|
|
||||||
Providers: map[string]OCRProviderConfig{
|
|
||||||
OCRProviderAliyun: {
|
|
||||||
Label: "阿里云文字识别",
|
|
||||||
Endpoint: "https://ocr-api.cn-hangzhou.aliyuncs.com",
|
|
||||||
Action: "RecognizeGeneral",
|
|
||||||
Version: "2021-07-07",
|
|
||||||
TimeoutSecs: 30,
|
|
||||||
},
|
|
||||||
OCRProviderVolcengine: {
|
|
||||||
Label: "火山引擎文字识别",
|
|
||||||
Endpoint: "https://visual.volcengineapi.com",
|
|
||||||
Region: "cn-north-1",
|
|
||||||
Service: "cv",
|
|
||||||
Action: "OCRNormal",
|
|
||||||
Version: "2020-08-26",
|
|
||||||
TimeoutSecs: 30,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize fills defaults into configs written by older app versions while
|
|
||||||
// preserving any user-provided provider fields.
|
|
||||||
func (c *AppConfig) Normalize() {
|
|
||||||
if c.Hotkey == "" {
|
|
||||||
c.Hotkey = "cmd+shift+a"
|
|
||||||
}
|
|
||||||
if c.Theme != ThemeLight && c.Theme != ThemeDark && c.Theme != ThemeAuto {
|
|
||||||
c.Theme = ThemeAuto
|
|
||||||
}
|
|
||||||
if c.S3.PathPrefix == "" {
|
|
||||||
c.S3.PathPrefix = "snapgo/"
|
|
||||||
}
|
|
||||||
if c.SSH.Port == 0 {
|
|
||||||
c.SSH.Port = 22
|
|
||||||
}
|
|
||||||
if c.SSH.PathPrefix == "" {
|
|
||||||
c.SSH.PathPrefix = "snapgo/"
|
|
||||||
}
|
|
||||||
if c.SSH.ConnectTimeoutSecs == 0 {
|
|
||||||
c.SSH.ConnectTimeoutSecs = 10
|
|
||||||
}
|
|
||||||
if c.FTP.Protocol != FTPProtocolFTP && c.FTP.Protocol != FTPProtocolSFTP {
|
|
||||||
c.FTP.Protocol = FTPProtocolFTP
|
|
||||||
}
|
|
||||||
if c.FTP.Port == 0 {
|
|
||||||
if c.FTP.Protocol == FTPProtocolSFTP {
|
|
||||||
c.FTP.Port = 22
|
|
||||||
} else {
|
|
||||||
c.FTP.Port = 21
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if c.FTP.AuthMethod != SSHAuthPassword && c.FTP.AuthMethod != SSHAuthKey {
|
|
||||||
c.FTP.AuthMethod = SSHAuthPassword
|
|
||||||
}
|
|
||||||
if c.FTP.PathPrefix == "" {
|
|
||||||
c.FTP.PathPrefix = "snapgo/"
|
|
||||||
}
|
|
||||||
if c.FTP.ConnectTimeoutSecs == 0 {
|
|
||||||
c.FTP.ConnectTimeoutSecs = 10
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultLLM := DefaultLLMConfig()
|
|
||||||
if c.LLM.ActiveProvider == "" {
|
|
||||||
c.LLM.ActiveProvider = defaultLLM.ActiveProvider
|
|
||||||
}
|
|
||||||
if c.LLM.Prompt == "" {
|
|
||||||
c.LLM.Prompt = defaultLLM.Prompt
|
|
||||||
}
|
|
||||||
if c.LLM.Providers == nil {
|
|
||||||
c.LLM.Providers = map[string]LLMProviderConfig{}
|
|
||||||
}
|
|
||||||
for id, def := range defaultLLM.Providers {
|
|
||||||
current, ok := c.LLM.Providers[id]
|
|
||||||
if !ok {
|
|
||||||
c.LLM.Providers[id] = def
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if current.Label == "" {
|
|
||||||
current.Label = def.Label
|
|
||||||
}
|
|
||||||
if current.BaseURL == "" {
|
|
||||||
current.BaseURL = def.BaseURL
|
|
||||||
}
|
|
||||||
if current.Model == "" && id != LLMProviderDoubao {
|
|
||||||
current.Model = def.Model
|
|
||||||
}
|
|
||||||
if current.MaxTokens == 0 {
|
|
||||||
current.MaxTokens = def.MaxTokens
|
|
||||||
}
|
|
||||||
if current.Temperature < 0 {
|
|
||||||
current.Temperature = def.Temperature
|
|
||||||
}
|
|
||||||
if current.TimeoutSecs == 0 {
|
|
||||||
current.TimeoutSecs = def.TimeoutSecs
|
|
||||||
}
|
|
||||||
if current.MaxInlineBytes <= 0 {
|
|
||||||
current.MaxInlineBytes = DefaultMaxInlineBytes
|
|
||||||
}
|
|
||||||
c.LLM.Providers[id] = current
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultOCR := DefaultOCRConfig()
|
|
||||||
if c.OCR.ActiveProvider == "" {
|
|
||||||
c.OCR.ActiveProvider = defaultOCR.ActiveProvider
|
|
||||||
}
|
|
||||||
if c.OCR.Providers == nil {
|
|
||||||
c.OCR.Providers = map[string]OCRProviderConfig{}
|
|
||||||
}
|
|
||||||
for id, def := range defaultOCR.Providers {
|
|
||||||
current, ok := c.OCR.Providers[id]
|
|
||||||
if !ok {
|
|
||||||
c.OCR.Providers[id] = def
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if current.Label == "" {
|
|
||||||
current.Label = def.Label
|
|
||||||
}
|
|
||||||
if current.Endpoint == "" {
|
|
||||||
current.Endpoint = def.Endpoint
|
|
||||||
}
|
|
||||||
if current.Region == "" {
|
|
||||||
current.Region = def.Region
|
|
||||||
}
|
|
||||||
if current.Service == "" {
|
|
||||||
current.Service = def.Service
|
|
||||||
}
|
|
||||||
if current.Action == "" {
|
|
||||||
current.Action = def.Action
|
|
||||||
}
|
|
||||||
if current.Version == "" {
|
|
||||||
current.Version = def.Version
|
|
||||||
}
|
|
||||||
if current.TimeoutSecs == 0 {
|
|
||||||
current.TimeoutSecs = def.TimeoutSecs
|
|
||||||
}
|
|
||||||
c.OCR.Providers[id] = current
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -470,58 +155,3 @@ func (c AppConfig) IsS3Configured() bool {
|
|||||||
func (c AppConfig) IsSSHConfigured() bool {
|
func (c AppConfig) IsSSHConfigured() bool {
|
||||||
return c.SSH.Host != "" && c.SSH.User != ""
|
return c.SSH.Host != "" && c.SSH.User != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsFTPConfigured reports whether the FTP/SFTP destination has the minimum
|
|
||||||
// fields required to attempt a connection. Password is not required because
|
|
||||||
// SFTP key authentication and password-less FTP accounts are both valid.
|
|
||||||
func (c AppConfig) IsFTPConfigured() bool {
|
|
||||||
return (c.FTP.Protocol == FTPProtocolFTP || c.FTP.Protocol == FTPProtocolSFTP) &&
|
|
||||||
c.FTP.Host != "" && c.FTP.User != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// ActiveLLMProvider returns the selected provider config plus a boolean
|
|
||||||
// indicating whether the selection exists.
|
|
||||||
func (c AppConfig) ActiveLLMProvider() (string, LLMProviderConfig, bool) {
|
|
||||||
id := c.LLM.ActiveProvider
|
|
||||||
if id == "" {
|
|
||||||
id = LLMProviderOpenAI
|
|
||||||
}
|
|
||||||
provider, ok := c.LLM.Providers[id]
|
|
||||||
return id, provider, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsLLMConfigured reports whether the selected multimodal provider has the
|
|
||||||
// fields required to make a request.
|
|
||||||
func (c AppConfig) IsLLMConfigured() bool {
|
|
||||||
_, provider, ok := c.ActiveLLMProvider()
|
|
||||||
return ok && provider.BaseURL != "" && provider.APIKey != "" && provider.Model != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// ActiveOCRProvider returns the selected OCR provider config plus a boolean
|
|
||||||
// indicating whether the selection exists.
|
|
||||||
func (c AppConfig) ActiveOCRProvider() (string, OCRProviderConfig, bool) {
|
|
||||||
id := c.OCR.ActiveProvider
|
|
||||||
if id == "" {
|
|
||||||
id = OCRProviderAliyun
|
|
||||||
}
|
|
||||||
provider, ok := c.OCR.Providers[id]
|
|
||||||
return id, provider, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsOCRConfigured reports whether the selected OCR provider has the fields
|
|
||||||
// required to sign and send a request.
|
|
||||||
func (c AppConfig) IsOCRConfigured() bool {
|
|
||||||
id, provider, ok := c.ActiveOCRProvider()
|
|
||||||
if !ok ||
|
|
||||||
provider.Endpoint == "" ||
|
|
||||||
provider.AccessKeyID == "" ||
|
|
||||||
provider.AccessKeySecret == "" ||
|
|
||||||
provider.Action == "" ||
|
|
||||||
provider.Version == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if id == OCRProviderVolcengine && (provider.Region == "" || provider.Service == "") {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
package domain
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestDefaultAppConfigIncludesFTPDefaults(t *testing.T) {
|
|
||||||
cfg := DefaultAppConfig()
|
|
||||||
if cfg.FTP.Protocol != FTPProtocolFTP {
|
|
||||||
t.Fatalf("expected default FTP protocol, got %q", cfg.FTP.Protocol)
|
|
||||||
}
|
|
||||||
if cfg.FTP.Port != 21 {
|
|
||||||
t.Fatalf("expected default FTP port 21, got %d", cfg.FTP.Port)
|
|
||||||
}
|
|
||||||
if cfg.FTP.PathPrefix != "snapgo/" {
|
|
||||||
t.Fatalf("expected default path prefix, got %q", cfg.FTP.PathPrefix)
|
|
||||||
}
|
|
||||||
if cfg.FTP.ConnectTimeoutSecs != 10 {
|
|
||||||
t.Fatalf("expected 10 second timeout, got %d", cfg.FTP.ConnectTimeoutSecs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNormalizeUsesSFTPDefaultPort(t *testing.T) {
|
|
||||||
cfg := AppConfig{
|
|
||||||
FTP: FTPConfig{
|
|
||||||
Protocol: FTPProtocolSFTP,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cfg.Normalize()
|
|
||||||
if cfg.FTP.Port != 22 {
|
|
||||||
t.Fatalf("expected SFTP port 22, got %d", cfg.FTP.Port)
|
|
||||||
}
|
|
||||||
if cfg.FTP.AuthMethod != SSHAuthPassword {
|
|
||||||
t.Fatalf("expected password auth default, got %q", cfg.FTP.AuthMethod)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsFTPConfiguredRequiresProtocolHostAndUser(t *testing.T) {
|
|
||||||
cfg := DefaultAppConfig()
|
|
||||||
if cfg.IsFTPConfigured() {
|
|
||||||
t.Fatal("empty FTP destination should not be configured")
|
|
||||||
}
|
|
||||||
cfg.FTP.Host = "files.example.com"
|
|
||||||
cfg.FTP.User = "snapgo"
|
|
||||||
if !cfg.IsFTPConfigured() {
|
|
||||||
t.Fatal("host and user should be enough for a normalized FTP config")
|
|
||||||
}
|
|
||||||
cfg.FTP.Protocol = "invalid"
|
|
||||||
if cfg.IsFTPConfigured() {
|
|
||||||
t.Fatal("invalid protocol should not be configured")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
// Package config provides JSON-file backed persistence for AppConfig.
|
// Package config provides JSON-file backed persistence for AppConfig.
|
||||||
//
|
//
|
||||||
// Design rationale:
|
// Design rationale:
|
||||||
// - JSON keeps the file human-readable, which is useful while the MVP has
|
// - JSON keeps the file human-readable, which is useful while the MVP has
|
||||||
// no settings UI for every option.
|
// no settings UI for every option.
|
||||||
// - Storage location follows os.UserConfigDir() so each platform places it
|
// - Storage location follows os.UserConfigDir() so each platform places it
|
||||||
// in the canonical spot (macOS: ~/Library/Application Support/SnapGo).
|
// in the canonical spot (macOS: ~/Library/Application Support/SnapGo).
|
||||||
// - File mode 0600 / dir mode 0700 mitigate accidental leakage of access
|
// - File mode 0600 / dir mode 0700 mitigate accidental leakage of access
|
||||||
// keys before we wire up the OS keychain (deferred to a later spec).
|
// keys before we wire up the OS keychain (deferred to a later spec).
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -65,7 +65,6 @@ func (s *FileStore) Load() (domain.AppConfig, error) {
|
|||||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||||
return domain.DefaultAppConfig(), fmt.Errorf("parse config: %w", err)
|
return domain.DefaultAppConfig(), fmt.Errorf("parse config: %w", err)
|
||||||
}
|
}
|
||||||
cfg.Normalize()
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +74,6 @@ func (s *FileStore) Save(cfg domain.AppConfig) error {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
cfg.Normalize()
|
|
||||||
data, err := json.MarshalIndent(cfg, "", " ")
|
data, err := json.MarshalIndent(cfg, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal config: %w", err)
|
return fmt.Errorf("marshal config: %w", err)
|
||||||
|
|||||||
@@ -1,288 +0,0 @@
|
|||||||
// Package ftp provides the FTP side of SnapGo's FTP/SFTP destination.
|
|
||||||
//
|
|
||||||
// Design rationale:
|
|
||||||
// - Plain FTP needs a real protocol client for PASV/EPSV handling; hand-
|
|
||||||
// rolling those details would be fragile across common servers.
|
|
||||||
// - SFTP is delegated to the sibling SSH adapter so authentication, agent,
|
|
||||||
// key discovery, and known_hosts behaviour stay consistent with SCP.
|
|
||||||
// - Both implementations upload to a temporary name and rename only after
|
|
||||||
// the payload is complete, preventing a failed transfer from exposing a
|
|
||||||
// partially written screenshot at the final path.
|
|
||||||
package ftp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log/slog"
|
|
||||||
"net"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
ftpclient "github.com/jlaffaye/ftp"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
sshpkg "github.com/mmmy/snapgo/internal/infrastructure/ssh"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RemoteUploader is the common capability exposed to app.go for FTP and
|
|
||||||
// SFTP. The application service only needs Upload; TestConnection remains an
|
|
||||||
// infrastructure concern used by the Settings probe RPC.
|
|
||||||
type RemoteUploader interface {
|
|
||||||
Upload(ctx context.Context, remoteRelPath string, data []byte) error
|
|
||||||
TestConnection(ctx context.Context) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewUploader validates cfg and returns the adapter for its selected protocol.
|
|
||||||
func NewUploader(cfg domain.FTPConfig) (RemoteUploader, error) {
|
|
||||||
normalized, err := normalizeConfig(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if normalized.Protocol == domain.FTPProtocolSFTP {
|
|
||||||
return sshpkg.NewSFTPUploader(normalized), nil
|
|
||||||
}
|
|
||||||
return &plainUploader{cfg: normalized}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type plainUploader struct {
|
|
||||||
cfg domain.FTPConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func ftpLog() *slog.Logger { return slog.Default().With("component", "ftp") }
|
|
||||||
|
|
||||||
func normalizeConfig(cfg domain.FTPConfig) (domain.FTPConfig, error) {
|
|
||||||
cfg.Protocol = strings.ToLower(strings.TrimSpace(cfg.Protocol))
|
|
||||||
if cfg.Protocol == "" {
|
|
||||||
cfg.Protocol = domain.FTPProtocolFTP
|
|
||||||
}
|
|
||||||
if cfg.Protocol != domain.FTPProtocolFTP && cfg.Protocol != domain.FTPProtocolSFTP {
|
|
||||||
return domain.FTPConfig{}, fmt.Errorf("file transfer: protocol must be ftp or sftp")
|
|
||||||
}
|
|
||||||
cfg.Host = strings.TrimSpace(cfg.Host)
|
|
||||||
cfg.User = strings.TrimSpace(cfg.User)
|
|
||||||
if cfg.Host == "" || cfg.User == "" {
|
|
||||||
return domain.FTPConfig{}, fmt.Errorf("file transfer: host and user are required")
|
|
||||||
}
|
|
||||||
if cfg.Port <= 0 {
|
|
||||||
if cfg.Protocol == domain.FTPProtocolSFTP {
|
|
||||||
cfg.Port = 22
|
|
||||||
} else {
|
|
||||||
cfg.Port = 21
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if cfg.Port > 65535 {
|
|
||||||
return domain.FTPConfig{}, fmt.Errorf("file transfer: port must be between 1 and 65535")
|
|
||||||
}
|
|
||||||
if cfg.ConnectTimeoutSecs <= 0 {
|
|
||||||
cfg.ConnectTimeoutSecs = 10
|
|
||||||
}
|
|
||||||
if cfg.AuthMethod == "" {
|
|
||||||
cfg.AuthMethod = domain.SSHAuthPassword
|
|
||||||
}
|
|
||||||
if cfg.Protocol == domain.FTPProtocolSFTP &&
|
|
||||||
cfg.AuthMethod != domain.SSHAuthPassword && cfg.AuthMethod != domain.SSHAuthKey {
|
|
||||||
return domain.FTPConfig{}, fmt.Errorf("sftp: auth method must be password or key")
|
|
||||||
}
|
|
||||||
return cfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *plainUploader) dial(ctx context.Context) (*ftpclient.ServerConn, error) {
|
|
||||||
timeout := time.Duration(u.cfg.ConnectTimeoutSecs) * time.Second
|
|
||||||
addr := net.JoinHostPort(u.cfg.Host, fmt.Sprintf("%d", u.cfg.Port))
|
|
||||||
ftpLog().Info("FTP dial start",
|
|
||||||
"addr", addr,
|
|
||||||
"user", u.cfg.User,
|
|
||||||
"timeout", timeout,
|
|
||||||
"has_password", u.cfg.Password != "")
|
|
||||||
conn, err := ftpclient.Dial(
|
|
||||||
addr,
|
|
||||||
ftpclient.DialWithDialFunc(contextDialFunc(ctx, timeout)),
|
|
||||||
ftpclient.DialWithShutTimeout(timeout),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("ftp: dial %s: %w", addr, err)
|
|
||||||
}
|
|
||||||
if err := conn.Login(u.cfg.User, u.cfg.Password); err != nil {
|
|
||||||
_ = conn.Quit()
|
|
||||||
return nil, fmt.Errorf("ftp: login: %w", err)
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upload stores one screenshot through plain FTP.
|
|
||||||
func (u *plainUploader) Upload(ctx context.Context, remoteRelPath string, data []byte) error {
|
|
||||||
cleaned, err := validateRemoteRelativePath(remoteRelPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
conn, err := u.dial(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() { _ = conn.Quit() }()
|
|
||||||
|
|
||||||
if err := enterFTPDirectory(conn, path.Dir(cleaned)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
base := path.Base(cleaned)
|
|
||||||
|
|
||||||
tempName := temporaryRemoteName(base)
|
|
||||||
removeTemp := true
|
|
||||||
defer func() {
|
|
||||||
if removeTemp {
|
|
||||||
_ = conn.Delete(tempName)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err := conn.Stor(tempName, &contextReader{ctx: ctx, reader: bytes.NewReader(data)}); err != nil {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
return fmt.Errorf("ftp: store %s: %w", cleaned, err)
|
|
||||||
}
|
|
||||||
if err := conn.Rename(tempName, base); err != nil {
|
|
||||||
return fmt.Errorf("ftp: commit %s: %w", cleaned, err)
|
|
||||||
}
|
|
||||||
removeTemp = false
|
|
||||||
ftpLog().Info("FTP upload ok", "remote_path", cleaned, "size", len(data))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConnection writes and removes a small probe under the configured path,
|
|
||||||
// proving that credentials and directory permissions are both usable.
|
|
||||||
func (u *plainUploader) TestConnection(ctx context.Context) error {
|
|
||||||
probe, err := probeRemotePath(u.cfg.PathPrefix)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
conn, err := u.dial(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer func() { _ = conn.Quit() }()
|
|
||||||
|
|
||||||
if err := enterFTPDirectory(conn, path.Dir(probe)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
base := path.Base(probe)
|
|
||||||
if err := conn.Stor(base, strings.NewReader("snapgo")); err != nil {
|
|
||||||
return fmt.Errorf("ftp probe: write: %w", err)
|
|
||||||
}
|
|
||||||
if err := conn.Delete(base); err != nil {
|
|
||||||
return fmt.Errorf("ftp probe: delete: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// enterFTPDirectory walks to dir relative to the login root, creating missing
|
|
||||||
// segments.
|
|
||||||
func enterFTPDirectory(conn *ftpclient.ServerConn, dir string) error {
|
|
||||||
dir = strings.Trim(dir, "/")
|
|
||||||
if dir == "" || dir == "." {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for _, segment := range strings.Split(dir, "/") {
|
|
||||||
if err := conn.ChangeDir(segment); err == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := conn.MakeDir(segment); err != nil {
|
|
||||||
// A concurrent uploader may have created the directory after our
|
|
||||||
// failed CWD. Retrying CWD distinguishes that race from a real error.
|
|
||||||
if retryErr := conn.ChangeDir(segment); retryErr == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return fmt.Errorf("ftp: create directory %q: %w", segment, err)
|
|
||||||
}
|
|
||||||
if err := conn.ChangeDir(segment); err != nil {
|
|
||||||
return fmt.Errorf("ftp: enter directory %q: %w", segment, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateRemoteRelativePath(value string) (string, error) {
|
|
||||||
value = strings.TrimSpace(value)
|
|
||||||
if value == "" || strings.HasPrefix(value, "/") || strings.HasPrefix(value, "~") {
|
|
||||||
return "", fmt.Errorf("file transfer: remote path must be relative to the login directory")
|
|
||||||
}
|
|
||||||
if strings.ContainsAny(value, "\x00\r\n\\") {
|
|
||||||
return "", fmt.Errorf("file transfer: remote path contains an invalid character")
|
|
||||||
}
|
|
||||||
for _, segment := range strings.Split(value, "/") {
|
|
||||||
if segment == ".." {
|
|
||||||
return "", fmt.Errorf("file transfer: remote path must not contain '..'")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cleaned := path.Clean(value)
|
|
||||||
if cleaned == "." || path.Base(cleaned) == "." || path.Base(cleaned) == ".." {
|
|
||||||
return "", fmt.Errorf("file transfer: remote filename is empty")
|
|
||||||
}
|
|
||||||
return cleaned, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func probeRemotePath(prefix string) (string, error) {
|
|
||||||
prefix = strings.TrimSpace(prefix)
|
|
||||||
prefix = strings.TrimPrefix(prefix, "~")
|
|
||||||
prefix = strings.TrimLeft(prefix, "/")
|
|
||||||
probe := fmt.Sprintf(".snapgo-probe-%d", time.Now().UnixNano())
|
|
||||||
return validateRemoteRelativePath(path.Join(prefix, probe))
|
|
||||||
}
|
|
||||||
|
|
||||||
func temporaryRemoteName(base string) string {
|
|
||||||
return fmt.Sprintf(".%s.snapgo-upload-%d", base, time.Now().UnixNano())
|
|
||||||
}
|
|
||||||
|
|
||||||
type contextReader struct {
|
|
||||||
ctx context.Context
|
|
||||||
reader io.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func contextDialFunc(ctx context.Context, timeout time.Duration) func(string, string) (net.Conn, error) {
|
|
||||||
dialer := &net.Dialer{Timeout: timeout}
|
|
||||||
return func(network, address string) (net.Conn, error) {
|
|
||||||
conn, err := dialer.DialContext(ctx, network, address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
wrapped := &cancelableConn{
|
|
||||||
Conn: conn,
|
|
||||||
done: make(chan struct{}),
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
_ = wrapped.Close()
|
|
||||||
case <-wrapped.done:
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return wrapped, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type cancelableConn struct {
|
|
||||||
net.Conn
|
|
||||||
done chan struct{}
|
|
||||||
once sync.Once
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cancelableConn) Close() error {
|
|
||||||
var err error
|
|
||||||
c.once.Do(func() {
|
|
||||||
close(c.done)
|
|
||||||
err = c.Conn.Close()
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *contextReader) Read(p []byte) (int, error) {
|
|
||||||
select {
|
|
||||||
case <-r.ctx.Done():
|
|
||||||
return 0, r.ctx.Err()
|
|
||||||
default:
|
|
||||||
return r.reader.Read(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package ftp
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestValidateRemoteRelativePath(t *testing.T) {
|
|
||||||
valid, err := validateRemoteRelativePath("snapgo/2026/07/image.png")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("valid path rejected: %v", err)
|
|
||||||
}
|
|
||||||
if valid != "snapgo/2026/07/image.png" {
|
|
||||||
t.Fatalf("unexpected normalized path %q", valid)
|
|
||||||
}
|
|
||||||
|
|
||||||
invalid := []string{
|
|
||||||
"",
|
|
||||||
"/absolute/image.png",
|
|
||||||
"~/image.png",
|
|
||||||
"../image.png",
|
|
||||||
"snapgo/../../image.png",
|
|
||||||
"snapgo\\image.png",
|
|
||||||
"snapgo/image.png\nnext",
|
|
||||||
}
|
|
||||||
for _, value := range invalid {
|
|
||||||
if _, err := validateRemoteRelativePath(value); err == nil {
|
|
||||||
t.Errorf("expected %q to be rejected", value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestProbeRemotePathRejectsTraversalPrefix(t *testing.T) {
|
|
||||||
if _, err := probeRemotePath("../../outside"); err == nil {
|
|
||||||
t.Fatal("expected traversal prefix to fail")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -104,7 +104,7 @@ func parseSpec(spec string) ([]hk.Modifier, hk.Key, error) {
|
|||||||
case "option", "alt":
|
case "option", "alt":
|
||||||
mods = append(mods, modOption())
|
mods = append(mods, modOption())
|
||||||
case "shift":
|
case "shift":
|
||||||
mods = append(mods, modShift())
|
mods = append(mods, hk.ModShift)
|
||||||
default:
|
default:
|
||||||
k, ok := lookupKey(p)
|
k, ok := lookupKey(p)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -1,15 +1,10 @@
|
|||||||
//go:build darwin && cgo
|
//go:build darwin
|
||||||
|
|
||||||
package hotkey
|
package hotkey
|
||||||
|
|
||||||
import hk "golang.design/x/hotkey"
|
import hk "golang.design/x/hotkey"
|
||||||
|
|
||||||
// macOS uses ⌘ as the primary modifier; Option corresponds to Alt.
|
// macOS uses ⌘ as the primary modifier; Option corresponds to Alt.
|
||||||
//
|
|
||||||
// This file is gated on cgo because golang.design/x/hotkey only exposes the
|
|
||||||
// Mod* constants from its cgo-backed darwin implementation; with CGO_ENABLED=0
|
|
||||||
// the package falls back to a stub that defines no constants.
|
|
||||||
func modCmd() hk.Modifier { return hk.ModCmd }
|
func modCmd() hk.Modifier { return hk.ModCmd }
|
||||||
func modCtrl() hk.Modifier { return hk.ModCtrl }
|
func modCtrl() hk.Modifier { return hk.ModCtrl }
|
||||||
func modOption() hk.Modifier { return hk.ModOption }
|
func modOption() hk.Modifier { return hk.ModOption }
|
||||||
func modShift() hk.Modifier { return hk.ModShift }
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
//go:build linux && cgo
|
|
||||||
|
|
||||||
package hotkey
|
|
||||||
|
|
||||||
import hk "golang.design/x/hotkey"
|
|
||||||
|
|
||||||
// On Linux we map "cmd" to Ctrl so the same config string ("cmd+shift+a")
|
|
||||||
// stays meaningful across platforms.
|
|
||||||
//
|
|
||||||
// The X11 backend exposes modifiers as Mod1..Mod5 rather than named Alt/Option
|
|
||||||
// constants; Mod1 is the conventional Alt mask under X11, so "option"/"alt"
|
|
||||||
// resolve to hk.Mod1. This file is gated on cgo because the constants only
|
|
||||||
// exist in the cgo-backed linux implementation (CGO_ENABLED=0 falls back to a
|
|
||||||
// stub with no constants).
|
|
||||||
func modCmd() hk.Modifier { return hk.ModCtrl }
|
|
||||||
func modCtrl() hk.Modifier { return hk.ModCtrl }
|
|
||||||
func modOption() hk.Modifier { return hk.Mod1 }
|
|
||||||
func modShift() hk.Modifier { return hk.ModShift }
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
//go:build !darwin
|
||||||
|
|
||||||
|
package hotkey
|
||||||
|
|
||||||
|
import hk "golang.design/x/hotkey"
|
||||||
|
|
||||||
|
// On Windows / Linux we map "cmd" to Ctrl so the same config string works
|
||||||
|
// across platforms; "option" is treated as Alt.
|
||||||
|
func modCmd() hk.Modifier { return hk.ModCtrl }
|
||||||
|
func modCtrl() hk.Modifier { return hk.ModCtrl }
|
||||||
|
func modOption() hk.Modifier { return hk.ModAlt }
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
//go:build !windows && !cgo
|
|
||||||
|
|
||||||
package hotkey
|
|
||||||
|
|
||||||
import hk "golang.design/x/hotkey"
|
|
||||||
|
|
||||||
// This file covers the configuration where golang.design/x/hotkey compiles its
|
|
||||||
// cgo-less stub (any non-Windows platform built with CGO_ENABLED=0). In that
|
|
||||||
// stub the package defines the Modifier/Key types but no Mod*/Key* constants,
|
|
||||||
// and Register panics at runtime. We still provide the package-internal
|
|
||||||
// helpers so that snapgo keeps cross-compiling (e.g. `go vet`, editor analysis,
|
|
||||||
// or CI cross-builds) without pulling in a C toolchain.
|
|
||||||
//
|
|
||||||
// The returned values are inert zero modifiers and lookupKey always reports
|
|
||||||
// "unsupported": callers on this build cannot register a real global hotkey,
|
|
||||||
// so surfacing an "unsupported token" parse error is preferable to reaching
|
|
||||||
// the upstream panic.
|
|
||||||
func modCmd() hk.Modifier { return 0 }
|
|
||||||
func modCtrl() hk.Modifier { return 0 }
|
|
||||||
func modOption() hk.Modifier { return 0 }
|
|
||||||
func modShift() hk.Modifier { return 0 }
|
|
||||||
|
|
||||||
// lookupKey has no key constants to map against in the cgo-less stub, so it
|
|
||||||
// always reports the token as unsupported.
|
|
||||||
func lookupKey(string) (hk.Key, bool) { return 0, false }
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
//go:build windows
|
|
||||||
|
|
||||||
package hotkey
|
|
||||||
|
|
||||||
import hk "golang.design/x/hotkey"
|
|
||||||
|
|
||||||
// On Windows we map "cmd" to Ctrl so the same config string ("cmd+shift+a")
|
|
||||||
// stays meaningful across platforms; "option" is treated as Alt. The Windows
|
|
||||||
// backend does not require cgo, hence the single-tag guard.
|
|
||||||
func modCmd() hk.Modifier { return hk.ModCtrl }
|
|
||||||
func modCtrl() hk.Modifier { return hk.ModCtrl }
|
|
||||||
func modOption() hk.Modifier { return hk.ModAlt }
|
|
||||||
func modShift() hk.Modifier { return hk.ModShift }
|
|
||||||
@@ -1,15 +1,9 @@
|
|||||||
//go:build windows || cgo
|
|
||||||
|
|
||||||
package hotkey
|
package hotkey
|
||||||
|
|
||||||
import hk "golang.design/x/hotkey"
|
import hk "golang.design/x/hotkey"
|
||||||
|
|
||||||
// lookupKey maps a token from the user-supplied hotkey spec to a hk.Key.
|
// lookupKey maps a token from the user-supplied hotkey spec to a hk.Key.
|
||||||
//
|
//
|
||||||
// The Key* constants only exist in the platform backends that ship them
|
|
||||||
// (windows, or the cgo-backed darwin/linux implementations); the cgo-less stub
|
|
||||||
// in hotkey_unsupported.go provides a fallback lookupKey for the complement.
|
|
||||||
//
|
|
||||||
// The map is intentionally exhaustive over the alphanumeric set so we never
|
// The map is intentionally exhaustive over the alphanumeric set so we never
|
||||||
// rely on an assumption that hk.KeyA..hk.KeyZ are contiguous values — the
|
// rely on an assumption that hk.KeyA..hk.KeyZ are contiguous values — the
|
||||||
// upstream package gives no such guarantee.
|
// upstream package gives no such guarantee.
|
||||||
|
|||||||
@@ -1,174 +0,0 @@
|
|||||||
// Package llm contains multimodal LLM adapters.
|
|
||||||
//
|
|
||||||
// The built-in providers are intentionally implemented through the
|
|
||||||
// OpenAI-compatible chat/completions shape. Qwen DashScope, Volcengine Ark,
|
|
||||||
// and OpenAI-compatible gateways all accept the same "text + image_url"
|
|
||||||
// message content pattern, so one small HTTP adapter is enough here.
|
|
||||||
package llm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
// VisionClient calls one configured multimodal chat endpoint.
|
|
||||||
type VisionClient struct {
|
|
||||||
providerID string
|
|
||||||
cfg domain.LLMProviderConfig
|
|
||||||
httpClient *http.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewVisionClient validates cfg and returns a reusable client.
|
|
||||||
func NewVisionClient(providerID string, cfg domain.LLMProviderConfig) (*VisionClient, error) {
|
|
||||||
if strings.TrimSpace(cfg.BaseURL) == "" {
|
|
||||||
return nil, fmt.Errorf("llm base url is required")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(cfg.APIKey) == "" {
|
|
||||||
return nil, fmt.Errorf("llm api key is required")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(cfg.Model) == "" {
|
|
||||||
return nil, fmt.Errorf("llm model is required")
|
|
||||||
}
|
|
||||||
timeout := cfg.TimeoutSecs
|
|
||||||
if timeout <= 0 {
|
|
||||||
timeout = 60
|
|
||||||
}
|
|
||||||
return &VisionClient{
|
|
||||||
providerID: providerID,
|
|
||||||
cfg: cfg,
|
|
||||||
httpClient: &http.Client{Timeout: time.Duration(timeout) * time.Second},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SummarizeImage asks the model to summarise an image available at imageURL.
|
|
||||||
func (c *VisionClient) SummarizeImage(ctx context.Context, prompt string, imageURL string) (string, error) {
|
|
||||||
prompt = strings.TrimSpace(prompt)
|
|
||||||
if prompt == "" {
|
|
||||||
return "", fmt.Errorf("llm prompt is empty")
|
|
||||||
}
|
|
||||||
imageURL = strings.TrimSpace(imageURL)
|
|
||||||
if imageURL == "" {
|
|
||||||
return "", fmt.Errorf("image url is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
body := map[string]any{
|
|
||||||
"model": c.cfg.Model,
|
|
||||||
"messages": []map[string]any{
|
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
"content": []map[string]any{
|
|
||||||
{"type": "text", "text": prompt},
|
|
||||||
{"type": "image_url", "image_url": map[string]any{"url": imageURL}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if c.cfg.MaxTokens > 0 {
|
|
||||||
if c.providerID == domain.LLMProviderOpenAI {
|
|
||||||
body["max_completion_tokens"] = c.cfg.MaxTokens
|
|
||||||
} else {
|
|
||||||
body["max_tokens"] = c.cfg.MaxTokens
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if c.cfg.Temperature >= 0 {
|
|
||||||
body["temperature"] = c.cfg.Temperature
|
|
||||||
}
|
|
||||||
|
|
||||||
payload, err := json.Marshal(body)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("marshal llm request: %w", err)
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, chatCompletionsURL(c.cfg.BaseURL), bytes.NewReader(payload))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("build llm request: %w", err)
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
|
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("llm request: %w", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
data, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
||||||
return "", fmt.Errorf("llm status %d: %s", resp.StatusCode, compactBody(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
var decoded chatCompletionResponse
|
|
||||||
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
||||||
return "", fmt.Errorf("decode llm response: %w", err)
|
|
||||||
}
|
|
||||||
if decoded.Error != nil && decoded.Error.Message != "" {
|
|
||||||
return "", fmt.Errorf("llm error: %s", decoded.Error.Message)
|
|
||||||
}
|
|
||||||
if len(decoded.Choices) == 0 {
|
|
||||||
return "", fmt.Errorf("llm response has no choices")
|
|
||||||
}
|
|
||||||
text := strings.TrimSpace(contentText(decoded.Choices[0].Message.Content))
|
|
||||||
if text == "" {
|
|
||||||
return "", fmt.Errorf("llm response is empty")
|
|
||||||
}
|
|
||||||
return text, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func chatCompletionsURL(baseURL string) string {
|
|
||||||
base := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
|
||||||
if strings.HasSuffix(base, "/chat/completions") {
|
|
||||||
return base
|
|
||||||
}
|
|
||||||
return base + "/chat/completions"
|
|
||||||
}
|
|
||||||
|
|
||||||
type chatCompletionResponse struct {
|
|
||||||
Choices []struct {
|
|
||||||
Message struct {
|
|
||||||
Content any `json:"content"`
|
|
||||||
} `json:"message"`
|
|
||||||
} `json:"choices"`
|
|
||||||
Error *struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Code any `json:"code"`
|
|
||||||
} `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func contentText(content any) string {
|
|
||||||
switch v := content.(type) {
|
|
||||||
case string:
|
|
||||||
return v
|
|
||||||
case []any:
|
|
||||||
parts := make([]string, 0, len(v))
|
|
||||||
for _, item := range v {
|
|
||||||
obj, ok := item.(map[string]any)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if text, ok := obj["text"].(string); ok {
|
|
||||||
parts = append(parts, text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return strings.Join(parts, "\n")
|
|
||||||
default:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func compactBody(data []byte) string {
|
|
||||||
text := strings.TrimSpace(string(data))
|
|
||||||
if text == "" {
|
|
||||||
return "<empty body>"
|
|
||||||
}
|
|
||||||
if len(text) > 800 {
|
|
||||||
return text[:800] + "..."
|
|
||||||
}
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
package llm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
|
||||||
|
|
||||||
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
||||||
return f(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVisionClientSummarizeImageUsesChatCompletionImageURL(t *testing.T) {
|
|
||||||
var requestBody map[string]any
|
|
||||||
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
|
||||||
if r.URL.Path != "/v1/chat/completions" {
|
|
||||||
t.Fatalf("expected /v1/chat/completions, got %s", r.URL.Path)
|
|
||||||
}
|
|
||||||
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
|
|
||||||
t.Fatalf("unexpected auth header %q", got)
|
|
||||||
}
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
|
||||||
t.Fatalf("decode request: %v", err)
|
|
||||||
}
|
|
||||||
return &http.Response{
|
|
||||||
StatusCode: http.StatusOK,
|
|
||||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
||||||
Body: io.NopCloser(bytes.NewBufferString(`{"choices":[{"message":{"content":"summary ok"}}]}`)),
|
|
||||||
}, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
client, err := NewVisionClient(domain.LLMProviderQwen, domain.LLMProviderConfig{
|
|
||||||
BaseURL: "https://example.test/v1",
|
|
||||||
APIKey: "test-key",
|
|
||||||
Model: "qwen-vl-plus",
|
|
||||||
MaxTokens: 321,
|
|
||||||
Temperature: 0.2,
|
|
||||||
TimeoutSecs: 5,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("new client: %v", err)
|
|
||||||
}
|
|
||||||
client.httpClient.Transport = transport
|
|
||||||
|
|
||||||
text, err := client.SummarizeImage(context.Background(), "describe", "https://cdn.example.com/a.png")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("summarize image: %v", err)
|
|
||||||
}
|
|
||||||
if text != "summary ok" {
|
|
||||||
t.Fatalf("expected response text, got %q", text)
|
|
||||||
}
|
|
||||||
if requestBody["model"] != "qwen-vl-plus" {
|
|
||||||
t.Fatalf("expected model forwarded, got %#v", requestBody["model"])
|
|
||||||
}
|
|
||||||
if requestBody["max_tokens"] != float64(321) {
|
|
||||||
t.Fatalf("expected max_tokens forwarded, got %#v", requestBody["max_tokens"])
|
|
||||||
}
|
|
||||||
|
|
||||||
messages := requestBody["messages"].([]any)
|
|
||||||
content := messages[0].(map[string]any)["content"].([]any)
|
|
||||||
if content[0].(map[string]any)["text"] != "describe" {
|
|
||||||
t.Fatalf("expected prompt content, got %#v", content[0])
|
|
||||||
}
|
|
||||||
image := content[1].(map[string]any)
|
|
||||||
if image["type"] != "image_url" {
|
|
||||||
t.Fatalf("expected image_url item, got %#v", image)
|
|
||||||
}
|
|
||||||
url := image["image_url"].(map[string]any)["url"]
|
|
||||||
if url != "https://cdn.example.com/a.png" {
|
|
||||||
t.Fatalf("expected image url forwarded, got %#v", url)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChatCompletionsURLAcceptsFullEndpoint(t *testing.T) {
|
|
||||||
full := "https://example.com/v1/chat/completions"
|
|
||||||
if got := chatCompletionsURL(full); got != full {
|
|
||||||
t.Fatalf("expected full endpoint unchanged, got %q", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,701 +0,0 @@
|
|||||||
// Package ocr contains cloud OCR adapters.
|
|
||||||
package ocr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"crypto/hmac"
|
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/application"
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client calls one configured OCR provider.
|
|
||||||
type Client struct {
|
|
||||||
providerID string
|
|
||||||
cfg domain.OCRProviderConfig
|
|
||||||
httpClient *http.Client
|
|
||||||
now func() time.Time
|
|
||||||
nonce func() string
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ application.OCRRecognizer = (*Client)(nil)
|
|
||||||
|
|
||||||
// NewClient validates cfg and returns a reusable OCR client.
|
|
||||||
func NewClient(providerID string, cfg domain.OCRProviderConfig) (*Client, error) {
|
|
||||||
if strings.TrimSpace(cfg.Endpoint) == "" {
|
|
||||||
return nil, fmt.Errorf("ocr endpoint is required")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(cfg.AccessKeyID) == "" {
|
|
||||||
return nil, fmt.Errorf("ocr access key id is required")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(cfg.AccessKeySecret) == "" {
|
|
||||||
return nil, fmt.Errorf("ocr access key secret is required")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(cfg.Action) == "" {
|
|
||||||
return nil, fmt.Errorf("ocr action is required")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(cfg.Version) == "" {
|
|
||||||
return nil, fmt.Errorf("ocr version is required")
|
|
||||||
}
|
|
||||||
switch providerID {
|
|
||||||
case domain.OCRProviderAliyun:
|
|
||||||
case domain.OCRProviderVolcengine:
|
|
||||||
if strings.TrimSpace(cfg.Region) == "" {
|
|
||||||
return nil, fmt.Errorf("volcengine ocr region is required")
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(cfg.Service) == "" {
|
|
||||||
return nil, fmt.Errorf("volcengine ocr service is required")
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unsupported ocr provider %q", providerID)
|
|
||||||
}
|
|
||||||
timeout := cfg.TimeoutSecs
|
|
||||||
if timeout <= 0 {
|
|
||||||
timeout = 30
|
|
||||||
}
|
|
||||||
return &Client{
|
|
||||||
providerID: providerID,
|
|
||||||
cfg: cfg,
|
|
||||||
httpClient: &http.Client{Timeout: time.Duration(timeout) * time.Second},
|
|
||||||
now: time.Now,
|
|
||||||
nonce: randomNonce,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RecognizeText extracts visible text from a PNG screenshot.
|
|
||||||
func (c *Client) RecognizeText(ctx context.Context, pngBytes []byte) (string, error) {
|
|
||||||
if len(pngBytes) == 0 {
|
|
||||||
return "", fmt.Errorf("empty screenshot")
|
|
||||||
}
|
|
||||||
switch c.providerID {
|
|
||||||
case domain.OCRProviderAliyun:
|
|
||||||
return c.recognizeAliyun(ctx, pngBytes)
|
|
||||||
case domain.OCRProviderVolcengine:
|
|
||||||
return c.recognizeVolcengine(ctx, pngBytes)
|
|
||||||
default:
|
|
||||||
return "", fmt.Errorf("unsupported ocr provider %q", c.providerID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) recognizeAliyun(ctx context.Context, pngBytes []byte) (string, error) {
|
|
||||||
endpoint, err := parseEndpoint(c.cfg.Endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(pngBytes))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("build aliyun ocr request: %w", err)
|
|
||||||
}
|
|
||||||
req.Header.Set("Accept", "application/json")
|
|
||||||
req.Header.Set("Content-Type", "application/octet-stream")
|
|
||||||
c.signAliyun(req, pngBytes)
|
|
||||||
return c.doOCRRequest(req, "aliyun ocr")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) recognizeVolcengine(ctx context.Context, pngBytes []byte) (string, error) {
|
|
||||||
endpoint, err := parseEndpoint(c.cfg.Endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
query := endpoint.Query()
|
|
||||||
query.Set("Action", c.cfg.Action)
|
|
||||||
query.Set("Version", c.cfg.Version)
|
|
||||||
endpoint.RawQuery = query.Encode()
|
|
||||||
|
|
||||||
body := map[string]string{
|
|
||||||
"image_base64": base64.StdEncoding.EncodeToString(pngBytes),
|
|
||||||
}
|
|
||||||
payload, err := json.Marshal(body)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("marshal volcengine ocr request: %w", err)
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), bytes.NewReader(payload))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("build volcengine ocr request: %w", err)
|
|
||||||
}
|
|
||||||
req.Header.Set("Accept", "application/json")
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
c.signVolcengine(req, payload)
|
|
||||||
return c.doOCRRequest(req, "volcengine ocr")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) doOCRRequest(req *http.Request, label string) (string, error) {
|
|
||||||
resp, err := c.httpClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("%s request: %w", label, err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
data, _ := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
||||||
return "", fmt.Errorf("%s status %d: %s", label, resp.StatusCode, compactBody(data))
|
|
||||||
}
|
|
||||||
text, err := parseOCRText(data)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("%s response: %w", label, err)
|
|
||||||
}
|
|
||||||
return text, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) signAliyun(req *http.Request, payload []byte) {
|
|
||||||
now := c.now().UTC()
|
|
||||||
payloadHash := sha256Hex(payload)
|
|
||||||
nonce := c.nonce()
|
|
||||||
|
|
||||||
req.Header.Set("x-acs-action", c.cfg.Action)
|
|
||||||
req.Header.Set("x-acs-version", c.cfg.Version)
|
|
||||||
req.Header.Set("x-acs-date", now.Format("2006-01-02T15:04:05Z"))
|
|
||||||
req.Header.Set("x-acs-signature-nonce", nonce)
|
|
||||||
req.Header.Set("x-acs-content-sha256", payloadHash)
|
|
||||||
|
|
||||||
signedHeaders := []string{
|
|
||||||
"content-type",
|
|
||||||
"host",
|
|
||||||
"x-acs-action",
|
|
||||||
"x-acs-content-sha256",
|
|
||||||
"x-acs-date",
|
|
||||||
"x-acs-signature-nonce",
|
|
||||||
"x-acs-version",
|
|
||||||
}
|
|
||||||
canonicalHeaders := canonicalHeaders(req, signedHeaders)
|
|
||||||
canonicalRequest := strings.Join([]string{
|
|
||||||
req.Method,
|
|
||||||
canonicalURI(req.URL),
|
|
||||||
canonicalQuery(req.URL),
|
|
||||||
canonicalHeaders,
|
|
||||||
strings.Join(signedHeaders, ";"),
|
|
||||||
payloadHash,
|
|
||||||
}, "\n")
|
|
||||||
stringToSign := "ACS3-HMAC-SHA256\n" + sha256Hex([]byte(canonicalRequest))
|
|
||||||
signature := hmacSHA256Hex([]byte(c.cfg.AccessKeySecret), []byte(stringToSign))
|
|
||||||
req.Header.Set(
|
|
||||||
"Authorization",
|
|
||||||
fmt.Sprintf("ACS3-HMAC-SHA256 Credential=%s,SignedHeaders=%s,Signature=%s",
|
|
||||||
c.cfg.AccessKeyID,
|
|
||||||
strings.Join(signedHeaders, ";"),
|
|
||||||
signature,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) signVolcengine(req *http.Request, payload []byte) {
|
|
||||||
now := c.now().UTC()
|
|
||||||
xDate := now.Format("20060102T150405Z")
|
|
||||||
shortDate := now.Format("20060102")
|
|
||||||
payloadHash := sha256Hex(payload)
|
|
||||||
|
|
||||||
req.Header.Set("X-Date", xDate)
|
|
||||||
req.Header.Set("X-Content-Sha256", payloadHash)
|
|
||||||
|
|
||||||
signedHeaders := []string{
|
|
||||||
"content-type",
|
|
||||||
"host",
|
|
||||||
"x-content-sha256",
|
|
||||||
"x-date",
|
|
||||||
}
|
|
||||||
canonicalHeaders := canonicalHeaders(req, signedHeaders)
|
|
||||||
canonicalRequest := strings.Join([]string{
|
|
||||||
req.Method,
|
|
||||||
canonicalURI(req.URL),
|
|
||||||
canonicalQuery(req.URL),
|
|
||||||
canonicalHeaders,
|
|
||||||
strings.Join(signedHeaders, ";"),
|
|
||||||
payloadHash,
|
|
||||||
}, "\n")
|
|
||||||
scope := strings.Join([]string{shortDate, c.cfg.Region, c.cfg.Service, "request"}, "/")
|
|
||||||
stringToSign := strings.Join([]string{
|
|
||||||
"HMAC-SHA256",
|
|
||||||
xDate,
|
|
||||||
scope,
|
|
||||||
sha256Hex([]byte(canonicalRequest)),
|
|
||||||
}, "\n")
|
|
||||||
signingKey := volcengineSigningKey(c.cfg.AccessKeySecret, shortDate, c.cfg.Region, c.cfg.Service)
|
|
||||||
signature := hmacSHA256Hex(signingKey, []byte(stringToSign))
|
|
||||||
req.Header.Set(
|
|
||||||
"Authorization",
|
|
||||||
fmt.Sprintf("HMAC-SHA256 Credential=%s/%s, SignedHeaders=%s, Signature=%s",
|
|
||||||
c.cfg.AccessKeyID,
|
|
||||||
scope,
|
|
||||||
strings.Join(signedHeaders, ";"),
|
|
||||||
signature,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseEndpoint(raw string) (*url.URL, error) {
|
|
||||||
endpoint := strings.TrimSpace(raw)
|
|
||||||
if endpoint == "" {
|
|
||||||
return nil, fmt.Errorf("ocr endpoint is required")
|
|
||||||
}
|
|
||||||
if !strings.Contains(endpoint, "://") {
|
|
||||||
endpoint = "https://" + endpoint
|
|
||||||
}
|
|
||||||
parsed, err := url.Parse(endpoint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("parse ocr endpoint: %w", err)
|
|
||||||
}
|
|
||||||
if parsed.Scheme == "" || parsed.Host == "" {
|
|
||||||
return nil, fmt.Errorf("invalid ocr endpoint %q", raw)
|
|
||||||
}
|
|
||||||
if parsed.Path == "" {
|
|
||||||
parsed.Path = "/"
|
|
||||||
}
|
|
||||||
return parsed, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseOCRText(data []byte) (string, error) {
|
|
||||||
var root any
|
|
||||||
if err := json.Unmarshal(data, &root); err != nil {
|
|
||||||
return "", fmt.Errorf("decode json: %w", err)
|
|
||||||
}
|
|
||||||
if msg := responseError(root); msg != "" {
|
|
||||||
return "", errors.New(msg)
|
|
||||||
}
|
|
||||||
if text := normalizeOCRText(extractOverallOCRText(root, "")); text != "" {
|
|
||||||
return text, nil
|
|
||||||
}
|
|
||||||
parts := collectOCRParts(root, "")
|
|
||||||
if len(parts) == 0 {
|
|
||||||
return "", fmt.Errorf("no text found")
|
|
||||||
}
|
|
||||||
return strings.Join(dedupeNonEmpty(parts), "\n"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func responseError(v any) string {
|
|
||||||
obj, ok := v.(map[string]any)
|
|
||||||
if !ok {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if meta, ok := obj["ResponseMetadata"].(map[string]any); ok {
|
|
||||||
if errObj, ok := meta["Error"].(map[string]any); ok {
|
|
||||||
if msg := stringValue(errObj["Message"]); msg != "" {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
if code := stringValue(errObj["Code"]); code != "" {
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if errObj, ok := obj["Error"].(map[string]any); ok {
|
|
||||||
if msg := stringValue(errObj["Message"]); msg != "" {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
if msg := stringValue(errObj["message"]); msg != "" {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if code, ok := numericCode(obj["code"]); ok && code != 0 && code != 10000 {
|
|
||||||
if msg := stringValue(obj["message"]); msg != "" {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
if msg := stringValue(obj["msg"]); msg != "" {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("provider code %.0f", code)
|
|
||||||
}
|
|
||||||
if code := stringValue(obj["Code"]); code != "" && !isSuccessCode(code) {
|
|
||||||
if msg := stringValue(obj["Message"]); msg != "" {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func extractOverallOCRText(v any, parentKey string) string {
|
|
||||||
switch value := v.(type) {
|
|
||||||
case string:
|
|
||||||
text := strings.TrimSpace(value)
|
|
||||||
if text == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if looksLikeJSON(text) {
|
|
||||||
var nested any
|
|
||||||
if err := json.Unmarshal([]byte(text), &nested); err == nil {
|
|
||||||
if nestedText := extractOverallOCRText(nested, parentKey); nestedText != "" {
|
|
||||||
return nestedText
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if isAggregateContainerKey(parentKey) {
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
case map[string]any:
|
|
||||||
for _, key := range aggregateTextKeys() {
|
|
||||||
if child, ok := value[key]; ok {
|
|
||||||
if text := extractOverallOCRText(child, key); text != "" {
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, key := range responseContainerKeys() {
|
|
||||||
if child, ok := value[key]; ok {
|
|
||||||
if text := extractOverallOCRText(child, key); text != "" {
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
keys := make([]string, 0, len(value))
|
|
||||||
for key := range value {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
for _, key := range keys {
|
|
||||||
if isAggregateTextKey(key) ||
|
|
||||||
isResponseContainerKey(key) ||
|
|
||||||
isBlockContainerKey(key) ||
|
|
||||||
isMetadataKey(key) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if text := extractOverallOCRText(value[key], key); text != "" {
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
default:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func collectOCRParts(v any, parentKey string) []string {
|
|
||||||
switch value := v.(type) {
|
|
||||||
case string:
|
|
||||||
text := strings.TrimSpace(value)
|
|
||||||
if text == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if looksLikeJSON(text) {
|
|
||||||
var nested any
|
|
||||||
if err := json.Unmarshal([]byte(text), &nested); err == nil {
|
|
||||||
return collectOCRParts(nested, parentKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if isTextKey(parentKey) || isContainerTextKey(parentKey) {
|
|
||||||
return []string{text}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
case []any:
|
|
||||||
parts := make([]string, 0, len(value))
|
|
||||||
for _, item := range value {
|
|
||||||
parts = append(parts, collectOCRParts(item, parentKey)...)
|
|
||||||
}
|
|
||||||
return parts
|
|
||||||
case map[string]any:
|
|
||||||
parts := make([]string, 0)
|
|
||||||
for _, key := range priorityOCRKeys() {
|
|
||||||
if child, ok := value[key]; ok {
|
|
||||||
parts = append(parts, collectOCRParts(child, key)...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
keys := make([]string, 0, len(value))
|
|
||||||
for key := range value {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
for _, key := range keys {
|
|
||||||
if isPriorityOCRKey(key) || isMetadataKey(key) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
parts = append(parts, collectOCRParts(value[key], key)...)
|
|
||||||
}
|
|
||||||
return parts
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func aggregateTextKeys() []string {
|
|
||||||
return []string{
|
|
||||||
"content",
|
|
||||||
"Content",
|
|
||||||
"text",
|
|
||||||
"Text",
|
|
||||||
"full_text",
|
|
||||||
"FullText",
|
|
||||||
"fullText",
|
|
||||||
"plain_text",
|
|
||||||
"PlainText",
|
|
||||||
"plainText",
|
|
||||||
"recognized_text",
|
|
||||||
"RecognizedText",
|
|
||||||
"recognizedText",
|
|
||||||
"ocr_text",
|
|
||||||
"OCRText",
|
|
||||||
"ocrText",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func responseContainerKeys() []string {
|
|
||||||
return []string{
|
|
||||||
"Data",
|
|
||||||
"data",
|
|
||||||
"Result",
|
|
||||||
"result",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func priorityOCRKeys() []string {
|
|
||||||
return []string{
|
|
||||||
"Data",
|
|
||||||
"data",
|
|
||||||
"Result",
|
|
||||||
"result",
|
|
||||||
"content",
|
|
||||||
"Content",
|
|
||||||
"text",
|
|
||||||
"Text",
|
|
||||||
"DetectedText",
|
|
||||||
"detected_text",
|
|
||||||
"line_text",
|
|
||||||
"LineText",
|
|
||||||
"line_texts",
|
|
||||||
"LineTexts",
|
|
||||||
"word",
|
|
||||||
"Word",
|
|
||||||
"words",
|
|
||||||
"Words",
|
|
||||||
"words_result",
|
|
||||||
"WordsResult",
|
|
||||||
"prism_wordsInfo",
|
|
||||||
"prism_words_info",
|
|
||||||
"ocr_infos",
|
|
||||||
"OCRInfos",
|
|
||||||
"items",
|
|
||||||
"Items",
|
|
||||||
"blocks",
|
|
||||||
"Blocks",
|
|
||||||
"regions",
|
|
||||||
"Regions",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAggregateTextKey(key string) bool {
|
|
||||||
for _, item := range aggregateTextKeys() {
|
|
||||||
if item == key {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAggregateContainerKey(key string) bool {
|
|
||||||
return isAggregateTextKey(key) || isResponseContainerKey(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isResponseContainerKey(key string) bool {
|
|
||||||
for _, item := range responseContainerKeys() {
|
|
||||||
if item == key {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func isBlockContainerKey(key string) bool {
|
|
||||||
switch normalizeKey(key) {
|
|
||||||
case "wordsresult", "prismwordsinfo", "prismwords", "ocrinfos", "items",
|
|
||||||
"blocks", "regions", "words", "linetexts", "lines", "cells":
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isPriorityOCRKey(key string) bool {
|
|
||||||
for _, item := range priorityOCRKeys() {
|
|
||||||
if item == key {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func isTextKey(key string) bool {
|
|
||||||
switch normalizeKey(key) {
|
|
||||||
case "content", "text", "detectedtext", "linetext", "word", "words", "value":
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isContainerTextKey(key string) bool {
|
|
||||||
switch normalizeKey(key) {
|
|
||||||
case "data", "result", "linetexts", "texts":
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isMetadataKey(key string) bool {
|
|
||||||
switch normalizeKey(key) {
|
|
||||||
case "requestid", "request", "code", "status", "statuscode", "success",
|
|
||||||
"error", "errors", "message", "msg", "cost", "angle", "probability",
|
|
||||||
"confidence", "height", "width", "left", "top", "right", "bottom",
|
|
||||||
"x", "y", "responsemetadata":
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeKey(key string) string {
|
|
||||||
key = strings.ToLower(key)
|
|
||||||
key = strings.ReplaceAll(key, "_", "")
|
|
||||||
key = strings.ReplaceAll(key, "-", "")
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
|
|
||||||
func dedupeNonEmpty(parts []string) []string {
|
|
||||||
seen := map[string]struct{}{}
|
|
||||||
out := make([]string, 0, len(parts))
|
|
||||||
for _, part := range parts {
|
|
||||||
for _, line := range strings.Split(part, "\n") {
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
if line == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, ok := seen[line]; ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
seen[line] = struct{}{}
|
|
||||||
out = append(out, line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeOCRText(text string) string {
|
|
||||||
lines := strings.Split(strings.TrimSpace(text), "\n")
|
|
||||||
out := make([]string, 0, len(lines))
|
|
||||||
for _, line := range lines {
|
|
||||||
line = strings.TrimSpace(line)
|
|
||||||
if line != "" {
|
|
||||||
out = append(out, line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return strings.Join(out, "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
func looksLikeJSON(text string) bool {
|
|
||||||
return strings.HasPrefix(text, "{") || strings.HasPrefix(text, "[")
|
|
||||||
}
|
|
||||||
|
|
||||||
func stringValue(v any) string {
|
|
||||||
if s, ok := v.(string); ok {
|
|
||||||
return strings.TrimSpace(s)
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func numericCode(v any) (float64, bool) {
|
|
||||||
switch n := v.(type) {
|
|
||||||
case float64:
|
|
||||||
return n, true
|
|
||||||
case int:
|
|
||||||
return float64(n), true
|
|
||||||
case json.Number:
|
|
||||||
f, err := n.Float64()
|
|
||||||
return f, err == nil
|
|
||||||
default:
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isSuccessCode(code string) bool {
|
|
||||||
switch strings.ToLower(strings.TrimSpace(code)) {
|
|
||||||
case "", "ok", "success", "200", "10000":
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func canonicalURI(u *url.URL) string {
|
|
||||||
if u == nil || u.EscapedPath() == "" {
|
|
||||||
return "/"
|
|
||||||
}
|
|
||||||
return u.EscapedPath()
|
|
||||||
}
|
|
||||||
|
|
||||||
func canonicalQuery(u *url.URL) string {
|
|
||||||
if u == nil || u.RawQuery == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
values, _ := url.ParseQuery(u.RawQuery)
|
|
||||||
return values.Encode()
|
|
||||||
}
|
|
||||||
|
|
||||||
func canonicalHeaders(req *http.Request, signedHeaders []string) string {
|
|
||||||
lines := make([]string, 0, len(signedHeaders))
|
|
||||||
for _, key := range signedHeaders {
|
|
||||||
var value string
|
|
||||||
if key == "host" {
|
|
||||||
value = req.URL.Host
|
|
||||||
} else {
|
|
||||||
value = req.Header.Get(key)
|
|
||||||
}
|
|
||||||
lines = append(lines, key+":"+normalizeHeaderValue(value)+"\n")
|
|
||||||
}
|
|
||||||
return strings.Join(lines, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeHeaderValue(value string) string {
|
|
||||||
return strings.Join(strings.Fields(strings.TrimSpace(value)), " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
func volcengineSigningKey(secret, date, region, service string) []byte {
|
|
||||||
kDate := hmacSHA256([]byte(secret), []byte(date))
|
|
||||||
kRegion := hmacSHA256(kDate, []byte(region))
|
|
||||||
kService := hmacSHA256(kRegion, []byte(service))
|
|
||||||
return hmacSHA256(kService, []byte("request"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func hmacSHA256(key, data []byte) []byte {
|
|
||||||
mac := hmac.New(sha256.New, key)
|
|
||||||
mac.Write(data)
|
|
||||||
return mac.Sum(nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func hmacSHA256Hex(key, data []byte) string {
|
|
||||||
return hex.EncodeToString(hmacSHA256(key, data))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sha256Hex(data []byte) string {
|
|
||||||
sum := sha256.Sum256(data)
|
|
||||||
return hex.EncodeToString(sum[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func randomNonce() string {
|
|
||||||
buf := make([]byte, 16)
|
|
||||||
if _, err := rand.Read(buf); err != nil {
|
|
||||||
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
func compactBody(data []byte) string {
|
|
||||||
text := strings.TrimSpace(string(data))
|
|
||||||
if text == "" {
|
|
||||||
return "<empty body>"
|
|
||||||
}
|
|
||||||
if len(text) > 800 {
|
|
||||||
return text[:800] + "..."
|
|
||||||
}
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
@@ -1,177 +0,0 @@
|
|||||||
package ocr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
type roundTripFunc func(*http.Request) (*http.Response, error)
|
|
||||||
|
|
||||||
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
||||||
return f(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAliyunRecognizeTextSignsRawPNGRequest(t *testing.T) {
|
|
||||||
var requestBody []byte
|
|
||||||
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
|
||||||
if r.URL.Host != "ocr-api.cn-hangzhou.aliyuncs.com" {
|
|
||||||
t.Fatalf("unexpected host %s", r.URL.Host)
|
|
||||||
}
|
|
||||||
if got := r.Header.Get("x-acs-action"); got != "RecognizeGeneral" {
|
|
||||||
t.Fatalf("unexpected action %q", got)
|
|
||||||
}
|
|
||||||
if got := r.Header.Get("x-acs-version"); got != "2021-07-07" {
|
|
||||||
t.Fatalf("unexpected version %q", got)
|
|
||||||
}
|
|
||||||
if got := r.Header.Get("x-acs-date"); got != "2026-07-08T01:02:03Z" {
|
|
||||||
t.Fatalf("unexpected date %q", got)
|
|
||||||
}
|
|
||||||
if got := r.Header.Get("Authorization"); !strings.Contains(got, "ACS3-HMAC-SHA256 Credential=ak") {
|
|
||||||
t.Fatalf("unexpected auth header %q", got)
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
requestBody, err = io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("read body: %v", err)
|
|
||||||
}
|
|
||||||
return &http.Response{
|
|
||||||
StatusCode: http.StatusOK,
|
|
||||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
||||||
Body: io.NopCloser(bytes.NewBufferString(
|
|
||||||
`{"Data":"{\"content\":\"第一行\\n第二行\"}"}`,
|
|
||||||
)),
|
|
||||||
}, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
client, err := NewClient(domain.OCRProviderAliyun, domain.OCRProviderConfig{
|
|
||||||
Endpoint: "https://ocr-api.cn-hangzhou.aliyuncs.com",
|
|
||||||
AccessKeyID: "ak",
|
|
||||||
AccessKeySecret: "sk",
|
|
||||||
Action: "RecognizeGeneral",
|
|
||||||
Version: "2021-07-07",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("new client: %v", err)
|
|
||||||
}
|
|
||||||
client.httpClient.Transport = transport
|
|
||||||
client.now = func() time.Time { return time.Date(2026, 7, 8, 1, 2, 3, 0, time.UTC) }
|
|
||||||
client.nonce = func() string { return "nonce" }
|
|
||||||
|
|
||||||
text, err := client.RecognizeText(context.Background(), []byte("png"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("recognize text: %v", err)
|
|
||||||
}
|
|
||||||
if string(requestBody) != "png" {
|
|
||||||
t.Fatalf("expected raw png body, got %q", string(requestBody))
|
|
||||||
}
|
|
||||||
if text != "第一行\n第二行" {
|
|
||||||
t.Fatalf("unexpected OCR text %q", text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestVolcengineRecognizeTextSignsBase64JSONRequest(t *testing.T) {
|
|
||||||
var requestBody map[string]string
|
|
||||||
transport := roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
|
||||||
if got := r.URL.Query().Get("Action"); got != "OCRNormal" {
|
|
||||||
t.Fatalf("unexpected action %q", got)
|
|
||||||
}
|
|
||||||
if got := r.URL.Query().Get("Version"); got != "2020-08-26" {
|
|
||||||
t.Fatalf("unexpected version %q", got)
|
|
||||||
}
|
|
||||||
if got := r.Header.Get("X-Date"); got != "20260708T010203Z" {
|
|
||||||
t.Fatalf("unexpected x-date %q", got)
|
|
||||||
}
|
|
||||||
if got := r.Header.Get("Authorization"); !strings.Contains(got, "HMAC-SHA256 Credential=ak/20260708/cn-north-1/cv/request") {
|
|
||||||
t.Fatalf("unexpected auth header %q", got)
|
|
||||||
}
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
|
|
||||||
t.Fatalf("decode request: %v", err)
|
|
||||||
}
|
|
||||||
return &http.Response{
|
|
||||||
StatusCode: http.StatusOK,
|
|
||||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
||||||
Body: io.NopCloser(bytes.NewBufferString(
|
|
||||||
`{"ResponseMetadata":{"RequestId":"r"},"Result":{"LineTexts":["你好","世界"]}}`,
|
|
||||||
)),
|
|
||||||
}, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
client, err := NewClient(domain.OCRProviderVolcengine, domain.OCRProviderConfig{
|
|
||||||
Endpoint: "https://visual.volcengineapi.com",
|
|
||||||
AccessKeyID: "ak",
|
|
||||||
AccessKeySecret: "sk",
|
|
||||||
Region: "cn-north-1",
|
|
||||||
Service: "cv",
|
|
||||||
Action: "OCRNormal",
|
|
||||||
Version: "2020-08-26",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("new client: %v", err)
|
|
||||||
}
|
|
||||||
client.httpClient.Transport = transport
|
|
||||||
client.now = func() time.Time { return time.Date(2026, 7, 8, 1, 2, 3, 0, time.UTC) }
|
|
||||||
|
|
||||||
text, err := client.RecognizeText(context.Background(), []byte("png"))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("recognize text: %v", err)
|
|
||||||
}
|
|
||||||
if requestBody["image_base64"] != base64.StdEncoding.EncodeToString([]byte("png")) {
|
|
||||||
t.Fatalf("expected base64 image body, got %#v", requestBody)
|
|
||||||
}
|
|
||||||
if text != "你好\n世界" {
|
|
||||||
t.Fatalf("unexpected OCR text %q", text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseOCRTextSupportsWordsResult(t *testing.T) {
|
|
||||||
text, err := parseOCRText([]byte(`{"data":{"words_result":[{"words":"foo"},{"words":"bar"}]}}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("parse ocr text: %v", err)
|
|
||||||
}
|
|
||||||
if text != "foo\nbar" {
|
|
||||||
t.Fatalf("unexpected OCR text %q", text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseOCRTextPrefersOverallContentOverBlocks(t *testing.T) {
|
|
||||||
text, err := parseOCRText([]byte(`{
|
|
||||||
"Data": "{\"content\":\"整体识别文本\",\"prism_wordsInfo\":[{\"word\":\"整体\"},{\"word\":\"识别\"},{\"word\":\"文本\"}]}"
|
|
||||||
}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("parse ocr text: %v", err)
|
|
||||||
}
|
|
||||||
if text != "整体识别文本" {
|
|
||||||
t.Fatalf("expected overall content only, got %q", text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseOCRTextPrefersResultTextOverWordsResult(t *testing.T) {
|
|
||||||
text, err := parseOCRText([]byte(`{
|
|
||||||
"Result": {
|
|
||||||
"text": "hello world",
|
|
||||||
"words_result": [{"words":"hello"},{"words":"world"}]
|
|
||||||
}
|
|
||||||
}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("parse ocr text: %v", err)
|
|
||||||
}
|
|
||||||
if text != "hello world" {
|
|
||||||
t.Fatalf("expected result text only, got %q", text)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseOCRTextReturnsProviderError(t *testing.T) {
|
|
||||||
_, err := parseOCRText([]byte(`{"ResponseMetadata":{"Error":{"Code":"BadRequest","Message":"bad image"}}}`))
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "bad image") {
|
|
||||||
t.Fatalf("expected provider error, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -101,20 +101,6 @@ func (p *S3Provider) BuildPublicURL(key string) string {
|
|||||||
return endpoint + "/" + p.cfg.Bucket + "/" + key
|
return endpoint + "/" + p.cfg.Bucket + "/" + key
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes a previously uploaded object by key.
|
|
||||||
//
|
|
||||||
// The summary pipeline uses this to clean up the temporary screenshot it had
|
|
||||||
// to upload only so a remote multimodal model could fetch it via public URL.
|
|
||||||
func (p *S3Provider) Delete(ctx context.Context, key string) error {
|
|
||||||
if _, err := p.client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
|
||||||
Bucket: awsv2.String(p.cfg.Bucket),
|
|
||||||
Key: awsv2.String(key),
|
|
||||||
}); err != nil {
|
|
||||||
return fmt.Errorf("s3 delete object: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConnection performs a small put + delete to verify credentials and
|
// TestConnection performs a small put + delete to verify credentials and
|
||||||
// bucket reachability. Used by the "Test connection" button in settings.
|
// bucket reachability. Used by the "Test connection" button in settings.
|
||||||
func (p *S3Provider) TestConnection(ctx context.Context) error {
|
func (p *S3Provider) TestConnection(ctx context.Context) error {
|
||||||
|
|||||||
@@ -0,0 +1,200 @@
|
|||||||
|
// bgo_uploader.go — an SSHUploader implementation that delegates to the
|
||||||
|
// internal `bgo scp` client. bgo is ByteDance's internal credential/auth
|
||||||
|
// management tool; it wraps ssh/scp and handles login transparently once the
|
||||||
|
// user has configured it (see the Lark guide linked in the Settings UI).
|
||||||
|
//
|
||||||
|
// 设计理由 (为什么不能直接 exec bgo, 也不能复用 KerberosUploader):
|
||||||
|
// - bgo 强制要求在 PTY (伪终端) 下运行: 它会对 stdin/stdout 做终端 ioctl
|
||||||
|
// (resize pty). 没有 PTY 时直接 panic ("operation not supported by
|
||||||
|
// device"). 因此必须用系统 /usr/bin/script 为其分配一个伪终端:
|
||||||
|
// script -q /dev/null <bgo> scp <src> <user@host:dst>
|
||||||
|
// 这样既不引入额外的 PTY 依赖, 又满足 bgo 的运行约束.
|
||||||
|
// - bgo scp 不会自动创建远程目录, 且 bgo ssh 无法执行远程命令 (会 panic),
|
||||||
|
// 所以无法像 Kerberos 那样先 `mkdir -p`. 故 bgo 模式只能把文件平铺上传
|
||||||
|
// 到远端用户的 $HOME 根目录 (文件名内嵌时间戳保证唯一), 上层 service
|
||||||
|
// 负责生成扁平文件名.
|
||||||
|
// - bgo 自带鉴权, 不需要也不应传 GSSAPI / 密码 / 公钥相关的 ssh 选项.
|
||||||
|
package ssh
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mmmy/snapgo/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
// systemScriptPath is the absolute path to the macOS BSD `script` utility,
|
||||||
|
// which we use purely to allocate a PTY for bgo. Absolute path avoids PATH
|
||||||
|
// hijacking and the minimal PATH GUI apps inherit on macOS.
|
||||||
|
const systemScriptPath = "/usr/bin/script"
|
||||||
|
|
||||||
|
// bgoSearchPaths lists the locations we probe for the bgo binary when it is
|
||||||
|
// not already resolvable via $PATH. GUI apps on macOS launch with a minimal
|
||||||
|
// PATH that usually omits ~/.local/bin and Homebrew dirs, so we look there
|
||||||
|
// explicitly rather than failing with a confusing "command not found".
|
||||||
|
func bgoSearchPaths() []string {
|
||||||
|
home, _ := os.UserHomeDir()
|
||||||
|
return []string{
|
||||||
|
filepath.Join(home, ".local", "bin", "bgo"),
|
||||||
|
"/usr/local/bin/bgo",
|
||||||
|
"/opt/homebrew/bin/bgo",
|
||||||
|
"/opt/bin/bgo",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BgoUploader satisfies application.SSHUploader by shelling out to `bgo scp`
|
||||||
|
// under a PTY allocated via the system `script` utility.
|
||||||
|
type BgoUploader struct {
|
||||||
|
cfg domain.SSHConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBgoUploader returns an uploader that delegates to the bgo client.
|
||||||
|
func NewBgoUploader(cfg domain.SSHConfig) *BgoUploader {
|
||||||
|
return &BgoUploader{cfg: cfg}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resolveBgoBinary locates the bgo executable, first via $PATH and then via
|
||||||
|
// the well-known install locations. Returns a clear error pointing the user
|
||||||
|
// to the setup guide when bgo cannot be found.
|
||||||
|
func resolveBgoBinary() (string, error) {
|
||||||
|
if p, err := exec.LookPath("bgo"); err == nil {
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
for _, candidate := range bgoSearchPaths() {
|
||||||
|
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
|
||||||
|
return candidate, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("bgo executable not found — install bgo and add it to PATH (see the setup guide)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upload writes data to a flat remote filename in the user's remote $HOME by
|
||||||
|
// staging it locally and invoking `bgo scp` once under a PTY.
|
||||||
|
//
|
||||||
|
// remoteRelPath is expected to be a flat filename (no directory components),
|
||||||
|
// because bgo cannot create remote directories. Any directory components are
|
||||||
|
// collapsed to the base name defensively.
|
||||||
|
func (u *BgoUploader) Upload(ctx context.Context, remoteRelPath string, data []byte, mode os.FileMode) error {
|
||||||
|
start := time.Now()
|
||||||
|
// Collapse to a flat filename: bgo scp cannot create remote directories.
|
||||||
|
name := path.Base(normaliseRemotePath(remoteRelPath))
|
||||||
|
if name == "" || name == "." || name == "/" {
|
||||||
|
return fmt.Errorf("ssh(bgo): remote filename is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
bgoBin, err := resolveBgoBinary()
|
||||||
|
if err != nil {
|
||||||
|
sshLog().Error("bgo uploader: binary not found", "err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sshLog().Info("bgo uploader start",
|
||||||
|
"host", u.cfg.Host, "user", u.cfg.User, "port", u.cfg.Port,
|
||||||
|
"remote_file", name, "size", len(data), "bgo", bgoBin)
|
||||||
|
|
||||||
|
// 1. Stage the payload to a local temp file for scp to read.
|
||||||
|
tmp, err := os.CreateTemp("", "snapgo-bgo-*.png")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("ssh(bgo): create temp: %w", err)
|
||||||
|
}
|
||||||
|
tmpPath := tmp.Name()
|
||||||
|
defer os.Remove(tmpPath)
|
||||||
|
if _, err := tmp.Write(data); err != nil {
|
||||||
|
_ = tmp.Close()
|
||||||
|
return fmt.Errorf("ssh(bgo): write temp: %w", err)
|
||||||
|
}
|
||||||
|
if err := tmp.Close(); err != nil {
|
||||||
|
return fmt.Errorf("ssh(bgo): close temp: %w", err)
|
||||||
|
}
|
||||||
|
if err := os.Chmod(tmpPath, mode.Perm()); err != nil {
|
||||||
|
sshLog().Debug("bgo uploader chmod temp failed", "err", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. bgo scp <tmp> <user@host:name> under a PTY (via `script`).
|
||||||
|
remoteTarget := fmt.Sprintf("%s@%s:%s", u.cfg.User, bracketHost(u.cfg.Host), name)
|
||||||
|
bgoArgs := u.bgoScpArgs(bgoBin, tmpPath, remoteTarget)
|
||||||
|
if err := u.runUnderPTY(ctx, bgoArgs); err != nil {
|
||||||
|
sshLog().Error("bgo uploader scp failed",
|
||||||
|
"remote_file", name, "elapsed", time.Since(start), "err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sshLog().Info("bgo uploader done",
|
||||||
|
"remote_file", name, "size", len(data), "total_elapsed", time.Since(start))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bgoScpArgs builds the argument vector "bgo scp [-P port] <src> <dst>".
|
||||||
|
// bgo manages its own auth, so we pass no ssh -o options here.
|
||||||
|
func (u *BgoUploader) bgoScpArgs(bgoBin, src, dst string) []string {
|
||||||
|
args := []string{bgoBin, "scp"}
|
||||||
|
if u.cfg.Port > 0 && u.cfg.Port != 22 {
|
||||||
|
args = append(args, "-P", strconv.Itoa(u.cfg.Port))
|
||||||
|
}
|
||||||
|
args = append(args, src, dst)
|
||||||
|
return args
|
||||||
|
}
|
||||||
|
|
||||||
|
// runUnderPTY runs the given command vector under a PTY allocated by the
|
||||||
|
// system `script` utility, capturing combined output for diagnostics.
|
||||||
|
//
|
||||||
|
// macOS BSD script signature: `script -q <typescript-file> command [args...]`.
|
||||||
|
// We discard the typescript by writing to /dev/null.
|
||||||
|
func (u *BgoUploader) runUnderPTY(ctx context.Context, cmdVec []string) error {
|
||||||
|
args := append([]string{"-q", "/dev/null"}, cmdVec...)
|
||||||
|
cmd := exec.CommandContext(ctx, systemScriptPath, args...)
|
||||||
|
var combined bytes.Buffer
|
||||||
|
cmd.Stdout = &combined
|
||||||
|
cmd.Stderr = &combined
|
||||||
|
sshLog().Debug("bgo exec", "args", strings.Join(args, " "))
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
msg := strings.TrimSpace(combined.String())
|
||||||
|
if msg == "" {
|
||||||
|
msg = err.Error()
|
||||||
|
}
|
||||||
|
return fmt.Errorf("bgo scp failed: %s", msg)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bracketHost wraps a bare IPv6 literal in [] so the "user@host:path" target
|
||||||
|
// parses correctly (e.g. fdbd:dc03:8:379::130 → [fdbd:dc03:8:379::130]).
|
||||||
|
// IPv4 addresses and hostnames are returned unchanged. Already-bracketed
|
||||||
|
// inputs are left as-is.
|
||||||
|
func bracketHost(host string) string {
|
||||||
|
host = strings.TrimSpace(host)
|
||||||
|
if host == "" {
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(host, "[") {
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
// An IPv6 literal contains multiple ':' separators; a hostname:port style
|
||||||
|
// is not expected here because the port lives in cfg.Port.
|
||||||
|
if strings.Count(host, ":") >= 2 {
|
||||||
|
return "[" + host + "]"
|
||||||
|
}
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBgoConnection verifies that the bgo binary is resolvable. A real
|
||||||
|
// transfer test is intentionally avoided: bgo requires a PTY and a live
|
||||||
|
// remote, and bgo ssh cannot run a harmless remote probe command (it panics).
|
||||||
|
// Confirming bgo is installed and on PATH is the most useful signal we can
|
||||||
|
// give without performing an actual upload.
|
||||||
|
func TestBgoConnection(ctx context.Context, cfg domain.SSHConfig) error {
|
||||||
|
sshLog().Info("bgo test connection start", "host", cfg.Host, "user", cfg.User)
|
||||||
|
bin, err := resolveBgoBinary()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sshLog().Info("bgo test connection ok", "bgo", bin)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,213 +0,0 @@
|
|||||||
package ssh
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/sftp"
|
|
||||||
|
|
||||||
"github.com/mmmy/snapgo/internal/domain"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SFTPUploader transfers screenshots through the SSH File Transfer Protocol.
|
|
||||||
// It lives beside the SCP client so both protocols share the same SSH
|
|
||||||
// authentication and host-key implementation without duplicating credential
|
|
||||||
// discovery code.
|
|
||||||
type SFTPUploader struct {
|
|
||||||
cfg domain.FTPConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSFTPUploader returns a short-lived SFTP adapter. A fresh SSH/SFTP
|
|
||||||
// connection is opened for every upload or connection probe.
|
|
||||||
func NewSFTPUploader(cfg domain.FTPConfig) *SFTPUploader {
|
|
||||||
return &SFTPUploader{cfg: cfg}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *SFTPUploader) sshConfig() domain.SSHConfig {
|
|
||||||
return domain.SSHConfig{
|
|
||||||
Host: u.cfg.Host,
|
|
||||||
Port: u.cfg.Port,
|
|
||||||
User: u.cfg.User,
|
|
||||||
AuthMethod: u.cfg.AuthMethod,
|
|
||||||
Password: u.cfg.Password,
|
|
||||||
StrictHostKey: u.cfg.StrictHostKey,
|
|
||||||
KnownHostsPath: u.cfg.KnownHostsPath,
|
|
||||||
ConnectTimeoutSecs: u.cfg.ConnectTimeoutSecs,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *SFTPUploader) dial(ctx context.Context) (*Client, *sftp.Client, error) {
|
|
||||||
sshClient, err := Dial(ctx, u.sshConfig())
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, fmt.Errorf("sftp: %w", err)
|
|
||||||
}
|
|
||||||
sftpClient, err := sftp.NewClient(sshClient.client)
|
|
||||||
if err != nil {
|
|
||||||
_ = sshClient.Close()
|
|
||||||
return nil, nil, fmt.Errorf("sftp: start subsystem: %w", err)
|
|
||||||
}
|
|
||||||
return sshClient, sftpClient, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upload writes data to a temporary file and atomically exposes it at the
|
|
||||||
// final relative path after the transfer completes.
|
|
||||||
func (u *SFTPUploader) Upload(ctx context.Context, remoteRelPath string, data []byte) error {
|
|
||||||
cleaned, err := validateSFTPRemotePath(remoteRelPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sshClient, sftpClient, err := u.dial(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
done := closeSFTPOnCancel(ctx, sshClient, sftpClient)
|
|
||||||
defer sshClient.Close()
|
|
||||||
defer sftpClient.Close()
|
|
||||||
defer close(done)
|
|
||||||
|
|
||||||
dir := path.Dir(cleaned)
|
|
||||||
if dir != "." {
|
|
||||||
if err := sftpClient.MkdirAll(dir); err != nil {
|
|
||||||
return fmt.Errorf("sftp: create directory %s: %w", dir, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tempPath := path.Join(dir, temporarySFTPName(path.Base(cleaned)))
|
|
||||||
removeTemp := true
|
|
||||||
defer func() {
|
|
||||||
if removeTemp {
|
|
||||||
_ = sftpClient.Remove(tempPath)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
file, err := sftpClient.OpenFile(tempPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("sftp: create temporary file: %w", err)
|
|
||||||
}
|
|
||||||
reader := &sftpContextReader{ctx: ctx, reader: bytes.NewReader(data)}
|
|
||||||
_, copyErr := io.Copy(file, reader)
|
|
||||||
if copyErr == nil {
|
|
||||||
copyErr = file.Chmod(0o644)
|
|
||||||
}
|
|
||||||
closeErr := file.Close()
|
|
||||||
if copyErr != nil {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
return fmt.Errorf("sftp: write %s: %w", cleaned, copyErr)
|
|
||||||
}
|
|
||||||
if closeErr != nil {
|
|
||||||
return fmt.Errorf("sftp: close %s: %w", cleaned, closeErr)
|
|
||||||
}
|
|
||||||
if err := sftpClient.Rename(tempPath, cleaned); err != nil {
|
|
||||||
return fmt.Errorf("sftp: commit %s: %w", cleaned, err)
|
|
||||||
}
|
|
||||||
removeTemp = false
|
|
||||||
sshLog().Info("SFTP upload ok", "remote_path", cleaned, "size", len(data))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConnection verifies SFTP write/delete permissions with a small probe.
|
|
||||||
func (u *SFTPUploader) TestConnection(ctx context.Context) error {
|
|
||||||
probe, err := sftpProbePath(u.cfg.PathPrefix)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sshClient, sftpClient, err := u.dial(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
done := closeSFTPOnCancel(ctx, sshClient, sftpClient)
|
|
||||||
defer sshClient.Close()
|
|
||||||
defer sftpClient.Close()
|
|
||||||
defer close(done)
|
|
||||||
|
|
||||||
dir := path.Dir(probe)
|
|
||||||
if dir != "." {
|
|
||||||
if err := sftpClient.MkdirAll(dir); err != nil {
|
|
||||||
return fmt.Errorf("sftp probe: create directory: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file, err := sftpClient.OpenFile(probe, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("sftp probe: create: %w", err)
|
|
||||||
}
|
|
||||||
_, writeErr := file.Write([]byte("snapgo"))
|
|
||||||
closeErr := file.Close()
|
|
||||||
if writeErr != nil {
|
|
||||||
_ = sftpClient.Remove(probe)
|
|
||||||
return fmt.Errorf("sftp probe: write: %w", writeErr)
|
|
||||||
}
|
|
||||||
if closeErr != nil {
|
|
||||||
_ = sftpClient.Remove(probe)
|
|
||||||
return fmt.Errorf("sftp probe: close: %w", closeErr)
|
|
||||||
}
|
|
||||||
if err := sftpClient.Remove(probe); err != nil {
|
|
||||||
return fmt.Errorf("sftp probe: delete: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func closeSFTPOnCancel(ctx context.Context, sshClient *Client, sftpClient *sftp.Client) chan struct{} {
|
|
||||||
done := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
_ = sftpClient.Close()
|
|
||||||
_ = sshClient.Close()
|
|
||||||
case <-done:
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return done
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateSFTPRemotePath(value string) (string, error) {
|
|
||||||
value = strings.TrimSpace(value)
|
|
||||||
if value == "" || strings.HasPrefix(value, "/") || strings.HasPrefix(value, "~") {
|
|
||||||
return "", fmt.Errorf("sftp: remote path must be relative to the login directory")
|
|
||||||
}
|
|
||||||
if strings.ContainsAny(value, "\x00\r\n\\") {
|
|
||||||
return "", fmt.Errorf("sftp: remote path contains an invalid character")
|
|
||||||
}
|
|
||||||
for _, segment := range strings.Split(value, "/") {
|
|
||||||
if segment == ".." {
|
|
||||||
return "", fmt.Errorf("sftp: remote path must not contain '..'")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cleaned := path.Clean(value)
|
|
||||||
if cleaned == "." || path.Base(cleaned) == "." || path.Base(cleaned) == ".." {
|
|
||||||
return "", fmt.Errorf("sftp: remote filename is empty")
|
|
||||||
}
|
|
||||||
return cleaned, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sftpProbePath(prefix string) (string, error) {
|
|
||||||
prefix = strings.TrimSpace(prefix)
|
|
||||||
prefix = strings.TrimPrefix(prefix, "~")
|
|
||||||
prefix = strings.TrimLeft(prefix, "/")
|
|
||||||
return validateSFTPRemotePath(path.Join(prefix, fmt.Sprintf(".snapgo-probe-%d", time.Now().UnixNano())))
|
|
||||||
}
|
|
||||||
|
|
||||||
func temporarySFTPName(base string) string {
|
|
||||||
return fmt.Sprintf(".%s.snapgo-upload-%d", base, time.Now().UnixNano())
|
|
||||||
}
|
|
||||||
|
|
||||||
type sftpContextReader struct {
|
|
||||||
ctx context.Context
|
|
||||||
reader io.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *sftpContextReader) Read(p []byte) (int, error) {
|
|
||||||
select {
|
|
||||||
case <-r.ctx.Done():
|
|
||||||
return 0, r.ctx.Err()
|
|
||||||
default:
|
|
||||||
return r.reader.Read(p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package ssh
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestValidateSFTPRemotePathRejectsTraversal(t *testing.T) {
|
|
||||||
if got, err := validateSFTPRemotePath("snapgo/2026/image.png"); err != nil || got != "snapgo/2026/image.png" {
|
|
||||||
t.Fatalf("valid path: got %q err=%v", got, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, value := range []string{
|
|
||||||
"",
|
|
||||||
"/absolute/image.png",
|
|
||||||
"~/image.png",
|
|
||||||
"../image.png",
|
|
||||||
"snapgo/../../image.png",
|
|
||||||
"snapgo\\image.png",
|
|
||||||
} {
|
|
||||||
if _, err := validateSFTPRemotePath(value); err == nil {
|
|
||||||
t.Errorf("expected %q to be rejected", value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSFTPProbePathRejectsTraversalPrefix(t *testing.T) {
|
|
||||||
if _, err := sftpProbePath("../../outside"); err == nil {
|
|
||||||
t.Fatal("expected traversal prefix to fail")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -33,10 +33,10 @@ func Start(cbs Callbacks) (start, stop func()) {
|
|||||||
systray.SetTemplateIcon(templateIconBytes, regularIconBytes)
|
systray.SetTemplateIcon(templateIconBytes, regularIconBytes)
|
||||||
systray.SetTooltip("SnapGo")
|
systray.SetTooltip("SnapGo")
|
||||||
|
|
||||||
mCapture := systray.AddMenuItem("截图", "Take a region screenshot")
|
mCapture := systray.AddMenuItem("Capture screenshot", "Take a region screenshot")
|
||||||
mSettings := systray.AddMenuItem("设置", "Open settings window")
|
mSettings := systray.AddMenuItem("Settings…", "Open settings window")
|
||||||
systray.AddSeparator()
|
systray.AddSeparator()
|
||||||
mQuit := systray.AddMenuItem("退出", "Quit the application")
|
mQuit := systray.AddMenuItem("Quit SnapGo", "Quit the application")
|
||||||
|
|
||||||
// Pump menu clicks on a dedicated goroutine. Channel sends from
|
// Pump menu clicks on a dedicated goroutine. Channel sends from
|
||||||
// systray are non-blocking, so a slow user callback does not back
|
// systray are non-blocking, so a slow user callback does not back
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ func main() {
|
|||||||
|
|
||||||
err := wails.Run(&options.App{
|
err := wails.Run(&options.App{
|
||||||
Title: "SnapGo",
|
Title: "SnapGo",
|
||||||
Width: 1000,
|
Width: 1080,
|
||||||
Height: 820,
|
Height: 720,
|
||||||
MinWidth: 720,
|
MinWidth: 720,
|
||||||
MinHeight: 520,
|
MinHeight: 520,
|
||||||
AssetServer: &assetserver.Options{
|
AssetServer: &assetserver.Options{
|
||||||
@@ -74,7 +74,7 @@ func main() {
|
|||||||
WindowIsTranslucent: true,
|
WindowIsTranslucent: true,
|
||||||
About: &mac.AboutInfo{
|
About: &mac.AboutInfo{
|
||||||
Title: "SnapGo",
|
Title: "SnapGo",
|
||||||
Message: "Cross-platform screenshot tool with one-click upload to S3, FTP, SFTP, or SSH.",
|
Message: "Cross-platform screenshot tool with one-click upload to S3.",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -74,42 +74,6 @@ func nativeOverlaySaveRemote(x, y, w, h C.int, annotationsJSON *C.char) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
//export nativeOverlayUploadFTP
|
|
||||||
func nativeOverlayUploadFTP(x, y, w, h C.int, annotationsJSON *C.char) {
|
|
||||||
app := consumeNativeOverlayApp()
|
|
||||||
if app == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
result := nativeCaptureResult(x, y, w, h, annotationsJSON)
|
|
||||||
go func() {
|
|
||||||
_ = app.UploadNativeRegionToFTP(result)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
//export nativeOverlaySummarize
|
|
||||||
func nativeOverlaySummarize(x, y, w, h C.int, annotationsJSON *C.char) {
|
|
||||||
app := consumeNativeOverlayApp()
|
|
||||||
if app == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
result := nativeCaptureResult(x, y, w, h, annotationsJSON)
|
|
||||||
go func() {
|
|
||||||
_ = app.SummarizeNativeRegion(result)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
//export nativeOverlayOCR
|
|
||||||
func nativeOverlayOCR(x, y, w, h C.int, annotationsJSON *C.char) {
|
|
||||||
app := consumeNativeOverlayApp()
|
|
||||||
if app == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
result := nativeCaptureResult(x, y, w, h, annotationsJSON)
|
|
||||||
go func() {
|
|
||||||
_ = app.ExtractTextNativeRegion(result)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
//export nativeOverlayCancel
|
//export nativeOverlayCancel
|
||||||
func nativeOverlayCancel() {
|
func nativeOverlayCancel() {
|
||||||
app := consumeNativeOverlayApp()
|
app := consumeNativeOverlayApp()
|
||||||
|
|||||||
+83
-941
File diff suppressed because it is too large
Load Diff
@@ -1,163 +0,0 @@
|
|||||||
//go:build darwin
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
/*
|
|
||||||
#cgo CFLAGS: -x objective-c -fobjc-arc -fblocks
|
|
||||||
#cgo LDFLAGS: -framework AppKit -framework QuartzCore
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <dispatch/dispatch.h>
|
|
||||||
#import <AppKit/AppKit.h>
|
|
||||||
#import <QuartzCore/QuartzCore.h>
|
|
||||||
|
|
||||||
static NSPanel *operationPanel = nil;
|
|
||||||
static NSTextField *operationTitleLabel = nil;
|
|
||||||
static NSTextField *operationDetailLabel = nil;
|
|
||||||
static NSTextField *operationIconLabel = nil;
|
|
||||||
static NSProgressIndicator *operationSpinner = nil;
|
|
||||||
|
|
||||||
static NSString *statusString(const char *value) {
|
|
||||||
if (value == NULL) {
|
|
||||||
return @"";
|
|
||||||
}
|
|
||||||
NSString *text = [NSString stringWithUTF8String:value];
|
|
||||||
return text ?: @"";
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ensureOperationPanel(void) {
|
|
||||||
if (operationPanel != nil) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
NSRect frame = NSMakeRect(0, 0, 360, 118);
|
|
||||||
operationPanel = [[NSPanel alloc]
|
|
||||||
initWithContentRect:frame
|
|
||||||
styleMask:NSWindowStyleMaskBorderless
|
|
||||||
backing:NSBackingStoreBuffered
|
|
||||||
defer:NO];
|
|
||||||
[operationPanel setOpaque:NO];
|
|
||||||
[operationPanel setBackgroundColor:[NSColor clearColor]];
|
|
||||||
[operationPanel setLevel:NSFloatingWindowLevel];
|
|
||||||
[operationPanel setHidesOnDeactivate:NO];
|
|
||||||
[operationPanel setCollectionBehavior:
|
|
||||||
NSWindowCollectionBehaviorCanJoinAllSpaces |
|
|
||||||
NSWindowCollectionBehaviorFullScreenAuxiliary |
|
|
||||||
NSWindowCollectionBehaviorStationary];
|
|
||||||
|
|
||||||
NSView *root = [[NSView alloc] initWithFrame:frame];
|
|
||||||
[root setWantsLayer:YES];
|
|
||||||
[[root layer] setCornerRadius:14];
|
|
||||||
[[root layer] setBackgroundColor:[[NSColor colorWithCalibratedWhite:0.08 alpha:0.92] CGColor]];
|
|
||||||
[[root layer] setShadowColor:[[NSColor blackColor] CGColor]];
|
|
||||||
[[root layer] setShadowOpacity:0.28];
|
|
||||||
[[root layer] setShadowRadius:18];
|
|
||||||
[[root layer] setShadowOffset:CGSizeMake(0, -8)];
|
|
||||||
|
|
||||||
operationSpinner = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(24, 42, 34, 34)];
|
|
||||||
[operationSpinner setStyle:NSProgressIndicatorStyleSpinning];
|
|
||||||
[operationSpinner setIndeterminate:YES];
|
|
||||||
|
|
||||||
operationIconLabel = [NSTextField labelWithString:@"OK"];
|
|
||||||
[operationIconLabel setFrame:NSMakeRect(21, 43, 42, 30)];
|
|
||||||
[operationIconLabel setAlignment:NSTextAlignmentCenter];
|
|
||||||
[operationIconLabel setFont:[NSFont systemFontOfSize:17 weight:NSFontWeightSemibold]];
|
|
||||||
[operationIconLabel setTextColor:[NSColor colorWithCalibratedRed:74.0/255.0 green:222.0/255.0 blue:128.0/255.0 alpha:1.0]];
|
|
||||||
[operationIconLabel setHidden:YES];
|
|
||||||
|
|
||||||
operationTitleLabel = [NSTextField labelWithString:@""];
|
|
||||||
[operationTitleLabel setFrame:NSMakeRect(76, 61, 260, 24)];
|
|
||||||
[operationTitleLabel setFont:[NSFont systemFontOfSize:15 weight:NSFontWeightSemibold]];
|
|
||||||
[operationTitleLabel setTextColor:[NSColor whiteColor]];
|
|
||||||
|
|
||||||
operationDetailLabel = [NSTextField labelWithString:@""];
|
|
||||||
[operationDetailLabel setFrame:NSMakeRect(76, 34, 260, 22)];
|
|
||||||
[operationDetailLabel setFont:[NSFont systemFontOfSize:12 weight:NSFontWeightRegular]];
|
|
||||||
[operationDetailLabel setTextColor:[NSColor colorWithCalibratedWhite:0.82 alpha:1.0]];
|
|
||||||
[operationDetailLabel setLineBreakMode:NSLineBreakByTruncatingTail];
|
|
||||||
|
|
||||||
[root addSubview:operationSpinner];
|
|
||||||
[root addSubview:operationIconLabel];
|
|
||||||
[root addSubview:operationTitleLabel];
|
|
||||||
[root addSubview:operationDetailLabel];
|
|
||||||
[operationPanel setContentView:root];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void snipShowOperationStatus(const char *titleC, const char *detailC, int state) {
|
|
||||||
NSString *title = statusString(titleC);
|
|
||||||
NSString *detail = statusString(detailC);
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
|
||||||
ensureOperationPanel();
|
|
||||||
[operationTitleLabel setStringValue:title];
|
|
||||||
[operationDetailLabel setStringValue:detail];
|
|
||||||
|
|
||||||
if (state == 0) {
|
|
||||||
[operationIconLabel setHidden:YES];
|
|
||||||
[operationSpinner setHidden:NO];
|
|
||||||
[operationSpinner startAnimation:nil];
|
|
||||||
} else {
|
|
||||||
[operationSpinner stopAnimation:nil];
|
|
||||||
[operationSpinner setHidden:YES];
|
|
||||||
[operationIconLabel setHidden:NO];
|
|
||||||
if (state == 2) {
|
|
||||||
[operationIconLabel setStringValue:@"!"];
|
|
||||||
[operationIconLabel setTextColor:[NSColor colorWithCalibratedRed:248.0/255.0 green:113.0/255.0 blue:113.0/255.0 alpha:1.0]];
|
|
||||||
} else {
|
|
||||||
[operationIconLabel setStringValue:@"OK"];
|
|
||||||
[operationIconLabel setTextColor:[NSColor colorWithCalibratedRed:74.0/255.0 green:222.0/255.0 blue:128.0/255.0 alpha:1.0]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
NSScreen *screen = [NSScreen mainScreen];
|
|
||||||
if (screen != nil) {
|
|
||||||
NSRect visible = [screen visibleFrame];
|
|
||||||
NSRect panelFrame = [operationPanel frame];
|
|
||||||
panelFrame.origin.x = NSMidX(visible) - panelFrame.size.width / 2;
|
|
||||||
panelFrame.origin.y = NSMaxY(visible) - panelFrame.size.height - 48;
|
|
||||||
[operationPanel setFrame:panelFrame display:YES];
|
|
||||||
}
|
|
||||||
[operationPanel orderFrontRegardless];
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static void snipHideOperationStatus(void) {
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
|
||||||
if (operationPanel != nil) {
|
|
||||||
[operationSpinner stopAnimation:nil];
|
|
||||||
[operationPanel orderOut:nil];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
operationStatusRunning = iota
|
|
||||||
operationStatusSuccess
|
|
||||||
operationStatusError
|
|
||||||
)
|
|
||||||
|
|
||||||
var operationStatusSeq atomic.Uint64
|
|
||||||
|
|
||||||
func showOperationStatus(title, detail string, state int) {
|
|
||||||
operationStatusSeq.Add(1)
|
|
||||||
ctitle := C.CString(title)
|
|
||||||
cdetail := C.CString(detail)
|
|
||||||
defer C.free(unsafe.Pointer(ctitle))
|
|
||||||
defer C.free(unsafe.Pointer(cdetail))
|
|
||||||
C.snipShowOperationStatus(ctitle, cdetail, C.int(state))
|
|
||||||
}
|
|
||||||
|
|
||||||
func hideOperationStatusAfter(delay time.Duration) {
|
|
||||||
seq := operationStatusSeq.Load()
|
|
||||||
time.AfterFunc(delay, func() {
|
|
||||||
if operationStatusSeq.Load() == seq {
|
|
||||||
C.snipHideOperationStatus()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
//go:build !darwin
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
const (
|
|
||||||
operationStatusRunning = iota
|
|
||||||
operationStatusSuccess
|
|
||||||
operationStatusError
|
|
||||||
)
|
|
||||||
|
|
||||||
func showOperationStatus(_ string, _ string, _ int) {}
|
|
||||||
|
|
||||||
func hideOperationStatusAfter(_ time.Duration) {}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
VITE_BASE_PATH=/
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
node_modules
|
|
||||||
dist
|
|
||||||
dist-ssr
|
|
||||||
.vite
|
|
||||||
.DS_Store
|
|
||||||
*.log
|
|
||||||
|
|
||||||
tsconfig.node.tsbuildinfo
|
|
||||||
tsconfig.app.tsbuildinfo
|
|
||||||
upload_koodo.py
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html lang="zh-CN">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<link rel="icon" type="image/png" sizes="64x64" href="%BASE_URL%favicon.png" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
||||||
<meta
|
|
||||||
name="description"
|
|
||||||
content="SnapGo — 截完图,链接已经在你剪贴板里。常驻菜单栏的轻量截图工具,一键上传到你自己的 S3 兼容对象存储。"
|
|
||||||
/>
|
|
||||||
<meta property="og:title" content="SnapGo — 截完图,链接已经在剪贴板里" />
|
|
||||||
<meta
|
|
||||||
property="og:description"
|
|
||||||
content="常驻菜单栏的轻量截图工具,一键上传到你自己的 S3 兼容对象存储。"
|
|
||||||
/>
|
|
||||||
<title>SnapGo — 截完图,链接已经在剪贴板里</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id="root"></div>
|
|
||||||
<script type="module" src="/src/main.tsx"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
Generated
-2239
File diff suppressed because it is too large
Load Diff
@@ -1,25 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "snapgo-landing",
|
|
||||||
"private": true,
|
|
||||||
"version": "0.1.0",
|
|
||||||
"type": "module",
|
|
||||||
"scripts": {
|
|
||||||
"dev": "vite",
|
|
||||||
"build": "tsc -b && vite build",
|
|
||||||
"preview": "vite preview",
|
|
||||||
"typecheck": "tsc -b --noEmit"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"react": "^18.3.1",
|
|
||||||
"react-dom": "^18.3.1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@tailwindcss/vite": "^4.0.0",
|
|
||||||
"@types/react": "^18.3.12",
|
|
||||||
"@types/react-dom": "^18.3.1",
|
|
||||||
"@vitejs/plugin-react": "^4.3.4",
|
|
||||||
"tailwindcss": "^4.0.0",
|
|
||||||
"typescript": "^5.6.3",
|
|
||||||
"vite": "^5.4.11"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 8.1 KiB |
@@ -1,268 +0,0 @@
|
|||||||
import { useMemo, useRef, useState, type AnimationEvent } from "react";
|
|
||||||
import logoUrl from "./assets/logo-universal.png";
|
|
||||||
import ShowcaseVisual from "./ShowcaseVisual";
|
|
||||||
|
|
||||||
const slides = [
|
|
||||||
{
|
|
||||||
eyebrow: "UPLOAD",
|
|
||||||
title: "直达云端",
|
|
||||||
body: "截图可直接保存至对象存储、远端服务器等,并将地址直接复制到剪切板。",
|
|
||||||
metric: "OBS / SSH / FTP",
|
|
||||||
feature: "支持多种远端存储配置",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
eyebrow: "IDENTIFICATION",
|
|
||||||
title: "智能识别",
|
|
||||||
body: "截图可按你配置进行 AI 识别与总结,直接将你所需要的答案复制到剪切板。",
|
|
||||||
metric: "AI",
|
|
||||||
feature: "支持自定义 prompt 与多 provider",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
eyebrow: "OCR",
|
|
||||||
title: "文字提取",
|
|
||||||
body: "截图可自动识别其中的文本文字,并将文字直接复制到剪切板。",
|
|
||||||
metric: "TEXT",
|
|
||||||
feature: "兼容多个主流的 OCR 服务",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
eyebrow: "CAPTURE",
|
|
||||||
title: "功能完备",
|
|
||||||
body: "完善的截图功能,支持各类标注、打码,直接复制与本机保存。",
|
|
||||||
metric: "⌘⇧S",
|
|
||||||
feature: "全局快捷键唤起",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
type SlideDirection = -1 | 1;
|
|
||||||
|
|
||||||
type SlideTransition = {
|
|
||||||
from: number;
|
|
||||||
direction: SlideDirection;
|
|
||||||
};
|
|
||||||
|
|
||||||
function Logo() {
|
|
||||||
return (
|
|
||||||
<div className="brand" aria-label="SnapGo logo">
|
|
||||||
<div className="brand-main">
|
|
||||||
<span className="logo-mark">
|
|
||||||
<img src={logoUrl} alt="SnapGo" />
|
|
||||||
</span>
|
|
||||||
<span className="brand-name">SnapGo</span>
|
|
||||||
</div>
|
|
||||||
<p className="brand-overline">AI SCREENSHOT UTILITY</p>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function App() {
|
|
||||||
const [activeSlide, setActiveSlide] = useState(0);
|
|
||||||
const [slideTransition, setSlideTransition] = useState<SlideTransition | null>(null);
|
|
||||||
const dragStartX = useRef<number | null>(null);
|
|
||||||
const active = slides[activeSlide];
|
|
||||||
|
|
||||||
function getTransitionClass(role: "entering" | "exiting") {
|
|
||||||
if (!slideTransition) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
const edge = slideTransition.direction === 1 ? "right" : "left";
|
|
||||||
return `is-${role}-from-${edge}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function finishSlideTransition(event: AnimationEvent<HTMLElement>) {
|
|
||||||
if (event.target === event.currentTarget) {
|
|
||||||
setSlideTransition(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function selectSlide(nextSlide: number, direction: SlideDirection) {
|
|
||||||
if (slideTransition || nextSlide === activeSlide) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSlideTransition({ from: activeSlide, direction });
|
|
||||||
setActiveSlide(nextSlide);
|
|
||||||
}
|
|
||||||
|
|
||||||
function selectDotSlide(nextSlide: number) {
|
|
||||||
const forwardDistance = (nextSlide - activeSlide + slides.length) % slides.length;
|
|
||||||
const backwardDistance = (activeSlide - nextSlide + slides.length) % slides.length;
|
|
||||||
selectSlide(nextSlide, forwardDistance <= backwardDistance ? 1 : -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const accentDots = useMemo(
|
|
||||||
() =>
|
|
||||||
slides.map((slide, index) => (
|
|
||||||
<button
|
|
||||||
key={slide.eyebrow}
|
|
||||||
className={`dot ${index === activeSlide ? "is-active" : ""}`}
|
|
||||||
type="button"
|
|
||||||
aria-label={`查看 ${slide.title}`}
|
|
||||||
onClick={() => selectDotSlide(index)}
|
|
||||||
/>
|
|
||||||
)),
|
|
||||||
[activeSlide, slideTransition],
|
|
||||||
);
|
|
||||||
|
|
||||||
function moveSlide(direction: SlideDirection) {
|
|
||||||
const nextSlide = (activeSlide + direction + slides.length) % slides.length;
|
|
||||||
selectSlide(nextSlide, direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
function handlePointerEnd(clientX: number) {
|
|
||||||
if (dragStartX.current === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const delta = clientX - dragStartX.current;
|
|
||||||
dragStartX.current = null;
|
|
||||||
if (Math.abs(delta) < 42) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
moveSlide(delta > 0 ? -1 : 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<main className="landing-shell">
|
|
||||||
<div className="ambient ambient-one" aria-hidden="true" />
|
|
||||||
<div className="ambient ambient-two" aria-hidden="true" />
|
|
||||||
<Logo />
|
|
||||||
|
|
||||||
<section className="hero-copy" aria-labelledby="landing-title">
|
|
||||||
<h1 id="landing-title">AI 时代的截图工具</h1>
|
|
||||||
<p className="subtitle">高效、便捷、专为 AI 交互设计</p>
|
|
||||||
<p className="intro">
|
|
||||||
智能识图,发布链接,提取文本..
|
|
||||||
<br />
|
|
||||||
你能想到的都能一键解决
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<div className="cta-row" aria-label="下载与项目链接">
|
|
||||||
<div className="download-cta-group">
|
|
||||||
<a className="primary-cta" href="https://gitea.mamamiyear.site/mamamiyear/SnapGo">
|
|
||||||
<span className="apple-logo" aria-hidden="true"></span>
|
|
||||||
下载 Mac 应用
|
|
||||||
<svg viewBox="0 0 20 20" aria-hidden="true">
|
|
||||||
<path d="M5 10h9M10.5 6.5 14 10l-3.5 3.5" />
|
|
||||||
</svg>
|
|
||||||
</a>
|
|
||||||
<span className="platform-note">Windows/Linux 版本紧锣密鼓开发中</span>
|
|
||||||
</div>
|
|
||||||
<a
|
|
||||||
className="github-cta"
|
|
||||||
href="https://gitea.mamamiyear.site/mamamiyear/SnapGo"
|
|
||||||
aria-label="查看源码"
|
|
||||||
title="查看源码"
|
|
||||||
>
|
|
||||||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
|
||||||
<path
|
|
||||||
fillRule="evenodd"
|
|
||||||
clipRule="evenodd"
|
|
||||||
d="M12 2C6.48 2 2 6.58 2 12.23c0 4.52 2.87 8.35 6.84 9.7.5.09.68-.22.68-.49 0-.24-.01-.88-.01-1.73-2.78.62-3.37-1.37-3.37-1.37-.45-1.18-1.11-1.5-1.11-1.5-.91-.64.07-.63.07-.63 1 .07 1.53 1.06 1.53 1.06.9 1.57 2.34 1.12 2.91.86.09-.67.35-1.12.64-1.38-2.22-.26-4.56-1.14-4.56-5.06 0-1.12.39-2.03 1.03-2.74-.1-.26-.45-1.3.1-2.7 0 0 .84-.28 2.75 1.05A9.32 9.32 0 0 1 12 6.96c.85 0 1.7.12 2.5.34 1.91-1.33 2.75-1.05 2.75-1.05.55 1.4.2 2.44.1 2.7.64.71 1.03 1.62 1.03 2.74 0 3.93-2.34 4.8-4.57 5.05.36.32.68.94.68 1.9 0 1.38-.01 2.49-.01 2.83 0 .27.18.59.69.49A10.1 10.1 0 0 0 22 12.23C22 6.58 17.52 2 12 2Z"
|
|
||||||
/>
|
|
||||||
</svg>
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section className="product-showcase" aria-label="SnapGo 功能效果展示">
|
|
||||||
<div
|
|
||||||
className="device-card"
|
|
||||||
onPointerDown={(event) => {
|
|
||||||
dragStartX.current = event.clientX;
|
|
||||||
}}
|
|
||||||
onPointerUp={(event) => handlePointerEnd(event.clientX)}
|
|
||||||
onPointerCancel={() => {
|
|
||||||
dragStartX.current = null;
|
|
||||||
}}
|
|
||||||
onPointerLeave={() => {
|
|
||||||
dragStartX.current = null;
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div className="window-chrome" aria-hidden="true">
|
|
||||||
<span />
|
|
||||||
<span />
|
|
||||||
<span />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="preview-stage">
|
|
||||||
{slideTransition && (
|
|
||||||
<div
|
|
||||||
key={`visual-${slideTransition.from}`}
|
|
||||||
className={`carousel-slide is-exiting ${getTransitionClass("exiting")}`}
|
|
||||||
aria-hidden="true"
|
|
||||||
>
|
|
||||||
<ShowcaseVisual activeIndex={slideTransition.from} />
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div
|
|
||||||
key={`visual-${activeSlide}`}
|
|
||||||
className={`carousel-slide ${slideTransition ? `is-entering ${getTransitionClass("entering")}` : ""}`}
|
|
||||||
onAnimationEnd={finishSlideTransition}
|
|
||||||
>
|
|
||||||
<ShowcaseVisual activeIndex={activeSlide} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="slide-panel-viewport">
|
|
||||||
{slideTransition && (
|
|
||||||
<div
|
|
||||||
key={`copy-${slideTransition.from}`}
|
|
||||||
className={`slide-panel is-exiting ${getTransitionClass("exiting")}`}
|
|
||||||
aria-hidden="true"
|
|
||||||
>
|
|
||||||
<p>{slides[slideTransition.from].eyebrow}</p>
|
|
||||||
<h2>{slides[slideTransition.from].title}</h2>
|
|
||||||
<span>{slides[slideTransition.from].body}</span>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div
|
|
||||||
key={`copy-${activeSlide}`}
|
|
||||||
className={`slide-panel ${slideTransition ? `is-entering ${getTransitionClass("entering")}` : ""}`}
|
|
||||||
>
|
|
||||||
<p>{active.eyebrow}</p>
|
|
||||||
<h2>{active.title}</h2>
|
|
||||||
<span>{active.body}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="carousel-bar">
|
|
||||||
<button type="button" className="nav-btn" aria-label="上一项" onClick={() => moveSlide(-1)}>
|
|
||||||
<svg viewBox="0 0 20 20" aria-hidden="true">
|
|
||||||
<path d="m12 5-5 5 5 5" />
|
|
||||||
</svg>
|
|
||||||
</button>
|
|
||||||
<div className="slide-meta-viewport" aria-live="polite">
|
|
||||||
{slideTransition && (
|
|
||||||
<div
|
|
||||||
key={`meta-${slideTransition.from}`}
|
|
||||||
className={`slide-meta is-exiting ${getTransitionClass("exiting")}`}
|
|
||||||
aria-hidden="true"
|
|
||||||
>
|
|
||||||
<strong>{slides[slideTransition.from].metric}</strong>
|
|
||||||
<span>{slides[slideTransition.from].feature}</span>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
<div
|
|
||||||
key={`meta-${activeSlide}`}
|
|
||||||
className={`slide-meta ${slideTransition ? `is-entering ${getTransitionClass("entering")}` : ""}`}
|
|
||||||
>
|
|
||||||
<strong>{active.metric}</strong>
|
|
||||||
<span>{active.feature}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="dots" aria-label="轮播分页">
|
|
||||||
{accentDots}
|
|
||||||
</div>
|
|
||||||
<button type="button" className="nav-btn" aria-label="下一项" onClick={() => moveSlide(1)}>
|
|
||||||
<svg viewBox="0 0 20 20" aria-hidden="true">
|
|
||||||
<path d="m8 5 5 5-5 5" />
|
|
||||||
</svg>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
</main>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,435 +0,0 @@
|
|||||||
import { useState, type PointerEvent } from "react";
|
|
||||||
|
|
||||||
type ShowcaseVisualProps = {
|
|
||||||
activeIndex: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
type CaptureTool = "rect" | "arrow" | "pen" | "mosaic";
|
|
||||||
type OcrLanguage = "zh" | "en" | "auto";
|
|
||||||
type UploadDestination = {
|
|
||||||
id: "obs" | "ssh" | "ftp";
|
|
||||||
label: string;
|
|
||||||
detail: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const destinations: UploadDestination[] = [
|
|
||||||
{ id: "obs", label: "OBS", detail: "Bucket" },
|
|
||||||
{ id: "ssh", label: "SSH", detail: "Server" },
|
|
||||||
{ id: "ftp", label: "FTP", detail: "Remote" },
|
|
||||||
];
|
|
||||||
|
|
||||||
const uploadRoutes: Record<UploadDestination["id"], string> = {
|
|
||||||
obs: "M 50 30 C 59 30 61 17 71 17",
|
|
||||||
ssh: "M 50 30 C 58 30 63 30 71 30",
|
|
||||||
ftp: "M 50 30 C 59 30 61 43 71 43",
|
|
||||||
};
|
|
||||||
|
|
||||||
const ocrModes: Array<{
|
|
||||||
id: OcrLanguage;
|
|
||||||
label: string;
|
|
||||||
name: string;
|
|
||||||
lines: string[];
|
|
||||||
characters: number;
|
|
||||||
confidence: string;
|
|
||||||
}> = [
|
|
||||||
{
|
|
||||||
id: "zh",
|
|
||||||
label: "中",
|
|
||||||
name: "中文",
|
|
||||||
lines: ["让每一次截图直接进入下一步。"],
|
|
||||||
characters: 15,
|
|
||||||
confidence: "99.5%",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "en",
|
|
||||||
label: "EN",
|
|
||||||
name: "English",
|
|
||||||
lines: ["Capture, understand, share."],
|
|
||||||
characters: 27,
|
|
||||||
confidence: "99.1%",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "auto",
|
|
||||||
label: "AUTO",
|
|
||||||
name: "中英混排",
|
|
||||||
lines: ["Capture, understand, share.", "让每一次截图直接进入下一步。"],
|
|
||||||
characters: 42,
|
|
||||||
confidence: "99.2%",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const aiModes = [
|
|
||||||
{
|
|
||||||
id: "vision",
|
|
||||||
label: "识图",
|
|
||||||
result: "检测到系统权限弹窗,截图权限尚未开启。",
|
|
||||||
score: "96%",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "summary",
|
|
||||||
label: "总结",
|
|
||||||
result: "当前页面包含 3 个设置项,建议先开启屏幕录制权限。",
|
|
||||||
score: "93%",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "question",
|
|
||||||
label: "问答",
|
|
||||||
result: "前往系统设置中的隐私与安全性即可完成授权。",
|
|
||||||
score: "98%",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const captureTools: Array<{ id: CaptureTool; label: string }> = [
|
|
||||||
{ id: "rect", label: "矩形" },
|
|
||||||
{ id: "arrow", label: "箭头" },
|
|
||||||
{ id: "pen", label: "画笔" },
|
|
||||||
{ id: "mosaic", label: "马赛克" },
|
|
||||||
];
|
|
||||||
|
|
||||||
function stopSceneDrag(event: PointerEvent<HTMLElement>) {
|
|
||||||
event.stopPropagation();
|
|
||||||
}
|
|
||||||
|
|
||||||
function UploadScene() {
|
|
||||||
const [selected, setSelected] = useState<UploadDestination>(destinations[0]);
|
|
||||||
const routePath = uploadRoutes[selected.id];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className={`visual-scene upload-scene target-${selected.id}`}>
|
|
||||||
<div className="upload-file-card">
|
|
||||||
<div className="upload-file-preview" aria-hidden="true">
|
|
||||||
<div className="screenshot-window-bar">
|
|
||||||
<span><i /><i /><i /></span>
|
|
||||||
<b className="screenshot-address-bar" />
|
|
||||||
</div>
|
|
||||||
<div className="screenshot-window-body">
|
|
||||||
<div className="screenshot-sidebar"><i /><i /><i /><i /></div>
|
|
||||||
<div className="screenshot-main">
|
|
||||||
<span className="screenshot-heading" />
|
|
||||||
<span className="screenshot-row row-long" />
|
|
||||||
<span className="screenshot-row row-medium" />
|
|
||||||
<div className="screenshot-card-grid">
|
|
||||||
<i /><i /><i />
|
|
||||||
</div>
|
|
||||||
<div className="screenshot-copy-lines">
|
|
||||||
<i /><i /><i />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="upload-capture-frame">
|
|
||||||
<i className="upload-capture-corner corner-nw" />
|
|
||||||
<i className="upload-capture-corner corner-ne" />
|
|
||||||
<i className="upload-capture-corner corner-sw" />
|
|
||||||
<i className="upload-capture-corner corner-se" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="upload-file-copy">
|
|
||||||
<span>SCREENSHOT-1428.PNG</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<svg
|
|
||||||
className="upload-route"
|
|
||||||
viewBox="0 0 100 60"
|
|
||||||
preserveAspectRatio="none"
|
|
||||||
aria-hidden="true"
|
|
||||||
>
|
|
||||||
<path
|
|
||||||
key={`route-${selected.id}`}
|
|
||||||
className="upload-route-line"
|
|
||||||
d={routePath}
|
|
||||||
vectorEffect="non-scaling-stroke"
|
|
||||||
/>
|
|
||||||
<path
|
|
||||||
key={`shimmer-${selected.id}`}
|
|
||||||
className="upload-route-shimmer"
|
|
||||||
d={routePath}
|
|
||||||
pathLength="100"
|
|
||||||
strokeDasharray="16 84"
|
|
||||||
vectorEffect="non-scaling-stroke"
|
|
||||||
/>
|
|
||||||
</svg>
|
|
||||||
|
|
||||||
<span key={`packet-${selected.id}`} className={`upload-packet packet-${selected.id}`} aria-hidden="true">
|
|
||||||
<svg viewBox="0 0 20 20">
|
|
||||||
<path d="M 5 10 H 14 M 10.5 6.5 L 14 10 L 10.5 13.5" />
|
|
||||||
</svg>
|
|
||||||
</span>
|
|
||||||
|
|
||||||
<div className="upload-destinations" role="group" aria-label="上传目标">
|
|
||||||
{destinations.map((destination) => (
|
|
||||||
<button
|
|
||||||
key={destination.id}
|
|
||||||
type="button"
|
|
||||||
className={destination.id === selected.id ? "is-selected" : ""}
|
|
||||||
aria-pressed={destination.id === selected.id}
|
|
||||||
aria-label={`选择 ${destination.label} 上传目标`}
|
|
||||||
onPointerDown={stopSceneDrag}
|
|
||||||
onClick={() => setSelected(destination)}
|
|
||||||
>
|
|
||||||
<span className="destination-status" />
|
|
||||||
<strong>{destination.label}</strong>
|
|
||||||
<small>{destination.detail}</small>
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="upload-result scene-float-delayed" aria-live="polite">
|
|
||||||
<span className="result-check" aria-hidden="true">✓</span>
|
|
||||||
<div>
|
|
||||||
<small>LINK COPIED</small>
|
|
||||||
<strong>cdn.snapgo.dev/{selected.id}/...</strong>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function IdentificationScene() {
|
|
||||||
const [mode, setMode] = useState(aiModes[0]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="visual-scene identification-scene">
|
|
||||||
<div className="ai-source-card">
|
|
||||||
<div className="mini-window-bar" aria-hidden="true">
|
|
||||||
<span />
|
|
||||||
<span />
|
|
||||||
<span />
|
|
||||||
</div>
|
|
||||||
<div className="ai-source-content">
|
|
||||||
<span className="source-sidebar" />
|
|
||||||
<div className="source-settings">
|
|
||||||
<i />
|
|
||||||
<i />
|
|
||||||
<i />
|
|
||||||
</div>
|
|
||||||
<div className="source-dialog">
|
|
||||||
<span className="dialog-icon">!</span>
|
|
||||||
<div>
|
|
||||||
<strong>Permission required</strong>
|
|
||||||
<small>Screen Recording</small>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<span className="ai-scan-line" aria-hidden="true" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="ai-core" aria-hidden="true">
|
|
||||||
<span>AI</span>
|
|
||||||
<i className="ai-orbit orbit-one" />
|
|
||||||
<i className="ai-orbit orbit-two" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="ai-result-card scene-float-soft" aria-live="polite">
|
|
||||||
<div className="ai-result-heading">
|
|
||||||
<span className="ai-spark" aria-hidden="true">✦</span>
|
|
||||||
<strong>SnapGo Vision</strong>
|
|
||||||
<i />
|
|
||||||
</div>
|
|
||||||
<p>{mode.result}</p>
|
|
||||||
<div className="confidence-row">
|
|
||||||
<span><i /></span>
|
|
||||||
<small>{mode.score} confidence</small>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="ai-mode-switch" role="group" aria-label="AI 处理模式">
|
|
||||||
{aiModes.map((item) => (
|
|
||||||
<button
|
|
||||||
key={item.id}
|
|
||||||
type="button"
|
|
||||||
className={item.id === mode.id ? "is-selected" : ""}
|
|
||||||
aria-pressed={item.id === mode.id}
|
|
||||||
onPointerDown={stopSceneDrag}
|
|
||||||
onClick={() => setMode(item)}
|
|
||||||
>
|
|
||||||
{item.label}
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function OcrScene() {
|
|
||||||
const [copied, setCopied] = useState(false);
|
|
||||||
const [language, setLanguage] = useState<OcrLanguage>("auto");
|
|
||||||
const activeMode = ocrModes.find((mode) => mode.id === language) ?? ocrModes[2];
|
|
||||||
|
|
||||||
function selectLanguage(mode: OcrLanguage) {
|
|
||||||
setLanguage(mode);
|
|
||||||
setCopied(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="visual-scene ocr-scene">
|
|
||||||
<div className="ocr-document">
|
|
||||||
<div className="document-header">
|
|
||||||
<span />
|
|
||||||
<div><i /><i /><i /></div>
|
|
||||||
</div>
|
|
||||||
<div className="document-copy">
|
|
||||||
<strong>Release notes</strong>
|
|
||||||
<span className="text-line line-long" />
|
|
||||||
<span className="text-line line-medium" />
|
|
||||||
<span className="text-line line-highlight"><i>Capture, understand, share.</i></span>
|
|
||||||
<span className="text-line line-long" />
|
|
||||||
<span className="text-line line-short" />
|
|
||||||
<span className="text-line line-highlight secondary"><i>让每一次截图直接进入下一步。</i></span>
|
|
||||||
</div>
|
|
||||||
<span className="ocr-scan-bar" aria-hidden="true" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="ocr-selection-card scene-float-delayed">
|
|
||||||
<div className="selection-heading">
|
|
||||||
<span>OCR</span>
|
|
||||||
<small>{activeMode.name}</small>
|
|
||||||
</div>
|
|
||||||
<p>
|
|
||||||
{activeMode.lines.map((line) => <span key={line}>{line}</span>)}
|
|
||||||
</p>
|
|
||||||
<div className="selection-meta">
|
|
||||||
<span>{activeMode.characters} characters</span>
|
|
||||||
<strong>{activeMode.confidence}</strong>
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className={copied ? "is-copied" : ""}
|
|
||||||
aria-pressed={copied}
|
|
||||||
onPointerDown={stopSceneDrag}
|
|
||||||
onClick={() => setCopied((current) => !current)}
|
|
||||||
>
|
|
||||||
<span aria-hidden="true">{copied ? "✓" : "⌘C"}</span>
|
|
||||||
{copied ? "已复制" : "复制文本"}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="ocr-language-pills" role="group" aria-label="OCR 识别语言">
|
|
||||||
{ocrModes.map((mode) => (
|
|
||||||
<button
|
|
||||||
key={mode.id}
|
|
||||||
type="button"
|
|
||||||
className={mode.id === language ? "is-selected" : ""}
|
|
||||||
aria-pressed={mode.id === language}
|
|
||||||
onPointerDown={stopSceneDrag}
|
|
||||||
onClick={() => selectLanguage(mode.id)}
|
|
||||||
>
|
|
||||||
{mode.label}
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function ToolGlyph({ tool }: { tool: CaptureTool }) {
|
|
||||||
if (tool === "rect") {
|
|
||||||
return <svg viewBox="0 0 20 20" aria-hidden="true"><rect x="4" y="4" width="12" height="12" rx="2" /></svg>;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tool === "arrow") {
|
|
||||||
return <svg viewBox="0 0 20 20" aria-hidden="true"><path d="M4 15 15 4M9 4h6v6" /></svg>;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tool === "pen") {
|
|
||||||
return <svg viewBox="0 0 20 20" aria-hidden="true"><path d="m5 15 2.2-4.7L13.8 3.7l2.5 2.5-6.6 6.6L5 15Z" /><path d="m11.8 5.7 2.5 2.5" /></svg>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<svg viewBox="0 0 20 20" aria-hidden="true">
|
|
||||||
<rect x="3" y="3" width="5" height="5" />
|
|
||||||
<rect x="12" y="3" width="5" height="5" />
|
|
||||||
<rect x="3" y="12" width="5" height="5" />
|
|
||||||
<rect x="12" y="12" width="5" height="5" />
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function AnnotationPreview({ tool }: { tool: CaptureTool }) {
|
|
||||||
if (tool === "arrow") {
|
|
||||||
return (
|
|
||||||
<svg className="annotation-arrow" viewBox="0 0 180 110" aria-hidden="true">
|
|
||||||
<path d="M28 84C66 78 91 56 139 31" />
|
|
||||||
<path d="m121 27 20 3-8 18" />
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tool === "pen") {
|
|
||||||
return (
|
|
||||||
<svg className="annotation-pen" viewBox="0 0 180 110" aria-hidden="true">
|
|
||||||
<path d="M24 76c20-42 36 25 57-6s33-33 48-4c9 18 21 13 29-7" />
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tool === "mosaic") {
|
|
||||||
return <div className="annotation-mosaic" aria-hidden="true">{Array.from({ length: 20 }, (_, index) => <span key={index} />)}</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
return <div className="annotation-rect" aria-hidden="true"><span /><span /><span /><span /></div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
function CaptureScene() {
|
|
||||||
const [tool, setTool] = useState<CaptureTool>("rect");
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="visual-scene capture-scene">
|
|
||||||
<div className="capture-canvas">
|
|
||||||
<div className="canvas-app-bar">
|
|
||||||
<span />
|
|
||||||
<strong>Project overview</strong>
|
|
||||||
<i />
|
|
||||||
</div>
|
|
||||||
<div className="canvas-layout" aria-hidden="true">
|
|
||||||
<span className="canvas-sidebar" />
|
|
||||||
<div className="canvas-content">
|
|
||||||
<i className="canvas-heading" />
|
|
||||||
<i className="canvas-line" />
|
|
||||||
<i className="canvas-line short" />
|
|
||||||
<div className="canvas-chart"><span /><span /><span /><span /><span /></div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="capture-selection">
|
|
||||||
<span className="selection-size">1280 × 720</span>
|
|
||||||
<i className="selection-handle handle-nw" />
|
|
||||||
<i className="selection-handle handle-ne" />
|
|
||||||
<i className="selection-handle handle-sw" />
|
|
||||||
<i className="selection-handle handle-se" />
|
|
||||||
<AnnotationPreview tool={tool} />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="capture-toolbar" role="toolbar" aria-label="截图标注工具">
|
|
||||||
{captureTools.map((item) => (
|
|
||||||
<button
|
|
||||||
key={item.id}
|
|
||||||
type="button"
|
|
||||||
className={item.id === tool ? "is-selected" : ""}
|
|
||||||
aria-label={item.label}
|
|
||||||
aria-pressed={item.id === tool}
|
|
||||||
title={item.label}
|
|
||||||
onPointerDown={stopSceneDrag}
|
|
||||||
onClick={() => setTool(item.id)}
|
|
||||||
>
|
|
||||||
<ToolGlyph tool={item.id} />
|
|
||||||
</button>
|
|
||||||
))}
|
|
||||||
<span className="toolbar-divider" />
|
|
||||||
<span className="toolbar-color" aria-hidden="true" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const scenes = [UploadScene, IdentificationScene, OcrScene, CaptureScene];
|
|
||||||
|
|
||||||
export default function ShowcaseVisual({ activeIndex }: ShowcaseVisualProps) {
|
|
||||||
const Scene = scenes[activeIndex] ?? UploadScene;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="scene-parallax" data-testid="showcase-visual">
|
|
||||||
<Scene />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.2 MiB |
File diff suppressed because it is too large
Load Diff
@@ -1,10 +0,0 @@
|
|||||||
import React from "react";
|
|
||||||
import ReactDOM from "react-dom/client";
|
|
||||||
import App from "./App";
|
|
||||||
import "./index.css";
|
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
||||||
<React.StrictMode>
|
|
||||||
<App />
|
|
||||||
</React.StrictMode>,
|
|
||||||
);
|
|
||||||
Vendored
-1
@@ -1 +0,0 @@
|
|||||||
/// <reference types="vite/client" />
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "ES2022",
|
|
||||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
||||||
"module": "ESNext",
|
|
||||||
"moduleResolution": "bundler",
|
|
||||||
"jsx": "react-jsx",
|
|
||||||
"strict": true,
|
|
||||||
"noUnusedLocals": true,
|
|
||||||
"noUnusedParameters": true,
|
|
||||||
"noFallthroughCasesInSwitch": true,
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"allowSyntheticDefaultImports": true,
|
|
||||||
"resolveJsonModule": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"useDefineForClassFields": true,
|
|
||||||
"allowImportingTsExtensions": false,
|
|
||||||
"noEmit": true
|
|
||||||
},
|
|
||||||
"include": ["src"]
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
"files": [],
|
|
||||||
"references": [
|
|
||||||
{ "path": "./tsconfig.app.json" },
|
|
||||||
{ "path": "./tsconfig.node.json" }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "ES2022",
|
|
||||||
"lib": ["ES2023"],
|
|
||||||
"module": "ESNext",
|
|
||||||
"moduleResolution": "bundler",
|
|
||||||
"strict": true,
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"esModuleInterop": true,
|
|
||||||
"allowSyntheticDefaultImports": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"noEmit": true
|
|
||||||
},
|
|
||||||
"include": ["vite.config.ts"]
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import { defineConfig, loadEnv } from "vite";
|
|
||||||
import react from "@vitejs/plugin-react";
|
|
||||||
import tailwindcss from "@tailwindcss/vite";
|
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
|
||||||
export default defineConfig(({ mode }) => {
|
|
||||||
const env = loadEnv(mode, ".", "");
|
|
||||||
|
|
||||||
return {
|
|
||||||
base: env.VITE_BASE_PATH || "/",
|
|
||||||
plugins: [react(), tailwindcss()],
|
|
||||||
server: {
|
|
||||||
port: 5273,
|
|
||||||
open: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
@@ -43,9 +43,6 @@ sleep 0.4
|
|||||||
# --identity flag in v2.
|
# --identity flag in v2.
|
||||||
ARCH="${ARCH:-arm64}"
|
ARCH="${ARCH:-arm64}"
|
||||||
echo "[dev-build.sh] building darwin/${ARCH}..."
|
echo "[dev-build.sh] building darwin/${ARCH}..."
|
||||||
# Allow the -Wl,-no_warn_duplicate_libraries flag declared in the darwin cgo
|
|
||||||
# files past Go's cgo LDFLAGS allowlist (see activation_policy_darwin.go).
|
|
||||||
export CGO_LDFLAGS_ALLOW='-Wl,-no_warn_duplicate_libraries'
|
|
||||||
wails build -platform "darwin/${ARCH}" -clean
|
wails build -platform "darwin/${ARCH}" -clean
|
||||||
|
|
||||||
# 5. Re-sign with the stable dev cert. We invoke sign.sh through env var
|
# 5. Re-sign with the stable dev cert. We invoke sign.sh through env var
|
||||||
|
|||||||
+1
-3
@@ -35,9 +35,7 @@ echo "================================================================"
|
|||||||
# ---- 1. Build ----
|
# ---- 1. Build ----
|
||||||
echo ""
|
echo ""
|
||||||
echo "[release.sh] (1/4) wails build"
|
echo "[release.sh] (1/4) wails build"
|
||||||
# Allow the -Wl,-no_warn_duplicate_libraries flag declared in the darwin cgo
|
( cd "${ROOT_DIR}" && wails build -platform "${WAILS_PLATFORM}" -clean )
|
||||||
# files past Go's cgo LDFLAGS allowlist (see activation_policy_darwin.go).
|
|
||||||
( cd "${ROOT_DIR}" && CGO_LDFLAGS_ALLOW='-Wl,-no_warn_duplicate_libraries' wails build -platform "${WAILS_PLATFORM}" -clean )
|
|
||||||
|
|
||||||
# ---- 2. Sign .app ----
|
# ---- 2. Sign .app ----
|
||||||
echo ""
|
echo ""
|
||||||
|
|||||||
Reference in New Issue
Block a user