Use new router
This commit is contained in:
@@ -2,9 +2,7 @@ package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -13,45 +11,9 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
|
||||
var output *string
|
||||
var err error
|
||||
|
||||
filteredSegments := removeEmptySegments(strings.Split(requestPath, "/"))
|
||||
switch {
|
||||
case len(filteredSegments) == 1:
|
||||
output, err = handleRoot(t)
|
||||
case len(filteredSegments) == 2:
|
||||
output, err = handleListOfTorrents(requestPath, t, log)
|
||||
case len(filteredSegments) == 3:
|
||||
output, err = handleSingleTorrent(requestPath, t, log)
|
||||
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
|
||||
}
|
||||
|
||||
if output != nil {
|
||||
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, *output)
|
||||
}
|
||||
}
|
||||
|
||||
func handleRoot(t *torrent.TorrentManager) (*string, error) {
|
||||
func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
|
||||
htmlDoc := "<ol>"
|
||||
directories := t.DirectoryMap.Keys()
|
||||
directories := torMgr.DirectoryMap.Keys()
|
||||
sort.Strings(directories)
|
||||
for _, directory := range directories {
|
||||
if strings.HasPrefix(directory, "int__") {
|
||||
@@ -64,8 +26,7 @@ func handleRoot(t *torrent.TorrentManager) (*string, error) {
|
||||
return &htmlDoc, nil
|
||||
}
|
||||
|
||||
func handleListOfTorrents(requestPath string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
||||
directory := path.Base(requestPath)
|
||||
func HandleListTorrents(directory string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
||||
torrents, ok := t.DirectoryMap.Get(directory)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot find directory %s", directory)
|
||||
@@ -85,7 +46,7 @@ func handleListOfTorrents(requestPath string, t *torrent.TorrentManager, log *za
|
||||
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)
|
||||
htmlDoc = htmlDoc + fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", filepath.Join(directory, url.PathEscape(tor.AccessKey)), tor.AccessKey)
|
||||
}
|
||||
return &htmlDoc, nil
|
||||
} else {
|
||||
@@ -94,20 +55,18 @@ func handleListOfTorrents(requestPath string, t *torrent.TorrentManager, log *za
|
||||
}
|
||||
}
|
||||
|
||||
func handleSingleTorrent(requestPath string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
||||
directory := path.Base(path.Dir(requestPath))
|
||||
func HandleListFiles(directory, torrentName string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
||||
torrents, ok := t.DirectoryMap.Get(directory)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot find directory %s", directory)
|
||||
}
|
||||
accessKey := path.Base(requestPath)
|
||||
tor, ok := torrents.Get(accessKey)
|
||||
tor, ok := torrents.Get(torrentName)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
|
||||
return nil, fmt.Errorf("cannot find torrent %s", torrentName)
|
||||
}
|
||||
|
||||
if resp, ok := t.ResponseCache.Get(directory + "/" + accessKey + ".html"); !ok {
|
||||
log.Debugf("Generating html for torrent %s", accessKey)
|
||||
if resp, ok := t.ResponseCache.Get(directory + "/" + torrentName + ".html"); !ok {
|
||||
log.Debugf("Generating html for torrent %s", torrentName)
|
||||
htmlDoc := "<ol>"
|
||||
filenames := tor.SelectedFiles.Keys()
|
||||
sort.Strings(filenames)
|
||||
@@ -116,8 +75,8 @@ func handleSingleTorrent(requestPath string, t *torrent.TorrentManager, log *zap
|
||||
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(directory, torrentName, url.PathEscape(filename))
|
||||
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", filePath, filename)
|
||||
}
|
||||
return &htmlDoc, nil
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user