-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace_handler.go
More file actions
240 lines (210 loc) · 7.51 KB
/
workspace_handler.go
File metadata and controls
240 lines (210 loc) · 7.51 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package module
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/GoCodeAlone/workflow/store"
)
// WorkspaceHandler handles file management API endpoints for project workspaces.
type WorkspaceHandler struct {
workspaces *store.WorkspaceManager
}
// NewWorkspaceHandler creates a new handler backed by the given workspace manager.
func NewWorkspaceHandler(wm *store.WorkspaceManager) *WorkspaceHandler {
return &WorkspaceHandler{workspaces: wm}
}
// HandleWorkspace dispatches workspace file API requests.
// Expected paths:
//
// POST /api/v1/workspaces/{project-id}/files (upload)
// GET /api/v1/workspaces/{project-id}/files (list)
// GET /api/v1/workspaces/{project-id}/files/{path} (download)
// DELETE /api/v1/workspaces/{project-id}/files/{path} (delete)
// POST /api/v1/workspaces/{project-id}/mkdir (create directory)
func (h *WorkspaceHandler) HandleWorkspace(w http.ResponseWriter, r *http.Request) {
// Check for mkdir endpoint first
if r.Method == http.MethodPost && isMkdirPath(r.URL.Path) {
projectID, ok := parseMkdirPath(r.URL.Path)
if !ok || projectID == "" {
writeWorkspaceJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid workspace path"})
return
}
storage, err := h.workspaces.StorageForProject(projectID)
if err != nil {
writeWorkspaceJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("storage error: %v", err)})
return
}
h.handleMkdir(w, r, storage)
return
}
// Extract project ID and file path from URL.
// Expected format: .../workspaces/{project-id}/files[/{path...}]
projectID, filePath, ok := parseWorkspacePath(r.URL.Path)
if !ok || projectID == "" {
writeWorkspaceJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid workspace path"})
return
}
storage, err := h.workspaces.StorageForProject(projectID)
if err != nil {
writeWorkspaceJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("storage error: %v", err)})
return
}
switch r.Method {
case http.MethodGet:
if filePath == "" || filePath == "/" {
h.handleListFiles(w, r, storage)
} else {
h.handleDownloadFile(w, r, storage, filePath)
}
case http.MethodPost:
h.handleUploadFile(w, r, storage)
case http.MethodDelete:
h.handleDeleteFile(w, r, storage, filePath)
default:
writeWorkspaceJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
}
}
func (h *WorkspaceHandler) handleListFiles(w http.ResponseWriter, r *http.Request, storage *store.LocalStorage) {
prefix := r.URL.Query().Get("prefix")
files, err := storage.List(r.Context(), prefix)
if err != nil {
writeWorkspaceJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("list files: %v", err)})
return
}
writeWorkspaceJSON(w, http.StatusOK, files)
}
func (h *WorkspaceHandler) handleDownloadFile(w http.ResponseWriter, r *http.Request, storage *store.LocalStorage, filePath string) {
rc, err := storage.Get(r.Context(), filePath)
if err != nil {
writeWorkspaceJSON(w, http.StatusNotFound, map[string]string{"error": fmt.Sprintf("file not found: %v", err)})
return
}
defer rc.Close()
// Try to detect content type from extension
w.Header().Set("Content-Type", "application/octet-stream")
w.WriteHeader(http.StatusOK)
if _, err := io.Copy(w, rc); err != nil {
// Headers already sent; can't change status
return
}
}
func (h *WorkspaceHandler) handleUploadFile(w http.ResponseWriter, r *http.Request, storage *store.LocalStorage) {
// Parse multipart form (max 32 MB)
if err := r.ParseMultipartForm(32 << 20); err != nil {
writeWorkspaceJSON(w, http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("parse form: %v", err)})
return
}
file, header, err := r.FormFile("file")
if err != nil {
writeWorkspaceJSON(w, http.StatusBadRequest, map[string]string{"error": "file field required"})
return
}
defer file.Close()
// Use the "path" form field if provided, otherwise use the original filename
uploadPath := r.FormValue("path")
if uploadPath == "" {
uploadPath = header.Filename
}
if err := storage.Put(r.Context(), uploadPath, file); err != nil {
writeWorkspaceJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("upload failed: %v", err)})
return
}
info, err := storage.Stat(r.Context(), uploadPath)
if err != nil {
writeWorkspaceJSON(w, http.StatusCreated, map[string]string{"path": uploadPath, "status": "uploaded"})
return
}
writeWorkspaceJSON(w, http.StatusCreated, info)
}
func (h *WorkspaceHandler) handleDeleteFile(w http.ResponseWriter, r *http.Request, storage *store.LocalStorage, filePath string) {
if filePath == "" || filePath == "/" {
writeWorkspaceJSON(w, http.StatusBadRequest, map[string]string{"error": "file path required"})
return
}
if err := storage.Delete(r.Context(), filePath); err != nil {
writeWorkspaceJSON(w, http.StatusNotFound, map[string]string{"error": fmt.Sprintf("delete failed: %v", err)})
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *WorkspaceHandler) handleMkdir(w http.ResponseWriter, r *http.Request, storage *store.LocalStorage) {
var req struct {
Path string `json:"path"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeWorkspaceJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
return
}
if req.Path == "" {
writeWorkspaceJSON(w, http.StatusBadRequest, map[string]string{"error": "path is required"})
return
}
if err := storage.MkdirAll(r.Context(), req.Path); err != nil {
writeWorkspaceJSON(w, http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("mkdir failed: %v", err)})
return
}
info, err := storage.Stat(r.Context(), req.Path)
if err != nil {
writeWorkspaceJSON(w, http.StatusCreated, map[string]string{"path": req.Path, "status": "created"})
return
}
writeWorkspaceJSON(w, http.StatusCreated, info)
}
// parseWorkspacePath extracts the project ID and file path from a URL path.
// Expected format: .../workspaces/{project-id}/files[/{path...}]
func parseWorkspacePath(urlPath string) (projectID, filePath string, ok bool) {
idx := strings.Index(urlPath, "/workspaces/")
if idx < 0 {
return "", "", false
}
rest := urlPath[idx+len("/workspaces/"):]
// Find /files separator
filesIdx := strings.Index(rest, "/files")
if filesIdx < 0 {
return "", "", false
}
projectID = rest[:filesIdx]
if projectID == "" {
return "", "", false
}
afterFiles := rest[filesIdx+len("/files"):]
if afterFiles == "" || afterFiles == "/" {
filePath = ""
} else {
filePath = strings.TrimPrefix(afterFiles, "/")
}
return projectID, filePath, true
}
// isMkdirPath checks if the URL path targets the mkdir endpoint.
func isMkdirPath(urlPath string) bool {
idx := strings.Index(urlPath, "/workspaces/")
if idx < 0 {
return false
}
rest := urlPath[idx+len("/workspaces/"):]
return strings.HasSuffix(rest, "/mkdir")
}
// parseMkdirPath extracts the project ID from a mkdir URL path.
// Expected format: .../workspaces/{project-id}/mkdir
func parseMkdirPath(urlPath string) (projectID string, ok bool) {
idx := strings.Index(urlPath, "/workspaces/")
if idx < 0 {
return "", false
}
rest := urlPath[idx+len("/workspaces/"):]
if !strings.HasSuffix(rest, "/mkdir") {
return "", false
}
projectID = rest[:len(rest)-len("/mkdir")]
if projectID == "" {
return "", false
}
return projectID, true
}
func writeWorkspaceJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}