This commit is contained in:
Dmitrii Andreev
2025-04-07 11:32:14 +03:00
parent f64163d12a
commit 42aed9decb
6 changed files with 43 additions and 17 deletions

View File

@@ -35,6 +35,12 @@ func main() {
slog.Info("Starting icecast-ripper", "version", "0.1.0") // Updated version
// Validate essential config
if cfg.StreamURL == "" {
slog.Error("Configuration error: STREAM_URL must be set.")
os.Exit(1)
}
// Initialize database
db, err := database.InitDB(cfg.DatabasePath)
if err != nil {

View File

@@ -67,11 +67,5 @@ func LoadConfig() (*Config, error) {
return nil, err
}
// Ensure required fields are set
if config.StreamURL == "" {
// Consider returning an error or logging a fatal error if essential config is missing
// return nil, errors.New("STREAM_URL environment variable is required")
}
return &config, nil
}

View File

@@ -87,7 +87,11 @@ func (db *DB) GetRecordedFiles(limit int) ([]RecordedFile, error) {
if err != nil {
return nil, fmt.Errorf("failed to query recorded files: %w", err)
}
defer rows.Close()
defer func() {
if err := rows.Close(); err != nil {
slog.Error("Failed to close rows", "error", err)
}
}()
var files []RecordedFile
for rows.Next() {

View File

@@ -26,7 +26,11 @@ func GenerateFileHash(filePath string) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to open file %s for hashing: %w", filePath, err)
}
defer file.Close()
defer func() {
if err := file.Close(); err != nil {
slog.Error("Failed to close file", "path", filePath, "error", err)
}
}()
hasher := sha256.New()

View File

@@ -103,7 +103,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) {
if tempFilePath != "" {
if _, err := os.Stat(tempFilePath); err == nil {
slog.Warn("Removing leftover temporary file", "path", tempFilePath)
os.Remove(tempFilePath)
if err := os.Remove(tempFilePath); err != nil {
slog.Error("Failed to remove temporary file", "path", tempFilePath, "error", err)
}
}
}
}()
@@ -136,7 +138,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) {
} else {
slog.Error("Failed to download stream", "error", err)
// No need to keep the temp file if download failed unexpectedly
os.Remove(tempFilePath)
if err := os.Remove(tempFilePath); err != nil {
slog.Error("Failed to remove temporary file after download error", "path", tempFilePath, "error", err)
}
tempFilePath = "" // Prevent deferred cleanup from trying again
return
}
@@ -145,7 +149,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) {
// If no bytes were written (e.g., stream closed immediately or cancelled before data), discard.
if bytesWritten == 0 {
slog.Warn("No data written to temporary file, discarding.", "path", tempFilePath)
os.Remove(tempFilePath)
if err := os.Remove(tempFilePath); err != nil {
slog.Error("Failed to remove temporary file after no data written", "path", tempFilePath, "error", err)
}
tempFilePath = "" // Prevent deferred cleanup
return
}
@@ -157,7 +163,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) {
fileHash, err := hash.GenerateFileHash(tempFilePath)
if err != nil {
slog.Error("Failed to generate file hash", "path", tempFilePath, "error", err)
os.Remove(tempFilePath) // Clean up if hashing fails
if err := os.Remove(tempFilePath); err != nil {
slog.Error("Failed to remove temporary file after hash error", "path", tempFilePath, "error", err)
}
tempFilePath = "" // Prevent deferred cleanup
return
}
@@ -170,7 +178,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) {
// Move temporary file to final location
if err := os.Rename(tempFilePath, finalPath); err != nil {
slog.Error("Failed to move temporary file to final location", "temp", tempFilePath, "final", finalPath, "error", err)
os.Remove(tempFilePath) // Attempt cleanup of temp file
if err := os.Remove(tempFilePath); err != nil {
slog.Error("Failed to remove temporary file after move error", "path", tempFilePath, "error", err)
}
tempFilePath = "" // Prevent deferred cleanup
return
}
@@ -201,7 +211,11 @@ func (r *Recorder) downloadStream(ctx context.Context, streamURL string, writer
}
return 0, fmt.Errorf("failed to connect to stream: %w", err)
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
slog.Error("Failed to close response body", "error", err)
}
}()
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("unexpected status code: %s", resp.Status)

View File

@@ -44,7 +44,11 @@ func (c *Checker) IsLive() (bool, error) {
slog.Warn("Failed to connect to stream URL during check", "url", c.streamURL, "error", err)
return false, nil // Treat connection errors as 'not live'
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
slog.Error("Failed to close response body", "error", err)
}
}()
// A 200 OK status generally indicates the stream is live and broadcasting.
if resp.StatusCode == http.StatusOK {