Files
skillshare/internal/server/handler_trash.go
T
Willie 5307b3276e fix(server): use snapshot pattern to eliminate map races and narrow lock scope
Addresses P1 and P2 from code review on the RWMutex commit:

P1 — Stream handlers now snapshot config under RLock before streaming:
- handleDiffStream: clone Targets map to prevent concurrent map panic
- handleCheckStream, handleAuditStream: snapshot source/projectRoot
- handleUpdateStream: snapshot source (per-item Lock already correct)

P2 — All read handlers now use narrow snapshot instead of defer RUnlock:
- RLock → copy needed fields to locals → RUnlock → do work with locals
- Prevents slow I/O (git fetch, version check, audit scan) from blocking
  the auto-reload middleware's exclusive Lock on concurrent requests

Other changes:
- computeTargetDiff takes source parameter (no more s.cfg.Source access)
- Add cloneTargets() helper for safe map copying
- Add writeOpsLogTo/writeAuditLogTo for lock-free oplog writes
2026-03-17 04:48:42 +08:00

136 lines
3.1 KiB
Go

package server
import (
"net/http"
"os"
"time"
"skillshare/internal/trash"
)
type trashItemJSON struct {
Name string `json:"name"`
Timestamp string `json:"timestamp"`
Date string `json:"date"`
Size int64 `json:"size"`
Path string `json:"path"`
}
// trashBase returns the trash directory for the current mode.
func (s *Server) trashBase() string {
if s.IsProjectMode() {
return trash.ProjectTrashDir(s.projectRoot)
}
return trash.TrashDir()
}
// handleListTrash returns all trashed items with total size.
func (s *Server) handleListTrash(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
base := s.trashBase()
s.mu.RUnlock()
items := trash.List(base)
out := make([]trashItemJSON, 0, len(items))
for _, item := range items {
out = append(out, trashItemJSON{
Name: item.Name,
Timestamp: item.Timestamp,
Date: item.Date.Format("2006-01-02T15:04:05Z07:00"),
Size: item.Size,
Path: item.Path,
})
}
writeJSON(w, map[string]any{
"items": out,
"totalSize": trash.TotalSize(base),
})
}
// handleRestoreTrash restores a trashed skill back to the source directory.
func (s *Server) handleRestoreTrash(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
name := r.PathValue("name")
base := s.trashBase()
entry := trash.FindByName(base, name)
if entry == nil {
writeError(w, http.StatusNotFound, "trashed item not found: "+name)
return
}
if err := trash.Restore(entry, s.cfg.Source); err != nil {
writeError(w, http.StatusInternalServerError, "failed to restore: "+err.Error())
return
}
s.writeOpsLog("trash", "ok", start, map[string]any{
"action": "restore",
"name": name,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true})
}
// handleDeleteTrash permanently deletes a single trashed item.
func (s *Server) handleDeleteTrash(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
name := r.PathValue("name")
base := s.trashBase()
entry := trash.FindByName(base, name)
if entry == nil {
writeError(w, http.StatusNotFound, "trashed item not found: "+name)
return
}
if err := os.RemoveAll(entry.Path); err != nil {
writeError(w, http.StatusInternalServerError, "failed to delete: "+err.Error())
return
}
s.writeOpsLog("trash", "ok", start, map[string]any{
"action": "delete",
"name": name,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true})
}
// handleEmptyTrash permanently deletes all trashed items.
func (s *Server) handleEmptyTrash(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
base := s.trashBase()
items := trash.List(base)
removed := 0
for _, item := range items {
if err := os.RemoveAll(item.Path); err != nil {
writeError(w, http.StatusInternalServerError, "failed to empty trash: "+err.Error())
return
}
removed++
}
s.writeOpsLog("trash", "ok", start, map[string]any{
"action": "empty",
"removed": removed,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true, "removed": removed})
}