Repair edge cases
This commit is contained in:
@@ -45,7 +45,7 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.allAccessKeys.Remove(accessKey)
|
t.allAccessKeys.Remove(accessKey)
|
||||||
t.log.Infof("Removing torrent %s from zurg database", accessKey)
|
t.log.Infof("Removing torrent %s from zurg database (not real-debrid)", accessKey)
|
||||||
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
|
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
|
||||||
torrents.Remove(accessKey)
|
torrents.Remove(accessKey)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -122,7 +122,11 @@ func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download {
|
|||||||
if download, exists := t.DownloadCache.Get(link); exists {
|
if download, exists := t.DownloadCache.Get(link); exists {
|
||||||
return download
|
return download
|
||||||
}
|
}
|
||||||
ret, _ := t.Api.UnrestrictLink(link, t.Config.ShouldServeFromRclone())
|
ret, err := t.Api.UnrestrictLink(link, t.Config.ShouldServeFromRclone())
|
||||||
|
if err != nil {
|
||||||
|
t.log.Warnf("Cannot unrestrict link %s: %v", link, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if ret != nil && ret.Link != "" {
|
if ret != nil && ret.Link != "" {
|
||||||
if strings.Contains(ret.Download, "download.real-debrid.") {
|
if strings.Contains(ret.Download, "download.real-debrid.") {
|
||||||
prefHost := t.Config.GetRandomPreferredHost()
|
prefHost := t.Config.GetRandomPreferredHost()
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func (t *TorrentManager) RefreshTorrents() []string {
|
|||||||
if instances[idx].IsDone() && t.fixers.Has(instances[idx].ID) {
|
if instances[idx].IsDone() && t.fixers.Has(instances[idx].ID) {
|
||||||
fixer := instances[idx]
|
fixer := instances[idx]
|
||||||
torrent, _ := t.fixers.Pop(fixer.ID)
|
torrent, _ := t.fixers.Pop(fixer.ID)
|
||||||
t.log.Debugf("Fixer %s is done, let's check if it fixed torrent %s", instances[idx].ID, t.GetKey(torrent))
|
t.log.Debugf("Fixer %s is done, let's check if it fixed torrent %s by redownloading", instances[idx].ID, t.GetKey(torrent))
|
||||||
brokenFiles := getBrokenFiles(torrent)
|
brokenFiles := getBrokenFiles(torrent)
|
||||||
info, err := t.redownloadTorrent(torrent, "")
|
info, err := t.redownloadTorrent(torrent, "")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
@@ -242,6 +242,10 @@ func (t *TorrentManager) repair(torrent *Torrent) {
|
|||||||
t.log.Infof("Successfully repaired torrent %s using repair_method#1", t.GetKey(torrent))
|
t.log.Infof("Successfully repaired torrent %s using repair_method#1", t.GetKey(torrent))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if info != nil && info.ID != "" {
|
||||||
|
t.log.Warnf("Torrent %s is still broken after repair_method#1, cleaning up", t.GetKey(torrent))
|
||||||
|
t.Api.DeleteTorrent(info.ID)
|
||||||
|
}
|
||||||
|
|
||||||
// second solution: add only the broken files
|
// second solution: add only the broken files
|
||||||
if len(brokenFiles) > 0 {
|
if len(brokenFiles) > 0 {
|
||||||
@@ -258,7 +262,6 @@ func (t *TorrentManager) repair(torrent *Torrent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) (*realdebrid.TorrentInfo, error) {
|
func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) (*realdebrid.TorrentInfo, error) {
|
||||||
t.log.Debugf("Redownloading torrent %s, broken files=%s (all if empty)", t.GetKey(torrent), brokenFiles)
|
|
||||||
oldTorrentIDs := make([]string, 0)
|
oldTorrentIDs := make([]string, 0)
|
||||||
// broken files means broken links
|
// broken files means broken links
|
||||||
// if brokenFiles is not provided
|
// if brokenFiles is not provided
|
||||||
@@ -424,7 +427,7 @@ func getBrokenFiles(torrent *Torrent) []*File {
|
|||||||
func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles []*File) bool {
|
func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles []*File) bool {
|
||||||
for _, oldFile := range brokenFiles {
|
for _, oldFile := range brokenFiles {
|
||||||
for idx, newFile := range info.Files {
|
for idx, newFile := range info.Files {
|
||||||
if oldFile.Path == newFile.Path {
|
if oldFile.Path == newFile.Path || oldFile.Bytes == newFile.Bytes {
|
||||||
unrestrict := t.UnrestrictUntilOk(info.Links[idx])
|
unrestrict := t.UnrestrictUntilOk(info.Links[idx])
|
||||||
if unrestrict == nil || oldFile.Bytes != unrestrict.Filesize {
|
if unrestrict == nil || oldFile.Bytes != unrestrict.Filesize {
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -286,26 +286,26 @@ func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*Downloa
|
|||||||
resp, err := rd.client.Do(req)
|
resp, err := rd.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// rd.log.Errorf("Error when executing the unrestrict link request: %v", err)
|
// rd.log.Errorf("Error when executing the unrestrict link request: %v", err)
|
||||||
return nil, fmt.Errorf("unrestrict link request failed so likely it has expired")
|
return nil, fmt.Errorf("unrestrict link request failed: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
rd.log.Errorf("Unrestrict link request returned status code %d for link %s", resp.StatusCode, link)
|
rd.log.Errorf("Unrestrict link request returned status code %d for link %s", resp.StatusCode, link)
|
||||||
// return nil, fmt.Errorf("unrestrict link request returned status code %d so likely it has expired", resp.StatusCode)
|
// return nil, fmt.Errorf("unrestrict link request returned status code %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// rd.log.Errorf("Error when reading the body of unrestrict link response: %v", err)
|
// rd.log.Errorf("Error when reading the body of unrestrict link response: %v", err)
|
||||||
return nil, fmt.Errorf("unreadable body so likely it has expired")
|
return nil, fmt.Errorf("unreadable body: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var response Download
|
var response Download
|
||||||
err = json.Unmarshal(body, &response)
|
err = json.Unmarshal(body, &response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
|
// rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
|
||||||
return nil, fmt.Errorf("undecodable response so likely it has expired")
|
return nil, fmt.Errorf("undecodable response: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if checkFirstByte && !rd.CanFetchFirstByte(response.Download) {
|
if checkFirstByte && !rd.CanFetchFirstByte(response.Download) {
|
||||||
|
|||||||
Reference in New Issue
Block a user