Identify broken links properly

This commit is contained in:
Ben Sarmiento
2024-01-11 07:43:27 +01:00
parent 628e3d6345
commit cc37a92d75
6 changed files with 29 additions and 22 deletions

View File

@@ -16,16 +16,12 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
torrent.DownloadedIDs.Each(func(id string) bool {
info, _ := infoCache.Get(id)
info.SelectedFiles.IterCb(func(_ string, file *File) {
found := false
for _, unselectedID := range unselectedIDs {
if file.ID == unselectedID {
found = true
file.Link = "unselect"
break
}
}
if found {
file.Link = "unselect"
}
})
t.writeTorrentToFile(id, info, false)
return false

View File

@@ -43,7 +43,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
Api: api,
allAccessKeys: mapset.NewSet[string](),
latestState: &LibraryState{},
requiredVersion: "10.01.2024",
requiredVersion: "11.01.2024",
workerPool: p,
log: log,
}

View File

@@ -180,6 +180,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
} else {
torrent.InProgressIDs.Add(info.ID)
}
torrent.BrokenLinks = mapset.NewSet[string]()
t.writeTorrentToFile(rdTorrent.ID, &torrent, true)
infoCache.Set(rdTorrent.ID, &torrent)

View File

@@ -102,26 +102,16 @@ func (t *TorrentManager) repairAll() {
func (t *TorrentManager) Repair(torrent *Torrent) {
// save the broken files to the file cache
var brokenFileIDs []int
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.Link == "repair" {
brokenFileIDs = append(brokenFileIDs, file.ID)
}
})
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
torrent.DownloadedIDs.Each(func(id string) bool {
info, _ := infoCache.Get(id)
info.SelectedFiles.IterCb(func(_ string, file *File) {
found := false
for _, brokenFileID := range brokenFileIDs {
if file.ID == brokenFileID {
found = true
break
torrent.BrokenLinks.Each(func(link string) bool {
if file.Link == link {
file.Link = "repair"
}
}
if found {
file.Link = "unselect"
}
return file.Link == link
})
})
t.writeTorrentToFile(id, info, false)
return false
@@ -221,7 +211,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// second solution: add only the broken files
var brokenFiles []File
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.Link == "repair" || file.Link == "" {
if !strings.HasPrefix(file.Link, "http") && file.Link != "unselect" {
brokenFiles = append(brokenFiles, *file)
}
})

View File

@@ -24,6 +24,7 @@ type Torrent struct {
DownloadedIDs mapset.Set[string] `json:"DownloadedIDs"`
InProgressIDs mapset.Set[string] `json:"InProgressIDs"`
UnassignedLinks mapset.Set[string] `json:"UnassignedLinks"`
BrokenLinks mapset.Set[string] `json:"BrokenLinks"`
Version string `json:"Version"` // only used for files
}
@@ -35,6 +36,7 @@ func (t *Torrent) MarshalJSON() ([]byte, error) {
DownloadedIDsJson stdjson.RawMessage `json:"DownloadedIDs"`
InProgressIDsJson stdjson.RawMessage `json:"InProgressIDs"`
UnassignedLinksJson stdjson.RawMessage `json:"UnassignedLinks"`
BrokenLinksJson stdjson.RawMessage `json:"BrokenLinks"`
*Alias
}{
Alias: (*Alias)(t),
@@ -67,6 +69,13 @@ func (t *Torrent) MarshalJSON() ([]byte, error) {
temp.UnassignedLinksJson = []byte(unassignedLinksStr)
}
if t.BrokenLinks.IsEmpty() {
temp.BrokenLinksJson = []byte(`""`)
} else {
brokenLinksStr := `"` + strings.Join(t.BrokenLinks.ToSlice(), ",") + `"`
temp.BrokenLinksJson = []byte(brokenLinksStr)
}
return json.Marshal(temp)
}
@@ -77,6 +86,7 @@ func (t *Torrent) UnmarshalJSON(data []byte) error {
DownloadedIDsJson stdjson.RawMessage `json:"DownloadedIDs"`
InProgressIDsJson stdjson.RawMessage `json:"InProgressIDs"`
UnassignedLinksJson stdjson.RawMessage `json:"UnassignedLinks"`
BrokenLinksJson stdjson.RawMessage `json:"BrokenLinks"`
*Alias
}{
Alias: (*Alias)(t),
@@ -113,6 +123,13 @@ func (t *Torrent) UnmarshalJSON(data []byte) error {
t.UnassignedLinks = mapset.NewSet[string]()
}
if len(temp.BrokenLinksJson) > 2 {
brokenLinks := strings.Split(strings.ReplaceAll(string(temp.BrokenLinksJson), `"`, ""), ",")
t.BrokenLinks = mapset.NewSet[string](brokenLinks...)
} else {
t.BrokenLinks = mapset.NewSet[string]()
}
return nil
}

View File

@@ -56,6 +56,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
if unrestrict == nil {
log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link)
if cfg.EnableRepair() {
torrent.BrokenLinks.Add(file.Link)
file.Link = "repair"
torMgr.Repair(torrent)
} else {
@@ -159,6 +160,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
if file != nil && unrestrict.Streamable == 1 {
log.Warnf("Cannot download file %s: %v", file.Path, err)
if cfg.EnableRepair() && torrent != nil {
torrent.BrokenLinks.Add(file.Link)
file.Link = "repair"
torMgr.Repair(torrent)
} else {
@@ -174,6 +176,7 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor
if file != nil && unrestrict.Streamable == 1 {
log.Warnf("Received a %s status code for file %s", download.Status, file.Path)
if cfg.EnableRepair() && torrent != nil {
torrent.BrokenLinks.Add(file.Link)
file.Link = "repair"
torMgr.Repair(torrent)
} else {