257 lines
10 KiB
Go
257 lines
10 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"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/debridmediamanager/zurg/pkg/logutil"
|
|
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
type ZurgRouter struct {
|
|
getfile *universal.GetFile
|
|
torMgr *torrent.TorrentManager
|
|
cfg config.ConfigInterface
|
|
api *realdebrid.RealDebrid
|
|
log *logutil.Logger
|
|
}
|
|
|
|
func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *logutil.Logger) {
|
|
zr := &ZurgRouter{
|
|
getfile: getfile,
|
|
torMgr: torMgr,
|
|
cfg: cfg,
|
|
api: api,
|
|
log: log,
|
|
}
|
|
|
|
// HTTP router
|
|
router.GET("/http/:directory/:torrent/", zr.httpTorrentHandler)
|
|
router.GET("/http/:directory/", zr.httpDirectoryHandler)
|
|
router.GET("/http/", zr.httpRootHandler)
|
|
|
|
// 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
|
|
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 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)
|
|
// HEAD route
|
|
router.HEAD("/http/:directory/:torrent/:file", zr.headHandler)
|
|
|
|
// Global OPTIONS route
|
|
router.GlobalOPTIONS = http.HandlerFunc(zr.globalOptionsHandler)
|
|
|
|
// root route
|
|
router.GET("/", zr.rootHandler)
|
|
|
|
// logs route
|
|
router.GET("/logs", zr.logsHandler)
|
|
router.GET("/logs/", zr.logsHandler)
|
|
|
|
router.MethodNotAllowed = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
zr.log.Debugf("Method not allowed: %s %s %v", req.Method, req.URL, req.Header)
|
|
http.Error(resp, "Method not allowed", http.StatusMethodNotAllowed)
|
|
})
|
|
}
|
|
|
|
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", contentType)
|
|
resp.WriteHeader(http.StatusOK)
|
|
resp.Write(out)
|
|
}
|
|
|
|
func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
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", contentType)
|
|
resp.WriteHeader(http.StatusOK)
|
|
resp.Write(out)
|
|
}
|
|
|
|
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")
|
|
out, err := handleFunc(directory, torrentName, zr.torMgr, zr.log)
|
|
if err != nil {
|
|
http.Error(resp, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
resp.Header().Set("Content-Type", contentType)
|
|
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) 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) 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) {
|
|
directory := params.ByName("directory")
|
|
torrentName := params.ByName("torrent")
|
|
fileName := params.ByName("file")
|
|
if dav.HandleDeleteFile(directory, torrentName, fileName, zr.torMgr) != nil {
|
|
http.Error(resp, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
resp.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (zr *ZurgRouter) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
directory := params.ByName("directory")
|
|
torrentName := params.ByName("torrent")
|
|
if dav.HandleDeleteTorrent(directory, torrentName, zr.torMgr) != nil {
|
|
http.Error(resp, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
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)
|
|
}
|
|
|
|
func (zr *ZurgRouter) moveFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
directory := params.ByName("directory")
|
|
torrentName := params.ByName("torrent")
|
|
fileName := params.ByName("file")
|
|
newName := req.Header.Get("Destination")
|
|
newName = filepath.Base(newName)
|
|
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler", fileName, ">>>>>>>>", newName)
|
|
if dav.HandleRenameFile(directory, torrentName, fileName, newName, zr.torMgr) != nil {
|
|
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler not found")
|
|
http.Error(resp, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler yay")
|
|
resp.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (zr *ZurgRouter) moveTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
directory := params.ByName("directory")
|
|
torrentName := params.ByName("torrent")
|
|
newName := req.Header.Get("Destination")
|
|
newName = filepath.Base(newName)
|
|
if dav.HandleRenameTorrent(directory, torrentName, newName, zr.torMgr) != nil {
|
|
http.Error(resp, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
resp.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (zr *ZurgRouter) globalOptionsHandler(resp http.ResponseWriter, req *http.Request) {
|
|
resp.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func (zr *ZurgRouter) universalDownloadFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
directory := params.ByName("directory")
|
|
torrentName := params.ByName("torrent")
|
|
fileName := params.ByName("file")
|
|
zr.getfile.HandleGetRequest(directory, torrentName, fileName, resp, req, zr.torMgr, zr.cfg, zr.log)
|
|
}
|
|
|
|
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")
|
|
universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, zr.torMgr, zr.log)
|
|
}
|
|
|
|
func (zr *ZurgRouter) logsHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
|
logs, err := zr.log.GetLogsFromFile()
|
|
if err != nil {
|
|
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
fmt.Fprint(resp, logs)
|
|
}
|
|
|
|
func bToMb(b uint64) uint64 {
|
|
return b / 1024 / 1024
|
|
}
|