A lot of fixes: filenames, dupes in directories, dupes in torrents
This commit is contained in:
@@ -24,7 +24,7 @@ type TorrentManager struct {
|
||||
cache *expirable.LRU[string, string]
|
||||
workerPool chan bool
|
||||
TorrentDirectoriesMap map[string][]string
|
||||
processedTorrents map[string]bool
|
||||
processedTorrents map[string][]string
|
||||
}
|
||||
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
@@ -32,19 +32,18 @@ type TorrentManager struct {
|
||||
// and store them in-memory
|
||||
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
|
||||
t := &TorrentManager{
|
||||
requiredVersion: "26.10.2023",
|
||||
requiredVersion: "28.10.2023",
|
||||
config: config,
|
||||
cache: cache,
|
||||
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
||||
TorrentDirectoriesMap: make(map[string][]string),
|
||||
processedTorrents: make(map[string]bool),
|
||||
processedTorrents: make(map[string][]string),
|
||||
}
|
||||
|
||||
// Initialize torrents for the first time
|
||||
t.torrents = t.getFreshListFromAPI()
|
||||
t.checksum = t.getChecksum()
|
||||
// log.Println("First checksum", t.checksum)
|
||||
go t.mapToDirectories()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
@@ -62,7 +61,8 @@ func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[strin
|
||||
go t.repairAll(&wg)
|
||||
}
|
||||
|
||||
// Start the periodic refresh
|
||||
wg.Wait()
|
||||
t.mapToDirectories()
|
||||
go t.startRefreshJob()
|
||||
|
||||
return t
|
||||
@@ -242,6 +242,7 @@ func (t *TorrentManager) addMoreInfo(torrent *Torrent) {
|
||||
// see if api data and file data still match
|
||||
// then it means data is still usable
|
||||
if len(torrentFromFile.Links) == len(torrent.Links) {
|
||||
torrent.Name = getName(torrentFromFile)
|
||||
torrent.ForRepair = torrentFromFile.ForRepair
|
||||
torrent.SelectedFiles = torrentFromFile.SelectedFiles[:]
|
||||
return
|
||||
@@ -305,6 +306,8 @@ func (t *TorrentManager) addMoreInfo(torrent *Torrent) {
|
||||
}
|
||||
}
|
||||
// update file cache
|
||||
torrent.OriginalName = info.OriginalName
|
||||
torrent.Name = getName(torrent)
|
||||
if len(selectedFiles) > 0 {
|
||||
// update the torrent with more data!
|
||||
torrent.SelectedFiles = selectedFiles
|
||||
@@ -313,6 +316,18 @@ func (t *TorrentManager) addMoreInfo(torrent *Torrent) {
|
||||
}
|
||||
}
|
||||
|
||||
func getName(torrent *Torrent) string {
|
||||
ret := ""
|
||||
if torrent.OriginalName != "" {
|
||||
ret = torrent.OriginalName
|
||||
} else {
|
||||
ret = torrent.Name
|
||||
}
|
||||
ret = strings.TrimSuffix(ret, ".mkv")
|
||||
ret = strings.TrimSuffix(ret, ".mp4")
|
||||
return ret
|
||||
}
|
||||
|
||||
// mapToDirectories maps torrents to directories
|
||||
func (t *TorrentManager) mapToDirectories() {
|
||||
// Map torrents to directories
|
||||
@@ -327,7 +342,13 @@ func (t *TorrentManager) mapToDirectories() {
|
||||
counter := make(map[string]int)
|
||||
for i := range t.torrents {
|
||||
// don't process torrents that are already mapped if it is not the first run
|
||||
if _, exists := t.processedTorrents[t.torrents[i].ID]; exists {
|
||||
alreadyMappedToGroup := false
|
||||
for _, mappedGroup := range t.processedTorrents[t.torrents[i].Name] {
|
||||
if mappedGroup == group {
|
||||
alreadyMappedToGroup = true
|
||||
}
|
||||
}
|
||||
if alreadyMappedToGroup {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -338,19 +359,21 @@ func (t *TorrentManager) mapToDirectories() {
|
||||
}
|
||||
if configV1.MeetsConditions(directory, t.torrents[i].ID, t.torrents[i].Name, filenames) {
|
||||
found := false
|
||||
// check if it is already mapped to this directory
|
||||
for _, dir := range t.TorrentDirectoriesMap[t.torrents[i].Name] {
|
||||
if dir == directory {
|
||||
found = true
|
||||
break
|
||||
break // it is already mapped to this directory
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
counter[directory]++
|
||||
t.TorrentDirectoriesMap[t.torrents[i].Name] = append(t.TorrentDirectoriesMap[t.torrents[i].Name], directory)
|
||||
break
|
||||
break // we found a directory for this torrent, so we can stop looking for more
|
||||
}
|
||||
}
|
||||
}
|
||||
t.processedTorrents[t.torrents[i].Name] = append(t.processedTorrents[t.torrents[i].Name], group)
|
||||
}
|
||||
sum := 0
|
||||
for _, count := range counter {
|
||||
@@ -365,9 +388,6 @@ func (t *TorrentManager) mapToDirectories() {
|
||||
default:
|
||||
log.Println("Unknown config version")
|
||||
}
|
||||
for _, torrent := range t.torrents {
|
||||
t.processedTorrents[torrent.ID] = true
|
||||
}
|
||||
log.Println("Finished mapping to directories")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user