- Be conservative about the number of goroutines
- Minor fixes
- Add Webdav to ui
- Add more configs to UI
This commit is contained in:
Mukhtar Akere
2025-03-28 00:25:02 +01:00
parent 4ae5de99e8
commit f9bc7ad914
14 changed files with 252 additions and 96 deletions

View File

@@ -39,7 +39,7 @@ type PropfindResponse struct {
type CachedTorrent struct {
*types.Torrent
LastRead time.Time `json:"last_read"`
AddedOn time.Time `json:"added_on"`
IsComplete bool `json:"is_complete"`
}
@@ -86,7 +86,7 @@ type Cache struct {
func NewCache(dc config.Debrid, client types.Client) *Cache {
cfg := config.GetConfig()
torrentRefreshInterval, err := time.ParseDuration(dc.TorrentRefreshInterval)
torrentRefreshInterval, err := time.ParseDuration(dc.TorrentsRefreshInterval)
if err != nil {
torrentRefreshInterval = time.Second * 15
}
@@ -109,7 +109,7 @@ func NewCache(dc config.Debrid, client types.Client) *Cache {
torrentRefreshInterval: torrentRefreshInterval,
downloadLinksRefreshInterval: downloadLinksRefreshInterval,
PropfindResp: xsync.NewMapOf[string, PropfindResponse](),
folderNaming: WebDavFolderNaming(dc.WebDavFolderNaming),
folderNaming: WebDavFolderNaming(dc.FolderNaming),
autoExpiresLinksAfter: autoExpiresLinksAfter,
repairsInProgress: xsync.NewMapOf[string, bool](),
saveSemaphore: make(chan struct{}, 10),
@@ -201,6 +201,7 @@ func (c *Cache) load() (map[string]*CachedTorrent, error) {
return torrents, fmt.Errorf("failed to read cache directory: %w", err)
}
now := time.Now()
for _, file := range files {
if file.IsDir() || filepath.Ext(file.Name()) != ".json" {
continue
@@ -232,7 +233,11 @@ func (c *Cache) load() (map[string]*CachedTorrent, error) {
linkStore[f.Link] = true
}
}
addedOn, err := time.Parse(time.RFC3339, ct.Added)
if err != nil {
addedOn = now
}
ct.AddedOn = addedOn
ct.IsComplete = true
torrents[ct.Id] = &ct
}
@@ -447,10 +452,15 @@ func (c *Cache) ProcessTorrent(t *types.Torrent, refreshRclone bool) error {
return fmt.Errorf("failed to update torrent: %w", err)
}
}
addedOn, err := time.Parse(time.RFC3339, t.Added)
if err != nil {
addedOn = time.Now()
}
ct := &CachedTorrent{
Torrent: t,
LastRead: time.Now(),
IsComplete: len(t.Files) > 0,
AddedOn: addedOn,
}
c.setTorrent(ct)
@@ -487,25 +497,27 @@ func (c *Cache) GetDownloadLink(torrentId, filename, fileLink string) string {
downloadLink, err := c.client.GetDownloadLink(ct.Torrent, &file)
if err != nil {
if errors.Is(err, request.HosterUnavailableError) {
// This code is commented iut due to the fact that if a torrent link is uncached, it's likely that we can't redownload it again
// Do not attempt to repair the torrent if the hoster is unavailable
// Check link here??
c.logger.Debug().Err(err).Msgf("Hoster is unavailable. Triggering repair for %s", ct.Name)
if err := c.repairTorrent(ct); err != nil {
c.logger.Error().Err(err).Msgf("Failed to trigger repair for %s", ct.Name)
return ""
}
// Generate download link for the file then
f := ct.Files[filename]
downloadLink, _ = c.client.GetDownloadLink(ct.Torrent, &f)
f.DownloadLink = downloadLink
file.Generated = time.Now()
ct.Files[filename] = f
c.updateDownloadLink(file.Link, downloadLink)
go func() {
go c.setTorrent(ct)
}()
return downloadLink // Gets download link in the next pass
//c.logger.Debug().Err(err).Msgf("Hoster is unavailable. Triggering repair for %s", ct.Name)
//if err := c.repairTorrent(ct); err != nil {
// c.logger.Error().Err(err).Msgf("Failed to trigger repair for %s", ct.Name)
// return ""
//}
//// Generate download link for the file then
//f := ct.Files[filename]
//downloadLink, _ = c.client.GetDownloadLink(ct.Torrent, &f)
//f.DownloadLink = downloadLink
//file.Generated = time.Now()
//ct.Files[filename] = f
//c.updateDownloadLink(file.Link, downloadLink)
//
//go func() {
// go c.setTorrent(ct)
//}()
//
//return downloadLink // Gets download link in the next pass
}
c.logger.Debug().Err(err).Msgf("Failed to get download link for :%s", file.Link)
@@ -537,10 +549,14 @@ func (c *Cache) AddTorrent(t *types.Torrent) error {
return fmt.Errorf("failed to update torrent: %w", err)
}
}
addedOn, err := time.Parse(time.RFC3339, t.Added)
if err != nil {
addedOn = time.Now()
}
ct := &CachedTorrent{
Torrent: t,
LastRead: time.Now(),
IsComplete: len(t.Files) > 0,
AddedOn: addedOn,
}
c.setTorrent(ct)
c.refreshListings()

View File

@@ -18,10 +18,9 @@ func NewEngine() *Engine {
caches := make(map[string]*Cache)
for _, dc := range cfg.Debrids {
dc = cfg.GetDebridWebDav(dc)
client := createDebridClient(dc)
logger := client.GetLogger()
if dc.UseWebdav {
if dc.UseWebDav {
caches[dc.Name] = NewCache(dc, client)
logger.Info().Msg("Debrid Service started with WebDAV")
} else {

View File

@@ -38,25 +38,25 @@ func (c *Cache) refreshListings() {
} else {
return
}
// Copy the current torrents to avoid concurrent issues
torrents := make([]string, 0, c.torrentsNames.Size())
// COpy the torrents to a string|time map
torrentsTime := make(map[string]time.Time, c.torrents.Size())
torrents := make([]string, 0, c.torrents.Size())
c.torrentsNames.Range(func(key string, value *CachedTorrent) bool {
torrentsTime[key] = value.AddedOn
torrents = append(torrents, key)
return true
})
sort.Slice(torrents, func(i, j int) bool {
return torrents[i] < torrents[j]
})
// Sort the torrents by name
sort.Strings(torrents)
files := make([]os.FileInfo, 0, len(torrents))
now := time.Now()
for _, t := range torrents {
files = append(files, &fileInfo{
name: t,
size: 0,
mode: 0755 | os.ModeDir,
modTime: now,
modTime: torrentsTime[t],
isDir: true,
})
}
@@ -219,10 +219,13 @@ func (c *Cache) refreshTorrent(t *CachedTorrent) *CachedTorrent {
if len(t.Files) == 0 {
return nil
}
addedOn, err := time.Parse(time.RFC3339, _torrent.Added)
if err != nil {
addedOn = time.Now()
}
ct := &CachedTorrent{
Torrent: _torrent,
LastRead: time.Now(),
AddedOn: addedOn,
IsComplete: len(t.Files) > 0,
}
c.setTorrent(ct)

View File

@@ -17,6 +17,7 @@ import (
"strconv"
"strings"
"sync"
"time"
)
type RealDebrid struct {
@@ -178,6 +179,7 @@ func (r *RealDebrid) UpdateTorrent(t *types.Torrent) error {
t.Links = data.Links
t.MountPath = r.MountPath
t.Debrid = r.Name
t.Added = data.Added
t.Files = getTorrentFiles(t, data, false) // Get selected files
return nil
}
@@ -422,6 +424,7 @@ func (r *RealDebrid) getTorrents(offset int, limit int) (int, []*types.Torrent,
InfoHash: t.Hash,
Debrid: r.Name,
MountPath: r.MountPath,
Added: t.Added.Format(time.RFC3339),
})
}
return totalItems, torrents, nil