Adjust file reads and writes

This commit is contained in:
Ben Sarmiento
2024-05-20 22:57:20 +02:00
parent a3a24124a8
commit d5dd9426ed
5 changed files with 22 additions and 34 deletions

View File

@@ -2,6 +2,7 @@ package torrent
import cmap "github.com/orcaman/concurrent-map/v2" import cmap "github.com/orcaman/concurrent-map/v2"
// CheckDeletedStatus checks if all files in a torrent are marked as deleted, if so, it returns true
func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool { func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
var deletedIDs []int var deletedIDs []int
torrent.SelectedFiles.IterCb(func(_ string, file *File) { torrent.SelectedFiles.IterCb(func(_ string, file *File) {
@@ -19,8 +20,10 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) { func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) {
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
if deleteInRD { var hash string
if torrent, ok := allTorrents.Get(accessKey); ok { if torrent, ok := allTorrents.Get(accessKey); ok {
hash = torrent.Hash
if deleteInRD {
for torrentID := range torrent.Components { for torrentID := range torrent.Components {
t.log.Debugf("Deleting torrent %s (id=%s) in RD", accessKey, torrentID) t.log.Debugf("Deleting torrent %s (id=%s) in RD", accessKey, torrentID)
t.api.DeleteTorrent(torrentID) t.api.DeleteTorrent(torrentID)
@@ -33,4 +36,7 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) {
torrents.Remove(accessKey) torrents.Remove(accessKey)
}) })
allTorrents.Remove(accessKey) allTorrents.Remove(accessKey)
if hash != "" {
t.deleteTorrentFile(hash)
}
} }

View File

@@ -70,7 +70,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
RemountTrigger: make(chan struct{}, 1), RemountTrigger: make(chan struct{}, 1),
latestState: &LibraryState{}, latestState: &LibraryState{},
allAccessKeys: mapset.NewSet[string](), allAccessKeys: mapset.NewSet[string](), // this is for tracking what is deleted
} }
t.fixers = t.readFixersFromFile() t.fixers = t.readFixersFromFile()

View File

@@ -79,8 +79,6 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
}) })
} }
} }
t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount)
// removed torrents // removed torrents
t.allAccessKeys.Difference(freshAccessKeys).Each(func(accessKey string) bool { t.allAccessKeys.Difference(freshAccessKeys).Each(func(accessKey string) bool {
t.Delete(accessKey, false) t.Delete(accessKey, false)
@@ -89,6 +87,8 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
}) })
t.allAccessKeys.Append(freshAccessKeys.ToSlice()...) t.allAccessKeys.Append(freshAccessKeys.ToSlice()...)
t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount)
if t.Config.EnableRepair() { if t.Config.EnableRepair() {
if isInitialRun { if isInitialRun {
t.removeExpiredFixers(instances) t.removeExpiredFixers(instances)
@@ -134,6 +134,13 @@ func (t *TorrentManager) StartRefreshJob() {
// getMoreInfo gets original name, size and files for a torrent // getMoreInfo gets original name, size and files for a torrent
func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
diskTor := t.readTorrentFromFile(rdTorrent.Hash)
if diskTor != nil {
if diskInfo, ok := diskTor.Components[rdTorrent.ID]; ok && diskInfo.Progress == 100 {
return diskTor
}
}
info := t.readInfoFromFile(rdTorrent.ID) info := t.readInfoFromFile(rdTorrent.ID)
if info == nil { if info == nil {
var err error var err error
@@ -142,6 +149,9 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
t.log.Warnf("Cannot get info for id=%s: %v", rdTorrent.ID, err) t.log.Warnf("Cannot get info for id=%s: %v", rdTorrent.ID, err)
return nil return nil
} }
if info.Progress == 100 {
t.writeInfoToFile(info)
}
} }
torrent := Torrent{ torrent := Torrent{
@@ -150,6 +160,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
Added: info.Added, Added: info.Added,
Hash: info.Hash, Hash: info.Hash,
} }
// SelectedFiles is a subset of Files with only the selected ones // SelectedFiles is a subset of Files with only the selected ones
// 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
@@ -192,10 +203,6 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
} }
torrent.Components = map[string]*realdebrid.TorrentInfo{rdTorrent.ID: info} torrent.Components = map[string]*realdebrid.TorrentInfo{rdTorrent.ID: info}
if info.Progress == 100 {
t.writeInfoToFile(info)
}
return &torrent return &torrent
} }

View File

@@ -544,27 +544,3 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles
} }
return false return false
} }
func getSelectedFiles(info *realdebrid.TorrentInfo) []*File {
var selectedFiles []*File
// if some Links are empty, we need to repair it
for _, file := range info.Files {
if file.Selected == 0 {
continue
}
selectedFiles = append(selectedFiles, &File{
File: file,
Ended: info.Ended,
Link: "", // no link yet
IsBroken: true,
})
}
if len(selectedFiles) == len(info.Links) {
// all links are still intact! good!
for i, file := range selectedFiles {
file.Link = info.Links[i]
file.IsBroken = false
}
}
return selectedFiles
}

View File

@@ -166,7 +166,6 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
totalCount := result.totalCount totalCount := result.totalCount
if onlyOne { if onlyOne {
rd.log.Debugf("Returning early the %d torrents", len(allTorrents))
return allTorrents, totalCount, nil return allTorrents, totalCount, nil
} }