Big commit
This commit is contained in:
@@ -16,12 +16,13 @@ import (
|
||||
)
|
||||
|
||||
type TorrentManager struct {
|
||||
torrents []Torrent
|
||||
inProgress []string
|
||||
checksum string
|
||||
config config.ConfigInterface
|
||||
cache *expirable.LRU[string, string]
|
||||
workerPool chan bool
|
||||
requiredVersion string
|
||||
torrents []Torrent
|
||||
inProgress []string
|
||||
checksum string
|
||||
config config.ConfigInterface
|
||||
cache *expirable.LRU[string, string]
|
||||
workerPool chan bool
|
||||
}
|
||||
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
@@ -29,9 +30,10 @@ type TorrentManager struct {
|
||||
// and store them in-memory
|
||||
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
|
||||
t := &TorrentManager{
|
||||
config: config,
|
||||
cache: cache,
|
||||
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
||||
requiredVersion: "v2",
|
||||
config: config,
|
||||
cache: cache,
|
||||
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
||||
}
|
||||
|
||||
// Initialize torrents for the first time
|
||||
@@ -67,12 +69,13 @@ func (t *TorrentManager) repairAll(wg *sync.WaitGroup) {
|
||||
for _, torrent := range t.torrents {
|
||||
if torrent.ForRepair {
|
||||
log.Println("Issues detected on", torrent.Name, "; fixing...")
|
||||
t.repair(torrent.ID, torrent.SelectedFiles)
|
||||
t.repair(torrent.ID, torrent.SelectedFiles, true)
|
||||
}
|
||||
if len(torrent.Links) == 0 {
|
||||
// If the torrent has no links
|
||||
// and already processing repair
|
||||
// delete it!
|
||||
log.Println("Deleting", torrent.Name, "as it has no links")
|
||||
realdebrid.DeleteTorrent(t.config.GetToken(), torrent.ID)
|
||||
}
|
||||
}
|
||||
@@ -91,13 +94,11 @@ func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
|
||||
return torrents
|
||||
}
|
||||
|
||||
// MarkFileAsDeleted marks a file as deleted
|
||||
func (t *TorrentManager) MarkFileAsDeleted(torrent *Torrent, file *File) {
|
||||
log.Println("Marking file as deleted", file.Path)
|
||||
// HideTheFile marks a file as deleted
|
||||
func (t *TorrentManager) HideTheFile(torrent *Torrent, file *File) {
|
||||
file.Link = ""
|
||||
t.writeToFile(torrent)
|
||||
log.Println("Healing a single file in the torrent", torrent.Name)
|
||||
t.repair(torrent.ID, []File{*file})
|
||||
t.repair(torrent.ID, []File{*file}, false)
|
||||
}
|
||||
|
||||
// FindAllTorrentsWithName finds all torrents in a given directory with a given name
|
||||
@@ -252,11 +253,16 @@ func (t *TorrentManager) addMoreInfo(torrent *Torrent) {
|
||||
// file cache
|
||||
torrentFromFile := t.readFromFile(torrent.ID)
|
||||
if torrentFromFile != nil {
|
||||
fmt.Println("from file!")
|
||||
// see if api data and file data still match
|
||||
// then it means data is still usable
|
||||
if len(torrentFromFile.Links) == len(torrent.Links) {
|
||||
torrent.ForRepair = torrentFromFile.ForRepair
|
||||
torrent.SelectedFiles = torrentFromFile.SelectedFiles
|
||||
torrent.SelectedFiles = torrentFromFile.SelectedFiles[:]
|
||||
if torrentFromFile.ID == "ABUNEKZP3UPMU" || torrentFromFile.ID == "TAA5WUJ6BVEAE" {
|
||||
fmt.Println(">>>>>>>>>>>>>>>>", torrentFromFile.ID, len(torrentFromFile.Links), len(torrent.Links), len(torrent.SelectedFiles[0].Link))
|
||||
torrent.Version = "v2"
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -306,11 +312,13 @@ func (t *TorrentManager) addMoreInfo(torrent *Torrent) {
|
||||
selectedFiles[i].Link = link
|
||||
}
|
||||
}
|
||||
// update the torrent with more data!
|
||||
torrent.SelectedFiles = selectedFiles
|
||||
torrent.ForRepair = forRepair
|
||||
// update file cache
|
||||
t.writeToFile(torrent)
|
||||
if len(selectedFiles) > 0 {
|
||||
// update the torrent with more data!
|
||||
torrent.SelectedFiles = selectedFiles
|
||||
torrent.ForRepair = forRepair
|
||||
t.writeToFile(torrent)
|
||||
}
|
||||
}
|
||||
|
||||
// getByID returns a torrent by its ID
|
||||
@@ -333,6 +341,7 @@ func (t *TorrentManager) writeToFile(torrent *Torrent) {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
torrent.Version = t.requiredVersion
|
||||
dataEncoder := gob.NewEncoder(file)
|
||||
dataEncoder.Encode(torrent)
|
||||
}
|
||||
@@ -362,6 +371,9 @@ func (t *TorrentManager) readFromFile(torrentID string) *Torrent {
|
||||
log.Fatalf("Failed decoding file: %s", err)
|
||||
return nil
|
||||
}
|
||||
if torrent.Version != t.requiredVersion {
|
||||
return nil
|
||||
}
|
||||
return &torrent
|
||||
}
|
||||
|
||||
@@ -489,7 +501,7 @@ func (t *TorrentManager) organizeChaos(info *realdebrid.Torrent, selectedFiles [
|
||||
return selectedFiles, isChaotic
|
||||
}
|
||||
|
||||
func (t *TorrentManager) repair(torrentID string, selectedFiles []File) {
|
||||
func (t *TorrentManager) repair(torrentID string, selectedFiles []File, tryReinsertionFirst bool) {
|
||||
torrent := t.getByID(torrentID)
|
||||
if torrent == nil {
|
||||
return
|
||||
@@ -540,7 +552,10 @@ func (t *TorrentManager) repair(torrentID string, selectedFiles []File) {
|
||||
}
|
||||
|
||||
// first solution: add the same selection, maybe it can be fixed by reinsertion?
|
||||
success := t.reinsertTorrent(torrent, "", true)
|
||||
success := false
|
||||
if tryReinsertionFirst {
|
||||
success = t.reinsertTorrent(torrent, "", true)
|
||||
}
|
||||
if !success {
|
||||
// if not, last resort: add only the missing files and do it in 2 batches
|
||||
half := len(missingFiles) / 2
|
||||
|
||||
Reference in New Issue
Block a user