merge
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
||||
"local/simpleserve/simpleserve"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
@@ -201,7 +200,6 @@ func (server *Server) putContentHandler(filePath string, w http.ResponseWriter,
|
||||
}
|
||||
|
||||
func (server *Server) uiSearchHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/ui/files")
|
||||
t, err := server.uiSubTemplates()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -238,9 +236,8 @@ func (server *Server) uiSearchHandler(w http.ResponseWriter, r *http.Request) er
|
||||
}
|
||||
|
||||
func (server *Server) uiFilesHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/ui/files")
|
||||
id := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")
|
||||
if len(id) == 0 || (len(id) == 1 && id[0] == "") {
|
||||
id := NewID(strings.TrimPrefix(r.URL.Path, "/ui/files"))
|
||||
if id == "" {
|
||||
return server.rootHandler(w, r)
|
||||
}
|
||||
t, err := server.uiSubTemplates()
|
||||
@@ -261,25 +258,25 @@ func (server *Server) uiFilesHandler(w http.ResponseWriter, r *http.Request) err
|
||||
return err
|
||||
}
|
||||
var parent Leaf
|
||||
if len(id) > 1 {
|
||||
parent, err = tree.Get(id[:len(id)-1])
|
||||
if id.Pop() != "" {
|
||||
parent, err = tree.Get(id.Pop())
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to get pid %q: %v", id.Pop(), err)
|
||||
}
|
||||
}
|
||||
leaf, err := tree.Get(id)
|
||||
if err != nil {
|
||||
if len(id) > 1 && parent.Title == "" {
|
||||
return err
|
||||
if id.Pop() != "" {
|
||||
return fmt.Errorf("failed to get id %q: %v", id, err)
|
||||
}
|
||||
leaf.Title = "My New File"
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"This": map[string]string{
|
||||
"This": map[string]interface{}{
|
||||
"Title": leaf.Title,
|
||||
"Content": leaf.Content,
|
||||
"ID": ID,
|
||||
"PID": ID.Pop(),
|
||||
"ID": id,
|
||||
"PID": id.Pop(),
|
||||
"PTitle": parent.Title,
|
||||
},
|
||||
"Tree": string(branchesJSON),
|
||||
@@ -362,20 +359,20 @@ func (server *Server) apiV0FilesPostHandler(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
|
||||
pid := server.fileId(r)
|
||||
id := append(pid, strings.Split(uuid.New().String(), "-")[0])
|
||||
id := NewID(pid).Push(strings.Split(uuid.New().String(), "-")[0])
|
||||
if err := server.tree().Put(id, Leaf{Title: r.Header.Get("Title"), Content: string(b)}); err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(w).Encode(map[string]map[string]string{
|
||||
"data": map[string]string{
|
||||
"filePath": path.Join("/api/v0/files/", server.urlFileId(id)),
|
||||
"filePath": path.Join("/api/v0/files/", id.URLSafeString()),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
id := server.fileId(r)
|
||||
if len(id) == 0 || id[0] == "" {
|
||||
id := NewID(server.fileId(r))
|
||||
if id.String() == "" {
|
||||
return fmt.Errorf("no id found: %+v", id)
|
||||
}
|
||||
|
||||
@@ -393,8 +390,8 @@ func (server *Server) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDDelHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
id := server.fileId(r)
|
||||
if len(id) == 0 || id[0] == "" {
|
||||
id := NewID(server.fileId(r))
|
||||
if id.String() == "" {
|
||||
return fmt.Errorf("no id found: %+v", id)
|
||||
}
|
||||
|
||||
@@ -409,32 +406,19 @@ func (server *Server) apiV0FilesIDDelHandler(w http.ResponseWriter, r *http.Requ
|
||||
return server.tree().Put(id, leaf)
|
||||
}
|
||||
|
||||
func (server *Server) urlFileId(id []string) string {
|
||||
if len(id) == 0 {
|
||||
return ""
|
||||
}
|
||||
result := id[0]
|
||||
for i := 1; i < len(id); i++ {
|
||||
result = strings.Join([]string{result, url.PathEscape(id[i])}, "/")
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (server *Server) fileId(r *http.Request) []string {
|
||||
return strings.Split(
|
||||
strings.Trim(
|
||||
strings.TrimPrefix(
|
||||
strings.Trim(r.URL.Path, "/"),
|
||||
"api/v0/files",
|
||||
),
|
||||
"/"),
|
||||
func (server *Server) fileId(r *http.Request) string {
|
||||
return strings.Trim(
|
||||
strings.TrimPrefix(
|
||||
strings.Trim(r.URL.Path, "/"),
|
||||
"api/v0/files",
|
||||
),
|
||||
"/",
|
||||
)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
id := server.fileId(r)
|
||||
if len(id) == 0 || id[0] == "" {
|
||||
id := NewID(server.fileId(r))
|
||||
if id.String() == "" {
|
||||
return fmt.Errorf("no id found: %+v", id)
|
||||
}
|
||||
|
||||
@@ -457,7 +441,7 @@ func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
return json.NewEncoder(w).Encode(map[string]map[string]string{
|
||||
"data": map[string]string{
|
||||
"filePath": path.Join("/api/v0/files/", server.urlFileId(id)),
|
||||
"filePath": path.Join("/api/v0/files/", id.URLSafeString()),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -496,21 +480,23 @@ func (server *Server) _apiV0SearchHandler(query string) ([][2]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
result := [][2]string{}
|
||||
if err := tree.ForEach(func(id []string, leaf Leaf) error {
|
||||
if err := tree.ForEach(func(id ID, leaf Leaf) error {
|
||||
for _, pattern := range patterns {
|
||||
if !pattern.MatchString(leaf.Content) && !pattern.MatchString(leaf.Title) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
title := leaf.Title
|
||||
for i := len(id) - 1; i >= 1; i-- {
|
||||
parent, err := server.tree().Get(id[:i])
|
||||
pid := id.Pop()
|
||||
for pid != "" {
|
||||
parent, err := server.tree().Get(pid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
title = path.Join(parent.Title, title)
|
||||
pid = pid.Pop()
|
||||
}
|
||||
result = append(result, [2]string{server.urlFileId(id), title})
|
||||
result = append(result, [2]string{id.URLSafeString(), title})
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user