2019-12-16 19:43:00 +08:00
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
2020-02-13 15:27:40 +08:00
|
|
|
|
model "github.com/HFO4/cloudreve/models"
|
2019-12-16 19:43:00 +08:00
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/webdav"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var handler *webdav.Handler
|
|
|
|
|
|
2019-12-17 12:02:50 +08:00
|
|
|
|
func init() {
|
|
|
|
|
handler = &webdav.Handler{
|
2019-12-21 18:32:13 +08:00
|
|
|
|
Prefix: "/dav",
|
2019-12-17 12:02:50 +08:00
|
|
|
|
LockSystem: make(map[uint]webdav.LockSystem),
|
2019-12-16 19:43:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 18:32:13 +08:00
|
|
|
|
// ServeWebDAV 处理WebDAV相关请求
|
2019-12-17 12:02:50 +08:00
|
|
|
|
func ServeWebDAV(c *gin.Context) {
|
|
|
|
|
fs, err := filesystem.NewFileSystemFromContext(c)
|
|
|
|
|
if err != nil {
|
2019-12-21 18:32:13 +08:00
|
|
|
|
util.Log().Warning("无法为WebDAV初始化文件系统,%s", err)
|
|
|
|
|
return
|
2019-12-16 19:43:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 15:27:40 +08:00
|
|
|
|
if webdavCtx, ok := c.Get("webdav"); ok {
|
|
|
|
|
application := webdavCtx.(*model.Webdav)
|
|
|
|
|
|
|
|
|
|
// 重定根目录
|
|
|
|
|
if application.Root != "/" {
|
|
|
|
|
if exist, root := fs.IsPathExist(application.Root); exist {
|
|
|
|
|
root.Position = ""
|
|
|
|
|
root.Name = "/"
|
|
|
|
|
fs.Root = root
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 12:02:50 +08:00
|
|
|
|
handler.ServeHTTP(c.Writer, c.Request, fs)
|
2019-12-16 19:43:00 +08:00
|
|
|
|
}
|