Cache for directories and torrents
This commit is contained in:
@@ -83,7 +83,7 @@ func handleDeleteFile(w http.ResponseWriter, segments []string, t *torrent.Torre
|
||||
}
|
||||
|
||||
file.Link = "unselect"
|
||||
t.SetNewLatestState(torrent.EmptyState())
|
||||
t.ScheduleForRefresh()
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -74,18 +75,27 @@ func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.To
|
||||
return nil, fmt.Errorf("cannot find directory %s", basePath)
|
||||
}
|
||||
|
||||
resp, _ := t.ResponseCache.Get(basePath + ".dav")
|
||||
davDoc := resp.(string)
|
||||
|
||||
return &davDoc, nil
|
||||
if resp, ok := t.ResponseCache.Get(basePath + ".dav"); !ok {
|
||||
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
|
||||
davDoc += dav.Directory("", "")
|
||||
directories := t.DirectoryMap.Keys()
|
||||
sort.Strings(directories)
|
||||
for _, directory := range directories {
|
||||
davDoc += dav.Directory(directory, "")
|
||||
}
|
||||
davDoc += "</d:multistatus>"
|
||||
return &davDoc, nil
|
||||
} else {
|
||||
davDoc := resp.(*string)
|
||||
return davDoc, nil
|
||||
}
|
||||
}
|
||||
|
||||
func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) (*string, error) {
|
||||
requestPath = strings.Trim(requestPath, "/")
|
||||
basePath := path.Base(path.Dir(requestPath))
|
||||
torrents, ok := t.DirectoryMap.Get(basePath)
|
||||
directory := path.Base(path.Dir(requestPath))
|
||||
torrents, ok := t.DirectoryMap.Get(directory)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot find directory %s", basePath)
|
||||
return nil, fmt.Errorf("cannot find directory %s", directory)
|
||||
}
|
||||
accessKey := path.Base(requestPath)
|
||||
tor, ok := torrents.Get(accessKey)
|
||||
@@ -93,18 +103,21 @@ func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.Torre
|
||||
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
|
||||
}
|
||||
|
||||
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(requestPath, tor.LatestAdded)
|
||||
|
||||
filenames := tor.SelectedFiles.Keys()
|
||||
sort.Strings(filenames)
|
||||
for _, filename := range filenames {
|
||||
file, _ := tor.SelectedFiles.Get(filename)
|
||||
if file == nil || !strings.HasPrefix(file.Link, "http") {
|
||||
continue
|
||||
if resp, ok := t.ResponseCache.Get(directory + "/" + accessKey + ".dav"); !ok {
|
||||
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded)
|
||||
filenames := tor.SelectedFiles.Keys()
|
||||
sort.Strings(filenames)
|
||||
for _, filename := range filenames {
|
||||
file, _ := tor.SelectedFiles.Get(filename)
|
||||
if file == nil || !strings.HasPrefix(file.Link, "http") {
|
||||
continue
|
||||
}
|
||||
davDoc += dav.File(filename, file.Bytes, file.Ended)
|
||||
}
|
||||
davDoc += dav.File(filename, file.Bytes, file.Ended)
|
||||
davDoc += "</d:multistatus>"
|
||||
return &davDoc, nil
|
||||
} else {
|
||||
davDoc := resp.(*string)
|
||||
return davDoc, nil
|
||||
}
|
||||
|
||||
davDoc += "</d:multistatus>"
|
||||
return &davDoc, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user