Files
openchamber-android-native/internal/appui/attachments.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

74 lines
1.5 KiB
Go

package appui
import (
"encoding/base64"
"fmt"
"mime"
"os"
"path/filepath"
"strings"
"time"
"github.com/egoist/mygo"
)
// keepImage copies a picked image into the app's data directory so the sent
// message can show it inline after a reload clears the picker's copy.
func keepImage(source, name string) string {
dir, err := mygo.App.Path(mygo.PathUserData)
if err != nil {
return ""
}
dir = filepath.Join(dir, "attachments")
_ = os.MkdirAll(dir, 0o700)
dst := filepath.Join(dir, fmt.Sprintf("%d-%s", time.Now().UnixNano(), sanitizeName(name)))
if err := copyFile(source, dst); err != nil {
return ""
}
return dst
}
func sanitizeName(name string) string {
clean := strings.Map(func(r rune) rune {
switch r {
case '/', '\\', ':', '*', '?', '"', '<', '>', '|':
return '_'
}
return r
}, name)
if clean == "" {
return "image"
}
return clean
}
func copyFile(src, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, data, 0o600)
}
func mimeByExt(path string) string {
if m := mime.TypeByExtension(filepath.Ext(path)); m != "" {
return m
}
return "application/octet-stream"
}
func base64URL(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}
func humanSize(bytes int64) string {
switch {
case bytes >= 1<<20:
return fmt.Sprintf("%.1fMB", float64(bytes)/(1<<20))
case bytes >= 1<<10:
return fmt.Sprintf("%.0fKB", float64(bytes)/(1<<10))
default:
return fmt.Sprintf("%dB", bytes)
}
}