|
|
|
@@ -3,6 +3,7 @@ package handlers
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
@@ -102,6 +103,7 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
|
|
|
|
func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
|
|
|
|
func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
|
|
|
|
out, err := handleFunc(hs.torMgr)
|
|
|
|
out, err := handleFunc(hs.torMgr)
|
|
|
|
if err != nil && strings.Contains(contentType, "xml") {
|
|
|
|
if err != nil && strings.Contains(contentType, "xml") {
|
|
|
|
|
|
|
|
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
|
|
|
|
resp.WriteHeader(http.StatusNotImplemented)
|
|
|
|
resp.WriteHeader(http.StatusNotImplemented)
|
|
|
|
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
|
|
|
|
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
|
|
|
|
return
|
|
|
|
return
|
|
|
|
@@ -133,9 +135,10 @@ func (hs *Handlers) handleInfuseRoot(resp http.ResponseWriter, req *http.Request
|
|
|
|
// handle torrent list request
|
|
|
|
// handle torrent list request
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
|
|
|
|
func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
out, err := handleFunc(directory, hs.torMgr)
|
|
|
|
out, err := handleFunc(directory, hs.torMgr)
|
|
|
|
if err != nil && strings.Contains(contentType, "xml") {
|
|
|
|
if err != nil && strings.Contains(contentType, "xml") {
|
|
|
|
|
|
|
|
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
|
|
|
|
resp.WriteHeader(http.StatusNotImplemented)
|
|
|
|
resp.WriteHeader(http.StatusNotImplemented)
|
|
|
|
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
|
|
|
|
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
|
|
|
|
return
|
|
|
|
return
|
|
|
|
@@ -153,7 +156,7 @@ func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
handlerFunc := intHttp.ServeTorrentsList
|
|
|
|
handlerFunc := intHttp.ServeTorrentsList
|
|
|
|
if directory == config.DOWNLOADS {
|
|
|
|
if directory == config.DOWNLOADS {
|
|
|
|
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
|
|
|
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
|
|
|
@@ -164,7 +167,7 @@ func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.R
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
handlerFunc := dav.ServeTorrentsList
|
|
|
|
handlerFunc := dav.ServeTorrentsList
|
|
|
|
if directory == config.DOWNLOADS {
|
|
|
|
if directory == config.DOWNLOADS {
|
|
|
|
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
|
|
|
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
|
|
|
@@ -175,7 +178,7 @@ func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Re
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
handlerFunc := dav.ServeTorrentsListForInfuse
|
|
|
|
handlerFunc := dav.ServeTorrentsListForInfuse
|
|
|
|
if directory == config.DOWNLOADS {
|
|
|
|
if directory == config.DOWNLOADS {
|
|
|
|
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
|
|
|
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
|
|
|
|
@@ -188,10 +191,11 @@ func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http
|
|
|
|
// handle files list request
|
|
|
|
// handle files list request
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) {
|
|
|
|
func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
out, err := handleFunc(directory, torrentName, hs.torMgr)
|
|
|
|
out, err := handleFunc(directory, torrentName, hs.torMgr)
|
|
|
|
if err != nil && strings.Contains(contentType, "xml") {
|
|
|
|
if err != nil && strings.Contains(contentType, "xml") {
|
|
|
|
|
|
|
|
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
|
|
|
|
resp.WriteHeader(http.StatusNotImplemented)
|
|
|
|
resp.WriteHeader(http.StatusNotImplemented)
|
|
|
|
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
|
|
|
|
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
|
|
|
|
return
|
|
|
|
return
|
|
|
|
@@ -246,9 +250,9 @@ func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *htt
|
|
|
|
// handle delete request
|
|
|
|
// handle delete request
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
fileName := chi.URLParam(req, "filename")
|
|
|
|
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
|
|
|
|
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
|
|
|
|
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
|
|
|
|
http.NotFound(resp, req)
|
|
|
|
http.NotFound(resp, req)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
@@ -257,8 +261,8 @@ func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Reques
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil {
|
|
|
|
if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil {
|
|
|
|
http.NotFound(resp, req)
|
|
|
|
http.NotFound(resp, req)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
@@ -269,9 +273,9 @@ func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Req
|
|
|
|
// other handlers
|
|
|
|
// other handlers
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
fileName := chi.URLParam(req, "filename")
|
|
|
|
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
|
|
|
|
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
|
|
|
|
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
http.NotFound(resp, req)
|
|
|
|
http.NotFound(resp, req)
|
|
|
|
@@ -287,9 +291,9 @@ func (hs *Handlers) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Requ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
fileName := chi.URLParam(req, "filename")
|
|
|
|
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
|
|
|
|
newName := req.Header.Get("Destination")
|
|
|
|
newName := req.Header.Get("Destination")
|
|
|
|
newName = filepath.Base(newName)
|
|
|
|
newName = filepath.Base(newName)
|
|
|
|
if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil {
|
|
|
|
if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil {
|
|
|
|
@@ -300,8 +304,8 @@ func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
newName := req.Header.Get("Destination")
|
|
|
|
newName := req.Header.Get("Destination")
|
|
|
|
newName = filepath.Base(newName)
|
|
|
|
newName = filepath.Base(newName)
|
|
|
|
if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil {
|
|
|
|
if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil {
|
|
|
|
@@ -314,14 +318,14 @@ func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Reque
|
|
|
|
// universal handlers
|
|
|
|
// universal handlers
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
fileName := chi.URLParam(req, "filename")
|
|
|
|
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
|
|
|
|
hs.downloader.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
|
|
|
|
hs.downloader.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
filename := chi.URLParam(req, "filename")
|
|
|
|
filename, _ := url.PathUnescape(chi.URLParam(req, "filename"))
|
|
|
|
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
|
|
|
|
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
|
|
|
|
hs.downloader.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log)
|
|
|
|
hs.downloader.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@@ -330,14 +334,14 @@ func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Reque
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) handleCheckFile(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) handleCheckFile(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
directory := chi.URLParam(req, "directory")
|
|
|
|
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
|
|
|
|
torrentName := chi.URLParam(req, "torrent")
|
|
|
|
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
|
|
|
|
fileName := chi.URLParam(req, "filename")
|
|
|
|
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
|
|
|
|
universal.CheckFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log)
|
|
|
|
universal.CheckFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (hs *Handlers) handleCheckDownloadLink(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
func (hs *Handlers) handleCheckDownloadLink(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
filename := chi.URLParam(req, "filename")
|
|
|
|
filename, _ := url.PathUnescape(chi.URLParam(req, "filename"))
|
|
|
|
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
|
|
|
|
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
|
|
|
|
universal.CheckDownloadLink(download, resp, req, hs.torMgr, hs.log)
|
|
|
|
universal.CheckDownloadLink(download, resp, req, hs.torMgr, hs.log)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|