Files
openchamber-android-native/third_party/mygo/downloads.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

83 lines
2.4 KiB
Go

package mygo
import (
"os"
"path/filepath"
"strconv"
"strings"
)
// DownloadEvent is passed to Page.OnWillDownload listeners. Setting Path
// chooses where the file goes; preventing the event cancels the download.
type DownloadEvent struct {
Preventable
// URL of the download.
URL string
// SuggestedName is the file name the page or the server suggests.
SuggestedName string
// Path is where the file is saved: by default SuggestedName in the
// Downloads directory, numbered when a file has that name. A file
// already at a Path listeners set is replaced.
Path string
}
// Download is a finished download, passed to Page.OnDownloadDone.
type Download struct {
URL string
Path string
// Err is why the download failed, or nil.
Err error
}
// OnWillDownload is called when a page starts a download: a link with the
// download attribute, or a response the page cannot show, such as a file
// sent as an attachment. Without listeners, downloads are saved to the
// Downloads directory, and those of an untrusted page
// (PageOptions.Untrusted) are canceled.
func (p *Page) OnWillDownload(fn func(e *DownloadEvent)) (off func()) {
return p.w.onWillDownload.add(fn, false)
}
// OnDownloadDone is called when a download ended.
func (p *Page) OnDownloadDone(fn func(d *Download)) (off func()) {
return p.w.onDownloadDone.add(fn, false)
}
func (h *windowHandler) DownloadStarted(url, suggested string) string {
if h.w.untrusted && h.w.onWillDownload.len() == 0 {
return ""
}
name := filepath.Base(filepath.Clean("/" + strings.ReplaceAll(suggested, `\`, "/")))
if name == "/" || name == "." || name == "" {
name = "download"
}
e := &DownloadEvent{URL: url, SuggestedName: name}
if dir, err := App.Path(PathDownloads); err == nil && os.MkdirAll(dir, 0o755) == nil {
e.Path = uniquePath(filepath.Join(dir, name))
}
fire1(&h.w.onWillDownload, e)
if e.prevented || e.Path == "" {
return ""
}
return e.Path
}
func (h *windowHandler) DownloadFinished(url, path string, err error) {
fire1(&h.w.onDownloadDone, &Download{URL: url, Path: path, Err: err})
}
// uniquePath numbers path, like "name (2).ext", when a file has its name.
func uniquePath(path string) string {
if _, err := os.Lstat(path); err != nil {
return path
}
ext := filepath.Ext(path)
base := strings.TrimSuffix(path, ext)
for i := 2; ; i++ {
p := base + " (" + strconv.Itoa(i) + ")" + ext
if _, err := os.Lstat(p); err != nil {
return p
}
}
}