Files
zurg/internal/universal/downloader.go
Ben Adrian Sarmiento 7fc8a4e0c5 Bandwidth tracking
2024-06-25 00:12:53 +02:00

279 lines
8.3 KiB
Go

package universal
import (
"context"
"fmt"
"io"
"net/http"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/debridmediamanager/zurg/internal/config"
intTor "github.com/debridmediamanager/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/panjf2000/ants/v2"
)
type Downloader struct {
client *zurghttp.HTTPClient
RequestedBytes atomic.Uint64
TotalBytes atomic.Uint64
}
func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Downloader {
dl := &Downloader{
client: client,
}
// track bandwidth usage and reset at 12AM CET
now := time.Now()
tomorrow := now.AddDate(0, 0, 1)
cetTZ, err := time.LoadLocation("CET")
if err != nil {
cetTZ = time.FixedZone("CET", 1*60*60)
}
nextMidnightInCET := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 0, 0, 0, 0, cetTZ)
duration := nextMidnightInCET.Sub(now)
timer := time.NewTimer(duration)
workerPool.Submit(func() {
<-timer.C
ticker := time.NewTicker(24 * time.Hour)
for {
dl.RequestedBytes.Store(0)
dl.TotalBytes.Store(0)
<-ticker.C
}
})
return dl
}
// DownloadFile handles a GET request for files in torrents
func (dl *Downloader) DownloadFile(
directory,
torrentName,
fileName string,
resp http.ResponseWriter,
req *http.Request,
torMgr *intTor.TorrentManager,
cfg config.ConfigInterface,
log *logutil.Logger,
) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
log.Warnf("Cannot find directory %s", directory)
http.Error(resp, "File not found", http.StatusNotFound)
return
}
torrent, ok := torrents.Get(torrentName)
if !ok {
log.Warnf("Cannot find torrent %sfrom path %s", torrentName, req.URL.Path)
http.Error(resp, "File not found", http.StatusNotFound)
return
}
file, ok := torrent.SelectedFiles.Get(fileName)
if !ok || !file.State.Is("ok_file") {
// log.Warnf("Cannot find file %s from path %s", fileName, req.URL.Path)
http.Error(resp, "File not found", http.StatusNotFound)
return
}
if !file.State.Is("ok_file") {
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
unrestrict, err := torMgr.UnrestrictFile(file, cfg.ShouldServeFromRclone())
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
log.Warnf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusLocked)
return
}
if err != nil {
log.Errorf("Error unrestricting file %s: %v", file.Path, err)
if file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
if unrestrict.Filesize != file.Bytes {
// this is possible if there's only 1 streamable file in the torrent
// and then suddenly it's a rar file
actualExt := strings.ToLower(filepath.Ext(unrestrict.Filename))
expectedExt := strings.ToLower(filepath.Ext(fileName))
if actualExt != expectedExt && unrestrict.Streamable != 1 {
log.Warnf("File was changed and is not streamable: %s and %s (link=%s)", fileName, unrestrict.Filename, unrestrict.Link)
} else {
log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename)
}
}
if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download)
} else {
dl.streamFileToResponse(torrent, file, unrestrict, resp, req, torMgr, cfg, log)
}
}
// DownloadLink handles a GET request for downloads
func (dl *Downloader) DownloadLink(
fileName,
link string,
resp http.ResponseWriter,
req *http.Request,
torMgr *intTor.TorrentManager,
cfg config.ConfigInterface,
log *logutil.Logger,
) {
// log.Debugf("Opening file %s (%s)", fileName, link)
unrestrict, err := torMgr.UnrestrictLink(link, cfg.ShouldServeFromRclone())
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
log.Warnf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "Link is not available (bandwidth limit reached)", http.StatusLocked)
return
}
if err != nil {
log.Errorf("Error unrestricting link %s: %v", link, err)
http.Error(resp, "File is not available", http.StatusInternalServerError)
return
}
if cfg.ShouldServeFromRclone() {
redirect(resp, req, unrestrict.Download)
} else {
dl.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log)
}
}
func (dl *Downloader) streamFileToResponse(
torrent *intTor.Torrent,
file *intTor.File,
unrestrict *realdebrid.Download,
resp http.ResponseWriter,
req *http.Request,
torMgr *intTor.TorrentManager,
cfg config.ConfigInterface,
log *logutil.Logger,
) {
// Create a new request for the file download
dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil)
if err != nil {
if file != nil {
log.Errorf("Error creating new request for file %s: %v", file.Path, err)
}
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
// Add the range header if it exists
if req.Header.Get("Range") != "" {
dlReq.Header.Add("Range", req.Header.Get("Range"))
// log.Debugf("Serving file %s: %s", unrestrict.Filename, req.Header.Get("Range"))
}
// Perform the request
downloadResp, err := dl.client.Do(dlReq)
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
log.Warnf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusLocked)
return
}
if err != nil {
log.Warnf("Cannot download file %s: %v", unrestrict.Download, err)
if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
defer downloadResp.Body.Close()
// Check if the download was not successful
if downloadResp.StatusCode != http.StatusOK && downloadResp.StatusCode != http.StatusPartialContent {
log.Warnf("Received a %s status code for file %s", downloadResp.Status, unrestrict.Filename)
if file != nil && file.State.Event(context.Background(), "break_file") == nil {
torMgr.EnqueueForRepair(torrent)
}
http.Error(resp, "File is not available", http.StatusNotFound)
return
}
// Copy the content-range header from the download response to the response
if cr := downloadResp.Header.Get("Content-Range"); cr != "" {
resp.Header().Set("Content-Range", cr)
}
buf := make([]byte, cfg.GetNetworkBufferSize())
n, _ := io.CopyBuffer(resp, downloadResp.Body, buf)
// Update the download statistics
reqBytes, _ := parseRangeHeader(req.Header.Get("Range"))
if reqBytes == 0 && unrestrict != nil {
reqBytes = uint64(unrestrict.Filesize)
}
dl.RequestedBytes.Add(reqBytes)
dl.TotalBytes.Add(uint64(n))
if cfg.ShouldLogRequests() {
log.Debugf("Served %d MB of the requested %d MB of file %s (range=%s)", bToMb(uint64(n)), bToMb(reqBytes), unrestrict.Filename, req.Header.Get("Range"))
}
}
func redirect(resp http.ResponseWriter, req *http.Request, url string) {
http.Redirect(resp, req, url, http.StatusFound)
}
func parseRangeHeader(rangeHeader string) (uint64, error) {
if rangeHeader == "" { // Empty header means no range request
return 0, nil
}
if !strings.HasPrefix(rangeHeader, "bytes=") {
return 0, fmt.Errorf("invalid range header format")
}
parts := strings.SplitN(rangeHeader[6:], "-", 2) // [6:] removes "bytes="
if len(parts) != 2 {
return 0, fmt.Errorf("invalid range specification")
}
var start, end uint64
var err error
// Case 1: "bytes=100-" (from byte 100 to the end)
if parts[0] != "" {
start, err = strconv.ParseUint(parts[0], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid start value: %w", err)
}
}
// Case 2: "bytes=-200" (last 200 bytes)
if parts[1] != "" {
end, err = strconv.ParseUint(parts[1], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid end value: %w", err)
}
}
// Handle "bytes=500-100" (invalid range)
if start > end {
return 0, fmt.Errorf("invalid range: start cannot be greater than end")
}
// Calculate bytes to read
bytesToRead := end - start + 1 // +1 because ranges are inclusive
return bytesToRead, nil
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}