Refactor with ordered maps
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
@@ -69,10 +70,28 @@ func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Req
|
||||
|
||||
for _, directory := range c.GetDirectories() {
|
||||
if basePath == directory {
|
||||
torrents := t.GetByDirectory(basePath)
|
||||
resp, err := createMultiTorrentResponse("/"+basePath, torrents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory (%s): %w", basePath, err)
|
||||
var responses []dav.Response
|
||||
|
||||
responses = append(responses, dav.Directory(basePath))
|
||||
|
||||
for el := t.TorrentMap.Front(); el != nil; el = el.Next() {
|
||||
accessKey := el.Key
|
||||
torrent := el.Value
|
||||
if torrent.InProgress {
|
||||
continue
|
||||
}
|
||||
for _, dir := range torrent.Directories {
|
||||
if dir == basePath {
|
||||
path := filepath.Join(basePath, accessKey)
|
||||
responses = append(responses, dav.Directory(path))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resp := &dav.MultiStatus{
|
||||
XMLNS: "DAV:",
|
||||
Response: responses,
|
||||
}
|
||||
return xml.Marshal(resp)
|
||||
}
|
||||
@@ -82,17 +101,37 @@ func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
|
||||
directory := path.Dir(requestPath)
|
||||
torrentName := path.Base(requestPath)
|
||||
|
||||
sameNameTorrents := t.FindAllTorrentsWithName(directory, torrentName)
|
||||
if len(sameNameTorrents) == 0 {
|
||||
return nil, fmt.Errorf("cannot find directory when generating single torrent: %s", requestPath)
|
||||
accessKey := path.Base(requestPath)
|
||||
torrent, exists := t.TorrentMap.Get(accessKey)
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("cannot find torrent %s", requestPath)
|
||||
}
|
||||
|
||||
resp, err := createSingleTorrentResponse("/"+directory, sameNameTorrents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory (%s): %w", requestPath, err)
|
||||
var responses []dav.Response
|
||||
|
||||
// initial response is the directory itself
|
||||
responses = append(responses, dav.Directory(requestPath))
|
||||
|
||||
for el := torrent.SelectedFiles.Front(); el != nil; el = el.Next() {
|
||||
file := el.Value
|
||||
if file.Link == "" {
|
||||
// will be caught by torrent manager's repairAll
|
||||
// just skip it for now
|
||||
continue
|
||||
}
|
||||
filename := filepath.Base(file.Path)
|
||||
filePath := filepath.Join(requestPath, filename)
|
||||
responses = append(responses, dav.File(
|
||||
filePath,
|
||||
file.Bytes,
|
||||
convertRFC3339toRFC1123(torrent.LatestAdded),
|
||||
file.Link,
|
||||
))
|
||||
}
|
||||
|
||||
resp := &dav.MultiStatus{
|
||||
XMLNS: "DAV:",
|
||||
Response: responses,
|
||||
}
|
||||
return xml.Marshal(resp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user