Refactor file links and merges

This commit is contained in:
Ben Sarmiento
2024-01-27 17:38:26 +01:00
parent 1913498cbe
commit 1aabcfd322
5 changed files with 59 additions and 104 deletions

View File

@@ -231,7 +231,6 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
} else {
torrent.InProgressIDs.Add(info.ID)
}
torrent.BrokenLinks = mapset.NewSet[string]()
infoCache.Set(rdTorrent.ID, &torrent)
t.saveTorrentChangesToDisk(&torrent, nil)
@@ -240,61 +239,63 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
}
func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
var newer, older *Torrent
if existing.Added < toMerge.Added {
newer = toMerge
older = existing
} else {
newer = existing
older = toMerge
}
// build the main torrent
mainTorrent := Torrent{
Name: existing.Name,
OriginalName: existing.OriginalName,
Rename: existing.Rename,
Hash: existing.Hash,
DownloadedIDs: mapset.NewSet[string](),
InProgressIDs: mapset.NewSet[string](),
// UnassignedLinks: mapset.NewSet[string](),
UnassignedLinks: existing.UnassignedLinks.Union(toMerge.UnassignedLinks),
BrokenLinks: existing.BrokenLinks.Union(toMerge.BrokenLinks),
Name: newer.Name,
OriginalName: newer.OriginalName,
Rename: newer.Rename,
Hash: newer.Hash,
Added: newer.Added,
DownloadedIDs: newer.DownloadedIDs.Union(older.DownloadedIDs),
InProgressIDs: newer.InProgressIDs.Union(older.InProgressIDs),
UnassignedLinks: newer.UnassignedLinks.Union(older.UnassignedLinks),
SelectedFiles: newer.SelectedFiles,
}
// unrepairable reason
if existing.UnrepairableReason != "" && toMerge.UnrepairableReason != "" && existing.UnrepairableReason != toMerge.UnrepairableReason {
mainTorrent.UnrepairableReason = fmt.Sprintf("%s, %s", existing.UnrepairableReason, toMerge.UnrepairableReason)
} else if existing.UnrepairableReason != "" {
mainTorrent.UnrepairableReason = existing.UnrepairableReason
} else if toMerge.UnrepairableReason != "" {
mainTorrent.UnrepairableReason = toMerge.UnrepairableReason
if newer.UnrepairableReason != "" && older.UnrepairableReason != "" && newer.UnrepairableReason != older.UnrepairableReason {
mainTorrent.UnrepairableReason = fmt.Sprintf("%s, %s", newer.UnrepairableReason, older.UnrepairableReason)
} else if newer.UnrepairableReason != "" {
mainTorrent.UnrepairableReason = newer.UnrepairableReason
} else if older.UnrepairableReason != "" {
mainTorrent.UnrepairableReason = older.UnrepairableReason
}
// this function triggers only when we have a new DownloadedID
toMerge.DownloadedIDs.Difference(existing.DownloadedIDs).Each(func(id string) bool {
mainTorrent.DownloadedIDs.Add(id)
// update in progress ids
mainTorrent.DownloadedIDs.Each(func(id string) bool {
mainTorrent.InProgressIDs.Remove(id)
return false
})
// the link can have the following values
// 1. https://*** - the file is available
// 2. repair - the link is there but we need to repair it
// 3. unselect - the file is deleted
// 4. empty - the file is not available
mainTorrent.SelectedFiles = existing.SelectedFiles
toMerge.SelectedFiles.IterCb(func(filepath string, fileToMerge *File) {
// see if it already exists in the main torrent
if originalFile, ok := mainTorrent.SelectedFiles.Get(filepath); !ok || fileToMerge.Link == "unselect" {
// if it doesn't exist in the main torrent, add it
mainTorrent.SelectedFiles.Set(filepath, fileToMerge)
} else if originalFile.Link != "unselect" {
// if it exists, compare the Added property and the link
if existing.Added < toMerge.Added {
// && strings.HasPrefix(fileToMerge.Link, "http")
// if torrentToMerge is more recent and its file has a link, update the main torrent's file
mainTorrent.SelectedFiles.Set(filepath, fileToMerge)
// 2. unselect - the file is deleted
// 3. empty - the file is not available
mainTorrent.SelectedFiles.IterCb(func(key string, file *File) {
if file.Link == "" {
file, ok := older.SelectedFiles.Get(key)
if ok {
mainTorrent.SelectedFiles.Set(key, file)
}
// else do nothing, the main torrent's file is more recent or has a valid link
}
})
if existing.Added < toMerge.Added {
mainTorrent.Added = toMerge.Added
} else {
mainTorrent.Added = existing.Added
}
older.SelectedFiles.IterCb(func(key string, file *File) {
if !mainTorrent.SelectedFiles.Has(key) {
mainTorrent.SelectedFiles.Set(key, file)
} else if file.Link == "unselect" {
mainTorrent.SelectedFiles.Set(key, file)
}
})
return mainTorrent
}