Add rename

This commit is contained in:
Ben Sarmiento
2023-12-10 21:00:02 +01:00
parent e46da8c589
commit 9e181c2b19
3 changed files with 130 additions and 9 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
@@ -55,6 +56,12 @@ func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torM
// 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)
// MOVE routes
router.Handle("MOVE", "/dav/:directory/:torrent/", zr.moveTorrentHandler)
// Global OPTIONS route
router.GlobalOPTIONS = http.HandlerFunc(zr.globalOptionsHandler)
@@ -65,6 +72,11 @@ func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torM
// 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) httpTorrentDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
@@ -103,6 +115,22 @@ func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Reques
fmt.Fprint(resp, *out)
}
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) propfindTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
@@ -113,7 +141,7 @@ func (zr *ZurgRouter) propfindTorrentHandler(resp http.ResponseWriter, req *http
}
resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK)
fmt.Fprint(resp, *out)
resp.Write(out)
}
func (zr *ZurgRouter) propfindDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
@@ -125,7 +153,7 @@ func (zr *ZurgRouter) propfindDirectoryHandler(resp http.ResponseWriter, req *ht
}
resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK)
fmt.Fprint(resp, *out)
resp.Write(out)
}
func (zr *ZurgRouter) propfindRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
@@ -136,7 +164,7 @@ func (zr *ZurgRouter) propfindRootHandler(resp http.ResponseWriter, req *http.Re
}
resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK)
fmt.Fprint(resp, *out)
resp.Write(out)
}
func (zr *ZurgRouter) deleteFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
@@ -160,6 +188,39 @@ func (zr *ZurgRouter) deleteTorrentHandler(resp http.ResponseWriter, req *http.R
resp.WriteHeader(http.StatusNoContent)
}
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)
}