package router import ( "fmt" "net/http" "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/dav" intHttp "github.com/debridmediamanager/zurg/internal/http" "github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/internal/universal" "github.com/julienschmidt/httprouter" "go.uber.org/zap" ) type ZurgRouter struct { getfile *universal.GetFile torMgr *torrent.TorrentManager cfg config.ConfigInterface log *zap.SugaredLogger } func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) { zr := &ZurgRouter{ getfile: getfile, torMgr: torMgr, cfg: cfg, log: log, } // http router router.GET("/http/:directory/:torrent/:file", zr.universalDownloadFileHandler) router.GET("/http/:directory/:torrent/", zr.httpTorrentDirectoryHandler) 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.GET("/dav/:directory/:torrent/", zr.propfindTorrentHandler) router.GET("/dav/:directory/", zr.propfindDirectoryHandler) router.GET("/dav/", zr.propfindRootHandler) // PROPFIND routes router.Handle("PROPFIND", "/dav/:directory/:torrent/", zr.propfindTorrentHandler) router.Handle("PROPFIND", "/dav/:directory/", zr.propfindDirectoryHandler) router.Handle("PROPFIND", "/dav/", zr.propfindRootHandler) // DELETE routes router.DELETE("/dav/:directory/:torrent/:file", zr.deleteFileHandler) router.DELETE("/dav/:directory/:torrent/", zr.deleteTorrentHandler) // Global OPTIONS route router.GlobalOPTIONS = http.HandlerFunc(zr.globalOptionsHandler) // root route router.GET("/", zr.rootHandler) } 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) 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) fmt.Fprint(resp, *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) fmt.Fprint(resp, *out) } func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { out, err := intHttp.HandleListDirectories(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) fmt.Fprint(resp, *out) } func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { fmt.Fprint(resp, "