ok this fork of data and tree meta is a problem we gonna trash some code

master
Bel LaPointe 2022-02-09 10:04:14 -07:00
parent 9ad262e607
commit ba154be6c2
3 changed files with 52 additions and 16 deletions

View File

@ -137,7 +137,12 @@ func (server *Server) apiV0MediaIDDelHandler(w http.ResponseWriter, r *http.Requ
func (server *Server) apiV0MediaIDGetHandler(w http.ResponseWriter, r *http.Request) error {
id := path.Base(r.URL.Path)
return server.getContentHandler(server.diskMediaPath(id), w, r)
tree := server.tree()
fullId, err := tree.FullId(id)
if err != nil {
return err
}
return server.getContentHandler(server.diskMediaPath(fullId), w, r)
}
func (server *Server) getContentHandler(filePath string, w http.ResponseWriter, r *http.Request) error {
@ -212,28 +217,33 @@ func (server *Server) rootHandler(w http.ResponseWriter, r *http.Request) error
}
func (server *Server) tree() *Tree {
return NewTree(path.Dir(server.diskFilePath("id")))
return NewTree(path.Dir(server.diskFileDir("id")))
}
func (server *Server) diskFileDir(id string) string {
return path.Dir(server.diskFilePath(id))
}
func (server *Server) diskFilePath(id string) string {
return path.Join(server.root, "files", id)
return path.Join(server.root, "files", id, "data")
}
func (server *Server) diskMediaPath(id string) string {
return path.Join(server.root, "media", id)
return path.Join(server.root, "media", id, "data")
}
func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error {
id, err := server.postContentHandler(path.Dir(server.diskFilePath("id")), w, r)
if err != nil {
return err
}
fileDir := server.diskFileDir("id")
tree := server.tree()
if pid := r.Header.Get("PID"); pid == "" {
} else if branches, err := tree.Get(); err != nil {
} else if fullId, err := tree.FullId(pid); err != nil {
return err
} else {
fileDir = server.diskFileDir(fullId)
}
id, err := server.postContentHandler(fileDir, w, r)
if err != nil {
return err
} else if _, ok := branches[pid]; !ok {
return errors.New("bad pid")
}
if err := tree.Put(id, Branch{
Title: r.Header.Get("Title"),
@ -277,7 +287,12 @@ func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Requ
branch.PID = pid
}
branch.Deleted = false
if err := server.putContentHandler(server.diskFilePath(id), w, r); err != nil {
fullPid, err := tree.FullId(branch.PID)
if err != nil {
return err
}
fullId := path.Join(fullPid, id)
if err := server.putContentHandler(server.diskFilePath(fullId), w, r); err != nil {
return err
}
if err := tree.Put(id, branch); err != nil {
@ -297,10 +312,14 @@ func (server *Server) apiV0FilesIDGetHandler(w http.ResponseWriter, r *http.Requ
return err
}
id := path.Base(r.URL.Path)
fullId, err := tree.FullId(id)
if err != nil {
return err
}
branch, _ := branches[id]
w.Header().Set("Title", branch.Title)
w.Header().Set("PID", branch.PID)
return server.getContentHandler(server.diskFilePath(id), w, r)
return server.getContentHandler(server.diskFilePath(fullId), w, r)
}
func (server *Server) apiV0FilesIDDelHandler(w http.ResponseWriter, r *http.Request) error {
@ -339,13 +358,16 @@ func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request)
return nil
}
results := []string{}
if err := filepath.Walk(path.Dir(server.diskFilePath("id")), func(p string, info os.FileInfo, err error) error {
if err := filepath.Walk(server.diskFileDir("id"), func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if info.Name() != "data" {
return nil
}
b, err := ioutil.ReadFile(p)
if err != nil {
return err

View File

@ -16,13 +16,17 @@ func TestServerRoutes(t *testing.T) {
t.Fatal(err)
}
ensureAndWrite(server.diskMediaPath("id"), []byte("hi"))
if err := ensureAndWrite(server.diskMediaPath("id"), []byte("hi")); err != nil {
t.Fatal(err)
}
ensureAndWrite(server.diskMediaPath("delid"), []byte("hi"))
ensureAndWrite(server.diskFilePath("getfid"), []byte("getfid body"))
ensureAndWrite(server.diskFilePath("putfid"), []byte("initial putfid body"))
ensureAndWrite(server.diskFilePath("delfid"), []byte("delfid body"))
ensureAndWrite(path.Join(server.root, "index.html"), []byte("mom"))
server.tree().Put("putfid", Branch{})
if err := server.tree().Put("putfid", Branch{}); err != nil {
t.Fatal(err)
}
server.tree().Put("delfid", Branch{Title: "delfid title"})
server.tree().Put("getfid", Branch{Title: "getfid title"})
@ -121,6 +125,7 @@ func TestServerPutTreeGetFile(t *testing.T) {
if err := server.apiV0FilesIDHandler(w, r); err != nil {
t.Fatal(err)
}
t.Logf("%s", w.Body.Bytes())
if w.Code != http.StatusOK {
t.Fatal(w)
}
@ -144,6 +149,7 @@ func TestServerPutTreeGetFile(t *testing.T) {
if err := server.apiV0FilesHandler(w, r); err != nil {
t.Fatal(err)
}
t.Logf("%s", w.Body.Bytes())
if w.Code != http.StatusOK {
t.Fatal(w)
}
@ -163,6 +169,7 @@ func TestServerPutTreeGetFile(t *testing.T) {
if err := server.apiV0TreeHandler(w, r); err != nil {
t.Fatal(err)
}
t.Logf("%s", w.Body.Bytes())
if w.Code != http.StatusOK {
t.Fatal(w)
}
@ -172,10 +179,12 @@ func TestServerPutTreeGetFile(t *testing.T) {
})
t.Run("get", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/"+id, nil)
t.Logf("%s", r.URL.String())
w := httptest.NewRecorder()
if err := server.apiV0FilesIDHandler(w, r); err != nil {
t.Fatal(err)
}
t.Logf("%s", w.Body.Bytes())
if w.Code != http.StatusOK {
t.Fatal(w)
}

View File

@ -113,6 +113,11 @@ func (tree *Tree) Get() (map[string]Branch, error) {
return m, err
}
func (tree *Tree) FullId(id string) (string, error) {
fullId, _, err := tree.fullIdAndMeta(id)
return fullId, err
}
func (tree *Tree) fullIdAndMeta(id string) (string, Branch, error) {
m, err := tree.Get()
if err != nil {