add rename method
parent
381cac79b2
commit
47faf0b0e5
18
fileinfo.go
18
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue