Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
type browserNavigationInput struct {
|
|
UUID string `json:"uuid"`
|
|
Label string `json:"label"`
|
|
Path string `json:"path"`
|
|
IsDir bool `json:"is_directory"`
|
|
}
|
|
|
|
func (s *Server) handleBrowserNavigationAPI(w http.ResponseWriter, r *http.Request) {
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
principal := s.requestPrincipal(r)
|
|
if principal == nil || principal.State != "active" {
|
|
http.Error(w, "authentication required", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
links, err := s.store.ListBrowserNavigationLinks(domainUUID, principal.UUID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
visible := links[:0]
|
|
for _, link := range links {
|
|
if s.requestCanReadPath(r, link.Path) {
|
|
visible = append(visible, link)
|
|
}
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"links": visible})
|
|
case http.MethodPost:
|
|
var input browserNavigationInput
|
|
if !decodeBrowserNavigationInput(w, r, &input) {
|
|
return
|
|
}
|
|
input.Path = normalizeAuthorizationPath(input.Path)
|
|
if input.Path == "" || input.Path == "/" || !s.requestCanReadPath(r, input.Path) {
|
|
http.Error(w, "invalid or forbidden path", http.StatusForbidden)
|
|
return
|
|
}
|
|
link, err := s.store.AddBrowserNavigationLink(domainUUID, principal.UUID, input.Label, input.Path, input.IsDir)
|
|
if err != nil {
|
|
status := http.StatusBadRequest
|
|
if errors.Is(err, store.ErrBrowserNavigationLimit) {
|
|
status = http.StatusConflict
|
|
}
|
|
http.Error(w, err.Error(), status)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusCreated)
|
|
_ = json.NewEncoder(w).Encode(link)
|
|
case http.MethodPatch:
|
|
var input browserNavigationInput
|
|
if !decodeBrowserNavigationInput(w, r, &input) {
|
|
return
|
|
}
|
|
if err := s.store.RenameBrowserNavigationLink(domainUUID, principal.UUID, strings.TrimSpace(input.UUID), input.Label); err != nil {
|
|
writeBrowserNavigationError(w, err)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]bool{"ok": true})
|
|
case http.MethodDelete:
|
|
var input browserNavigationInput
|
|
if !decodeBrowserNavigationInput(w, r, &input) {
|
|
return
|
|
}
|
|
if err := s.store.DeleteBrowserNavigationLink(domainUUID, principal.UUID, strings.TrimSpace(input.UUID)); err != nil {
|
|
writeBrowserNavigationError(w, err)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]bool{"ok": true})
|
|
default:
|
|
w.Header().Set("Allow", "GET, POST, PATCH, DELETE")
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func decodeBrowserNavigationInput(w http.ResponseWriter, r *http.Request, input *browserNavigationInput) bool {
|
|
r.Body = http.MaxBytesReader(w, r.Body, 16<<10)
|
|
decoder := json.NewDecoder(r.Body)
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(input); err != nil {
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func writeBrowserNavigationError(w http.ResponseWriter, err error) {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
http.Error(w, "navigator link not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
}
|