add rename method

This commit is contained in:
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 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 return http.StatusOK, nil
case http.MethodPatch: case http.MethodPatch:
// Rename a file or directory // Rename a file or directory
return fi.Rename(w, r)
return http.StatusOK, nil
default: default:
return http.StatusNotImplemented, nil return http.StatusNotImplemented, nil
} }