A lot of fixes: filenames, dupes in directories, dupes in torrents

This commit is contained in:
Ben Sarmiento
2023-10-28 02:49:49 +02:00
parent ecf82c0131
commit ce6729ade9
4 changed files with 46 additions and 41 deletions

View File

@@ -5,7 +5,6 @@ import (
"github.com/debridmediamanager.com/zurg/internal/torrent" "github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/dav" "github.com/debridmediamanager.com/zurg/pkg/dav"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
) )
// createMultiTorrentResponse creates a WebDAV response for a list of torrents // createMultiTorrentResponse creates a WebDAV response for a list of torrents
@@ -43,7 +42,6 @@ func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (*
currentPath := filepath.Join(basePath, torrents[0].Name) currentPath := filepath.Join(basePath, torrents[0].Name)
responses = append(responses, dav.Directory(currentPath)) responses = append(responses, dav.Directory(currentPath))
nameAndLink := make(map[string]bool)
finalName := make(map[string]bool) finalName := make(map[string]bool)
var torrentResponses []dav.Response var torrentResponses []dav.Response
@@ -56,16 +54,10 @@ func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (*
} }
filename := filepath.Base(file.Path) filename := filepath.Base(file.Path)
key := filename + file.Link
if nameAndLink[key] {
continue
}
nameAndLink[key] = true
if finalName[filename] { if finalName[filename] {
fragment := davextra.GetLinkFragment(file.Link) // fragment := davextra.GetLinkFragment(file.Link)
filename = davextra.InsertLinkFragment(filename, fragment) // filename = davextra.InsertLinkFragment(filename, fragment)
continue
} }
finalName[filename] = true finalName[filename] = true

View File

@@ -6,7 +6,6 @@ import (
"path/filepath" "path/filepath"
"github.com/debridmediamanager.com/zurg/internal/torrent" "github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
) )
// createMultiTorrentResponse creates a WebDAV response for a list of torrents // createMultiTorrentResponse creates a WebDAV response for a list of torrents
@@ -34,7 +33,6 @@ func createMultiTorrentResponse(basePath string, torrents []torrent.Torrent) (st
func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) { func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) {
htmlDoc := "<ul>" htmlDoc := "<ul>"
nameAndLink := make(map[string]bool)
finalName := make(map[string]bool) finalName := make(map[string]bool)
currentPath := filepath.Join(basePath) currentPath := filepath.Join(basePath)
@@ -47,16 +45,10 @@ func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (s
} }
filename := filepath.Base(file.Path) filename := filepath.Base(file.Path)
key := filename + file.Link
if nameAndLink[key] {
continue
}
nameAndLink[key] = true
if finalName[filename] { if finalName[filename] {
fragment := davextra.GetLinkFragment(file.Link) // fragment := davextra.GetLinkFragment(file.Link)
filename = davextra.InsertLinkFragment(filename, fragment) // filename = davextra.InsertLinkFragment(filename, fragment)
continue
} }
finalName[filename] = true finalName[filename] = true

View File

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

View File

@@ -21,6 +21,7 @@ type UnrestrictResponse struct {
type Torrent struct { type Torrent struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"filename"` Name string `json:"filename"`
OriginalName string `json:"original_filename"`
Hash string `json:"hash"` Hash string `json:"hash"`
Progress int `json:"-"` Progress int `json:"-"`
Added string `json:"added"` Added string `json:"added"`