Refactor route handlers

This commit is contained in:
Ben Sarmiento
2023-12-12 02:50:39 +01:00
parent d0ecaa0752
commit 47848e1feb

View File

@@ -32,37 +32,39 @@ func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torM
log: log, log: log,
} }
// http router // HTTP router
router.GET("/http/:directory/:torrent/:file", zr.universalDownloadFileHandler) router.GET("/http/:directory/:torrent/", zr.httpTorrentHandler)
router.GET("/http/:directory/:torrent/", zr.httpTorrentDirectoryHandler)
router.GET("/http/:directory/", zr.httpDirectoryHandler) router.GET("/http/:directory/", zr.httpDirectoryHandler)
router.GET("/http/", zr.httpRootHandler) router.GET("/http/", zr.httpRootHandler)
// HEAD route
router.HEAD("/http/:directory/:torrent/:file", zr.headFileHandler)
// dav router // WEBDAV router
router.GET("/dav/:directory/:torrent/:file", zr.universalDownloadFileHandler) router.Handle("PROPFIND", "/dav/:directory/:torrent/", zr.davTorrentHandler)
router.Handle("PROPFIND", "/dav/:directory/:torrent/", zr.propfindTorrentHandler) router.Handle("PROPFIND", "/dav/:directory/", zr.davDirectoryHandler)
router.Handle("PROPFIND", "/dav/:directory/", zr.propfindDirectoryHandler) router.Handle("PROPFIND", "/dav/", zr.davRootHandler)
router.Handle("PROPFIND", "/dav/", zr.propfindRootHandler) // EXTRA: for browser handling
// extras router.GET("/dav/:directory/:torrent/", zr.davTorrentHandler)
router.GET("/dav/:directory/:torrent/", zr.propfindTorrentHandler) router.GET("/dav/:directory/", zr.davDirectoryHandler)
router.GET("/dav/:directory/", zr.propfindDirectoryHandler) router.GET("/dav/", zr.davRootHandler)
router.GET("/dav/", zr.propfindRootHandler)
// DELETE routes // DELETE routes
router.DELETE("/dav/:directory/:torrent/:file", zr.deleteFileHandler) router.DELETE("/dav/:directory/:torrent/:file", zr.deleteFileHandler)
router.DELETE("/dav/:directory/:torrent/", zr.deleteTorrentHandler) router.DELETE("/dav/:directory/:torrent/", zr.deleteTorrentHandler)
// rename sequence // RENAME sequence
router.Handle("PROPFIND", "/dav/:directory/:torrent/:file", zr.propfindFileHandler) router.Handle("PROPFIND", "/dav/:directory/:torrent/:file", zr.propfindFileHandler)
router.Handle("MKCOL", "/dav/:directory/:torrent/", zr.mkcolTorrentHandler) router.Handle("MKCOL", "/dav/:directory/:torrent/", zr.mkcolTorrentHandler)
router.Handle("MOVE", "/dav/:directory/:torrent/:file", zr.moveFileHandler) router.Handle("MOVE", "/dav/:directory/:torrent/:file", zr.moveFileHandler)
router.Handle("MOVE", "/dav/:directory/:torrent/", zr.moveTorrentHandler) 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.GET("/infuse/:directory/:torrent/:file", zr.universalDownloadFileHandler)
router.Handle("PROPFIND", "/infuse/:directory/:torrent/", zr.propfindInfuseTorrentHandler) // HEAD route
router.Handle("PROPFIND", "/infuse/:directory/", zr.propfindInfuseDirectoryHandler) router.HEAD("/http/:directory/:torrent/:file", zr.headHandler)
router.Handle("PROPFIND", "/infuse/", zr.propfindInfuseRootHandler)
// Global OPTIONS route // Global OPTIONS route
router.GlobalOPTIONS = http.HandlerFunc(zr.globalOptionsHandler) 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) { func (zr *ZurgRouter) handleRootRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
directory := params.ByName("directory") out, err := handleFunc(zr.torMgr)
torrentName := params.ByName("torrent")
out, err := intHttp.HandleListFiles(directory, torrentName, zr.torMgr, zr.log)
if err != nil { if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound) http.Error(resp, "Not Found", http.StatusNotFound)
return 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) 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.WriteHeader(http.StatusOK) resp.WriteHeader(http.StatusOK)
resp.Write(out) resp.Write(out)
} }
func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { 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 { if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound) http.Error(resp, "Not Found", http.StatusNotFound)
return return
} }
resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") resp.Header().Set("Content-Type", contentType)
resp.WriteHeader(http.StatusOK) resp.WriteHeader(http.StatusOK)
resp.Write(out) 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") directory := params.ByName("directory")
torrentName := params.ByName("torrent") torrentName := params.ByName("torrent")
fileName := params.ByName("file") out, err := handleFunc(directory, torrentName, zr.torMgr, zr.log)
out, err := dav.HandlePropfindFile(directory, torrentName, fileName, zr.torMgr, zr.log)
if err != nil { if err != nil {
fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err)
http.Error(resp, "Not Found", http.StatusNotFound) http.Error(resp, "Not Found", http.StatusNotFound)
return return
} }
fmt.Println(">>>>>>>>>>>>>>>>>>>. found yey") resp.Header().Set("Content-Type", contentType)
resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK) resp.WriteHeader(http.StatusOK)
resp.Write(out) resp.Write(out)
} }
func (zr *ZurgRouter) propfindInfuseTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { func (zr *ZurgRouter) httpTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory") zr.handleTorrentRequest(resp, req, params, intHttp.HandleListFiles, "text/html; charset=\"utf-8\"")
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) propfindInfuseDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { func (zr *ZurgRouter) davTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory") zr.handleTorrentRequest(resp, req, params, dav.HandleListFiles, "text/xml; charset=\"utf-8\"")
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) propfindInfuseRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { func (zr *ZurgRouter) infuseDavTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
out, err := dav.HandleInfuseListDirectories(zr.torMgr) zr.handleTorrentRequest(resp, req, params, dav.HandleInfuseListFiles, "text/xml; charset=\"utf-8\"")
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) deleteFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { 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) 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) { func (zr *ZurgRouter) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
fmt.Println(">>>>>>>>>>>>>>>>>>> mkcolTorrentHandler") fmt.Println(">>>>>>>>>>>>>>>>>>> mkcolTorrentHandler")
resp.WriteHeader(http.StatusNoContent) 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) 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") directory := params.ByName("directory")
torrentName := params.ByName("torrent") torrentName := params.ByName("torrent")
fileName := params.ByName("file") fileName := params.ByName("file")