we made it wow

master
Bel LaPointe 2022-02-09 12:37:48 -07:00
parent f41b34aa5c
commit dc6df3f0bf
1 changed files with 33 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import (
"local/gziphttp" "local/gziphttp"
"local/router" "local/router"
"local/simpleserve/simpleserve" "local/simpleserve/simpleserve"
"log"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -36,7 +37,7 @@ func (server *Server) Routes() error {
return strings.TrimSuffix(s, "/") + "/" + router.Wildcard return strings.TrimSuffix(s, "/") + "/" + router.Wildcard
} }
wildcards := func(s string) string { wildcards := func(s string) string {
return wildcard(wildcard(s)) return wildcard(s) + router.Wildcard
} }
_ = wildcards _ = wildcards
for path, handler := range map[string]func(http.ResponseWriter, *http.Request) error{ for path, handler := range map[string]func(http.ResponseWriter, *http.Request) error{
@ -44,10 +45,10 @@ func (server *Server) Routes() error {
"/api/v0/tree": server.apiV0TreeHandler, "/api/v0/tree": server.apiV0TreeHandler,
"/api/v0/media": server.apiV0MediaHandler, "/api/v0/media": server.apiV0MediaHandler,
wildcard("/api/v0/media"): server.apiV0MediaIDHandler, wildcard("/api/v0/media"): server.apiV0MediaIDHandler,
"/api/v0/files": server.apiV0FilesHandler, wildcards("/api/v0/files"): server.apiV0FilesHandler,
wildcard("/api/v0/files/"): server.apiV0FilesIDHandler,
"/api/v0/search": server.apiV0SearchHandler, "/api/v0/search": server.apiV0SearchHandler,
} { } {
log.Printf("listening for %s", path)
if err := server.router.Add(path, server.tryCatchHttpHandler(handler)); err != nil { if err := server.router.Add(path, server.tryCatchHttpHandler(handler)); err != nil {
return err return err
} }
@ -90,13 +91,13 @@ func ensureAndWrite(p string, b []byte) error {
func (server *Server) apiV0MediaHandler(w http.ResponseWriter, r *http.Request) error { func (server *Server) apiV0MediaHandler(w http.ResponseWriter, r *http.Request) error {
id := uuid.New().String() id := uuid.New().String()
id, err := server.postContentHandler(server.diskMediaPath(id), w, r) filePath, err := server.postContentHandler(server.diskMediaPath(id), w, r)
if err != nil { if err != nil {
return err return err
} }
return json.NewEncoder(w).Encode(map[string]map[string]string{ return json.NewEncoder(w).Encode(map[string]map[string]string{
"data": map[string]string{ "data": map[string]string{
"filePath": path.Join("/api/v0/media", id), "filePath": path.Join("/api/v0/media", path.Base(filePath)),
}, },
}) })
} }
@ -201,11 +202,9 @@ func (server *Server) diskMediaPath(id string) string {
} }
func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error { func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl")
}
func (server *Server) apiV0FilesIDHandler(w http.ResponseWriter, r *http.Request) error {
switch r.Method { switch r.Method {
case http.MethodPost:
return server.apiV0FilesPostHandler(w, r)
case http.MethodGet: case http.MethodGet:
return server.apiV0FilesIDGetHandler(w, r) return server.apiV0FilesIDGetHandler(w, r)
case http.MethodPut: case http.MethodPut:
@ -213,10 +212,35 @@ func (server *Server) apiV0FilesIDHandler(w http.ResponseWriter, r *http.Request
case http.MethodDelete: case http.MethodDelete:
return server.apiV0FilesIDDelHandler(w, r) return server.apiV0FilesIDDelHandler(w, r)
} }
log.Printf("no method for %s: %s", r.Method, r.URL)
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil
} }
func (server *Server) apiV0FilesPostHandler(w http.ResponseWriter, r *http.Request) error {
f, err := ioutil.TempFile(os.TempDir(), "filesPost*")
if err != nil {
return err
}
f.Close()
defer os.Remove(f.Name())
filePath, err := server.postContentHandler(f.Name(), w, r)
if err != nil {
return err
}
defer os.Remove(filePath)
b, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
pid := server.fileId(r)
id := append(pid, strings.Split(uuid.New().String(), "-")[0])
return server.tree().Put(id, Leaf{Title: r.Header.Get("Title"), Content: string(b)})
}
func (server *Server) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Request) error { func (server *Server) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Request) error {
id := server.fileId(r) id := server.fileId(r)