dedupe via file serve
parent
3cc18ba4d5
commit
af4c915b11
|
|
@ -79,16 +79,9 @@ func ensureAndWrite(p string, b []byte) error {
|
|||
}
|
||||
|
||||
func (server *Server) apiV0MediaHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
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()
|
||||
if err := ensureAndWrite(server.diskMediaPath(id), b); err != nil {
|
||||
filePath := server.diskMediaPath(id)
|
||||
if err := server.postContentHandler(filePath, w, r); err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(w).Encode(map[string]map[string]string{
|
||||
|
|
@ -117,7 +110,18 @@ 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)
|
||||
f, err := os.Open(server.diskMediaPath(id))
|
||||
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")
|
||||
}
|
||||
f, err := os.Open(filePath)
|
||||
if os.IsNotExist(err) {
|
||||
http.NotFound(w, r)
|
||||
return nil
|
||||
|
|
@ -130,8 +134,16 @@ func (server *Server) apiV0MediaIDGetHandler(w http.ResponseWriter, r *http.Requ
|
|||
return nil
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
return errors.New("not impl" + r.URL.Path)
|
||||
func (server *Server) postContentHandler(filePath string, w http.ResponseWriter, r *http.Request) error {
|
||||
if r.Method != http.MethodPost {
|
||||
return errors.New("not found")
|
||||
}
|
||||
defer r.Body.Close()
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ensureAndWrite(filePath, b)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesIDHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
|
|
@ -143,7 +155,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)
|
||||
return server.getContentHandler(path.Join(server.root, "index.html"), w, r)
|
||||
}
|
||||
|
||||
func (server *Server) tree() *Tree {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -15,14 +17,17 @@ func TestServerRoutes(t *testing.T) {
|
|||
|
||||
ensureAndWrite(server.diskMediaPath("id"), []byte("hi"))
|
||||
ensureAndWrite(server.diskMediaPath("delid"), []byte("hi"))
|
||||
ensureAndWrite(path.Join(server.root, "index.html"), []byte("mom"))
|
||||
|
||||
cases := map[string]struct {
|
||||
path string
|
||||
method string
|
||||
want string
|
||||
}{
|
||||
"v0: /: get": {
|
||||
path: "/",
|
||||
method: http.MethodGet,
|
||||
want: "mom",
|
||||
},
|
||||
"v0: search: get": {
|
||||
path: "/api/v0/search",
|
||||
|
|
@ -31,10 +36,12 @@ func TestServerRoutes(t *testing.T) {
|
|||
"v0: tree: get": {
|
||||
path: "/api/v0/tree",
|
||||
method: http.MethodGet,
|
||||
want: "{}",
|
||||
},
|
||||
"v0: media: post": {
|
||||
path: "/api/v0/media",
|
||||
method: http.MethodPost,
|
||||
want: `{"data":{"filePath":"/api/v0/media/`,
|
||||
},
|
||||
"v0: media id: del": {
|
||||
path: "/api/v0/media/delid",
|
||||
|
|
@ -43,6 +50,7 @@ func TestServerRoutes(t *testing.T) {
|
|||
"v0: media id: get": {
|
||||
path: "/api/v0/media/id",
|
||||
method: http.MethodGet,
|
||||
want: "hi",
|
||||
},
|
||||
"v0: files: post": {
|
||||
path: "/api/v0/files",
|
||||
|
|
@ -67,6 +75,9 @@ func TestServerRoutes(t *testing.T) {
|
|||
if w.Code != http.StatusOK {
|
||||
t.Fatal(w)
|
||||
}
|
||||
if len(c.want) > 0 && !strings.Contains(string(w.Body.Bytes()), c.want) {
|
||||
t.Fatal(w)
|
||||
}
|
||||
t.Logf("%s %s => %s", c.method, c.path, w.Body.Bytes())
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue