added webdav
This commit is contained in:
parent
2e78593526
commit
78bd754298
25 changed files with 7783 additions and 90 deletions
|
|
@ -32,24 +32,26 @@ func skipLog(path string) bool {
|
|||
func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s := &statusInterceptor{w: w}
|
||||
w = s
|
||||
defer func() {
|
||||
if skipLog(r.RequestURI) {
|
||||
return
|
||||
}
|
||||
fs.log.Info("access",
|
||||
"client", r.RemoteAddr,
|
||||
"method", r.Method,
|
||||
"status", s.Status(),
|
||||
"uri", r.RequestURI,
|
||||
)
|
||||
}()
|
||||
if !skipLog(r.RequestURI) {
|
||||
defer func() {
|
||||
if s.Status() == http.StatusPartialContent {
|
||||
return
|
||||
}
|
||||
fs.log.Info("access",
|
||||
"client", r.RemoteAddr,
|
||||
"method", r.Method,
|
||||
"status", s.Status(),
|
||||
"uri", r.RequestURI,
|
||||
)
|
||||
}()
|
||||
}
|
||||
upath := r.URL.Path
|
||||
if !strings.HasPrefix(upath, "/") {
|
||||
upath = "/" + upath
|
||||
r.URL.Path = upath
|
||||
}
|
||||
p := path.Clean(upath)
|
||||
d, offline, err := fs.fs.StatWithOffline(p)
|
||||
d, _, err := fs.fs.StatWithOffline(p)
|
||||
if err != nil {
|
||||
msg, code := toHTTPError(err)
|
||||
http.Error(w, msg, code)
|
||||
|
|
@ -83,7 +85,7 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
i := &responseInterceptor{w: w}
|
||||
// TODO implement directory listing cache
|
||||
offline = true
|
||||
offline := true
|
||||
if offline {
|
||||
r.Header.Del("If-Modified-Since")
|
||||
r.Header.Del("Cache-Control")
|
||||
|
|
@ -117,6 +119,5 @@ func toHTTPError(err error) (string, int) {
|
|||
if errors.Is(err, stdfs.ErrPermission) {
|
||||
return "403 Forbidden", http.StatusForbidden
|
||||
}
|
||||
// Default:
|
||||
return "500 Internal Server Error", http.StatusInternalServerError
|
||||
}
|
||||
|
|
|
|||
74
pkg/srv/webdav.go
Normal file
74
pkg/srv/webdav.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// Copyright (C) 2022 Marius Schellenberger
|
||||
|
||||
package srv
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"cachefs/pkg/fs"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"golang.org/x/net/webdav"
|
||||
)
|
||||
|
||||
type WebDavServer struct {
|
||||
log logr.Logger
|
||||
h http.Handler
|
||||
}
|
||||
|
||||
func NewWebDavServer(fs *fs.WebDavFS, log logr.Logger) http.Handler {
|
||||
return &WebDavServer{
|
||||
log,
|
||||
&webdav.Handler{
|
||||
FileSystem: fs,
|
||||
LockSystem: webdav.NewMemLS(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (dav *WebDavServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s := &statusInterceptor{w: w}
|
||||
if r.Method == http.MethodGet && !skipDavLog(r.RequestURI) {
|
||||
defer func() {
|
||||
if s.Status() == http.StatusPartialContent {
|
||||
return
|
||||
}
|
||||
dav.log.Info("access",
|
||||
"client", r.RemoteAddr,
|
||||
"method", r.Method,
|
||||
"status", s.Status(),
|
||||
"uri", r.RequestURI,
|
||||
)
|
||||
}()
|
||||
}
|
||||
dav.h.ServeHTTP(s, r)
|
||||
}
|
||||
|
||||
var skipDavFiles = []string{
|
||||
"favicon.ico",
|
||||
"AlbumArtSmall.jpg",
|
||||
"AlbumArt.jpg",
|
||||
"Album.jpg",
|
||||
"cover.jpg",
|
||||
"cover.png",
|
||||
"cover.gif",
|
||||
"front.jpg",
|
||||
"front.png",
|
||||
"front.gif",
|
||||
"front.bmp",
|
||||
"thumb.jpg",
|
||||
}
|
||||
|
||||
func skipDavLog(path string) bool {
|
||||
i := strings.LastIndex(path, "/")
|
||||
if i > 0 {
|
||||
path = path[i+1:]
|
||||
}
|
||||
for _, v := range skipDavFiles {
|
||||
if path == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue