From 47848e1feb4bb03502e3b00762780c4ea8511f6f Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Tue, 12 Dec 2023 02:50:39 +0100 Subject: [PATCH] Refactor route handlers --- internal/router/router.go | 188 ++++++++++++++++---------------------- 1 file changed, 77 insertions(+), 111 deletions(-) diff --git a/internal/router/router.go b/internal/router/router.go index 3c02173..e51750b 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -32,37 +32,39 @@ func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torM log: log, } - // http router - router.GET("/http/:directory/:torrent/:file", zr.universalDownloadFileHandler) - router.GET("/http/:directory/:torrent/", zr.httpTorrentDirectoryHandler) + // HTTP router + router.GET("/http/:directory/:torrent/", zr.httpTorrentHandler) router.GET("/http/:directory/", zr.httpDirectoryHandler) router.GET("/http/", zr.httpRootHandler) - // HEAD route - router.HEAD("/http/:directory/:torrent/:file", zr.headFileHandler) - // dav router - router.GET("/dav/:directory/:torrent/:file", zr.universalDownloadFileHandler) - router.Handle("PROPFIND", "/dav/:directory/:torrent/", zr.propfindTorrentHandler) - router.Handle("PROPFIND", "/dav/:directory/", zr.propfindDirectoryHandler) - router.Handle("PROPFIND", "/dav/", zr.propfindRootHandler) - // extras - router.GET("/dav/:directory/:torrent/", zr.propfindTorrentHandler) - router.GET("/dav/:directory/", zr.propfindDirectoryHandler) - router.GET("/dav/", zr.propfindRootHandler) + // WEBDAV router + router.Handle("PROPFIND", "/dav/:directory/:torrent/", zr.davTorrentHandler) + router.Handle("PROPFIND", "/dav/:directory/", zr.davDirectoryHandler) + router.Handle("PROPFIND", "/dav/", zr.davRootHandler) + // EXTRA: for browser handling + router.GET("/dav/:directory/:torrent/", zr.davTorrentHandler) + router.GET("/dav/:directory/", zr.davDirectoryHandler) + router.GET("/dav/", zr.davRootHandler) // DELETE routes router.DELETE("/dav/:directory/:torrent/:file", zr.deleteFileHandler) router.DELETE("/dav/:directory/:torrent/", zr.deleteTorrentHandler) - // rename sequence + // RENAME sequence router.Handle("PROPFIND", "/dav/:directory/:torrent/:file", zr.propfindFileHandler) router.Handle("MKCOL", "/dav/:directory/:torrent/", zr.mkcolTorrentHandler) router.Handle("MOVE", "/dav/:directory/:torrent/:file", zr.moveFileHandler) router.Handle("MOVE", "/dav/:directory/:torrent/", zr.moveTorrentHandler) - // infuse routes + // INFUSE DAV routes + router.Handle("PROPFIND", "/infuse/:directory/:torrent/", zr.infuseDavTorrentHandler) + router.Handle("PROPFIND", "/infuse/:directory/", zr.infuseDavDirectoryHandler) + router.Handle("PROPFIND", "/infuse/", zr.infuseDavRootHandler) + + // file download handler + router.GET("/http/:directory/:torrent/:file", zr.universalDownloadFileHandler) + router.GET("/dav/:directory/:torrent/:file", zr.universalDownloadFileHandler) router.GET("/infuse/:directory/:torrent/:file", zr.universalDownloadFileHandler) - router.Handle("PROPFIND", "/infuse/:directory/:torrent/", zr.propfindInfuseTorrentHandler) - router.Handle("PROPFIND", "/infuse/:directory/", zr.propfindInfuseDirectoryHandler) - router.Handle("PROPFIND", "/infuse/", zr.propfindInfuseRootHandler) + // HEAD route + router.HEAD("/http/:directory/:torrent/:file", zr.headHandler) // Global OPTIONS route router.GlobalOPTIONS = http.HandlerFunc(zr.globalOptionsHandler) @@ -80,128 +82,76 @@ func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torM }) } -func (zr *ZurgRouter) httpTorrentDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - directory := params.ByName("directory") - torrentName := params.ByName("torrent") - out, err := intHttp.HandleListFiles(directory, torrentName, zr.torMgr, zr.log) +func (zr *ZurgRouter) handleRootRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) { + out, err := handleFunc(zr.torMgr) if err != nil { http.Error(resp, "Not Found", http.StatusNotFound) return } - resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) -} - -func (zr *ZurgRouter) httpDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - directory := params.ByName("directory") - out, err := intHttp.HandleListTorrents(directory, zr.torMgr, zr.log) - if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) - return - } - resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") + resp.Header().Set("Content-Type", contentType) resp.WriteHeader(http.StatusOK) resp.Write(out) } func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - out, err := intHttp.HandleListDirectories(zr.torMgr) + zr.handleRootRequest(resp, req, params, intHttp.HandleListDirectories, "text/html; charset=\"utf-8\"") +} + +func (zr *ZurgRouter) davRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleRootRequest(resp, req, params, dav.HandleListDirectories, "text/xml; charset=\"utf-8\"") +} + +func (zr *ZurgRouter) infuseDavRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleRootRequest(resp, req, params, dav.HandleInfuseListDirectories, "text/xml; charset=\"utf-8\"") +} + +func (zr *ZurgRouter) handleDirectoryRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, *torrent.TorrentManager, *logutil.Logger) ([]byte, error), contentType string) { + directory := params.ByName("directory") + out, err := handleFunc(directory, zr.torMgr, zr.log) if err != nil { http.Error(resp, "Not Found", http.StatusNotFound) return } - resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") + resp.Header().Set("Content-Type", contentType) resp.WriteHeader(http.StatusOK) resp.Write(out) } -func (zr *ZurgRouter) propfindFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { +func (zr *ZurgRouter) httpDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleDirectoryRequest(resp, req, params, intHttp.HandleListTorrents, "text/html; charset=\"utf-8\"") +} + +func (zr *ZurgRouter) davDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleDirectoryRequest(resp, req, params, dav.HandleListTorrents, "text/xml; charset=\"utf-8\"") +} + +func (zr *ZurgRouter) infuseDavDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleDirectoryRequest(resp, req, params, dav.HandleInfuseListTorrents, "text/xml; charset=\"utf-8\"") +} + +func (zr *ZurgRouter) handleTorrentRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, string, *torrent.TorrentManager, *logutil.Logger) ([]byte, error), contentType string) { directory := params.ByName("directory") torrentName := params.ByName("torrent") - fileName := params.ByName("file") - out, err := dav.HandlePropfindFile(directory, torrentName, fileName, zr.torMgr, zr.log) + out, err := handleFunc(directory, torrentName, zr.torMgr, zr.log) if err != nil { - fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err) http.Error(resp, "Not Found", http.StatusNotFound) return } - fmt.Println(">>>>>>>>>>>>>>>>>>>. found yey") - resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") + resp.Header().Set("Content-Type", contentType) resp.WriteHeader(http.StatusOK) resp.Write(out) } -func (zr *ZurgRouter) propfindInfuseTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - directory := params.ByName("directory") - torrentName := params.ByName("torrent") - out, err := dav.HandleInfuseListFiles(directory, torrentName, zr.torMgr, zr.log) - if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) - return - } - resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) +func (zr *ZurgRouter) httpTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleTorrentRequest(resp, req, params, intHttp.HandleListFiles, "text/html; charset=\"utf-8\"") } -func (zr *ZurgRouter) propfindInfuseDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - directory := params.ByName("directory") - out, err := dav.HandleInfuseListTorrents(directory, zr.torMgr, zr.log) - if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) - return - } - resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) +func (zr *ZurgRouter) davTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleTorrentRequest(resp, req, params, dav.HandleListFiles, "text/xml; charset=\"utf-8\"") } -func (zr *ZurgRouter) propfindInfuseRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - out, err := dav.HandleInfuseListDirectories(zr.torMgr) - if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) - return - } - resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) -} - -func (zr *ZurgRouter) propfindTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - directory := params.ByName("directory") - torrentName := params.ByName("torrent") - out, err := dav.HandleListFiles(directory, torrentName, zr.torMgr, zr.log) - if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) - return - } - resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) -} - -func (zr *ZurgRouter) propfindDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - directory := params.ByName("directory") - out, err := dav.HandleListTorrents(directory, zr.torMgr, zr.log) - if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) - return - } - resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) -} - -func (zr *ZurgRouter) propfindRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { - out, err := dav.HandleListDirectories(zr.torMgr) - if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) - return - } - resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) +func (zr *ZurgRouter) infuseDavTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + zr.handleTorrentRequest(resp, req, params, dav.HandleInfuseListFiles, "text/xml; charset=\"utf-8\"") } func (zr *ZurgRouter) deleteFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { @@ -225,6 +175,22 @@ func (zr *ZurgRouter) deleteTorrentHandler(resp http.ResponseWriter, req *http.R resp.WriteHeader(http.StatusNoContent) } +func (zr *ZurgRouter) propfindFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { + directory := params.ByName("directory") + torrentName := params.ByName("torrent") + fileName := params.ByName("file") + out, err := dav.HandlePropfindFile(directory, torrentName, fileName, zr.torMgr, zr.log) + if err != nil { + fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err) + http.Error(resp, "Not Found", http.StatusNotFound) + return + } + fmt.Println(">>>>>>>>>>>>>>>>>>>. found yey") + resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") + resp.WriteHeader(http.StatusOK) + resp.Write(out) +} + func (zr *ZurgRouter) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { fmt.Println(">>>>>>>>>>>>>>>>>>> mkcolTorrentHandler") resp.WriteHeader(http.StatusNoContent) @@ -269,7 +235,7 @@ func (zr *ZurgRouter) universalDownloadFileHandler(resp http.ResponseWriter, req zr.getfile.HandleGetRequest(directory, torrentName, fileName, resp, req, zr.torMgr, zr.cfg, zr.log) } -func (zr *ZurgRouter) headFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { +func (zr *ZurgRouter) headHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { directory := params.ByName("directory") torrentName := params.ByName("torrent") fileName := params.ByName("file")