Add caching

This commit is contained in:
Ben Sarmiento
2023-10-22 13:29:41 +02:00
parent c789ebc96d
commit 6eccba394c
9 changed files with 74 additions and 55 deletions

View File

@@ -12,22 +12,27 @@ import (
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/hashicorp/golang-lru/v2/expirable"
)
// HandleGetRequest handles a GET request to a file
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) {
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
requestPath := path.Clean(r.URL.Path)
if requestPath == "/favicon.ico" {
return
}
segments := strings.Split(requestPath, "/")
fmt.Println(segments, len(segments))
// If there are less than 3 segments, return an error or adjust as needed
if len(segments) < 4 {
// log.Println("Invalid url", requestPath)
// http.Error(w, "Cannot find file", http.StatusNotFound)
HandlePropfindRequest(w, r, t, c)
HandlePropfindRequest(w, r, t, c, cache)
return
}
if data, exists := cache.Get(requestPath); exists {
http.Redirect(w, r, data, http.StatusFound)
return
}
// Get the last two segments
baseDirectory := segments[len(segments)-3]
torrentName := segments[len(segments)-2]
@@ -77,6 +82,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
// If the file extension changed, that means it's a different file
log.Println("Filename mismatch", resp.Filename, filenameV2)
}
cache.Add(requestPath, resp.Download)
http.Redirect(w, r, resp.Download, http.StatusFound)
}