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

@@ -11,23 +11,22 @@ import (
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/dav"
"github.com/hashicorp/golang-lru/v2/expirable"
)
// HandlePropfindRequest handles a PROPFIND request
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) {
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
requestPath := path.Clean(r.URL.Path)
if data, exists := cache.Get(requestPath); exists {
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
w.WriteHeader(http.StatusMultiStatus)
fmt.Fprint(w, data)
return
}
var output []byte
var err error
requestPath := path.Clean(r.URL.Path)
pathSegments := strings.Split(requestPath, "/")
// Remove empty segments caused by leading or trailing slashes
filteredSegments := pathSegments[:0]
for _, segment := range pathSegments {
if segment != "" {
filteredSegments = append(filteredSegments, segment)
}
}
filteredSegments := strings.Split(strings.Trim(requestPath, "/"), "/")
switch len(filteredSegments) {
case 0:
@@ -37,20 +36,23 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
case 2:
output, err = handleSingleTorrent(requestPath, w, r, t)
default:
http.Error(w, "Not Found", http.StatusNotFound)
writeHTTPError(w, "Not Found", http.StatusNotFound)
return
}
if err != nil {
log.Printf("Error processing request: %v\n", err)
http.Error(w, "Server error", http.StatusInternalServerError)
writeHTTPError(w, "Server error", http.StatusInternalServerError)
return
}
if output != nil {
respBody := fmt.Sprintf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n%s\n", output)
cache.Add(requestPath, respBody)
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
w.WriteHeader(http.StatusMultiStatus)
fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n%s\n", output)
fmt.Fprint(w, respBody)
}
}
@@ -64,47 +66,43 @@ func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface
XMLNS: "DAV:",
Response: responses,
}
return xml.MarshalIndent(rootResponse, "", " ")
return xml.Marshal(rootResponse)
}
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) ([]byte, error) {
basePath := path.Base(requestPath)
directories := c.GetDirectories()
for _, directory := range directories {
for _, directory := range c.GetDirectories() {
if basePath == directory {
torrents := t.GetByDirectory(basePath)
resp, err := createMultiTorrentResponse("/"+basePath, torrents)
if err != nil {
log.Printf("Cannot read directory (%s): %v\n", basePath, err)
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
return nil, nil
return nil, fmt.Errorf("cannot read directory (%s): %w", basePath, err)
}
return xml.MarshalIndent(resp, "", " ")
return xml.Marshal(resp)
}
}
log.Println("Cannot find directory when generating list", requestPath)
http.Error(w, "Cannot find directory", http.StatusNotFound)
return nil, nil
return nil, fmt.Errorf("cannot find directory when generating list: %s", requestPath)
}
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
directory := strings.TrimPrefix(path.Dir(requestPath), "/")
directory := path.Dir(requestPath)
torrentName := path.Base(requestPath)
sameNameTorrents := findAllTorrentsWithName(t, directory, torrentName)
if len(sameNameTorrents) == 0 {
log.Println("Cannot find directory when generating single torrent", requestPath)
http.Error(w, "Cannot find directory", http.StatusNotFound)
return nil, nil
return nil, fmt.Errorf("cannot find directory when generating single torrent: %s", requestPath)
}
resp, err := createSingleTorrentResponse("/"+directory, sameNameTorrents, t)
if err != nil {
log.Printf("Cannot read directory (%s): %v\n", requestPath, err)
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
return nil, nil
return nil, fmt.Errorf("cannot read directory (%s): %w", requestPath, err)
}
return xml.MarshalIndent(resp, "", " ")
return xml.Marshal(resp)
}
func writeHTTPError(w http.ResponseWriter, errorMessage string, statusCode int) {
log.Println(errorMessage)
http.Error(w, errorMessage, statusCode)
}