32 lines
670 B
Go
32 lines
670 B
Go
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
|
|
}
|