mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
- Pointing a symlink-mode extra at a new source after its old folder was moved or deleted left the target broken: the old link no longer resolved, so it was not recognised as ours, and the next sync skipped it as pointing elsewhere. The link text is now compared with the old source as well. - A negated include such as "!draft.md" only narrows earlier rules, so on its own it never matches and was always reported as matching no file. Negations are no longer checked for matches. Refs #435
363 lines
11 KiB
Go
363 lines
11 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/config"
|
|
syncpkg "skillshare/internal/sync"
|
|
)
|
|
|
|
// handleExtrasPreview — POST /api/extras/{name}/preview
|
|
// Shows what draft include and exclude patterns do to the extra's source files.
|
|
func (s *Server) handleExtrasPreview(w http.ResponseWriter, r *http.Request) {
|
|
var body struct {
|
|
Include []string `json:"include"`
|
|
Exclude []string `json:"exclude"`
|
|
}
|
|
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
|
|
if !errors.Is(err, errBodyTooLarge) {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
|
}
|
|
return
|
|
}
|
|
|
|
s.mu.RLock()
|
|
extras := s.extrasConfig()
|
|
idx := slices.IndexFunc(extras, func(e config.ExtraConfig) bool { return e.Name == r.PathValue("name") })
|
|
if idx < 0 {
|
|
s.mu.RUnlock()
|
|
writeError(w, http.StatusNotFound, "extra not found: "+r.PathValue("name"))
|
|
return
|
|
}
|
|
sourceDir := s.extrasSourceDir(extras[idx])
|
|
s.mu.RUnlock()
|
|
|
|
files, err := syncpkg.DiscoverExtraFiles(sourceDir)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "source not readable: "+err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, map[string]any{
|
|
"files": syncpkg.PreviewExtraFilter(files, body.Include, body.Exclude),
|
|
"unmatched": syncpkg.UnmatchedIncludes(files, body.Include),
|
|
})
|
|
}
|
|
|
|
// handleExtrasEditTarget — PUT /api/extras/{name}/targets
|
|
// Replaces one target's settings. A new folder or file name removes what
|
|
// skillshare put at the old place (copies stay) and syncs the extra; other
|
|
// changes only write config and take effect on the next sync.
|
|
func (s *Server) handleExtrasEditTarget(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
name := r.PathValue("name")
|
|
|
|
var body struct {
|
|
Path string `json:"path"` // the target as stored in config
|
|
Target struct {
|
|
Path string `json:"path"`
|
|
Mode string `json:"mode"`
|
|
Flatten bool `json:"flatten"`
|
|
Extension string `json:"extension"`
|
|
As string `json:"as"`
|
|
Include []string `json:"include"`
|
|
Exclude []string `json:"exclude"`
|
|
} `json:"target"`
|
|
}
|
|
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 == "" || body.Target.Path == "" {
|
|
writeError(w, http.StatusBadRequest, "path is required")
|
|
return
|
|
}
|
|
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
extras := s.extrasConfig()
|
|
idx := slices.IndexFunc(extras, func(e config.ExtraConfig) bool { return e.Name == name })
|
|
if idx == -1 {
|
|
writeError(w, http.StatusNotFound, "extra not found: "+name)
|
|
return
|
|
}
|
|
extra := extras[idx]
|
|
tIdx := slices.IndexFunc(extra.Targets, func(t config.ExtraTargetConfig) bool { return t.Path == body.Path })
|
|
if tIdx == -1 {
|
|
writeError(w, http.StatusNotFound, "target not found: "+body.Path)
|
|
return
|
|
}
|
|
old := extra.Targets[tIdx]
|
|
|
|
in := body.Target
|
|
next := config.ExtraTargetConfig{Path: in.Path, Mode: in.Mode, Flatten: in.Flatten, Extension: in.Extension, As: in.As, Include: in.Include, Exclude: in.Exclude}
|
|
if next.Extension != "" {
|
|
// A transform extension only makes sense with copy mode.
|
|
next.Mode = "copy"
|
|
}
|
|
if next.Mode == "symlink" {
|
|
// One link to the whole folder cannot leave files out.
|
|
next.Include, next.Exclude = nil, nil
|
|
}
|
|
newPath := filepath.Clean(resolveExtrasTargetPath(s.projectRoot, next.Path))
|
|
if !s.IsProjectMode() {
|
|
next.Path = newPath
|
|
}
|
|
oldPath := filepath.Clean(resolveExtrasTargetPath(s.projectRoot, old.Path))
|
|
for j, t := range extra.Targets {
|
|
if j != tIdx && filepath.Clean(resolveExtrasTargetPath(s.projectRoot, t.Path)) == newPath {
|
|
writeError(w, http.StatusConflict, "target already exists: "+in.Path)
|
|
return
|
|
}
|
|
}
|
|
if err := config.ValidateExtraConfig(config.ExtraConfig{Name: name, File: extra.File, Targets: []config.ExtraTargetConfig{next}}); err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
prev := extra.Targets
|
|
extras[idx].Targets = slices.Clone(prev)
|
|
extras[idx].Targets[tIdx] = next
|
|
if err := s.validateExtra(name); err != nil {
|
|
extras[idx].Targets = prev
|
|
if !writeExtraTargetConflict(w, err, in.Path) {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
}
|
|
return
|
|
}
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
moved := newPath != oldPath || next.As != old.As
|
|
resp := map[string]any{"success": true, "name": name, "target": next.Path}
|
|
status, msg := "ok", ""
|
|
if moved {
|
|
sourceDir := s.extrasSourceDir(extra)
|
|
oldMode := syncpkg.ExtraTargetMode(old.Mode, extra.File != "")
|
|
var errs []string
|
|
resp["pruned"], errs = clearExtraTarget(extra.File, sourceDir, oldPath, old.As, oldMode)
|
|
results := s.syncExtras(name, false, false)
|
|
resp["extras"] = results
|
|
if len(errs) > 0 {
|
|
resp["prune_errors"] = errs
|
|
}
|
|
if status, _ = extrasSyncStatus(results); len(errs) > 0 {
|
|
status, msg = "partial", errs[0]
|
|
}
|
|
}
|
|
|
|
s.writeOpsLog("extras-target", status, start, map[string]any{
|
|
"name": name, "target": next.Path, "from": old.Path, "action": "edit", "scope": "ui",
|
|
}, msg)
|
|
writeJSON(w, resp)
|
|
}
|
|
|
|
// handleExtrasEdit — PATCH /api/extras/{name}
|
|
// Renames an extra or points it at another source folder. A rename keeps the
|
|
// folder: an extra in its default folder gets that folder as its source. A new
|
|
// source must already exist; the old folder is left as it is, and every target
|
|
// is relinked to the new one.
|
|
func (s *Server) handleExtrasEdit(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
name := r.PathValue("name")
|
|
|
|
var body struct {
|
|
Name string `json:"name"`
|
|
// Source is left alone when omitted; "" means the default folder. Global:
|
|
// a path; project: relative to the project root or a path inside it.
|
|
Source *string `json:"source"`
|
|
}
|
|
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
|
|
if !errors.Is(err, errBodyTooLarge) {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
|
}
|
|
return
|
|
}
|
|
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
extras := s.extrasConfig()
|
|
idx := slices.IndexFunc(extras, func(e config.ExtraConfig) bool { return e.Name == name })
|
|
if idx == -1 {
|
|
writeError(w, http.StatusNotFound, "extra not found: "+name)
|
|
return
|
|
}
|
|
extra := extras[idx]
|
|
oldSource := s.extrasSourceDir(extra)
|
|
next := extra
|
|
|
|
if body.Name != "" && body.Name != name {
|
|
// Sync skips agent target folders for the extra named agents, and the
|
|
// memory notes live in the extra named memory.
|
|
for _, special := range []string{"agents", "memory"} {
|
|
if strings.EqualFold(name, special) || strings.EqualFold(body.Name, special) {
|
|
writeError(w, http.StatusBadRequest, "an extra cannot be renamed to or from "+special)
|
|
return
|
|
}
|
|
}
|
|
if err := config.ValidateExtraName(body.Name); err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
others := slices.DeleteFunc(slices.Clone(extras), func(e config.ExtraConfig) bool { return e.Name == name })
|
|
if err := config.ValidateExtraNameUnique(body.Name, others); err != nil {
|
|
writeError(w, http.StatusConflict, err.Error())
|
|
return
|
|
}
|
|
next.Name = body.Name
|
|
if next.Source == "" {
|
|
source, err := s.storedExtraSource(oldSource)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
next.Source = source
|
|
}
|
|
}
|
|
|
|
if body.Source != nil {
|
|
source := *body.Source
|
|
if source != "" && s.IsProjectMode() {
|
|
if abs := config.ExpandPath(source); filepath.IsAbs(abs) {
|
|
if rel, err := filepath.Rel(s.projectRoot, abs); err == nil {
|
|
source = rel
|
|
}
|
|
}
|
|
if err := config.ValidateProjectExtraSource(source); err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
source = filepath.ToSlash(filepath.Clean(source))
|
|
} else if source != "" {
|
|
source = filepath.Clean(config.ExpandPath(source))
|
|
}
|
|
next.Source = source
|
|
}
|
|
newSource := s.extrasSourceDir(next)
|
|
relink := filepath.Clean(newSource) != filepath.Clean(oldSource)
|
|
if relink {
|
|
if extra.File != "" {
|
|
if info, err := os.Stat(filepath.Join(newSource, extra.File)); err != nil || info.IsDir() {
|
|
writeError(w, http.StatusBadRequest, fmt.Sprintf("%s not found in %s", extra.File, newSource))
|
|
return
|
|
}
|
|
} else if info, err := os.Stat(newSource); err != nil || !info.IsDir() {
|
|
writeError(w, http.StatusBadRequest, "folder not found: "+newSource)
|
|
return
|
|
}
|
|
}
|
|
|
|
extras[idx] = next
|
|
if err := s.validateExtra(next.Name); err != nil {
|
|
extras[idx] = extra
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
// Links into the old folder go only once the config points elsewhere, so a
|
|
// failed save leaves working targets alone.
|
|
var pruneErrs []string
|
|
if relink {
|
|
for _, t := range extra.Targets {
|
|
mode := syncpkg.ExtraTargetMode(t.Mode, extra.File != "")
|
|
_, errs := clearExtraTarget(extra.File, oldSource, resolveExtrasTargetPath(s.projectRoot, t.Path), t.As, mode)
|
|
pruneErrs = append(pruneErrs, errs...)
|
|
}
|
|
}
|
|
|
|
resp := map[string]any{"success": true, "name": next.Name}
|
|
status, msg := "ok", ""
|
|
if relink {
|
|
results := s.syncExtras(next.Name, false, false)
|
|
resp["extras"] = results
|
|
status, _ = extrasSyncStatus(results)
|
|
}
|
|
if len(pruneErrs) > 0 {
|
|
resp["prune_errors"] = pruneErrs
|
|
status, msg = "partial", pruneErrs[0]
|
|
}
|
|
|
|
s.writeOpsLog("extras-edit", status, start, map[string]any{
|
|
"name": next.Name, "from": name, "source": next.Source, "scope": "ui",
|
|
}, msg)
|
|
writeJSON(w, resp)
|
|
}
|
|
|
|
// clearExtraTarget removes what skillshare put at one target: links into
|
|
// sourceDir in merge mode, the folder link in symlink mode, and a single-file
|
|
// target goes back to how it was. Copies stay, since nothing records which
|
|
// copied files are skillshare's.
|
|
func clearExtraTarget(file, sourceDir, targetPath, as, mode string) (int, []string) {
|
|
if file != "" {
|
|
changed, err := syncpkg.RestoreExtraTarget(syncpkg.NewExtraFile(sourceDir, file, targetPath, as, mode))
|
|
if err != nil {
|
|
return 0, []string{err.Error()}
|
|
}
|
|
if changed {
|
|
return 1, nil
|
|
}
|
|
return 0, nil
|
|
}
|
|
if mode == "symlink" && !linksTo(targetPath, sourceDir) {
|
|
return 0, nil
|
|
}
|
|
return syncpkg.PruneExtraTargetFiles(targetPath, sourceDir, mode, nil)
|
|
}
|
|
|
|
// storedExtraSource is dir as an extra's source field: absolute in global
|
|
// mode, relative to the project root in project mode.
|
|
func (s *Server) storedExtraSource(dir string) (string, error) {
|
|
if !s.IsProjectMode() {
|
|
return dir, nil
|
|
}
|
|
rel, err := filepath.Rel(s.projectRoot, dir)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.ToSlash(rel), nil
|
|
}
|
|
|
|
// validateExtra runs the ownership and import checks for one extra against the
|
|
// whole config. Callers must hold s.mu.
|
|
func (s *Server) validateExtra(name string) error {
|
|
if s.IsProjectMode() {
|
|
return s.projectCfg.ValidateExtras(s.projectRoot, name)
|
|
}
|
|
return s.cfg.ValidateExtras(name)
|
|
}
|
|
|
|
// linksTo reports whether link is a symlink to dir, comparing the link text
|
|
// too, so a link whose folder has since moved or been deleted still counts.
|
|
func linksTo(link, dir string) bool {
|
|
dest, err := os.Readlink(link)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if !filepath.IsAbs(dest) {
|
|
dest = filepath.Join(filepath.Dir(link), dest)
|
|
}
|
|
if filepath.Clean(dest) == filepath.Clean(dir) {
|
|
return true
|
|
}
|
|
real, err := filepath.EvalSymlinks(link)
|
|
src, srcErr := filepath.EvalSymlinks(dir)
|
|
return err == nil && srcErr == nil && real == src
|
|
}
|