Finalize repair
This commit is contained in:
@@ -1,278 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"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/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
func HandleGetRequest(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 {
|
||||
HandleDirectoryListing(w, r, t, c, cache)
|
||||
return
|
||||
}
|
||||
if data, exists := cache.Get(requestPath); exists {
|
||||
http.Redirect(w, r, data, http.StatusFound)
|
||||
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 (get)", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if file.Link == "" {
|
||||
log.Println("Link not found (get)", filename)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
link := file.Link
|
||||
|
||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
||||
}
|
||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if resp == nil {
|
||||
log.Println("Cannot unrestrict link", link, filenameV2)
|
||||
// t.HideTheFile(torrent, file)
|
||||
// http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
http.Redirect(w, r, "https://send.nukes.wtf/tDeTd0", http.StatusFound)
|
||||
return
|
||||
}
|
||||
if resp.Filename != filenameV2 {
|
||||
actualExt := filepath.Ext(resp.Filename)
|
||||
expectedExt := filepath.Ext(filenameV2)
|
||||
if actualExt != expectedExt {
|
||||
log.Println("File extension mismatch", resp.Filename, filenameV2)
|
||||
} else {
|
||||
log.Println("Filename mismatch", resp.Filename, filenameV2)
|
||||
}
|
||||
}
|
||||
cache.Add(requestPath, resp.Download)
|
||||
http.Redirect(w, r, resp.Download, http.StatusFound)
|
||||
}
|
||||
|
||||
// getFile finds a link by a fragment, it might be wrong
|
||||
func getFile(torrents []torrent.Torrent, filename, fragment string) (*torrent.Torrent, *torrent.File) {
|
||||
for t := range torrents {
|
||||
for f, file := range torrents[t].SelectedFiles {
|
||||
fname := filepath.Base(file.Path)
|
||||
if filename == fname && strings.Contains(file.Link, fragment) {
|
||||
return &torrents[t], &torrents[t].SelectedFiles[f]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
|
||||
if data, exists := cache.Get(requestPath); exists {
|
||||
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, data)
|
||||
return
|
||||
}
|
||||
|
||||
var output *string
|
||||
var err error
|
||||
|
||||
filteredSegments := removeEmptyStrings(strings.Split(requestPath, "/"))
|
||||
switch {
|
||||
case len(filteredSegments) == 1:
|
||||
output, err = handleRoot(w, r, c)
|
||||
case len(filteredSegments) == 2:
|
||||
output, err = handleListOfTorrents(requestPath, w, r, t, c)
|
||||
case len(filteredSegments) == 3:
|
||||
output, err = handleSingleTorrent(requestPath, w, r, t)
|
||||
case len(filteredSegments) > 3:
|
||||
HandleGetRequest(w, r, t, c, cache)
|
||||
default:
|
||||
writeHTTPError(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error processing request: %v\n", err)
|
||||
writeHTTPError(w, "Server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if output != nil {
|
||||
cache.Add(requestPath, *output)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, *output)
|
||||
}
|
||||
}
|
||||
|
||||
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) (*string, error) {
|
||||
htmlDoc := "<ul>"
|
||||
|
||||
for _, directory := range c.GetDirectories() {
|
||||
directoryPath := url.PathEscape(directory)
|
||||
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory)
|
||||
}
|
||||
|
||||
return &htmlDoc, nil
|
||||
}
|
||||
|
||||
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) (*string, error) {
|
||||
basePath := path.Base(requestPath)
|
||||
|
||||
for _, directory := range c.GetDirectories() {
|
||||
if basePath == directory {
|
||||
torrents := t.GetByDirectory(basePath)
|
||||
resp, err := createMultiTorrentResponse(requestPath, torrents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory (%s): %w", basePath, err)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("cannot find directory when generating list: %s", requestPath)
|
||||
}
|
||||
|
||||
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) (*string, error) {
|
||||
fullDir := path.Dir(requestPath)
|
||||
directory := path.Base(fullDir)
|
||||
torrentName := path.Base(requestPath)
|
||||
|
||||
sameNameTorrents := t.FindAllTorrentsWithName(directory, torrentName)
|
||||
if len(sameNameTorrents) == 0 {
|
||||
return nil, fmt.Errorf("cannot find directory when generating single torrent: %s", requestPath)
|
||||
}
|
||||
|
||||
resp, err := createSingleTorrentResponse(requestPath, sameNameTorrents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory (%s): %w", requestPath, err)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func writeHTTPError(w http.ResponseWriter, errorMessage string, statusCode int) {
|
||||
log.Println(errorMessage)
|
||||
http.Error(w, errorMessage, statusCode)
|
||||
}
|
||||
|
||||
func removeEmptyStrings(slice []string) []string {
|
||||
var result []string
|
||||
for _, s := range slice {
|
||||
if s != "" {
|
||||
result = append(result, s)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
101
internal/http/listing.go
Normal file
101
internal/http/listing.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||
)
|
||||
|
||||
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
|
||||
if data, exists := cache.Get(requestPath); exists {
|
||||
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, data)
|
||||
return
|
||||
}
|
||||
|
||||
var output *string
|
||||
var err error
|
||||
|
||||
filteredSegments := removeEmptySegments(strings.Split(requestPath, "/"))
|
||||
switch {
|
||||
case len(filteredSegments) == 1:
|
||||
output, err = handleRoot(w, r, c)
|
||||
case len(filteredSegments) == 2:
|
||||
output, err = handleListOfTorrents(requestPath, w, r, t, c)
|
||||
case len(filteredSegments) == 3:
|
||||
output, err = handleSingleTorrent(requestPath, w, r, t)
|
||||
default:
|
||||
log.Println("Not Found (http)", r.Method, requestPath, len(filteredSegments))
|
||||
writeHTTPError(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error processing request: %v\n", err)
|
||||
writeHTTPError(w, "Server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if output != nil {
|
||||
cache.Add(requestPath, *output)
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, *output)
|
||||
}
|
||||
}
|
||||
|
||||
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) (*string, error) {
|
||||
htmlDoc := "<ul>"
|
||||
|
||||
for _, directory := range c.GetDirectories() {
|
||||
directoryPath := url.PathEscape(directory)
|
||||
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory)
|
||||
}
|
||||
|
||||
return &htmlDoc, nil
|
||||
}
|
||||
|
||||
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) (*string, error) {
|
||||
basePath := path.Base(requestPath)
|
||||
|
||||
for _, directory := range c.GetDirectories() {
|
||||
if basePath == directory {
|
||||
torrents := t.GetByDirectory(basePath)
|
||||
resp, err := createMultiTorrentResponse(requestPath, torrents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory (%s): %w", basePath, err)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("cannot find directory when generating list: %s", requestPath)
|
||||
}
|
||||
|
||||
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) (*string, error) {
|
||||
fullDir := path.Dir(requestPath)
|
||||
directory := path.Base(fullDir)
|
||||
torrentName := path.Base(requestPath)
|
||||
|
||||
sameNameTorrents := t.FindAllTorrentsWithName(directory, torrentName)
|
||||
if len(sameNameTorrents) == 0 {
|
||||
return nil, fmt.Errorf("cannot find directory when generating single torrent: %s", requestPath)
|
||||
}
|
||||
|
||||
resp, err := createSingleTorrentResponse(requestPath, sameNameTorrents)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read directory (%s): %w", requestPath, err)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
19
internal/http/util.go
Normal file
19
internal/http/util.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func writeHTTPError(w http.ResponseWriter, errorMessage string, statusCode int) {
|
||||
http.Error(w, errorMessage, statusCode)
|
||||
}
|
||||
|
||||
func removeEmptySegments(urlSegments []string) []string {
|
||||
var result []string
|
||||
for _, s := range urlSegments {
|
||||
if s != "" {
|
||||
result = append(result, s)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user