Handle deletes better

This commit is contained in:
Ben Sarmiento
2023-12-02 03:09:58 +01:00
parent 1cb80e5047
commit a8e8bab853
2 changed files with 25 additions and 15 deletions

View File

@@ -32,6 +32,10 @@ func HandleDeleteFile(directory, torrentName, fileName string, t *torrent.Torren
return fmt.Errorf("cannot find file %s", fileName) return fmt.Errorf("cannot find file %s", fileName)
} }
file.Link = "unselect" file.Link = "unselect"
t.UpdateTorrentResponseCache(torrent) if t.CheckDeletedState(torrent) {
t.Delete(torrentName, true, true)
} else {
t.UpdateTorrentResponseCache(torrent)
}
return nil return nil
} }

View File

@@ -155,6 +155,7 @@ func (t *TorrentManager) RefreshTorrents() {
oldTorrents.Set(info.AccessKey, mainTorrent) oldTorrents.Set(info.AccessKey, mainTorrent)
} }
} }
t.log.Infof("Compiled into %d torrents, %d were missing info", oldTorrents.Count(), noInfoCount)
// removed // removed
strset.Difference(t.accessKeySet, freshKeys).Each(func(accessKey string) bool { strset.Difference(t.accessKeySet, freshKeys).Each(func(accessKey string) bool {
t.Delete(accessKey, false, false) t.Delete(accessKey, false, false)
@@ -170,7 +171,6 @@ func (t *TorrentManager) RefreshTorrents() {
t.checkForOtherDeletedTorrents() t.checkForOtherDeletedTorrents()
// now we can build the directory responses // now we can build the directory responses
t.UpdateDirectoryResponsesCache() t.UpdateDirectoryResponsesCache()
t.log.Infof("Compiled into %d torrents, %d were missing info", oldTorrents.Count(), noInfoCount)
t.SetNewLatestState(t.getCurrentState()) t.SetNewLatestState(t.getCurrentState())
@@ -557,20 +557,8 @@ func (t *TorrentManager) checkForOtherDeletedTorrents() {
var toDelete []string var toDelete []string
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
allTorrents.IterCb(func(_ string, torrent *Torrent) { allTorrents.IterCb(func(_ string, torrent *Torrent) {
unselected := 0 if t.CheckDeletedState(torrent) {
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.Link == "unselect" {
unselected++
}
})
if unselected == torrent.SelectedFiles.Count() && unselected > 0 {
t.log.Infof("Deleting %s", torrent.AccessKey)
toDelete = append(toDelete, torrent.AccessKey) toDelete = append(toDelete, torrent.AccessKey)
} else if unselected > 0 {
// save to file
for i := range torrent.Instances {
t.writeTorrentToFile(&torrent.Instances[i])
}
} }
}) })
for _, accessKey := range toDelete { for _, accessKey := range toDelete {
@@ -578,6 +566,24 @@ func (t *TorrentManager) checkForOtherDeletedTorrents() {
} }
} }
func (t *TorrentManager) CheckDeletedState(torrent *Torrent) bool {
unselected := 0
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.Link == "unselect" {
unselected++
}
})
if unselected == torrent.SelectedFiles.Count() && unselected > 0 {
return true
} else if unselected > 0 {
// save to file
for i := range torrent.Instances {
t.writeTorrentToFile(&torrent.Instances[i])
}
}
return false
}
func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirectoryResponses bool) { func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirectoryResponses bool) {
if deleteInRD { if deleteInRD {
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) allTorrents, _ := t.DirectoryMap.Get(INT_ALL)