diff --git a/fileinfo.go b/fileinfo.go index d3972a75..365d0829 100644 --- a/fileinfo.go +++ b/fileinfo.go @@ -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 +} diff --git a/filemanager.go b/filemanager.go index 0a0155ba..5873815d 100644 --- a/filemanager.go +++ b/filemanager.go @@ -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 }