Refactor with ordered maps

This commit is contained in:
Ben Sarmiento
2023-11-10 19:03:07 +01:00
parent 15a0ba95d8
commit b97f859a32
12 changed files with 180 additions and 256 deletions

View File

@@ -47,17 +47,17 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
}
baseDirectory := segments[len(segments)-3]
torrentName := segments[len(segments)-2]
accessKey := segments[len(segments)-2]
filename := segments[len(segments)-1]
torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName)
if torrents == nil {
log.Errorf("Cannot find torrent %s in the directory %s", requestPath, baseDirectory)
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
log.Errorf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
http.Error(w, "File not found", http.StatusNotFound)
return
}
_, file := getFile(torrents, filename)
file, _ := torrent.SelectedFiles.Get(filename)
if file == nil {
log.Errorf("Cannot find file from path %s", requestPath)
http.Error(w, "File not found", http.StatusNotFound)
@@ -73,10 +73,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
resp := t.UnrestrictUntilOk(link)
if resp == nil {
if !file.Unavailable {
log.Errorf("Cannot unrestrict file %s %s", filename, link)
// t.HideTheFile(torrent, file)
}
// TODO: maybe repair the torrent?
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log)
return
} else if resp.Filename != filename {

View File

@@ -41,17 +41,17 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
}
baseDirectory := segments[len(segments)-3]
torrentName := segments[len(segments)-2]
accessKey := segments[len(segments)-2]
filename := segments[len(segments)-1]
torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName)
if torrents == nil {
log.Errorf("Cannot find torrent %s in the directory %s", requestPath, baseDirectory)
http.Error(w, "Cannot find file", http.StatusNotFound)
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
log.Errorf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
http.Error(w, "File not found", http.StatusNotFound)
return
}
_, file := getFile(torrents, filename)
file, _ := torrent.SelectedFiles.Get(filename)
if file == nil {
log.Errorf("Cannot find file from path %s", requestPath)
http.Error(w, "Cannot find file", http.StatusNotFound)

View File

@@ -1,20 +1 @@
package universal
import (
"path/filepath"
"github.com/debridmediamanager.com/zurg/internal/torrent"
)
// getFile finds a link by a fragment, it might be wrong
func getFile(torrents []torrent.Torrent, filename string) (*torrent.Torrent, *torrent.File) {
for t := range torrents {
for f, file := range torrents[t].SelectedFiles {
fname := filepath.Base(file.Path)
if filename == fname {
return &torrents[t], &torrents[t].SelectedFiles[f]
}
}
}
return nil, nil
}