diff --git a/internal/dav/dav.go b/internal/dav/dav.go index c914941..8031534 100644 --- a/internal/dav/dav.go +++ b/internal/dav/dav.go @@ -101,7 +101,7 @@ func ServeTorrentFilesForDav(directory, torrentName string, torMgr *torrent.Torr return buf.Bytes(), nil } -func ServeDownloadsForDav(torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeDownloadsForDav(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { var buf bytes.Buffer buf.WriteString("") buf.WriteString(dav.Directory(addSlash(config.DOWNLOADS), "")) diff --git a/internal/dav/infuse.go b/internal/dav/infuse.go index c836d24..5c94677 100644 --- a/internal/dav/infuse.go +++ b/internal/dav/infuse.go @@ -110,7 +110,7 @@ func ServeTorrentFilesForInfuse(directory, torrentName string, torMgr *torrent.T return buf.Bytes(), nil } -func ServeDownloadsForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeDownloadsForInfuse(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { var buf bytes.Buffer buf.WriteString("") diff --git a/internal/dav/vidhub.go b/internal/dav/vidhub.go index 1d09f2f..c5e482c 100644 --- a/internal/dav/vidhub.go +++ b/internal/dav/vidhub.go @@ -104,7 +104,7 @@ func ServeTorrentFilesForVidHub(directory, torrentName string, torMgr *torrent.T return buf.Bytes(), nil } -func ServeDownloadsForVidHub(torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeDownloadsForVidHub(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { var buf bytes.Buffer buf.WriteString("") prefixPath := addSlash(config.DOWNLOADS) diff --git a/internal/handlers/home.go b/internal/handlers/home.go index 7dae642..9fd4272 100644 --- a/internal/handlers/home.go +++ b/internal/handlers/home.go @@ -47,10 +47,9 @@ type RootResponse struct { TrafficServedPerAPI map[string]uint64 `json:"traffic_served_per_api"` } -func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request) (*RootResponse, error) { +func (zr *Handlers) generateResponse(req *http.Request) (*RootResponse, error) { userInfo, err := zr.rd.GetUserInformation() if err != nil { - http.Error(resp, err.Error(), http.StatusInternalServerError) return nil, err } @@ -130,8 +129,9 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request } func (zr *Handlers) handleHomeJson(resp http.ResponseWriter, req *http.Request) { - response, err := zr.generateResponse(resp, req) + response, err := zr.generateResponse(req) if err != nil { + http.Error(resp, err.Error(), http.StatusInternalServerError) return } @@ -148,7 +148,7 @@ func (zr *Handlers) handleHomeJson(resp http.ResponseWriter, req *http.Request) } func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { - response, err := zr.generateResponse(resp, req) + response, err := zr.generateResponse(req) if err != nil { return } diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 467045f..38f8cde 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -102,17 +102,12 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t router.Get("/vidhub/{directory}/", hs.handleVidHubTorrentsList) router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList) router.MethodFunc("PROPFIND", "/vidhub/", hs.handleVidHubRoot) - // router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s/", config.DOWNLOADS), hs.handleVidHubDownloadsList) - // router.MethodFunc("PROPFIND", "/vidhub/{directory}/", hs.handleVidHubTorrentsList) - // router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList) - // router.MethodFunc("PROPFIND", "/vidhub", hs.handleVidHubRoot) router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s", config.DOWNLOADS), hs.handleVidHubDownloadsList) router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubTorrentsList) router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubFilesList) - router.MethodFunc("PROPFIND", "/{mountType}/{directory}/{torrent}/{filename}", hs.checkSingleFileHandler) - // note: reused handlers for dav and infuse + router.MethodFunc("PROPFIND", "/{mountType}/{directory}/{torrent}/{filename}", hs.checkSingleFileHandler) router.Delete("/{mountType}/{directory}/{torrent}/", hs.deleteTorrentHandler) router.Delete("/{mountType}/{directory}/{torrent}/{filename}", hs.deleteFileHandler) router.MethodFunc("MKCOL", "/{mountType}/{directory}/{torrent}/", hs.mkcolTorrentHandler) @@ -196,32 +191,19 @@ func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http } func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) { - directory, err := url.PathUnescape(chi.URLParam(req, "directory")) - if err != nil { - directory = chi.URLParam(req, "directory") - } - handlerFunc := intHttp.ServeTorrentsList - if directory == config.DOWNLOADS { - handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { - return intHttp.ServeDownloadsList(torMgr) - } - } - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/html; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, intHttp.ServeGroupDirectory, "text/html; charset=\"utf-8\"") } func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) { - handlerFunc := dav.ServeGroupDirectoryForDav - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForDav, "text/xml; charset=\"utf-8\"") } func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) { - handlerFunc := dav.ServeGroupDirectoryForInfuse - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForInfuse, "text/xml; charset=\"utf-8\"") } func (hs *Handlers) handleVidHubTorrentsList(resp http.ResponseWriter, req *http.Request) { - handlerFunc := dav.ServeGroupDirectoryForVidHub - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForVidHub, "text/xml; charset=\"utf-8\"") } // handle files list request @@ -273,31 +255,19 @@ func (hs *Handlers) handleVidHubFilesList(resp http.ResponseWriter, req *http.Re // handle downloads list request func (hs *Handlers) handleHttpDownloadsList(resp http.ResponseWriter, req *http.Request) { - handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { - return intHttp.ServeDownloadsList(torMgr) - } - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/html; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, intHttp.ServeDownloadsList, "text/html; charset=\"utf-8\"") } func (hs *Handlers) handleDavDownloadsList(resp http.ResponseWriter, req *http.Request) { - handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { - return dav.ServeDownloadsForDav(torMgr) - } - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForDav, "text/xml; charset=\"utf-8\"") } func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *http.Request) { - handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { - return dav.ServeDownloadsForInfuse(torMgr) - } - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForInfuse, "text/xml; charset=\"utf-8\"") } func (hs *Handlers) handleVidHubDownloadsList(resp http.ResponseWriter, req *http.Request) { - handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { - return dav.ServeDownloadsForVidHub(torMgr) - } - hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") + hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForVidHub, "text/xml; charset=\"utf-8\"") } // handle delete request diff --git a/internal/http/listing.go b/internal/http/listing.go index b403f8d..4f35bf3 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -31,7 +31,7 @@ func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) { return buf.Bytes(), nil } -func ServeTorrentsList(directory string, torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeGroupDirectory(directory string, torMgr *torrent.TorrentManager) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) @@ -94,7 +94,7 @@ func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManage return buf.Bytes(), nil } -func ServeDownloadsList(torMgr *torrent.TorrentManager) ([]byte, error) { +func ServeDownloadsList(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { var buf bytes.Buffer buf.WriteString("
    ") filenames := torMgr.DownloadMap.Keys()