Optimizations

This commit is contained in:
Ben Sarmiento
2023-11-28 22:57:25 +00:00
parent e7e5806b20
commit e797824ab0
8 changed files with 139 additions and 101 deletions

View File

@@ -10,10 +10,11 @@ import (
"strings"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/dgraph-io/ristretto"
"go.uber.org/zap"
)
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, cache *ristretto.Cache, log *zap.SugaredLogger) {
requestPath := path.Clean(r.URL.Path)
var output *string
@@ -66,29 +67,14 @@ func handleRoot(t *torrent.TorrentManager) (*string, error) {
func handleListOfTorrents(requestPath string, t *torrent.TorrentManager) (*string, error) {
basePath := path.Base(requestPath)
torrents, ok := t.DirectoryMap.Get(basePath)
_, ok := t.DirectoryMap.Get(basePath)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", basePath)
}
htmlDoc := "<ol>"
resp, _ := t.ResponseCache.Get(basePath + ".html")
htmlDoc := resp.(string)
var allTorrents []torrent.Torrent
torrents.IterCb(func(key string, tor *torrent.Torrent) {
if tor.AllInProgress() {
return
}
copy := *tor
copy.AccessKey = key
allTorrents = append(allTorrents, copy)
})
sort.Slice(allTorrents, func(i, j int) bool {
return allTorrents[i].AccessKey < allTorrents[j].AccessKey
})
for _, tor := range allTorrents {
htmlDoc = htmlDoc + fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", filepath.Join(requestPath, url.PathEscape(tor.AccessKey)), tor.AccessKey)
}
return &htmlDoc, nil
}