Add serve from rclone; add readiness check for each debrid, rather than waiting for all to be ready

This commit is contained in:
Mukhtar Akere
2025-05-22 20:01:10 +01:00
parent a2bdad7c2a
commit 83a453cd0c
6 changed files with 105 additions and 65 deletions
+1 -1
View File
@@ -202,7 +202,7 @@ func (f *File) Read(p []byte) (n int, err error) {
// Make the request to get the file
resp, err := f.stream()
if err != nil {
return 0, io.EOF
return 0, err
}
if resp == nil {
return 0, io.EOF
+20
View File
@@ -45,6 +45,20 @@ func (h *Handler) Mkdir(ctx context.Context, name string, perm os.FileMode) erro
return os.ErrPermission // Read-only filesystem
}
func (h *Handler) readinessMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-h.cache.IsReady():
// WebDAV is ready, proceed
next.ServeHTTP(w, r)
default:
// WebDAV is still initializing
w.Header().Set("Retry-After", "5")
http.Error(w, "WebDAV service is initializing, please try again shortly", http.StatusServiceUnavailable)
}
})
}
// RemoveAll implements webdav.FileSystem
func (h *Handler) RemoveAll(ctx context.Context, name string) error {
if name[0] != '/' {
@@ -399,6 +413,7 @@ func (h *Handler) handleGet(w http.ResponseWriter, r *http.Request) {
if err != nil {
h.logger.Debug().
Err(err).
Str("link", file.link).
Str("path", r.URL.Path).
Msg("Could not fetch download link")
http.Error(w, "Could not fetch download link", http.StatusPreconditionFailed)
@@ -409,6 +424,11 @@ func (h *Handler) handleGet(w http.ResponseWriter, r *http.Request) {
return
}
file.downloadLink = link
if h.cache.StreamWithRclone() {
// Redirect to the download link
http.Redirect(w, r, file.downloadLink, http.StatusFound)
return
}
}
rs, ok := fRaw.(io.ReadSeeker)
+4 -1
View File
@@ -153,7 +153,10 @@ func (wd *WebDav) Start(ctx context.Context) error {
func (wd *WebDav) mountHandlers(r chi.Router) {
for _, h := range wd.Handlers {
r.Mount("/"+h.Name, h) // Mount to /name since router is already prefixed with /webdav
r.Route("/"+h.Name, func(r chi.Router) {
r.Use(h.readinessMiddleware)
r.Mount("/", h)
}) // Mount to /name since router is already prefixed with /webdav
}
}