dedupe via file serve
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user