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