put is upsert for files

master
Bel LaPointe 2022-02-08 15:26:33 -07:00
parent d0cea24d88
commit 1e01088b92
2 changed files with 32 additions and 6 deletions

View File

@ -268,11 +268,7 @@ func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Requ
if err != nil {
return err
}
branch, ok := branches[id]
if !ok {
http.NotFound(w, r)
return nil
}
branch, _ := branches[id]
branch.Updated = time.Now().UTC()
if title := r.Header.Get("Title"); title != "" {
branch.Title = title
@ -284,7 +280,14 @@ func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Requ
if err := server.putContentHandler(server.diskFilePath(id), w, r); err != nil {
return err
}
return tree.Put(id, branch)
if err := tree.Put(id, branch); 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) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Request) error {

View File

@ -113,6 +113,29 @@ func TestServerPutTreeGetFile(t *testing.T) {
}
server.tree().Put("my pid", Branch{})
var id string
t.Run("put to create an id", func(t *testing.T) {
r := httptest.NewRequest(http.MethodPut, "/my-put-id", strings.NewReader("body"))
r.Header.Set("Title", "my put title")
r.Header.Set("PID", "my pid")
w := httptest.NewRecorder()
if err := server.apiV0FilesIDHandler(w, r); err != nil {
t.Fatal(err)
}
if w.Code != http.StatusOK {
t.Fatal(w)
}
var resp struct {
Data struct {
FilePath string `json:"filePath"`
} `json:"data"`
}
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatal(err)
}
if path.Base(resp.Data.FilePath) != "my-put-id" {
t.Fatal(resp.Data.FilePath)
}
})
t.Run("post", func(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("body"))
r.Header.Set("Title", "my title")