diff --git a/m3u/parser.go b/m3u/parser.go index ab99363d..f0069fd3 100644 --- a/m3u/parser.go +++ b/m3u/parser.go @@ -6,9 +6,7 @@ import ( "fmt" "io" "log" - "net/http" "os" - "path/filepath" "regexp" "strconv" "strings" @@ -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 diff --git a/stream_handler.go b/stream_handler.go index 6705b3b6..8be2e628 100644 --- a/stream_handler.go +++ b/stream_handler.go @@ -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) diff --git a/utils/http.go b/utils/http.go index 82745f01..5b53354a 100644 --- a/utils/http.go +++ b/utils/http.go @@ -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 @@ -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