Resolve infuse routing issues

This commit is contained in:
Ben Sarmiento
2024-01-20 14:51:16 +01:00
parent 14f6169601
commit a6b02ba74e
5 changed files with 57 additions and 38 deletions

View File

@@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"net/http"
"net/url"
"path/filepath"
"strings"
@@ -102,6 +103,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
out, err := handleFunc(hs.torMgr)
if err != nil && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
@@ -133,9 +135,10 @@ func (hs *Handlers) handleInfuseRoot(resp http.ResponseWriter, req *http.Request
// handle torrent list request
func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory := chi.URLParam(req, "directory")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
out, err := handleFunc(directory, hs.torMgr)
if err != nil && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
@@ -153,7 +156,7 @@ func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http
}
func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
handlerFunc := intHttp.ServeTorrentsList
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
@@ -164,7 +167,7 @@ func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.R
}
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
handlerFunc := dav.ServeTorrentsList
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
@@ -175,7 +178,7 @@ func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Re
}
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
handlerFunc := dav.ServeTorrentsListForInfuse
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
@@ -188,10 +191,11 @@ func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http
// handle files list request
func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
out, err := handleFunc(directory, torrentName, hs.torMgr)
if err != nil && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
@@ -246,9 +250,9 @@ func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *htt
// handle delete request
func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "filename")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
@@ -257,8 +261,8 @@ func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Reques
}
func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
@@ -269,9 +273,9 @@ func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Req
// other handlers
func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "filename")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
if err != nil {
http.NotFound(resp, req)
@@ -287,9 +291,9 @@ func (hs *Handlers) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Requ
}
func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "filename")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil {
@@ -300,8 +304,8 @@ func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request)
}
func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil {
@@ -314,14 +318,14 @@ func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Reque
// universal handlers
func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "filename")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
hs.downloader.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
}
func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) {
filename := chi.URLParam(req, "filename")
filename, _ := url.PathUnescape(chi.URLParam(req, "filename"))
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
hs.downloader.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log)
} else {
@@ -330,14 +334,14 @@ 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")
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(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")
filename, _ := url.PathUnescape(chi.URLParam(req, "filename"))
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
universal.CheckDownloadLink(download, resp, req, hs.torMgr, hs.log)
} else {