Fix http mount

This commit is contained in:
Ben Sarmiento
2024-01-08 23:39:34 +01:00
parent 8a76cb0267
commit 14cb593b81
2 changed files with 55 additions and 11 deletions

View File

@@ -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) {

View File

@@ -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":