aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/api_files.go
blob: 2a87dd36f3297a1625d830d1122628957b54ca80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package admin

import (
	"io"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

func (s *AdminServer) handleFiles(w http.ResponseWriter, r *http.Request) {
	relPath := r.URL.Query().Get("path")
	relPath = strings.TrimPrefix(relPath, "/")
	relPath = strings.TrimPrefix(relPath, "data/")

	switch r.Method {
	case http.MethodGet:
		if relPath == "" {
			entries, err := os.ReadDir(s.dataDir)
			if err != nil {
				writeJSON(w, map[string]any{"error": err.Error()})
				return
			}
			var files []map[string]any
			for _, e := range entries {
				files = append(files, map[string]any{
					"name": e.Name(),
					"dir":  e.IsDir(),
				})
			}
			writeJSON(w, files)
			return
		}

		fullPath := filepath.Join(s.dataDir, relPath)
		info, err := os.Stat(fullPath)
		if err != nil {
			writeJSON(w, map[string]any{"error": "not found: " + relPath})
			return
		}

		if info.IsDir() {
			entries, err := os.ReadDir(fullPath)
			if err != nil {
				writeJSON(w, map[string]any{"error": err.Error()})
				return
			}
			var files []map[string]any
			for _, e := range entries {
				files = append(files, map[string]any{
					"name": e.Name(),
					"dir":  e.IsDir(),
				})
			}
			writeJSON(w, files)
			return
		}

		data, err := os.ReadFile(fullPath)
		if err != nil {
			writeJSON(w, map[string]any{"error": "read error: " + err.Error()})
			return
		}
		writeJSON(w, map[string]any{
			"name":    info.Name(),
			"content": string(data),
			"path":    relPath,
		})

	case http.MethodPut, http.MethodPost:
		if relPath == "" {
			writeJSON(w, map[string]any{"error": "missing path"})
			return
		}
		fullPath := filepath.Join(s.dataDir, relPath)

		body, err := io.ReadAll(r.Body)
		if err != nil {
			writeJSON(w, map[string]any{"error": "read body: " + err.Error()})
			return
		}

		var oldContent []byte
		if existing, err := snapshotFile(fullPath); err == nil {
			oldContent = existing
		}

		dir := filepath.Dir(fullPath)
		if err := os.MkdirAll(dir, 0755); err != nil {
			writeJSON(w, map[string]any{"error": "mkdir: " + err.Error()})
			return
		}

		if err := os.WriteFile(fullPath, body, 0644); err != nil {
			writeJSON(w, map[string]any{"error": "write error: " + err.Error()})
			return
		}

		isCreate := oldContent == nil
		desc := "Updated file " + relPath
		if isCreate {
			desc = "Created file " + relPath
		}
		s.undoStack.Push(ChangeDesc{
			Description: desc,
			FilePath:    fullPath,
			OldContent:  oldContent,
			NewContent:  body,
			IsCreate:    isCreate,
		})
		writeJSON(w, map[string]any{"ok": true, "path": relPath})

	case http.MethodDelete:
		if relPath == "" {
			writeJSON(w, map[string]any{"error": "missing path"})
			return
		}
		fullPath := filepath.Join(s.dataDir, relPath)
		oldContent, _ := snapshotFile(fullPath)
		if err := os.Remove(fullPath); err != nil {
			writeJSON(w, map[string]any{"error": "delete error: " + err.Error()})
			return
		}
		s.undoStack.Push(ChangeDesc{
			Description: "Deleted file " + relPath,
			FilePath:    fullPath,
			OldContent:  oldContent,
			IsDelete:    true,
		})
		writeJSON(w, map[string]any{"ok": true, "path": relPath})

	default:
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
	}
}