Files
skillshare/internal/server/handler_source_links.go
T
Willie 6c9c3f5fce fix(server): roll back the dashboard link when enabling fails
POST /api/source-links with enable: true left the new link in the source
when the config could not be saved, so the dialog reported a failure the
filesystem did not match and a retry failed with "already exists". Share
one sourcelink.Discard rollback between the CLI and the handler.
2026-10-05 23:16:58 +08:00

131 lines
3.9 KiB
Go

package server
import (
"errors"
"net/http"
"os"
"path/filepath"
"time"
"skillshare/internal/config"
"skillshare/internal/sourcelink"
"skillshare/internal/sourcewalk"
"skillshare/internal/utils"
)
type sourceLinkRequest struct {
Path string `json:"path"`
Name string `json:"name,omitempty"`
Enable bool `json:"enable,omitempty"`
}
type sourceLinkItem struct {
Name string `json:"name"`
Target string `json:"target"`
Available bool `json:"available"`
Warning string `json:"warning,omitempty"`
}
func listSourceLinks(source string, walk sourcewalk.Options) ([]sourceLinkItem, error) {
entries, err := sourcewalk.ReadDir(source, sourcewalk.Options{})
if err != nil && !os.IsNotExist(err) {
return nil, err
}
// Skips recorded so far, including traversal failures discovery met inside
// a target that was readable at its root; call this after discovery.
skipped := map[string]string{}
for _, sk := range walk.Follow.Skipped() {
skipped[sk.Name] = sk.Reason
}
links := []sourceLinkItem{}
for _, entry := range entries {
path := filepath.Join(source, entry.Name())
if !utils.IsLinkMode(path, entry.Type()) {
continue
}
item := sourceLinkItem{Name: entry.Name()}
item.Target, err = utils.ResolveLinkTarget(path)
resolved, followed := walk.Follow.Resolve(path)
switch {
case err != nil:
item.Warning = err.Error()
case followed && skipped[item.Name] == "":
item.Target, item.Available = resolved, true
case walk.Follow == nil:
item.Warning = "follow_source_links is off"
case skipped[item.Name] != "":
item.Warning = skipped[item.Name]
default:
item.Warning = "target is not a directory"
}
links = append(links, item)
}
return links, nil
}
func (s *Server) handleCreateSourceLink(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var body sourceLinkRequest
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid JSON body")
}
return
}
if body.Path == "" {
writeError(w, http.StatusBadRequest, "path is required")
return
}
s.mu.Lock()
defer s.mu.Unlock()
res, err := sourcelink.Create(s.skillsSource(), config.SkillsTargetPaths(s.cfg.Targets), body.Path, body.Name)
args := map[string]any{"name": body.Name, "target": body.Path, "enable": body.Enable, "scope": "ui"}
if err != nil {
s.writeOpsLog("link", "error", start, args, err.Error())
writeError(w, http.StatusBadRequest, err.Error())
return
}
defer s.snapshotFollow()
following := s.cfg.FollowSourceLinks
if s.IsProjectMode() {
following = s.projectCfg.FollowSourceLinks
}
if body.Enable && !following {
if s.IsProjectMode() {
s.projectCfg.FollowSourceLinks = true
} else {
s.cfg.FollowSourceLinks = true
}
if err := s.saveConfig(); err != nil {
if s.IsProjectMode() {
s.projectCfg.FollowSourceLinks = false
} else {
s.cfg.FollowSourceLinks = false
}
// Enabling is part of the request: do not leave a link behind a failure.
_ = sourcelink.Discard(s.skillsSource(), filepath.Base(res.Path))
s.writeOpsLog("link", "error", start, args, err.Error())
writeError(w, http.StatusInternalServerError, "failed to enable follow_source_links: "+err.Error())
return
}
}
s.writeOpsLog("link", "ok", start, args, "")
writeJSON(w, map[string]any{"path": res.Path, "target": res.Target, "kind": res.Kind, "warning": res.Warning})
}
func (s *Server) handleRemoveSourceLink(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
name := r.PathValue("name")
args := map[string]any{"name": name, "scope": "ui"}
if err := sourcelink.Remove(s.skillsSource(), s.trashBase(), name); err != nil {
s.writeOpsLog("unlink", "error", start, args, err.Error())
writeError(w, http.StatusBadRequest, err.Error())
return
}
s.snapshotFollow()
s.writeOpsLog("unlink", "ok", start, args, "")
writeJSON(w, map[string]any{"success": true, "name": name})
}