apiV0MediaHandler, apiV0TreeHandler

This commit is contained in:
Bel LaPointe
2022-02-08 10:54:19 -07:00
parent 9622c48395
commit 06deb5d0c9
4 changed files with 47 additions and 3 deletions

View File

@@ -1,10 +1,16 @@
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"local/router"
"net/http"
"os"
"path"
"strings"
"github.com/google/uuid"
)
type Server struct {
@@ -56,11 +62,33 @@ func (server *Server) tryCatchHttpHandler(handler func(http.ResponseWriter, *htt
}
func (server *Server) apiV0TreeHandler(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl" + r.URL.Path)
tree := server.tree()
branches, err := tree.Get()
if err != nil {
return err
}
return json.NewEncoder(w).Encode(branches)
}
func (server *Server) apiV0MediaHandler(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl" + r.URL.Path)
if r.Method != http.MethodPost {
http.NotFound(w, r)
return nil
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
id := uuid.New().String()
os.MkdirAll(path.Join(server.root, "media"), os.ModePerm)
if err := ioutil.WriteFile(path.Join(server.root, "media", id), b, os.ModePerm); err != nil {
return err
}
return json.NewEncoder(w).Encode(map[string]map[string]string{
"data": map[string]string{
"filePath": path.Join("/api/v0/media", id),
},
})
}
func (server *Server) apiV0MediaIDHandler(w http.ResponseWriter, r *http.Request) error {
@@ -82,3 +110,7 @@ func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request)
func (server *Server) rootHandler(w http.ResponseWriter, r *http.Request) error {
return errors.New("not impl" + r.URL.Path)
}
func (server *Server) tree() *Tree {
return NewTree(path.Join(server.root, "tree.yaml"))
}