feat(ui): add audio upload button in chat view

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto 2025-05-29 22:42:08 +02:00
parent 0870bf5af6
commit dbee564f28
5 changed files with 201 additions and 180 deletions

View file

@ -5,14 +5,19 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"
"github.com/rs/zerolog/log"
)
var base64DownloadClient http.Client = http.Client{
Timeout: 30 * time.Second,
}
var dataURIPattern = regexp.MustCompile(`^data:([^;]+);base64,`)
// GetContentURIAsBase64 checks if the string is an URL, if it's an URL downloads the content in memory encodes it in base64 and returns the base64 string, otherwise returns the string by stripping base64 data headers
func GetContentURIAsBase64(s string) (string, error) {
if strings.HasPrefix(s, "http") {
@ -36,12 +41,11 @@ func GetContentURIAsBase64(s string) (string, error) {
return encoded, nil
}
// if the string instead is prefixed with "data:image/...;base64,", drop it
dropPrefix := []string{"data:image/jpeg;base64,", "data:image/png;base64,"}
for _, prefix := range dropPrefix {
if strings.HasPrefix(s, prefix) {
return strings.ReplaceAll(s, prefix, ""), nil
}
// Match any data URI prefix pattern
if match := dataURIPattern.FindString(s); match != "" {
log.Debug().Msgf("Found data URI prefix: %s", match)
return strings.Replace(s, match, "", 1), nil
}
return "", fmt.Errorf("not valid string")
return "", fmt.Errorf("not valid base64 data type string")
}