Files
magpie/internal/gui/importlink.go
T
yetone 03314b25a4 import: magpie no longer registers CC Switch's ccswitch:// scheme (asombrado_a on X)
889758605 (v0.1.3981, coder_shaun on X asking for one-click import from
relays' CC Switch links) made magpie claim ccswitch:// on every system:
Info.plist on macOS, HKCU\Software\Classes\ccswitch on Windows, and
magpie-ccswitch.desktop on Linux. That scheme is CC Switch's, and magpie
claiming another app's links is not something it should do (asombrado_a
on X: "Magpie为啥要注册 ccswitch://协议的深链").

From this version magpie registers magpie:// only: the ccswitch entry is
out of Info.plist, and the Windows and Linux registration is back to what
it was before 889758605. A start doesn't touch what an older version wrote
(the owner's call); uninstall still takes it out while it is magpie's, as
before. The ccswitch:// parser (importccswitch.go, its test and CC
Switch's example links) goes with it: with nothing registered, no link
reaches it, and ParseImport, ImportLink and `magpie import` take magpie://
links only. The docs' "CC Switch links" paragraph is out in en, zh and ja,
and llms-full.txt and the search indexes are regenerated.

TestRegisterScheme now holds that registration never asks about or claims
x-scheme-handler/ccswitch, and TestImportLink that a ccswitch:// argument
is not taken as an import link.

Run at this commit's tree (rebased on 1318864d1), on mica (mote's load was
38): gofmt clean; go vet darwin, GOOS=windows
(gui, uninstall, provider), GOOS=linux nogui (provider, uninstall);
go test provider, gui, uninstall, cli ok; node --test site/*.test.mjs
135 pass. Linux (wsl-run, magpie-desktop, -tags gtk3), at the tree before the rebase
(the scheme files are the same at this one): provider, uninstall ok; gui ok
except TestClipboardReadBack/copies_after_a_read_back,
which fails the same on origin/main 3ec89c522 (probe segfault, #7073); at this tree TestRegisterScheme*, TestImportLink
and TestImportIsReadOnce ok.
Pushed on 3e66d25d8, which landed after the run and changes internal/agent
only; not run again with it.
Not tried: a real Windows or Linux desktop with an older magpie's
ccswitch registration, which this leaves in place; macOS Launch Services
dropping the scheme once the updated bundle replaces the old one.
2026-11-18 16:52:58 +08:00

92 lines
2.3 KiB
Go

package gui
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"net/http"
"strings"
"sync"
"github.com/yetone/magpie/internal/provider"
)
// An import link (magpie://import?…, see provider.ParseImport) is parsed
// here, parked under a random id, and the window opens on the Providers tab
// with that id; the page fetches it once and asks the user before saving
// anything.
type importJSON struct {
Provider provider.Provider `json:"provider"`
Error string `json:"error,omitempty"`
Replaces string `json:"replaces,omitempty"` // the name of the provider it would replace
}
var imports = struct {
sync.Mutex
m map[string]importJSON
}{m: map[string]importJSON{}}
func stashImport(link string) string {
var in importJSON
p, err := provider.ParseImport(link)
if err != nil {
in.Error = err.Error()
} else {
in.Provider = p
}
b := make([]byte, 8)
_, _ = rand.Read(b)
id := hex.EncodeToString(b)
imports.Lock()
imports.m[id] = in
imports.Unlock()
return id
}
// ImportLink is the link among a launch's arguments, if any.
func ImportLink(args []string) string {
for _, a := range args {
if strings.HasPrefix(strings.ToLower(a), provider.Scheme+":") {
return a
}
}
return ""
}
func importRoutes(mux *http.ServeMux) {
// Read once: the key is in there, and a reload should not ask again.
mux.HandleFunc("GET /api/import/{id}", func(rw http.ResponseWriter, r *http.Request) {
imports.Lock()
in, ok := imports.m[r.PathValue("id")]
delete(imports.m, r.PathValue("id"))
imports.Unlock()
if !ok {
http.NotFound(rw, r)
return
}
if in.Error == "" {
if old, err := provider.Find(in.Provider.ID); err == nil {
in.Replaces = old.Name
}
}
writeJSON(rw, in)
})
// fetch the picture a link named, once the user has the dialog open. The
// URL is validated again in provider.FetchIcon: this handler only ever
// fetches into the icons folder and answers with the reference.
mux.HandleFunc("POST /api/import/icon", func(rw http.ResponseWriter, r *http.Request) {
var in struct{ URL string }
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
icon, err := provider.FetchIcon(r.Context(), in.URL)
if err != nil {
fail(rw, err)
return
}
writeJSON(rw, map[string]string{"icon": icon})
})
}