impl some file handles
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -113,10 +114,6 @@ func (server *Server) apiV0MediaIDGetHandler(w http.ResponseWriter, r *http.Requ
|
||||
return server.getContentHandler(server.diskMediaPath(id), w, r)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.New("not impl: apiV0FilesHandler")
|
||||
}
|
||||
|
||||
func (server *Server) getContentHandler(filePath string, w http.ResponseWriter, r *http.Request) error {
|
||||
if r.Method != http.MethodGet {
|
||||
return errors.New("not found")
|
||||
@@ -138,6 +135,10 @@ func (server *Server) postContentHandler(filePath string, w http.ResponseWriter,
|
||||
if r.Method != http.MethodPost {
|
||||
return errors.New("not found")
|
||||
}
|
||||
return server.putContentHandler(filePath, w, r)
|
||||
}
|
||||
|
||||
func (server *Server) putContentHandler(filePath string, w http.ResponseWriter, r *http.Request) error {
|
||||
defer r.Body.Close()
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -146,14 +147,6 @@ func (server *Server) postContentHandler(filePath string, w http.ResponseWriter,
|
||||
return ensureAndWrite(filePath, b)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.New("not impl" + r.URL.Path)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.New("not impl" + r.URL.Path)
|
||||
}
|
||||
|
||||
func (server *Server) rootHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return server.getContentHandler(path.Join(server.root, "index.html"), w, r)
|
||||
}
|
||||
@@ -162,6 +155,91 @@ func (server *Server) tree() *Tree {
|
||||
return NewTree(path.Join(server.root, "tree.yaml"))
|
||||
}
|
||||
|
||||
func (server *Server) diskFilePath(id string) string {
|
||||
return path.Join(server.root, "files", id)
|
||||
}
|
||||
|
||||
func (server *Server) diskMediaPath(id string) string {
|
||||
return path.Join(server.root, "media", id)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
id := uuid.New().String()
|
||||
filePath := server.diskFilePath(id)
|
||||
if err := server.postContentHandler(filePath, w, r); err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(w).Encode(map[string]map[string]string{
|
||||
"data": map[string]string{
|
||||
"filePath": path.Join("/api/v0/files", id),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
switch r.Method {
|
||||
case http.MethodPut:
|
||||
return server.apiV0FilesIDPutHandler(w, r)
|
||||
case http.MethodGet:
|
||||
return server.apiV0FilesIDGetHandler(w, r)
|
||||
case http.MethodDelete:
|
||||
return server.apiV0FilesIDDelHandler(w, r)
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
id := path.Base(r.URL.Path)
|
||||
tree := server.tree()
|
||||
branches, err := tree.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
branch, ok := branches[id]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return nil
|
||||
}
|
||||
branch.Updated = time.Now().UTC()
|
||||
if title := r.Header.Get("Title"); title != "" {
|
||||
branch.Title = title
|
||||
}
|
||||
branch.Deleted = false
|
||||
if err := server.putContentHandler(server.diskFilePath(id), w, r); err != nil {
|
||||
return err
|
||||
}
|
||||
return tree.Put(id, branch)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
tree := server.tree()
|
||||
branches, err := tree.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id := path.Base(r.URL.Path)
|
||||
branch, _ := branches[id]
|
||||
w.Header().Set("Title", branch.Title)
|
||||
return server.getContentHandler(server.diskFilePath(id), w, r)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDDelHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
tree := server.tree()
|
||||
branches, err := tree.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id := path.Base(r.URL.Path)
|
||||
branch, ok := branches[id]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
branch.Deleted = true
|
||||
branch.Updated = time.Now().UTC()
|
||||
return tree.Put(id, branch)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.New("not impl" + r.URL.Path)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user