Refactors
This commit is contained in:
@@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
buf.WriteString(dav.Directory(addSlash(config.DOWNLOADS), ""))
|
||||
|
||||
@@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
|
||||
@@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
prefixPath := addSlash(config.DOWNLOADS)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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("<ol>")
|
||||
filenames := torMgr.DownloadMap.Keys()
|
||||
|
||||
Reference in New Issue
Block a user