Files
magpie/internal/gui/importlink.go
T
yetone c2fd48782b Put back what 1f05c311d took out of main by mistake
1f05c311d was committed with its worktree's older tree on top of a
fresh origin/main (git reset --soft), so beside the memory fix it
undid every file 610ae1f54..c1312ac15 changed (45 files: key balance,
import links, project subagents, uninstall, site, LESSONS.md and
more). This commit restores each of them to c1312ac15 byte for byte;
the tree is now c1312ac15 plus the memory fix alone
(git diff --stat c1312ac15 HEAD: the 4 files of the fix).
2026-11-13 12:18:23 +08:00

94 lines
2.5 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?… or ccswitch://v1/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: a magpie:// one,
// or a ccswitch:// one, which relays put on their pages for CC Switch.
func ImportLink(args []string) string {
for _, a := range args {
if l := strings.ToLower(a); strings.HasPrefix(l, provider.Scheme+":") || strings.HasPrefix(l, provider.CCSwitchScheme+":") {
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})
})
}