Skip to content

Commit

Permalink
Merge pull request #134 from sonroyaalmerol/revert-download
Browse files Browse the repository at this point in the history
Revert downloader change
  • Loading branch information
sonroyaalmerol authored Aug 26, 2024
2 parents eee55d7 + f274ab2 commit 330fd71
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 50 deletions.
61 changes: 17 additions & 44 deletions m3u/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -114,66 +112,41 @@ func downloadM3UToBuffer(m3uURL string, buffer *bytes.Buffer) (err error) {
utils.SafeLogPrintf(nil, &m3uURL, "[DEBUG] Downloading M3U from: %s\n", m3uURL)
}

var tempFilePath string
var file io.Reader

if strings.HasPrefix(m3uURL, "file://") {
// Handle local file directly
localPath := strings.TrimPrefix(m3uURL, "file://")
utils.SafeLogPrintf(nil, &localPath, "[DEBUG] Reading M3U from local file: %s\n", localPath)
tempFilePath = localPath
} else {
// Create temporary file path
fileName := filepath.Base(m3uURL)
tempFilePath = fmt.Sprintf("/tmp/%s.m3u-incomplete", fileName)

// Make HTTP request to download the file
resp, err := http.Get(m3uURL)
if err != nil {
return fmt.Errorf("HTTP GET error: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Failed to download M3U: HTTP status %d", resp.StatusCode)
}
utils.SafeLogPrintf(nil, &localPath, "Reading M3U from local file: %s\n", localPath)

// Create temporary file
tempFile, err := os.Create(tempFilePath)
localFile, err := os.Open(localPath)
if err != nil {
return fmt.Errorf("Error creating temp file: %v", err)
return fmt.Errorf("Error opening file: %v", err)
}
defer tempFile.Close()
defer localFile.Close()

// Download to the temporary file
_, err = io.Copy(tempFile, resp.Body)
if err != nil {
return fmt.Errorf("Error writing to temp file: %v", err)
}

// Rename the file to remove the "-incomplete" suffix
finalFilePath := strings.TrimSuffix(tempFilePath, "-incomplete")
err = os.Rename(tempFilePath, finalFilePath)
file = localFile
} else {
utils.SafeLogPrintf(nil, &m3uURL, "Downloading M3U from URL: %s\n", m3uURL)
resp, err := utils.CustomHttpRequest("GET", m3uURL)
if err != nil {
return fmt.Errorf("Error renaming temp file: %v", err)
return fmt.Errorf("HTTP GET error: %v", err)
}

tempFilePath = finalFilePath
}
defer func() {
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()

// Read the final file into the buffer
file, err := os.Open(tempFilePath)
if err != nil {
return fmt.Errorf("Error opening file: %v", err)
file = resp.Body
}
defer file.Close()

_, err = io.Copy(buffer, file)
if err != nil {
return fmt.Errorf("Error reading file to buffer: %v", err)
return fmt.Errorf("Error reading file: %v", err)
}

if debug {
log.Println("[DEBUG] Successfully read M3U content into buffer")
log.Println("[DEBUG] Successfully copied M3U content to buffer")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion stream_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func loadBalancer(stream database.StreamInfo, previous *[]int, method string) (*

allSkipped = false // At least one URL is not skipped

resp, err := utils.CustomHttpRequest(method, url, "")
resp, err := utils.CustomHttpRequest(method, url)
if err == nil {
if debug {
utils.SafeLogPrintf(nil, &url, "[DEBUG] Successfully fetched stream from %s\n", url)
Expand Down
6 changes: 1 addition & 5 deletions utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package utils

import "net/http"

func CustomHttpRequest(method string, url string, rangeHeader string) (*http.Response, error) {
func CustomHttpRequest(method string, url string) (*http.Response, error) {
userAgent := GetEnv("USER_AGENT")

// Create a new HTTP client with a custom User-Agent header
Expand All @@ -15,10 +15,6 @@ func CustomHttpRequest(method string, url string, rangeHeader string) (*http.Res

req.Header.Set("User-Agent", userAgent)

if rangeHeader != "" {
req.Header.Set("Range", rangeHeader)
}

resp, err := client.Do(req)
if err != nil {
return nil, err
Expand Down

0 comments on commit 330fd71

Please sign in to comment.