Optimizations
This commit is contained in:
@@ -22,7 +22,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
||||
pathSegments := strings.Split(requestPath, "/")
|
||||
|
||||
// Remove empty segments caused by leading or trailing slashes
|
||||
filteredSegments := make([]string, 0, len(pathSegments))
|
||||
filteredSegments := pathSegments[:0]
|
||||
for _, segment := range pathSegments {
|
||||
if segment != "" {
|
||||
filteredSegments = append(filteredSegments, segment)
|
||||
@@ -30,36 +30,35 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
||||
}
|
||||
|
||||
switch len(filteredSegments) {
|
||||
case 0: // Just the root "/"
|
||||
case 0:
|
||||
output, err = handleRoot(w, r, c)
|
||||
case 1: // It's just the basedir e.g. "/basedir"
|
||||
case 1:
|
||||
output, err = handleListOfTorrents(requestPath, w, r, t, c)
|
||||
case 2: // It's a specific torrent under a basedir e.g. "/basedir/torrentname/"
|
||||
case 2:
|
||||
output, err = handleSingleTorrent(requestPath, w, r, t)
|
||||
default:
|
||||
// Handle any other paths, e.g., send a 404 Not Found response
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Cannot marshal xml: %v\n", err)
|
||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||
log.Printf("Error processing request: %v\n", err)
|
||||
http.Error(w, "Server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if output != nil {
|
||||
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
|
||||
w.WriteHeader(http.StatusMultiStatus)
|
||||
fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n%s\n", output)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// handleRoot handles a PROPFIND request to the root directory
|
||||
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) ([]byte, error) {
|
||||
var responses []dav.Response
|
||||
responses = append(responses, dav.Directory("/"))
|
||||
for _, directory := range c.GetDirectories() {
|
||||
responses = append(responses, dav.Directory(fmt.Sprintf("/%s", directory)))
|
||||
responses = append(responses, dav.Directory("/"+directory))
|
||||
}
|
||||
rootResponse := dav.MultiStatus{
|
||||
XMLNS: "DAV:",
|
||||
@@ -68,45 +67,40 @@ func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface
|
||||
return xml.MarshalIndent(rootResponse, "", " ")
|
||||
}
|
||||
|
||||
// handleListOfTorrents handles a PROPFIND request to the base directory
|
||||
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) ([]byte, error) {
|
||||
basePath := path.Base(requestPath)
|
||||
|
||||
found := false
|
||||
for _, directory := range c.GetDirectories() {
|
||||
directories := c.GetDirectories()
|
||||
for _, directory := range directories {
|
||||
if basePath == directory {
|
||||
found = true
|
||||
torrents := t.GetByDirectory(basePath)
|
||||
resp, err := createMultiTorrentResponse("/"+basePath, torrents)
|
||||
if err != nil {
|
||||
log.Printf("Cannot read directory (%s): %v\n", basePath, err)
|
||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||
return nil, nil
|
||||
}
|
||||
return xml.MarshalIndent(resp, "", " ")
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
log.Println("Cannot find directory when generating list", requestPath)
|
||||
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
torrents := t.GetByDirectory(basePath)
|
||||
resp, err := createMultiTorrentResponse(fmt.Sprintf("/%s", basePath), torrents)
|
||||
if err != nil {
|
||||
log.Printf("Cannot read directory (%s): %v\n", basePath, err)
|
||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||
return nil, nil
|
||||
}
|
||||
return xml.MarshalIndent(resp, "", " ")
|
||||
log.Println("Cannot find directory when generating list", requestPath)
|
||||
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// handleSingleTorrent handles a PROPFIND request to a single torrent directory
|
||||
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
|
||||
directory := strings.TrimPrefix(path.Dir(requestPath), "/")
|
||||
|
||||
torrentName := path.Base(requestPath)
|
||||
|
||||
sameNameTorrents := findAllTorrentsWithName(t, directory, torrentName)
|
||||
if len(sameNameTorrents) == 0 {
|
||||
log.Println("Cannot find directory when generating single torrent", requestPath)
|
||||
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||
return nil, nil
|
||||
}
|
||||
var resp *dav.MultiStatus
|
||||
resp, err := createSingleTorrentResponse(fmt.Sprintf("/%s", directory), sameNameTorrents, t)
|
||||
|
||||
resp, err := createSingleTorrentResponse("/"+directory, sameNameTorrents, t)
|
||||
if err != nil {
|
||||
log.Printf("Cannot read directory (%s): %v\n", requestPath, err)
|
||||
http.Error(w, "Cannot read directory", http.StatusInternalServerError)
|
||||
|
||||
Reference in New Issue
Block a user