diff --git a/internal/dav/delete.go b/internal/dav/delete.go index d5ac49b..24413f1 100644 --- a/internal/dav/delete.go +++ b/internal/dav/delete.go @@ -32,6 +32,10 @@ func HandleDeleteFile(directory, torrentName, fileName string, t *torrent.Torren return fmt.Errorf("cannot find file %s", fileName) } file.Link = "unselect" - t.UpdateTorrentResponseCache(torrent) + if t.CheckDeletedState(torrent) { + t.Delete(torrentName, true, true) + } else { + t.UpdateTorrentResponseCache(torrent) + } return nil } diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index c48cc71..8996bf9 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -155,6 +155,7 @@ func (t *TorrentManager) RefreshTorrents() { oldTorrents.Set(info.AccessKey, mainTorrent) } } + t.log.Infof("Compiled into %d torrents, %d were missing info", oldTorrents.Count(), noInfoCount) // removed strset.Difference(t.accessKeySet, freshKeys).Each(func(accessKey string) bool { t.Delete(accessKey, false, false) @@ -170,7 +171,6 @@ func (t *TorrentManager) RefreshTorrents() { t.checkForOtherDeletedTorrents() // now we can build the directory responses t.UpdateDirectoryResponsesCache() - t.log.Infof("Compiled into %d torrents, %d were missing info", oldTorrents.Count(), noInfoCount) t.SetNewLatestState(t.getCurrentState()) @@ -557,20 +557,8 @@ func (t *TorrentManager) checkForOtherDeletedTorrents() { var toDelete []string allTorrents, _ := t.DirectoryMap.Get(INT_ALL) allTorrents.IterCb(func(_ string, torrent *Torrent) { - unselected := 0 - 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) + if t.CheckDeletedState(torrent) { 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 { @@ -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) { if deleteInRD { allTorrents, _ := t.DirectoryMap.Get(INT_ALL)