From 4385244d39b37d003dbb7a66a2ff3b05079b7047 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Sat, 4 Jul 2026 23:04:00 -0400 Subject: feat: drag/drop files in gui to change directories --- internal/admin/yaml_util.go | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'internal/admin/yaml_util.go') diff --git a/internal/admin/yaml_util.go b/internal/admin/yaml_util.go index 465be38..519a04c 100644 --- a/internal/admin/yaml_util.go +++ b/internal/admin/yaml_util.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "strings" "gopkg.in/yaml.v3" ) @@ -120,6 +121,8 @@ func listYAMLTree(dataDir, subdir string) (*YAMLTree, error) { } if len(ids) > 0 { tree.Dirs[e.Name()] = ids + } else { + tree.Dirs[e.Name()] = []string{} } } } @@ -198,3 +201,64 @@ func moveEntityFile(dataDir, subdir, id, targetDir string) (string, string, []by } return oldPath, newPath, data, nil } + +func createEntityDir(dataDir, subdir, name string) error { + name = strings.TrimSpace(name) + if name == "" || strings.ContainsAny(name, "/\\") || name == "." || name == ".." { + return fmt.Errorf("invalid directory name") + } + dirPath := filepath.Join(dataDir, subdir, name) + if _, err := os.Stat(dirPath); err == nil { + return fmt.Errorf("directory already exists") + } + return os.Mkdir(dirPath, 0755) +} + +func deleteEntityDir(dataDir, subdir, dir string) ([]ChangeDesc, error) { + dir = strings.TrimSpace(dir) + if dir == "" || strings.ContainsAny(dir, "/\\") || dir == "." || dir == ".." { + return nil, fmt.Errorf("invalid directory") + } + srcDir := filepath.Join(dataDir, subdir, dir) + entries, err := os.ReadDir(srcDir) + if err != nil { + return nil, fmt.Errorf("directory not found: %s", dir) + } + + var changes []ChangeDesc + for _, e := range entries { + if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" { + continue + } + oldPath := filepath.Join(srcDir, e.Name()) + newPath := filepath.Join(dataDir, subdir, e.Name()) + data, err := os.ReadFile(oldPath) + if err != nil { + return nil, fmt.Errorf("read %s: %w", e.Name(), err) + } + if err := os.WriteFile(newPath, data, 0644); err != nil { + return nil, fmt.Errorf("write %s: %w", e.Name(), err) + } + if err := os.Remove(oldPath); err != nil { + return nil, fmt.Errorf("remove %s: %w", e.Name(), err) + } + changes = append(changes, ChangeDesc{ + Description: fmt.Sprintf("move %s %s to root", subdir, e.Name()), + FilePath: oldPath, + NewFilePath: newPath, + OldContent: data, + NewContent: data, + }) + } + + if err := os.Remove(srcDir); err != nil { + return nil, fmt.Errorf("remove directory: %w", err) + } + changes = append(changes, ChangeDesc{ + Description: fmt.Sprintf("delete directory %s/%s", subdir, dir), + FilePath: srcDir, + IsDelete: true, + }) + + return changes, nil +} -- cgit v1.2.3