Refactors

This commit is contained in:
Ben Adrian Sarmiento
2024-07-11 22:13:34 +02:00
parent d8e6b2523b
commit 4aafe5b4fc
6 changed files with 18 additions and 48 deletions

View File

@@ -101,7 +101,7 @@ func ServeTorrentFilesForDav(directory, torrentName string, torMgr *torrent.Torr
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func ServeDownloadsForDav(torMgr *torrent.TorrentManager) ([]byte, error) { func ServeDownloadsForDav(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">") buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.Directory(addSlash(config.DOWNLOADS), "")) buf.WriteString(dav.Directory(addSlash(config.DOWNLOADS), ""))

View File

@@ -110,7 +110,7 @@ func ServeTorrentFilesForInfuse(directory, torrentName string, torMgr *torrent.T
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func ServeDownloadsForInfuse(torMgr *torrent.TorrentManager) ([]byte, error) { func ServeDownloadsForInfuse(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">") buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")

View File

@@ -104,7 +104,7 @@ func ServeTorrentFilesForVidHub(directory, torrentName string, torMgr *torrent.T
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func ServeDownloadsForVidHub(torMgr *torrent.TorrentManager) ([]byte, error) { func ServeDownloadsForVidHub(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">") buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
prefixPath := addSlash(config.DOWNLOADS) prefixPath := addSlash(config.DOWNLOADS)

View File

@@ -47,10 +47,9 @@ type RootResponse struct {
TrafficServedPerAPI map[string]uint64 `json:"traffic_served_per_api"` 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() userInfo, err := zr.rd.GetUserInformation()
if err != nil { if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return nil, err 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) { func (zr *Handlers) handleHomeJson(resp http.ResponseWriter, req *http.Request) {
response, err := zr.generateResponse(resp, req) response, err := zr.generateResponse(req)
if err != nil { if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return 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) { func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response, err := zr.generateResponse(resp, req) response, err := zr.generateResponse(req)
if err != nil { if err != nil {
return return
} }

View File

@@ -102,17 +102,12 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.Get("/vidhub/{directory}/", hs.handleVidHubTorrentsList) router.Get("/vidhub/{directory}/", hs.handleVidHubTorrentsList)
router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList) router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList)
router.MethodFunc("PROPFIND", "/vidhub/", hs.handleVidHubRoot) 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", fmt.Sprintf("/vidhub/%s", config.DOWNLOADS), hs.handleVidHubDownloadsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubTorrentsList) router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubTorrentsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubFilesList) router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubFilesList)
router.MethodFunc("PROPFIND", "/{mountType}/{directory}/{torrent}/{filename}", hs.checkSingleFileHandler)
// note: reused handlers for dav and infuse // 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}/", hs.deleteTorrentHandler)
router.Delete("/{mountType}/{directory}/{torrent}/{filename}", hs.deleteFileHandler) router.Delete("/{mountType}/{directory}/{torrent}/{filename}", hs.deleteFileHandler)
router.MethodFunc("MKCOL", "/{mountType}/{directory}/{torrent}/", hs.mkcolTorrentHandler) 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) { func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory")) hs.innerTorrentsListHandler(resp, req, intHttp.ServeGroupDirectory, "text/html; charset=\"utf-8\"")
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\"")
} }
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) { func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeGroupDirectoryForDav hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForDav, "text/xml; charset=\"utf-8\"")
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
} }
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) { func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeGroupDirectoryForInfuse hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
} }
func (hs *Handlers) handleVidHubTorrentsList(resp http.ResponseWriter, req *http.Request) { func (hs *Handlers) handleVidHubTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeGroupDirectoryForVidHub hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
} }
// handle files list request // handle files list request
@@ -273,31 +255,19 @@ func (hs *Handlers) handleVidHubFilesList(resp http.ResponseWriter, req *http.Re
// handle downloads list request // handle downloads list request
func (hs *Handlers) handleHttpDownloadsList(resp http.ResponseWriter, req *http.Request) { func (hs *Handlers) handleHttpDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { hs.innerTorrentsListHandler(resp, req, intHttp.ServeDownloadsList, "text/html; charset=\"utf-8\"")
return intHttp.ServeDownloadsList(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/html; charset=\"utf-8\"")
} }
func (hs *Handlers) handleDavDownloadsList(resp http.ResponseWriter, req *http.Request) { func (hs *Handlers) handleDavDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForDav, "text/xml; charset=\"utf-8\"")
return dav.ServeDownloadsForDav(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
} }
func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *http.Request) { func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForInfuse, "text/xml; charset=\"utf-8\"")
return dav.ServeDownloadsForInfuse(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
} }
func (hs *Handlers) handleVidHubDownloadsList(resp http.ResponseWriter, req *http.Request) { func (hs *Handlers) handleVidHubDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForVidHub, "text/xml; charset=\"utf-8\"")
return dav.ServeDownloadsForVidHub(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
} }
// handle delete request // handle delete request

View File

@@ -31,7 +31,7 @@ func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) {
return buf.Bytes(), nil 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) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory) 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 return buf.Bytes(), nil
} }
func ServeDownloadsList(torMgr *torrent.TorrentManager) ([]byte, error) { func ServeDownloadsList(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
buf.WriteString("<ol>") buf.WriteString("<ol>")
filenames := torMgr.DownloadMap.Keys() filenames := torMgr.DownloadMap.Keys()