Fix file mismatch issue

This commit is contained in:
Ben Sarmiento
2023-11-18 14:00:51 +01:00
parent 0e9302f3b5
commit e85109bcf3

View File

@@ -367,7 +367,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
// it also has a Link field, which can be empty // it also has a Link field, which can be empty
// if it is empty, it means the file is no longer available // if it is empty, it means the file is no longer available
// Files+Links together are the same as SelectedFiles // Files+Links together are the same as SelectedFiles
selectedFiles := cmap.New[*File]() var selectedFiles []*File
streamableCount := 0 streamableCount := 0
// if some Links are empty, we need to repair it // if some Links are empty, we need to repair it
forRepair := false forRepair := false
@@ -378,14 +378,14 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
if file.Selected == 0 { if file.Selected == 0 {
continue continue
} }
selectedFiles.Set(filepath.Base(file.Path), &File{ selectedFiles = append(selectedFiles, &File{
File: file, File: file,
Added: info.Added, Added: info.Added,
Link: "", // no link yet Link: "", // no link yet
ZurgFS: hashStringToFh(file.Path + info.Hash), ZurgFS: hashStringToFh(file.Path + info.Hash),
}) })
} }
if selectedFiles.Count() > len(info.Links) && info.Progress == 100 { if len(selectedFiles) > len(info.Links) && info.Progress == 100 {
// chaotic file means RD will not output the desired file selection // chaotic file means RD will not output the desired file selection
// e.g. even if we select just a single mkv, it will output a rar // e.g. even if we select just a single mkv, it will output a rar
var isChaotic bool var isChaotic bool
@@ -408,27 +408,27 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
return nil return nil
} }
} }
} else if selectedFiles.Count() == len(info.Links) { } else if len(selectedFiles) == len(info.Links) {
// all links are still intact! good! // all links are still intact! good!
// side note: iteration works! // side note: iteration works!
i := 0 for i, file := range selectedFiles {
selectedFiles.IterCb(func(_ string, file *File) { file.Link = info.Links[i]
if i < len(info.Links) {
file.Link = info.Links[i] // verified working!
i++ i++
} }
})
} }
info.ForRepair = forRepair info.ForRepair = forRepair
torrent := Torrent{ torrent := Torrent{
AccessKey: t.getName(info.Name, info.OriginalName), AccessKey: t.getName(info.Name, info.OriginalName),
SelectedFiles: selectedFiles,
LatestAdded: info.Added, LatestAdded: info.Added,
InProgress: info.Progress != 100, InProgress: info.Progress != 100,
Instances: []realdebrid.TorrentInfo{*info}, Instances: []realdebrid.TorrentInfo{*info},
} }
if selectedFiles.Count() > 0 && torrentFromFile == nil { torrent.SelectedFiles = cmap.New[*File]()
for _, file := range selectedFiles {
torrent.SelectedFiles.Set(filepath.Base(file.Path), file)
}
if len(selectedFiles) > 0 && torrentFromFile == nil {
t.writeToFile(info) // only when there are selected files, else it's useless t.writeToFile(info) // only when there are selected files, else it's useless
} }
return &torrent return &torrent
@@ -493,7 +493,7 @@ func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo
return &torrent return &torrent
} }
func (t *TorrentManager) organizeChaos(links []string, selectedFiles cmap.ConcurrentMap[string, *File]) (cmap.ConcurrentMap[string, *File], bool) { func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([]*File, bool) {
type Result struct { type Result struct {
Response *realdebrid.UnrestrictResponse Response *realdebrid.UnrestrictResponse
} }
@@ -523,15 +523,15 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles cmap.Concur
continue continue
} }
found := false found := false
selectedFiles.IterCb(func(_ string, file *File) { for _, file := range selectedFiles {
if strings.Contains(file.Path, result.Response.Filename) { if strings.Contains(file.Path, result.Response.Filename) {
file.Link = result.Response.Link file.Link = result.Response.Link
found = true found = true
} }
}) }
if !found { if !found {
if result.Response.Streamable == 1 { if result.Response.Streamable == 1 {
selectedFiles.Set(filepath.Base(result.Response.Filename), &File{ selectedFiles = append(selectedFiles, &File{
File: realdebrid.File{ File: realdebrid.File{
ID: math.MaxInt32, ID: math.MaxInt32,
Path: result.Response.Filename, Path: result.Response.Filename,