add rename method

pull/144/head
Henrique Dias 2016-06-11 21:34:38 +01:00
parent 381cac79b2
commit 47faf0b0e5
2 changed files with 19 additions and 2 deletions

View File

@ -79,3 +79,21 @@ func (fi FileInfo) Delete() (int, error) {
return http.StatusOK, nil
}
// Rename function is used tor rename a file or a directory
func (fi FileInfo) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
newname := r.Header.Get("Rename-To")
if newname == "" {
return http.StatusBadRequest, nil
}
newpath := filepath.Clean(newname)
newpath = strings.Replace(fi.Path, fi.Name, newname, 1)
if err := os.Rename(fi.Path, newpath); err != nil {
return ErrorToHTTPCode(err), err
}
http.Redirect(w, r, strings.Replace(fi.URL, fi.Name, newname, 1), http.StatusTemporaryRedirect)
return 0, nil
}

View File

@ -79,8 +79,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
return http.StatusOK, nil
case http.MethodPatch:
// Rename a file or directory
return http.StatusOK, nil
return fi.Rename(w, r)
default:
return http.StatusNotImplemented, nil
}