mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-08 19:22:57 +00:00

Former-commit-id: 6f9843613e6abfe0b19e6e43f9299afb98645477 [formerly 805fa39b073401c946b559e0e967fdc553f16295] [formerly f9e2de337abc0cbeb5ccdb8b6962871b0bd3ee0e [formerly 1d26b8e95e73a94ba92673861c4e83dfded0d92e]] Former-commit-id: 259a1ba8cdc71e7d32add58f9487e5827aa6685e [formerly 161a1a49bc1158f11f07fc9ff638506dafb6c918] Former-commit-id: 67fe1fef4602baf4ddae40b28e65ac9a2a42ca9d
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package filemanager
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// serveWebDAV handles the webDAV route of the File Manager.
|
|
func serveWebDAV(ctx *requestContext, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
var err error
|
|
|
|
// Checks for user permissions relatively to this path.
|
|
if !ctx.User.Allowed(strings.TrimPrefix(r.URL.Path, ctx.FileManager.WebDavURL)) {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
|
|
switch r.Method {
|
|
case "GET", "HEAD":
|
|
// Excerpt from RFC4918, section 9.4:
|
|
//
|
|
// GET, when applied to a collection, may return the contents of an
|
|
// "index.html" resource, a human-readable view of the contents of
|
|
// the collection, or something else altogether.
|
|
//
|
|
// It was decided on https://github.com/hacdias/caddy-filemanager/issues/85
|
|
// that GET, for collections, will return the same as PROPFIND method.
|
|
path := strings.Replace(r.URL.Path, ctx.FileManager.WebDavURL, "", 1)
|
|
path = ctx.User.scope + "/" + path
|
|
path = filepath.Clean(path)
|
|
|
|
var i os.FileInfo
|
|
i, err = os.Stat(path)
|
|
if err != nil {
|
|
// Is there any error? WebDav will handle it... no worries.
|
|
break
|
|
}
|
|
|
|
if i.IsDir() {
|
|
r.Method = "PROPFIND"
|
|
|
|
if r.Method == "HEAD" {
|
|
w = newResponseWriterNoBody(w)
|
|
}
|
|
}
|
|
case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE":
|
|
if !ctx.User.AllowEdit {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
case "MKCOL", "COPY":
|
|
if !ctx.User.AllowNew {
|
|
return http.StatusForbidden, nil
|
|
}
|
|
}
|
|
|
|
// Preprocess the PUT request if it's the case
|
|
if r.Method == http.MethodPut {
|
|
if err = ctx.FileManager.BeforeSave(r, ctx.FileManager, ctx.User); err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
if put(ctx, w, r) != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
}
|
|
|
|
ctx.FileManager.handler.ServeHTTP(w, r)
|
|
if err = ctx.FileManager.AfterSave(r, ctx.FileManager, ctx.User); err != nil {
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return 0, nil
|
|
}
|