68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package net
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/debridmediamanager.com/zurg/internal/config"
|
|
"github.com/debridmediamanager.com/zurg/internal/dav"
|
|
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
|
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
|
"github.com/debridmediamanager.com/zurg/internal/universal"
|
|
"github.com/dgraph-io/ristretto"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Router creates a WebDAV router
|
|
func Router(mux *http.ServeMux, getfile *universal.GetFile, c config.ConfigInterface, t *torrent.TorrentManager, cache *ristretto.Cache, log *zap.SugaredLogger) {
|
|
mux.HandleFunc("/http/", func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
requestPath := path.Clean(r.URL.Path)
|
|
if countNonEmptySegments(strings.Split(requestPath, "/")) > 3 {
|
|
getfile.HandleGetRequest(w, r, t, c, cache, log)
|
|
} else {
|
|
intHttp.HandleDirectoryListing(w, r, t, cache, log)
|
|
}
|
|
|
|
case http.MethodHead:
|
|
universal.HandleHeadRequest(w, r, t, log)
|
|
|
|
default:
|
|
log.Errorf("Request %s %s not supported yet", r.Method, r.URL.Path)
|
|
http.Error(w, "Method not implemented", http.StatusMethodNotAllowed)
|
|
}
|
|
})
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case "PROPFIND":
|
|
dav.HandlePropfindRequest(w, r, t, cache, log)
|
|
|
|
case http.MethodDelete:
|
|
dav.HandleDeleteRequest(w, r, t, log)
|
|
|
|
case http.MethodGet:
|
|
getfile.HandleGetRequest(w, r, t, c, cache, log)
|
|
|
|
case http.MethodOptions:
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
default:
|
|
log.Errorf("Request %s %s not supported yet", r.Method, r.URL.Path)
|
|
http.Error(w, "Method not implemented", http.StatusMethodNotAllowed)
|
|
}
|
|
})
|
|
}
|
|
|
|
func countNonEmptySegments(urlSegments []string) int {
|
|
count := 0
|
|
for _, s := range urlSegments {
|
|
if s != "" {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|