This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
/
webdav.go
70 lines (62 loc) · 1.54 KB
/
webdav.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package http
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"golang.org/x/net/webdav"
"net/http"
)
type WebDAV struct {
fs webdav.FileSystem
HandlerFunc gin.HandlerFunc
}
func NewWebDav() (wd *WebDAV) {
return &WebDAV{}
}
func (wd *WebDAV) Init() {
wd.fs = webdav.Dir(viper.GetString("webdav.folder_path"))
handler := &webdav.Handler{
Prefix: "/api/webdav",
FileSystem: wd.fs,
LockSystem: webdav.NewMemLS(),
}
wd.HandlerFunc = func(c *gin.Context) {
r := c.Request
w := c.Writer
u, p, ok := r.BasicAuth()
err := handleThirPartyAuth(u, p)
if !ok || err != nil {
w.Header().Set("WWW-Authenticate", `Basic realm="davfs"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
handler.ServeHTTP(w, r)
//c.String(http.StatusOK, "")
}
}
func handleThirPartyAuth(u, p string) (err error) {
request := new(http.Request)
request.Header = make(http.Header)
if u == "" || p == "" {
return fmt.Errorf("no user validate")
}
if u != viper.GetString("webdav.username") || p != viper.GetString("webdav.password") {
return fmt.Errorf("username or password validate")
}
request.SetBasicAuth(u, p)
request.Method = "OPTIONS"
return nil
}
func webDAVHandler(rg *gin.RouterGroup, fn gin.HandlerFunc) {
r := rg.Group("/webdav")
{
r.Any("/*any", fn)
r.Handle("PROPFIND", "/*any", fn)
r.Handle("PROPPATCH", "/*any", fn)
r.Handle("MKCOL", "/*any", fn)
r.Handle("COPY", "/*any", fn)
r.Handle("MOVE", "/*any", fn)
r.Handle("LOCK", "/*any", fn)
r.Handle("UNLOCK", "/*any", fn)
}
}