Multi-token support

This commit is contained in:
Ben Adrian Sarmiento
2024-06-28 04:47:43 +02:00
parent 5e06f04f33
commit 962845fb81
15 changed files with 214 additions and 108 deletions

View File

@@ -30,14 +30,13 @@ type TorrentManager struct {
requiredVersion string
Config config.ConfigInterface
api *realdebrid.RealDebrid
rd *realdebrid.RealDebrid
workerPool *ants.Pool
log *logutil.Logger
repairLog *logutil.Logger
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
UnrestrictMap cmap.ConcurrentMap[string, *realdebrid.Download]
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
RootNode *fs.FileNode
@@ -67,14 +66,13 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
requiredVersion: "0.10.0",
Config: cfg,
api: api,
rd: api,
workerPool: workerPool,
log: log,
repairLog: repairLog,
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
DownloadMap: cmap.New[*realdebrid.Download](),
UnrestrictMap: cmap.New[*realdebrid.Download](),
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
DownloadMap: cmap.New[*realdebrid.Download](),
RootNode: fs.NewFileNode("root", true),
@@ -140,34 +138,13 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
}
// proxy function
func (t *TorrentManager) UnrestrictFile(file *File, checkFirstByte bool) (*realdebrid.Download, error) {
func (t *TorrentManager) UnrestrictFile(file *File) (*realdebrid.Download, error) {
if file.State.Is("deleted_file") {
return nil, fmt.Errorf("file %s has been deleted", file.Path)
} else if file.State.Is("broken_file") {
return nil, fmt.Errorf("file %s is broken", file.Path)
}
return t.UnrestrictLink(file.Link, checkFirstByte)
}
func (t *TorrentManager) UnrestrictLink(link string, verifyURL bool) (*realdebrid.Download, error) {
isRealDebrid := strings.HasPrefix(link, "https://real-debrid.com/d/")
if isRealDebrid && t.UnrestrictMap.Has(link[0:39]) {
ret, _ := t.UnrestrictMap.Get(link[0:39])
return ret, nil
} else if !isRealDebrid && t.UnrestrictMap.Has(link) {
ret, _ := t.UnrestrictMap.Get(link)
return ret, nil
}
ret, err := t.api.UnrestrictLink(link, verifyURL)
if err != nil {
return nil, err
}
if isRealDebrid {
t.UnrestrictMap.Set(ret.Link[0:39], ret)
} else {
t.UnrestrictMap.Set(ret.Link, ret)
}
return ret, nil
return t.rd.UnrestrictLink(file.Link)
}
func (t *TorrentManager) GetKey(torrent *Torrent) string {
@@ -242,7 +219,7 @@ func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) error {
if file.MediaInfo != nil || !file.State.Is("ok_file") || !isPlayable {
return
}
unrestrict, err := t.UnrestrictFile(file, true)
unrestrict, err := t.UnrestrictFile(file)
if dlErr, ok := err.(*http.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
bwLimitReached = true
return
@@ -355,17 +332,29 @@ func (t *TorrentManager) deleteInfoFile(torrentID string) {
/// end info functions
func (t *TorrentManager) mountNewDownloads() {
downloads := t.api.GetDownloads()
token, _ := t.rd.GetToken()
var tokenMap cmap.ConcurrentMap[string, *realdebrid.Download]
if token != "" {
tokenMap, _ = t.rd.UnrestrictMap.Get(token)
}
downloads := t.rd.GetDownloads()
mountedCount := 0
for i := range downloads {
isRealDebrid := strings.HasPrefix(downloads[i].Link, "https://real-debrid.com/d/")
if isRealDebrid {
t.UnrestrictMap.SetIfAbsent(downloads[i].Link[0:39], &downloads[i])
} else {
t.UnrestrictMap.SetIfAbsent(downloads[i].Link, &downloads[i])
if !isRealDebrid {
filename := filepath.Base(downloads[i].Filename)
t.DownloadMap.Set(filename, &downloads[i])
mountedCount++
} else if token != "" {
tokenMap.Set(downloads[i].Link, &downloads[i])
}
}
if mountedCount > 0 {
t.log.Infof("Mounted %d new downloads", mountedCount)
} else {
t.log.Debugf("No new downloads to mount")
}
}
// StartDownloadsJob: permanent job for remounting downloads