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/pkg/dav"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
)
// 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)
responses = append(responses, dav.Directory(currentPath))
nameAndLink := make(map[string]bool)
finalName := make(map[string]bool)
var torrentResponses []dav.Response
@@ -56,16 +54,10 @@ func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (*
}
filename := filepath.Base(file.Path)
key := filename + file.Link
if nameAndLink[key] {
continue
}
nameAndLink[key] = true
if finalName[filename] {
fragment := davextra.GetLinkFragment(file.Link)
filename = davextra.InsertLinkFragment(filename, fragment)
// fragment := davextra.GetLinkFragment(file.Link)
// filename = davextra.InsertLinkFragment(filename, fragment)
continue
}
finalName[filename] = true

View File

@@ -6,7 +6,6 @@ import (
"path/filepath"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
)
// 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) {
htmlDoc := "<ul>"
nameAndLink := make(map[string]bool)
finalName := make(map[string]bool)
currentPath := filepath.Join(basePath)
@@ -47,16 +45,10 @@ func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (s
}
filename := filepath.Base(file.Path)
key := filename + file.Link
if nameAndLink[key] {
continue
}
nameAndLink[key] = true
if finalName[filename] {
fragment := davextra.GetLinkFragment(file.Link)
filename = davextra.InsertLinkFragment(filename, fragment)
// fragment := davextra.GetLinkFragment(file.Link)
// filename = davextra.InsertLinkFragment(filename, fragment)
continue
}
finalName[filename] = true

View File

@@ -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")
}

View File

@@ -19,14 +19,15 @@ type UnrestrictResponse struct {
}
type Torrent struct {
ID string `json:"id"`
Name string `json:"filename"`
Hash string `json:"hash"`
Progress int `json:"-"`
Added string `json:"added"`
Bytes int64 `json:"bytes"`
Links []string `json:"links"`
Files []File `json:"files,omitempty"`
ID string `json:"id"`
Name string `json:"filename"`
OriginalName string `json:"original_filename"`
Hash string `json:"hash"`
Progress int `json:"-"`
Added string `json:"added"`
Bytes int64 `json:"bytes"`
Links []string `json:"links"`
Files []File `json:"files,omitempty"`
}
func (t *Torrent) UnmarshalJSON(data []byte) error {