Finalize repair
This commit is contained in:
94
internal/universal/head.go
Normal file
94
internal/universal/head.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package universal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/davextra"
|
||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||
)
|
||||
|
||||
func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||
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.Println("Method not implemented", r.Method, r.URL.Path)
|
||||
http.Error(w, "Method not implemented", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
if data, exists := cache.Get("head:" + requestPath); exists {
|
||||
splits := strings.Split(data, " ")
|
||||
contentType := splits[0]
|
||||
contentLength := splits[1]
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Content-Length", contentLength)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
baseDirectory := segments[len(segments)-3]
|
||||
torrentName := segments[len(segments)-2]
|
||||
filename := segments[len(segments)-1]
|
||||
|
||||
torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName)
|
||||
if torrents == nil {
|
||||
log.Println("Cannot find torrent", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
||||
_, file := getFile(torrents, filenameV2, linkFragment)
|
||||
if file == nil {
|
||||
log.Println("Cannot find file (head)", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if file.Link == "" {
|
||||
log.Println("Link not found (head)", filename)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
contentType := getContentMimeType(filename)
|
||||
contentLength := fmt.Sprintf("%d", file.Bytes)
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Content-Length", contentLength)
|
||||
cache.Add("head:"+requestPath, contentType+" "+contentLength)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func getContentMimeType(filePath string) string {
|
||||
switch filepath.Ext(filePath) {
|
||||
case ".mkv":
|
||||
return "video/x-matroska"
|
||||
case ".mp4":
|
||||
return "video/mp4"
|
||||
case ".avi":
|
||||
return "video/x-msvideo"
|
||||
case ".mp3":
|
||||
return "audio/mpeg"
|
||||
case ".rar":
|
||||
return "application/x-rar-compressed"
|
||||
case ".zip":
|
||||
return "application/zip"
|
||||
case ".7z":
|
||||
return "application/x-7z-compressed"
|
||||
case ".srt":
|
||||
return "text/plain"
|
||||
default:
|
||||
return "application/octet-stream"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user