Cache for directories and torrents

This commit is contained in:
Ben Sarmiento
2023-11-30 03:16:10 +01:00
parent 253b92a3b6
commit 5914af80fd
6 changed files with 226 additions and 177 deletions

View File

@@ -65,23 +65,39 @@ func handleRoot(t *torrent.TorrentManager) (*string, error) {
}
func handleListOfTorrents(requestPath string, t *torrent.TorrentManager) (*string, error) {
basePath := path.Base(requestPath)
_, ok := t.DirectoryMap.Get(basePath)
directory := path.Base(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)
}
resp, _ := t.ResponseCache.Get(basePath + ".html")
htmlDoc := resp.(string)
return &htmlDoc, nil
if resp, ok := t.ResponseCache.Get(directory + ".html"); !ok {
htmlDoc := "<ol>"
var allTorrents []*torrent.Torrent
torrents.IterCb(func(_ string, tor *torrent.Torrent) {
if tor.AllInProgress() {
return
}
allTorrents = append(allTorrents, tor)
})
sort.Slice(allTorrents, func(i, j int) bool {
return allTorrents[i].AccessKey < allTorrents[j].AccessKey
})
for _, tor := range allTorrents {
htmlDoc = htmlDoc + fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", filepath.Join(requestPath, url.PathEscape(tor.AccessKey)), tor.AccessKey)
}
return &htmlDoc, nil
} else {
htmlDoc := resp.(*string)
return htmlDoc, nil
}
}
func handleSingleTorrent(requestPath string, t *torrent.TorrentManager) (*string, error) {
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)
@@ -89,18 +105,21 @@ func handleSingleTorrent(requestPath string, t *torrent.TorrentManager) (*string
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
}
htmlDoc := "<ol>"
filenames := tor.SelectedFiles.Keys()
sort.Strings(filenames)
for _, filename := range filenames {
file, _ := tor.SelectedFiles.Get(filename)
if file == nil || !strings.HasPrefix(file.Link, "http") {
// will be caught by torrent manager's repairAll
// just skip it for now
continue
if resp, ok := t.ResponseCache.Get(directory + "/" + accessKey + ".html"); !ok {
htmlDoc := "<ol>"
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
}
filePath := filepath.Join(requestPath, url.PathEscape(filename))
htmlDoc += fmt.Sprintf("<li><a href=\"%s\">%s</a></li>", filePath, filename)
}
filePath := filepath.Join(requestPath, url.PathEscape(filename))
htmlDoc += fmt.Sprintf("<li><a href=\"%s\">%s</a></li>", filePath, filename)
return &htmlDoc, nil
} else {
htmlDoc := resp.(*string)
return htmlDoc, nil
}
return &htmlDoc, nil
}