first commit
This commit is contained in:
31
utils/encoder.go
Normal file
31
utils/encoder.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// mp3ToBase64 reads an MP3 file and returns its base64-encoded content
|
||||
func Mp3ToBase64(filePath string) (string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open MP3 file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Verify file is an MP3
|
||||
if !strings.HasSuffix(strings.ToLower(filePath), ".mp3") {
|
||||
return "", fmt.Errorf("file is not an MP3: %s", filePath)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read MP3 file: %v", err)
|
||||
}
|
||||
|
||||
encoded := base64.StdEncoding.EncodeToString(data)
|
||||
return encoded, nil
|
||||
}
|
66
utils/encoder_test.go
Normal file
66
utils/encoder_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.savin.nyc/alex/go-iar-notificator/utils"
|
||||
)
|
||||
|
||||
func TestMp3ToBase64_Success(t *testing.T) {
|
||||
// Create a temporary MP3 file
|
||||
tempDir := t.TempDir()
|
||||
mp3File := filepath.Join(tempDir, "test.mp3")
|
||||
testData := "fake mp3 data"
|
||||
err := os.WriteFile(mp3File, []byte(testData), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp file: %v", err)
|
||||
}
|
||||
|
||||
// Test the function
|
||||
result, err := utils.Mp3ToBase64(mp3File)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
// Verify the result is base64 encoded
|
||||
expected := "ZmFrZSBtcDMgZGF0YQ==" // base64 of "fake mp3 data"
|
||||
if result != expected {
|
||||
t.Errorf("Expected %s, got %s", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMp3ToBase64_FileNotFound(t *testing.T) {
|
||||
_, err := utils.Mp3ToBase64("/nonexistent/file.mp3")
|
||||
if err == nil {
|
||||
t.Error("Expected error for non-existent file, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "failed to open MP3 file") {
|
||||
t.Errorf("Expected error message to contain 'failed to open MP3 file', got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMp3ToBase64_NotMP3(t *testing.T) {
|
||||
// Create a temporary non-MP3 file
|
||||
tempDir := t.TempDir()
|
||||
txtFile := filepath.Join(tempDir, "test.txt")
|
||||
err := os.WriteFile(txtFile, []byte("text data"), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp file: %v", err)
|
||||
}
|
||||
|
||||
_, err = utils.Mp3ToBase64(txtFile)
|
||||
if err == nil {
|
||||
t.Error("Expected error for non-MP3 file, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "file is not an MP3") {
|
||||
t.Errorf("Expected error message to contain 'file is not an MP3', got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMp3ToBase64_ReadError(t *testing.T) {
|
||||
// This is harder to test without mocking, but we can assume the file creation/read works as above
|
||||
// For now, the success test covers the happy path
|
||||
}
|
Reference in New Issue
Block a user