From 14cb593b81823f9c2867286be023649c766187e4 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 8 Jan 2024 23:39:34 +0100 Subject: [PATCH] Fix http mount --- internal/handlers/router.go | 36 ++++++++++++++++++------ internal/universal/{head.go => check.go} | 30 ++++++++++++++++++-- 2 files changed, 55 insertions(+), 11 deletions(-) rename internal/universal/{head.go => check.go} (57%) diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 6089229..0d34c5c 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -42,10 +42,15 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t router.Use(optionsMiddleware) router.Get("/", hs.handleHome) - router.Get("/{mountType}/version.txt", hs.handleVersionFile) + // version + router.Get(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleVersionFile) + router.Head(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleCheckVersionFile) + // download router.Get(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleDownloadLink) + router.Head(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleCheckDownloadLink) + // file router.Get("/{mountType}/{directory}/{torrent}/{filename}", hs.handleDownloadFile) - router.Head("/{mountType}/{directory}/{torrent}/{filename}", hs.handleCheckCachedLink) + router.Head("/{mountType}/{directory}/{torrent}/{filename}", hs.handleCheckFile) router.Get("/http/", hs.handleHttpRoot) router.Get(fmt.Sprintf("/http/%s/", config.DOWNLOADS), hs.handleHttpDownloadsList) @@ -297,13 +302,6 @@ func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Reque hs.downloader.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log) } -func (hs *Handlers) handleCheckCachedLink(resp http.ResponseWriter, req *http.Request) { - directory := chi.URLParam(req, "directory") - torrentName := chi.URLParam(req, "torrent") - fileName := chi.URLParam(req, "filename") - universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log) -} - func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) { filename := chi.URLParam(req, "filename") if download, ok := hs.torMgr.DownloadMap.Get(filename); ok { @@ -313,6 +311,22 @@ func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Reque } } +func (hs *Handlers) handleCheckFile(resp http.ResponseWriter, req *http.Request) { + directory := chi.URLParam(req, "directory") + torrentName := chi.URLParam(req, "torrent") + fileName := chi.URLParam(req, "filename") + universal.CheckFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log) +} + +func (hs *Handlers) handleCheckDownloadLink(resp http.ResponseWriter, req *http.Request) { + filename := chi.URLParam(req, "filename") + if download, ok := hs.torMgr.DownloadMap.Get(filename); ok { + universal.CheckDownloadLink(download, resp, req, hs.torMgr, hs.log) + } else { + http.NotFound(resp, req) + } +} + // handle version file request func (hs *Handlers) handleVersionFile(resp http.ResponseWriter, req *http.Request) { @@ -322,6 +336,10 @@ func (hs *Handlers) handleVersionFile(resp http.ResponseWriter, req *http.Reques resp.Write(out) } +func (hs *Handlers) handleCheckVersionFile(resp http.ResponseWriter, req *http.Request) { + universal.CheckVersionFile(resp, req, hs.torMgr, hs.log) +} + // logs handler func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) { diff --git a/internal/universal/head.go b/internal/universal/check.go similarity index 57% rename from internal/universal/head.go rename to internal/universal/check.go index 370dffd..5580c04 100644 --- a/internal/universal/head.go +++ b/internal/universal/check.go @@ -7,11 +7,12 @@ import ( "strings" "github.com/debridmediamanager/zurg/internal/torrent" + "github.com/debridmediamanager/zurg/internal/version" "github.com/debridmediamanager/zurg/pkg/logutil" + "github.com/debridmediamanager/zurg/pkg/realdebrid" ) -func HandleHeadRequest(directory, torrentName, fileName string, w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) { - +func CheckFile(directory, torrentName, fileName string, w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { log.Warnf("Cannot find directory %s", directory) @@ -46,6 +47,31 @@ func HandleHeadRequest(directory, torrentName, fileName string, w http.ResponseW w.WriteHeader(http.StatusOK) } +func CheckDownloadLink(download *realdebrid.Download, w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) { + if !strings.HasPrefix(download.Link, "http") { + // This is a dead file, serve an alternate file + log.Warnf("File %s is no longer available", download.Filename) + http.Error(w, "Cannot find file", http.StatusNotFound) + return + } + contentType := getContentMimeType(download.Filename) + contentLength := fmt.Sprintf("%d", download.Filesize) + lastModified := download.Generated + w.Header().Set("Content-Type", contentType) + w.Header().Set("Content-Length", contentLength) + w.Header().Set("Last-Modified", lastModified) + w.WriteHeader(http.StatusOK) +} + +func CheckVersionFile(w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) { + _, size := version.GetFile() + contentType := getContentMimeType(version.FILE) + contentLength := fmt.Sprintf("%d", size) + w.Header().Set("Content-Type", contentType) + w.Header().Set("Content-Length", contentLength) + w.WriteHeader(http.StatusOK) +} + func getContentMimeType(filePath string) string { switch filepath.Ext(filePath) { case ".mkv":