Introduce components

This commit is contained in:
Ben Sarmiento
2024-05-20 20:43:19 +02:00
parent ab81eb5f39
commit a3a24124a8
11 changed files with 222 additions and 202 deletions

View File

@@ -154,7 +154,11 @@ func (t *TorrentManager) Repair(torrent *Torrent, wg *sync.WaitGroup) {
}
func (t *TorrentManager) repair(torrent *Torrent) {
t.log.Infof("Started repair process for torrent %s (ids=%v)", t.GetKey(torrent), torrent.DownloadedIDs.Union(torrent.InProgressIDs).ToSlice())
torrentIDs := []string{}
for id := range torrent.Components {
torrentIDs = append(torrentIDs, id)
}
t.log.Infof("Started repair process for torrent %s (ids=%v)", t.GetKey(torrent), torrentIDs)
// handle torrents with incomplete links for selected files
// torrent can be rare'ed by RD, so we need to check for that
@@ -171,27 +175,14 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// first step: redownload the whole torrent
info, err := t.redownloadTorrent(torrent, "") // reinsert the torrent, passing ""
if info != nil && info.Progress != 100 {
torrent.InProgressIDs.Add(info.ID)
t.saveTorrentChangesToDisk(torrent, nil)
t.log.Infof("Torrent %s (files=%s) is still in progress after redownloading but it should be repaired once done", t.GetKey(torrent), brokenFileIDs)
return
} else if info != nil && info.Progress == 100 && !t.isStillBroken(info, brokenFiles) {
selectedFiles := getSelectedFiles(info)
torrent.SelectedFiles.IterCb(func(_ string, oldFile *File) {
for _, newFile := range selectedFiles {
if oldFile.Bytes == newFile.Bytes {
oldFile.Link = newFile.Link
oldFile.IsBroken = false
break
}
}
})
torrent.DownloadedIDs.Add(info.ID)
t.saveTorrentChangesToDisk(torrent, nil)
t.log.Infof("Successfully repaired torrent %s (files=%s) by redownloading", t.GetKey(torrent), brokenFileIDs)
return
}
t.log.Warnf("Cannot repair torrent %s by redownloading (error=%s)", t.GetKey(torrent), err.Error())
t.log.Warnf("Cannot repair torrent %s by redownloading all files (error=%s)", t.GetKey(torrent), err.Error())
if torrent.UnrepairableReason != "" {
t.log.Debugf("Torrent %s has been marked as unfixable during redownload (%s), ending repair process early", t.GetKey(torrent), torrent.UnrepairableReason)
@@ -209,7 +200,10 @@ func (t *TorrentManager) repair(torrent *Torrent) {
} else if len(brokenFiles) > 1 {
t.log.Infof("Repairing by downloading 2 batches of the %d broken files of torrent %s", len(brokenFiles), t.GetKey(torrent))
oldTorrentIDs := torrent.DownloadedIDs.Union(torrent.InProgressIDs).ToSlice()
oldTorrentIDs := []string{}
for id := range torrent.Components {
oldTorrentIDs = append(torrentIDs, id)
}
newlyDownloadedIds := make([]string, 0)
group := make([]*File, 0)
@@ -270,7 +264,7 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
assigned := false
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
// base it on size because why not?
if file.Bytes == unrestrict.Filesize || strings.HasSuffix(strings.ToLower(file.Path), strings.ToLower(unrestrict.Filename)) {
if (unrestrict.Filesize > 1_000_000 && file.Bytes == unrestrict.Filesize) || strings.HasSuffix(strings.ToLower(file.Path), strings.ToLower(unrestrict.Filename)) {
file.Link = link
file.IsBroken = false
assigned = true
@@ -329,9 +323,8 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
// empty/reset the unassigned links as we have assigned them already
if torrent.UnassignedLinks.Cardinality() > 0 {
t.saveTorrentChangesToDisk(torrent, func(info *Torrent) {
info.UnassignedLinks = mapset.NewSet[string]()
})
torrent.UnassignedLinks = mapset.NewSet[string]()
t.writeTorrentToFile(torrent)
}
return true
@@ -343,7 +336,9 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
oldTorrentIDs := make([]string, 0)
if selection == "" {
// only delete the old torrent if we are redownloading all files
oldTorrentIDs = torrent.DownloadedIDs.Union(torrent.InProgressIDs).ToSlice()
for id := range torrent.Components {
oldTorrentIDs = append(oldTorrentIDs, id)
}
tmpSelection := ""
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
tmpSelection += fmt.Sprintf("%d,", file.ID) // select all files
@@ -488,9 +483,7 @@ func (t *TorrentManager) markAsUnplayable(torrent *Torrent, reason string) {
func (t *TorrentManager) markAsUnfixable(torrent *Torrent, reason string) {
t.log.Warnf("Marking torrent %s as unfixable - %s", t.GetKey(torrent), reason)
torrent.UnrepairableReason = reason
t.saveTorrentChangesToDisk(torrent, func(t *Torrent) {
t.UnrepairableReason = reason
})
t.writeTorrentToFile(torrent)
}
// getBrokenFiles returns the files that are not http links and not deleted