delete functionality
This commit is contained in:
88
internal/dav/delete.go
Normal file
88
internal/dav/delete.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package dav
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HandleDeleteRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
|
||||||
|
requestPath := path.Clean(r.URL.Path)
|
||||||
|
filteredSegments := splitIntoSegments(requestPath)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
switch {
|
||||||
|
case len(filteredSegments) == 0:
|
||||||
|
err = fmt.Errorf("cannot delete root")
|
||||||
|
case len(filteredSegments) == 1:
|
||||||
|
err = fmt.Errorf("cannot delete configured directory")
|
||||||
|
case len(filteredSegments) == 2:
|
||||||
|
err = handleDeleteTorrent(w, filteredSegments, t)
|
||||||
|
case len(filteredSegments) >= 3:
|
||||||
|
err = handleDeleteFile(w, filteredSegments, t)
|
||||||
|
default:
|
||||||
|
log.Warnf("Request %s %s not found", r.Method, requestPath)
|
||||||
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if strings.Contains(err.Error(), "cannot find") {
|
||||||
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Errorf("Error processing request: %v", err)
|
||||||
|
http.Error(w, "Server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleDeleteTorrent(w http.ResponseWriter, segments []string, t *torrent.TorrentManager) error {
|
||||||
|
directory := segments[0]
|
||||||
|
torrents, ok := t.DirectoryMap.Get(directory)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("cannot find directory %s", directory)
|
||||||
|
}
|
||||||
|
|
||||||
|
accessKey := segments[1]
|
||||||
|
tor, ok := torrents.Get(accessKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("cannot find torrent %s", accessKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Delete(tor.AccessKey)
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleDeleteFile(w http.ResponseWriter, segments []string, t *torrent.TorrentManager) error {
|
||||||
|
directory := segments[0]
|
||||||
|
torrents, ok := t.DirectoryMap.Get(directory)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("cannot find directory %s", directory)
|
||||||
|
}
|
||||||
|
|
||||||
|
accessKey := segments[1]
|
||||||
|
tor, ok := torrents.Get(accessKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("cannot find torrent %s", accessKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set filepath to last segment
|
||||||
|
filepath := segments[len(segments)-1]
|
||||||
|
file, ok := tor.SelectedFiles.Get(filepath)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("cannot find file %s", filepath)
|
||||||
|
}
|
||||||
|
|
||||||
|
file.Link = "-"
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -10,26 +10,21 @@ import (
|
|||||||
|
|
||||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||||
"github.com/debridmediamanager.com/zurg/pkg/dav"
|
"github.com/debridmediamanager.com/zurg/pkg/dav"
|
||||||
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) {
|
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
|
||||||
log := logutil.NewLogger().Named("dav")
|
|
||||||
|
|
||||||
requestPath := path.Clean(r.URL.Path)
|
requestPath := path.Clean(r.URL.Path)
|
||||||
requestPath = strings.Trim(requestPath, "/")
|
filteredSegments := splitIntoSegments(requestPath)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
filteredSegments := strings.Split(requestPath, "/")
|
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case len(filteredSegments) == 1 && filteredSegments[0] == "":
|
case len(filteredSegments) == 0:
|
||||||
err = handleRoot(w, t)
|
err = handleListDirectories(w, t)
|
||||||
case len(filteredSegments) == 1:
|
case len(filteredSegments) == 1:
|
||||||
err = handleListOfTorrents(w, requestPath, t)
|
err = handleListTorrents(w, requestPath, t)
|
||||||
case len(filteredSegments) == 2:
|
case len(filteredSegments) == 2:
|
||||||
err = handleSingleTorrent(w, requestPath, t)
|
err = handleListFiles(w, requestPath, t)
|
||||||
default:
|
default:
|
||||||
log.Warnf("Request %s %s not found", r.Method, requestPath)
|
log.Warnf("Request %s %s not found", r.Method, requestPath)
|
||||||
http.Error(w, "Not Found", http.StatusNotFound)
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
@@ -49,7 +44,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
|||||||
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
|
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleRoot(w http.ResponseWriter, t *torrent.TorrentManager) error {
|
func handleListDirectories(w http.ResponseWriter, t *torrent.TorrentManager) error {
|
||||||
fmt.Fprint(w, "<?xml?><d:multistatus xmlns:d=\"DAV:\">")
|
fmt.Fprint(w, "<?xml?><d:multistatus xmlns:d=\"DAV:\">")
|
||||||
// initial response is the directory itself
|
// initial response is the directory itself
|
||||||
fmt.Fprint(w, dav.Directory("", ""))
|
fmt.Fprint(w, dav.Directory("", ""))
|
||||||
@@ -64,7 +59,7 @@ func handleRoot(w http.ResponseWriter, t *torrent.TorrentManager) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleListOfTorrents(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
|
func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
|
||||||
basePath := path.Base(requestPath)
|
basePath := path.Base(requestPath)
|
||||||
torrents, ok := t.DirectoryMap.Get(basePath)
|
torrents, ok := t.DirectoryMap.Get(basePath)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -73,7 +68,7 @@ func handleListOfTorrents(w http.ResponseWriter, requestPath string, t *torrent.
|
|||||||
|
|
||||||
fmt.Fprint(w, "<?xml?><d:multistatus xmlns:d=\"DAV:\">")
|
fmt.Fprint(w, "<?xml?><d:multistatus xmlns:d=\"DAV:\">")
|
||||||
// initial response is the directory itself
|
// initial response is the directory itself
|
||||||
fmt.Fprint(w, dav.Directory(basePath+"/", ""))
|
fmt.Fprint(w, dav.Directory(basePath, ""))
|
||||||
|
|
||||||
var allTorrents []*torrent.Torrent
|
var allTorrents []*torrent.Torrent
|
||||||
torrents.IterCb(func(_ string, tor *torrent.Torrent) {
|
torrents.IterCb(func(_ string, tor *torrent.Torrent) {
|
||||||
@@ -91,7 +86,8 @@ func handleListOfTorrents(w http.ResponseWriter, requestPath string, t *torrent.
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSingleTorrent(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
|
func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
|
||||||
|
requestPath = strings.TrimPrefix(requestPath, "/")
|
||||||
basePath := path.Base(path.Dir(requestPath))
|
basePath := path.Base(path.Dir(requestPath))
|
||||||
torrents, ok := t.DirectoryMap.Get(basePath)
|
torrents, ok := t.DirectoryMap.Get(basePath)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -105,7 +101,7 @@ func handleSingleTorrent(w http.ResponseWriter, requestPath string, t *torrent.T
|
|||||||
|
|
||||||
fmt.Fprint(w, "<?xml?><d:multistatus xmlns:d=\"DAV:\">")
|
fmt.Fprint(w, "<?xml?><d:multistatus xmlns:d=\"DAV:\">")
|
||||||
// initial response is the directory itself
|
// initial response is the directory itself
|
||||||
fmt.Fprint(w, dav.Directory(requestPath+"/", tor.LatestAdded))
|
fmt.Fprint(w, dav.Directory(requestPath, tor.LatestAdded))
|
||||||
|
|
||||||
filenames := tor.SelectedFiles.Keys()
|
filenames := tor.SelectedFiles.Keys()
|
||||||
sort.Strings(filenames)
|
sort.Strings(filenames)
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
package dav
|
package dav
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// convertRFC3339toRFC1123 converts a date from RFC3339 to RFC1123
|
func splitIntoSegments(path string) []string {
|
||||||
func convertRFC3339toRFC1123(input string) string {
|
segments := strings.Split(path, "/")
|
||||||
t, err := time.Parse(time.RFC3339, input)
|
// remove empty segments
|
||||||
if err != nil {
|
for i := 0; i < len(segments); i++ {
|
||||||
return ""
|
if segments[i] == "" {
|
||||||
|
segments = append(segments[:i], segments[i+1:]...)
|
||||||
|
i--
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return t.Format("Mon, 02 Jan 2006 15:04:05 GMT")
|
return segments
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,9 +38,13 @@ func Router(mux *http.ServeMux, c config.ConfigInterface, t *torrent.TorrentMana
|
|||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
davlog := logutil.NewLogger().Named("dav")
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "PROPFIND":
|
case "PROPFIND":
|
||||||
dav.HandlePropfindRequest(w, r, t)
|
dav.HandlePropfindRequest(w, r, t, davlog)
|
||||||
|
|
||||||
|
case "DELETE":
|
||||||
|
dav.HandleDeleteRequest(w, r, t, davlog)
|
||||||
|
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
universal.HandleGetRequest(w, r, t, c, cache)
|
universal.HandleGetRequest(w, r, t, c, cache)
|
||||||
|
|||||||
@@ -548,6 +548,17 @@ func (t *TorrentManager) repairAll() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TorrentManager) Delete(accessKey string) {
|
||||||
|
t.log.Infof("Deleting torrent %s", accessKey)
|
||||||
|
allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS)
|
||||||
|
if torrent, ok := allTorrents.Get(accessKey); ok {
|
||||||
|
for _, instance := range torrent.Instances {
|
||||||
|
t.api.DeleteTorrent(instance.ID)
|
||||||
|
}
|
||||||
|
allTorrents.Remove(accessKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TorrentManager) Repair(accessKey string) {
|
func (t *TorrentManager) Repair(accessKey string) {
|
||||||
if !t.cfg.EnableRepair() {
|
if !t.cfg.EnableRepair() {
|
||||||
t.log.Warn("Repair is disabled; if you do not have other zurg instances running, you should enable repair")
|
t.log.Warn("Repair is disabled; if you do not have other zurg instances running, you should enable repair")
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
|
|||||||
// If there are less than 3 segments, return an error or adjust as needed
|
// If there are less than 3 segments, return an error or adjust as needed
|
||||||
if len(segments) <= 3 {
|
if len(segments) <= 3 {
|
||||||
if isDav {
|
if isDav {
|
||||||
dav.HandlePropfindRequest(w, r, t)
|
dav.HandlePropfindRequest(w, r, t, log)
|
||||||
} else {
|
} else {
|
||||||
intHttp.HandleDirectoryListing(w, r, t)
|
intHttp.HandleDirectoryListing(w, r, t)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func DavFile(path string, fileSize int64, added string, link string) Response {
|
|||||||
// optimized versions, no more marshalling
|
// optimized versions, no more marshalling
|
||||||
|
|
||||||
func Directory(path, added string) string {
|
func Directory(path, added string) string {
|
||||||
return fmt.Sprintf("<d:response><d:href>/%s</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype></d:prop><d:prop><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape(path), added)
|
return fmt.Sprintf("<d:response><d:href>/%s</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype><d:getlastmodified>%s</d:getlastmodified></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>", customPathEscape(path), added)
|
||||||
}
|
}
|
||||||
|
|
||||||
func File(path string, fileSize int64, added string) string {
|
func File(path string, fileSize int64, added string) string {
|
||||||
|
|||||||
11
rclone.conf
11
rclone.conf
@@ -1,10 +1,11 @@
|
|||||||
[zurg]
|
[zurg]
|
||||||
|
type = webdav
|
||||||
|
url = http://zurg:9999
|
||||||
|
vendor = other
|
||||||
|
|
||||||
|
[zurghttp]
|
||||||
|
# it's readonly but it's crazy fast
|
||||||
type = http
|
type = http
|
||||||
url = http://zurg:9999/http
|
url = http://zurg:9999/http
|
||||||
no_head = false
|
no_head = false
|
||||||
no_slash = false
|
no_slash = false
|
||||||
|
|
||||||
[zurgwd]
|
|
||||||
type = webdav
|
|
||||||
url = http://zurg:9999
|
|
||||||
vendor = other
|
|
||||||
|
|||||||
Reference in New Issue
Block a user