Add proper logging

This commit is contained in:
Ben Sarmiento
2023-11-05 01:23:41 +01:00
parent 1b116c2194
commit a9c71a3e93
11 changed files with 103 additions and 96 deletions

View File

@@ -2,7 +2,6 @@ package universal
import (
"io"
"log"
"net/http"
"path"
"path/filepath"
@@ -13,12 +12,17 @@ import (
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/hashicorp/golang-lru/v2/expirable"
"go.uber.org/zap"
)
// HandleGetRequest handles a GET request universally for both WebDAV and HTTP
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
rlog := logutil.NewLogger()
log := rlog.Named("uniget")
requestPath := path.Clean(r.URL.Path)
isDav := true
if strings.Contains(requestPath, "/http") {
@@ -39,7 +43,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
return
}
if data, exists := cache.Get(requestPath); exists {
streamFileToResponse(data, w, r, c.GetNetworkBufferSize())
streamFileToResponse(data, w, r, c.GetNetworkBufferSize(), log)
return
}
@@ -49,7 +53,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName)
if torrents == nil {
log.Println("Cannot find torrent", requestPath)
log.Errorf("Cannot find torrent %s in the directory %s", requestPath, baseDirectory)
http.Redirect(w, r, "https://send.nukes.wtf/tDeTd0", http.StatusFound)
return
}
@@ -57,13 +61,13 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
torrent, file := getFile(torrents, filenameV2, linkFragment)
if file == nil {
log.Println("Cannot find file (get)", requestPath)
log.Errorf("Cannot find file from path %s", requestPath)
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}
if file.Link == "" {
log.Println("Link not found (get)", filename)
http.Error(w, "Cannot find file", http.StatusNotFound)
// This is a dead file, serve an alternate file
log.Errorf("File %s is no longer available", filename)
return
}
link := file.Link
@@ -74,7 +78,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
resp := realdebrid.RetryUntilOk(unrestrictFn)
if resp == nil {
if !file.Unavailable {
log.Println("Cannot unrestrict link", link, filenameV2)
log.Errorf("Cannot unrestrict file %s %s", filenameV2, link)
t.HideTheFile(torrent, file)
}
http.Redirect(w, r, "https://send.nukes.wtf/tDeTd0", http.StatusFound)
@@ -83,19 +87,19 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
actualExt := filepath.Ext(resp.Filename)
expectedExt := filepath.Ext(filenameV2)
if actualExt != expectedExt {
log.Println("File extension mismatch", resp.Filename, filenameV2)
log.Errorf("File extension mismatch: %s and %s", filenameV2, resp.Filename)
} else {
log.Println("Filename mismatch", resp.Filename, filenameV2)
log.Errorf("Filename mismatch: %s and %s", filenameV2, resp.Filename)
}
}
cache.Add(requestPath, resp.Download)
streamFileToResponse(resp.Download, w, r, c.GetNetworkBufferSize())
streamFileToResponse(resp.Download, w, r, c.GetNetworkBufferSize(), log)
}
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, bufferSize int) {
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, bufferSize int, log *zap.SugaredLogger) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Println("Error creating new request:", err)
log.Errorf("Error creating new request %v", err)
http.Redirect(w, r, "https://send.nukes.wtf/ARjVWb", http.StatusFound)
return
}
@@ -108,14 +112,14 @@ func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, bu
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("Error downloading file:", err)
log.Errorf("Error downloading file %v", err)
http.Redirect(w, r, "https://send.nukes.wtf/TB2u2n", http.StatusFound)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
log.Printf("Received a non-OK status code: %d", resp.StatusCode)
log.Errorf("Received a nonOK status code %d", resp.StatusCode)
http.Redirect(w, r, "https://send.nukes.wtf/b5AiON", http.StatusFound)
return
}