package universal import ( "fmt" "net/http" "path/filepath" "strings" "github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/pkg/logutil" ) func HandleHeadRequest(directory, torrentName, fileName string, w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *logutil.Logger) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { log.Warnf("Cannot find directory %s", directory) http.Error(w, "File not found", http.StatusNotFound) return } torrent, ok := torrents.Get(torrentName) if !ok { 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) if file == nil { 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) http.Error(w, "Cannot find file", http.StatusNotFound) return } contentType := getContentMimeType(fileName) contentLength := fmt.Sprintf("%d", file.Bytes) lastModified := file.Ended w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Length", contentLength) w.Header().Set("Last-Modified", lastModified) 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" default: return "application/octet-stream" } }