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

View File

@@ -10,6 +10,7 @@ type WebDav struct {
DownloadLinksRefreshInterval string `json:"download_links_refresh_interval,omitempty"`
Workers int `json:"workers,omitempty"`
AutoExpireLinksAfter string `json:"auto_expire_links_after,omitempty"`
ServeFromRclone bool `json:"serve_from_rclone,omitempty"`
// Folder
FolderNaming string `json:"folder_naming,omitempty"`

View File

@@ -168,6 +168,10 @@ func (c *Cache) IsReady() chan struct{} {
return c.ready
}
func (c *Cache) StreamWithRclone() bool {
return c.config.ServeFromRclone
}
// Reset clears all internal state so the Cache can be reused without leaks.
// Call this after stopping the old Cache (so no goroutines are holding references),
// and before you discard the instance on a restart.

View File

@@ -357,7 +357,7 @@
</div>
<div class="col-md-3">
<div class="form-check me-3">
<input type="checkbox" class="form-check-input addSamples" name="debrid[${index}].add_samples" id="debrid[${index}].add_samples">
<input type="checkbox" class="form-check-input" name="debrid[${index}].add_samples" id="debrid[${index}].add_samples">
<label class="form-check-label" for="debrid[${index}].add_samples">Add Samples</label>
</div>
<small class="form-text text-muted">Add samples, extras etc when adding torrent to debrid(disabled by default)</small>
@@ -370,70 +370,81 @@
<small class="form-text text-muted">Create an internal webdav for this debrid</small>
</div>
</div>
<div class="row mt-3 webdav d-none">
<div class="webdav d-none">
<h6 class="pb-2">Webdav</h6>
<div class="col-md-2 mb-3">
<label class="form-label" for="debrid[${index}].torrents_refresh_interval">Torrents Refresh Interval</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].torrents_refresh_interval" id="debrid[${index}].torrents_refresh_interval" placeholder="15s" value="15s">
<small class="form-text text-muted">How often to refresh the torrents list from debrid(instant when using webdav)</small>
</div>
<div class="col-md-2 mb-3">
<label class="form-label" for="debrid[${index}].download_links_refresh_interval">Links Refresh Interval</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].download_links_refresh_interval" id="debrid[${index}].download_links_refresh_interval" placeholder="40m" value="40m">
<small class="form-text text-muted">How often to refresh the download links list from debrid</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].auto_expire_links_after">Expire Links After</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].auto_expire_links_after" id="debrid[${index}].auto_expire_links_after" placeholder="3d" value="3d">
<small class="form-text text-muted">How long to keep the links in the webdav before expiring</small>
</div>
<div class="col-md-2 mb-3">
<label class="form-label" for="debrid[${index}].workers">Number of Workers</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].workers" id="debrid[${index}].workers" placeholder="e.g., 50" value="50">
<small class="form-text text-muted">Number of workers to use for the webdav server(when refreshing)</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].folder_naming">Folder Naming Structure</label>
<select class="form-select webdav-field" name="debrid[${index}].folder_naming" id="debrid[${index}].folder_naming">
<option value="original_no_ext" selected>Original name with No Ext</option>
<option value="original">Original name</option>
<option value="filename">File name</option>
<option value="filename_no_ext">File name with No Ext</option>
<option value="id">Use ID</option>
<option value="infohash">Use Infohash</option>
</select>
<small class="form-text text-muted">How to name each torrent directory in the webdav</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_url">Rclone RC URL</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].rc_url" id="debrid[${index}].rc_url" placeholder="e.g., http://localhost:9990">
<small class="form-text text-muted">Rclone RC URL for the webdav server(speeds up import significantly)</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_refresh_dirs">Rclone RC Dirs</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].rc_refresh_dirs" id="debrid[${index}].rc_refresh_dirs">
<small class="form-text text-muted">Directories to refresh via RC(comma-seperated e.g. __all__, torrents) </small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_user">Rclone RC User</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].rc_user" id="debrid[${index}].rc_user">
<small class="form-text text-muted">Rclone RC User for the webdav server</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_pass">Rclone RC Password</label>
<input type="password" class="form-control webdav-field" name="debrid[${index}].rc_pass" id="debrid[${index}].rc_pass">
<small class="form-text text-muted">Rclone RC Password for the webdav server</small>
</div>
<div class="col mt-3">
<h6 class="pb-2">Custom Folders</h6>
<div class="col-12">
<p class="text-muted small">Create virtual directories with filters to organize your content</p>
<div class="directories-container" id="debrid[${index}].directories">
<!-- Dynamic directories will be added here -->
<div class="row mt-3">
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].torrents_refresh_interval">Torrents Refresh Interval</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].torrents_refresh_interval" id="debrid[${index}].torrents_refresh_interval" placeholder="15s" value="15s">
<small class="form-text text-muted">How often to refresh the torrents list from debrid</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].download_links_refresh_interval">Links Refresh Interval</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].download_links_refresh_interval" id="debrid[${index}].download_links_refresh_interval" placeholder="40m" value="40m">
<small class="form-text text-muted">How often to refresh the download links list from debrid</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].auto_expire_links_after">Expire Links After</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].auto_expire_links_after" id="debrid[${index}].auto_expire_links_after" placeholder="3d" value="3d">
<small class="form-text text-muted">How long to keep the links in the webdav before expiring</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].workers">Number of Workers</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].workers" id="debrid[${index}].workers" placeholder="e.g., 50" value="50">
<small class="form-text text-muted">Number of workers to use for the webdav server(when refreshing)</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].folder_naming">Folder Naming Structure</label>
<select class="form-select webdav-field" name="debrid[${index}].folder_naming" id="debrid[${index}].folder_naming">
<option value="original_no_ext" selected>Original name with No Ext</option>
<option value="original">Original name</option>
<option value="filename">File name</option>
<option value="filename_no_ext">File name with No Ext</option>
<option value="id">Use ID</option>
<option value="infohash">Use Infohash</option>
</select>
<small class="form-text text-muted">How to name each torrent directory in the webdav</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_url">Rclone RC URL</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].rc_url" id="debrid[${index}].rc_url" placeholder="e.g., http://localhost:9990">
<small class="form-text text-muted">Rclone RC URL for the webdav server(speeds up import significantly)</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_refresh_dirs">Rclone RC Dirs</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].rc_refresh_dirs" id="debrid[${index}].rc_refresh_dirs">
<small class="form-text text-muted">Directories to refresh via RC(comma-seperated e.g. __all__, torrents) </small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_user">Rclone RC User</label>
<input type="text" class="form-control webdav-field" name="debrid[${index}].rc_user" id="debrid[${index}].rc_user">
<small class="form-text text-muted">Rclone RC User for the webdav server</small>
</div>
<div class="col-md-3 mb-3">
<label class="form-label" for="debrid[${index}].rc_pass">Rclone RC Password</label>
<input type="password" class="form-control webdav-field" name="debrid[${index}].rc_pass" id="debrid[${index}].rc_pass">
<small class="form-text text-muted">Rclone RC Password for the webdav server</small>
</div>
<div class="col-md-3 mb-3">
<div class="form-check me-3">
<input type="checkbox" class="form-check-input" name="debrid[${index}].serve_from_rclone" id="debrid[${index}].serve_from_rclone">
<label class="form-check-label" for="debrid[${index}].serve_from_rclone">Serve From Rclone</label>
</div>
<small class="form-text text-muted">Rclone handles serving/streaming the download link</small>
</div>
</div>
<div class="row mt-3">
<div class="col mt-3">
<h6 class="pb-2">Custom Folders</h6>
<div class="col-12">
<p class="text-muted small">Create virtual directories with filters to organize your content</p>
<div class="directories-container" id="debrid[${index}].directories">
<!-- Dynamic directories will be added here -->
</div>
<button type="button" class="btn btn-secondary mt-2 webdav-field" onclick="addDirectory(${index});">
<i class="bi bi-plus"></i> Add Directory
</button>
</div>
<button type="button" class="btn btn-secondary mt-2 webdav-field" onclick="addDirectory(${index});">
<i class="bi bi-plus"></i> Add Directory
</button>
</div>
</div>
</div>
@@ -1096,6 +1107,7 @@
debrid.rc_user = document.querySelector(`[name="debrid[${i}].rc_user"]`).value;
debrid.rc_pass = document.querySelector(`[name="debrid[${i}].rc_pass"]`).value;
debrid.rc_refresh_dirs = document.querySelector(`[name="debrid[${i}].rc_refresh_dirs"]`).value;
debrid.serve_from_rclone = document.querySelector(`[name="debrid[${i}].serve_from_rclone"]`).checked;
//custom folders
debrid.directories = {};

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

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)

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
}
}