Use new router
This commit is contained in:
@@ -3,7 +3,6 @@ package universal
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -11,55 +10,34 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
SPLIT_TOKEN = "$"
|
||||
)
|
||||
func HandleHeadRequest(directory, torrentName, fileName string, w http.ResponseWriter, req *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
|
||||
|
||||
func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
requestPath = strings.Replace(requestPath, "/http", "", 1)
|
||||
if requestPath == "/favicon.ico" {
|
||||
return
|
||||
}
|
||||
|
||||
segments := strings.Split(requestPath, "/")
|
||||
// If there are less than 3 segments, return an error or adjust as needed
|
||||
if len(segments) < 4 {
|
||||
log.Errorf("Request %s %s not supported yet", r.Method, r.URL.Path)
|
||||
http.Error(w, "Method not implemented", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
baseDirectory := segments[len(segments)-3]
|
||||
accessKey := segments[len(segments)-2]
|
||||
filename := segments[len(segments)-1]
|
||||
|
||||
torrents, ok := t.DirectoryMap.Get(baseDirectory)
|
||||
torrents, ok := t.DirectoryMap.Get(directory)
|
||||
if !ok {
|
||||
log.Warnf("Cannot find directory %s", baseDirectory)
|
||||
log.Warnf("Cannot find directory %s", directory)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
torrent, ok := torrents.Get(accessKey)
|
||||
torrent, ok := torrents.Get(torrentName)
|
||||
if !ok {
|
||||
log.Warnf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
|
||||
log.Warnf("Cannot find torrent %s from path %s", torrentName, req.URL.Path)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
file, _ := torrent.SelectedFiles.Get(filename)
|
||||
file, _ := torrent.SelectedFiles.Get(fileName)
|
||||
if file == nil {
|
||||
log.Warnf("Cannot find file from path %s", requestPath)
|
||||
log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(file.Link, "http") {
|
||||
// This is a dead file, serve an alternate file
|
||||
log.Warnf("File %s is no longer available", filename)
|
||||
log.Warnf("File %s is no longer available", fileName)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
contentType := getContentMimeType(filename)
|
||||
contentType := getContentMimeType(fileName)
|
||||
contentLength := fmt.Sprintf("%d", file.Bytes)
|
||||
lastModified := file.Ended
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
|
||||
Reference in New Issue
Block a user