Mount downloads directory

This commit is contained in:
Ben Sarmiento
2024-01-08 22:56:10 +01:00
parent 96e41d6cbc
commit 5a23d0ff7b
6 changed files with 145 additions and 62 deletions

View File

@@ -285,3 +285,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
fmt.Fprint(resp, out)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

View File

@@ -39,40 +39,44 @@ func AttachHandlers(router *chi.Mux, getfile *universal.GetFile, torMgr *torrent
log: log,
}
router.Use(globalOptionsHandler)
router.Use(optionsMiddleware)
router.Get("/", hs.handleHome)
router.Get("/{mountType}/version.txt", hs.handleVersionFile)
router.Get(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleDownloadLink)
router.Get("/{mountType}/{directory}/{torrent}/{filename}", hs.handleDownloadFile)
router.Head("/{mountType}/{directory}/{torrent}/{filename}", hs.handleCheckCachedLink)
router.Get("/http/", hs.handleHttpRoot)
router.Get(fmt.Sprintf("/http/%s/", config.DOWNLOADS), hs.handleHttpDownloadsList)
router.Get("/http/{directory}/", hs.handleHttpTorrentsList)
router.Get("/http/{directory}/{torrent}/", hs.handleHttpFilesList)
router.Get("/http/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler)
router.Head("/http/{directory}/{torrent}/{file}", hs.httpHeadHandler)
router.Get("/dav/", hs.handleDavRoot)
router.Get(fmt.Sprintf("/dav/%s/", config.DOWNLOADS), hs.handleDavDownloadsList)
router.Get("/dav/{directory}/", hs.handleDavTorrentsList)
router.Get("/dav/{directory}/{torrent}/", hs.handleDavFilesList)
router.Get("/dav/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler)
router.MethodFunc("PROPFIND", "/dav/", hs.handleDavRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/dav/%s/", config.DOWNLOADS), hs.handleDavDownloadsList)
router.MethodFunc("PROPFIND", "/dav/{directory}/", hs.handleDavTorrentsList)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavFilesList)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/{file}", hs.davCheckSingleFileHandler)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/{filename}", hs.davCheckSingleFileHandler)
router.Get("/infuse/", hs.handleInfuseRoot)
router.Get(fmt.Sprintf("/infuse/%s/", config.DOWNLOADS), hs.handleInfuseDownloadsList)
router.Get("/infuse/{directory}/", hs.handleInfuseTorrentsList)
router.Get("/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList)
router.Get("/infuse/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler)
router.MethodFunc("PROPFIND", "/infuse/", hs.handleInfuseRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/infuse/%s/", config.DOWNLOADS), hs.handleInfuseDownloadsList)
router.MethodFunc("PROPFIND", "/infuse/{directory}/", hs.handleInfuseTorrentsList)
router.MethodFunc("PROPFIND", "/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList)
// note: reused handlers for dav and infuse
router.Delete("/{mountType}/{directory}/{torrent}/", hs.deleteTorrentHandler)
router.Delete("/{mountType}/{directory}/{torrent}/{file}", hs.deleteFileHandler)
router.Delete("/{mountType}/{directory}/{torrent}/{filename}", hs.deleteFileHandler)
router.MethodFunc("MKCOL", "/{mountType}/{directory}/{torrent}/", hs.mkcolTorrentHandler)
router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/", hs.moveTorrentHandler)
router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/{file}", hs.moveFileHandler)
router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/{filename}", hs.moveFileHandler)
// logs route
router.Get("/logs", hs.logsHandler)
@@ -89,7 +93,7 @@ func AttachHandlers(router *chi.Mux, getfile *universal.GetFile, torMgr *torrent
func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
out, err := handleFunc(hs.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
resp.Header().Set("Content-Type", contentType)
@@ -115,7 +119,7 @@ func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http
directory := chi.URLParam(req, "directory")
out, err := handleFunc(directory, hs.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
resp.Header().Set("Content-Type", contentType)
@@ -163,7 +167,7 @@ func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Re
torrentName := chi.URLParam(req, "torrent")
out, err := handleFunc(directory, torrentName, hs.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
resp.Header().Set("Content-Type", contentType)
@@ -183,19 +187,37 @@ func (hs *Handlers) handleInfuseFilesList(resp http.ResponseWriter, req *http.Re
hs.innerFilesListHandler(resp, req, dav.ServeFilesListForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVersionFile(resp http.ResponseWriter, req *http.Request) {
out, _ := version.GetFile()
resp.Header().Set("Content-Type", "text/plain; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK)
resp.Write(out)
// handle downloads list request
func (hs *Handlers) handleHttpDownloadsList(resp http.ResponseWriter, req *http.Request) {
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) handleDavDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsList(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsListForInfuse(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
// handle delete request
func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "file")
fileName := chi.URLParam(req, "filename")
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
resp.WriteHeader(http.StatusNoContent)
@@ -205,46 +227,42 @@ func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Req
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
resp.WriteHeader(http.StatusNoContent)
}
// other handlers
func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "file")
fileName := chi.URLParam(req, "filename")
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
if err != nil {
fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err)
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
fmt.Println(">>>>>>>>>>>>>>>>>>>. found yey")
resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK)
resp.Write(out)
}
func (hs *Handlers) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Request) {
fmt.Println(">>>>>>>>>>>>>>>>>>> mkcolTorrentHandler")
resp.WriteHeader(http.StatusNoContent)
}
func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "file")
fileName := chi.URLParam(req, "filename")
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler", fileName, ">>>>>>>>", newName)
if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil {
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler not found")
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler yay")
resp.WriteHeader(http.StatusNoContent)
}
@@ -254,13 +272,13 @@ func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Reque
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
http.NotFound(resp, req)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func globalOptionsHandler(next http.Handler) http.Handler {
func optionsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
@@ -270,20 +288,42 @@ func globalOptionsHandler(next http.Handler) http.Handler {
})
}
func (hs *Handlers) universalDownloadFileHandler(resp http.ResponseWriter, req *http.Request) {
// universal handlers
func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "file")
hs.getfile.ServeFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
fileName := chi.URLParam(req, "filename")
hs.getfile.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
}
func (hs *Handlers) httpHeadHandler(resp http.ResponseWriter, req *http.Request) {
func (hs *Handlers) handleCheckCachedLink(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "file")
fileName := chi.URLParam(req, "filename")
universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log)
}
func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) {
filename := chi.URLParam(req, "filename")
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
hs.getfile.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log)
} else {
http.NotFound(resp, req)
}
}
// handle version file request
func (hs *Handlers) handleVersionFile(resp http.ResponseWriter, req *http.Request) {
out, _ := version.GetFile()
resp.Header().Set("Content-Type", "text/plain; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK)
resp.Write(out)
}
// logs handler
func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) {
logs, err := hs.log.GetLogsFromFile()
if err != nil {
@@ -292,7 +332,3 @@ func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) {
}
fmt.Fprint(resp, logs)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}