128 lines
3.4 KiB
Go
128 lines
3.4 KiB
Go
package torrent
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
mapset "github.com/deckarep/golang-set/v2"
|
|
)
|
|
|
|
const BINS_FILE = "data/bins.json"
|
|
|
|
// initializeBins reads from bins.json and assigns values to t.trashBin and t.repairBin
|
|
func (t *TorrentManager) initializeBins() {
|
|
if _, err := os.Stat(BINS_FILE); os.IsNotExist(err) {
|
|
t.log.Warn("data/bins.json does not exist. Initializing empty bins.")
|
|
t.ImmediateBin = mapset.NewSet[string]()
|
|
t.OnceDoneBin = mapset.NewSet[string]()
|
|
return
|
|
}
|
|
|
|
fileData, err := os.ReadFile(BINS_FILE)
|
|
if err != nil {
|
|
t.log.Errorf("Failed to read bins.json file: %v", err)
|
|
t.ImmediateBin = mapset.NewSet[string]()
|
|
t.OnceDoneBin = mapset.NewSet[string]()
|
|
return
|
|
}
|
|
|
|
data := map[string][]string{}
|
|
|
|
err = json.Unmarshal(fileData, &data)
|
|
if err != nil {
|
|
t.log.Errorf("Failed to unmarshal bin data: %v", err)
|
|
return
|
|
}
|
|
|
|
t.ImmediateBin = mapset.NewSet[string](data["trash_bin"]...)
|
|
t.OnceDoneBin = mapset.NewSet[string](data["repair_bin"]...)
|
|
|
|
t.log.Debugf("Bin immediately: %v", t.ImmediateBin.ToSlice())
|
|
t.log.Debugf("Bin once done: %v", t.OnceDoneBin.ToSlice())
|
|
}
|
|
|
|
func (t *TorrentManager) persistBins() {
|
|
data := map[string]interface{}{
|
|
"trash_bin": t.ImmediateBin.ToSlice(), // Assuming trashBin is a mapset.Set[string]
|
|
"repair_bin": t.OnceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string]
|
|
}
|
|
|
|
jsonData, err := json.Marshal(data)
|
|
if err != nil {
|
|
t.log.Errorf("Failed to marshal bin data: %v", err)
|
|
return
|
|
}
|
|
|
|
file, err := os.Create(BINS_FILE)
|
|
if err != nil {
|
|
t.log.Errorf("Failed to create bins.json file: %v", err)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = file.Write(jsonData)
|
|
if err != nil {
|
|
t.log.Errorf("Failed to write to bins.json file: %v", err)
|
|
}
|
|
}
|
|
|
|
func (t *TorrentManager) setToBinImmediately(torrentId string) {
|
|
t.log.Debugf("Set to delete immediately: %s", torrentId)
|
|
t.ImmediateBin.Add(torrentId)
|
|
t.persistBins()
|
|
}
|
|
|
|
func (t *TorrentManager) setToBinOnceDone(torrentId string) {
|
|
t.log.Debugf("Set to delete once completed: %s", torrentId)
|
|
t.OnceDoneBin.Add(torrentId)
|
|
t.persistBins()
|
|
}
|
|
|
|
func (t *TorrentManager) binImmediately(torrentId string) bool {
|
|
if t.ImmediateBin.Contains(torrentId) {
|
|
if err := t.api.DeleteTorrent(torrentId); err != nil {
|
|
t.log.Errorf("Failed to delete torrent %s: %v", torrentId, err)
|
|
return false
|
|
}
|
|
t.ImmediateBin.Remove(torrentId)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (t *TorrentManager) binOnceDoneErrorCheck(torrentId, status string) bool {
|
|
okStatuses := mapset.NewSet("downloading", "downloaded", "uploading", "queued", "compressing")
|
|
if !okStatuses.Contains(status) {
|
|
return t.binOnceDone(torrentId)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (t *TorrentManager) binOnceDone(torrentId string) bool {
|
|
found := false
|
|
binnedIDs := t.OnceDoneBin.ToSlice()
|
|
// special case: xxx-yyy means if xxx is done, delete yyy
|
|
specialCase := fmt.Sprintf("%s-", torrentId)
|
|
for _, entry := range binnedIDs {
|
|
if strings.Contains(entry, specialCase) {
|
|
idToDelete := strings.Split(entry, "-")[1]
|
|
if err := t.api.DeleteTorrent(idToDelete); err != nil {
|
|
t.log.Errorf("Failed to delete torrent %s: %v", idToDelete, err)
|
|
continue
|
|
}
|
|
t.OnceDoneBin.Remove(entry)
|
|
found = true
|
|
} else if entry == torrentId {
|
|
if err := t.api.DeleteTorrent(torrentId); err != nil {
|
|
t.log.Errorf("Failed to delete torrent %s: %v", torrentId, err)
|
|
return false
|
|
}
|
|
t.OnceDoneBin.Remove(torrentId)
|
|
return true
|
|
}
|
|
}
|
|
|
|
return found
|
|
}
|