From 7772064d87468d62c7f7afee20a033328c07080d Mon Sep 17 00:00:00 2001 From: "Laisky.Cai" Date: Mon, 1 Jan 2024 17:38:35 +0800 Subject: [PATCH] fix: support base64 encoded image_url (#872) - Add support for base64 encoded image in OpenAI's image_url Co-authored-by: JustSong <39998050+songquanpeng@users.noreply.github.com> --- common/image/image.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/image/image.go b/common/image/image.go index 27045d2819..de8fefd3f7 100644 --- a/common/image/image.go +++ b/common/image/image.go @@ -15,6 +15,9 @@ import ( _ "golang.org/x/image/webp" ) +// Regex to match data URL pattern +var dataURLPattern = regexp.MustCompile(`data:image/([^;]+);base64,(.*)`) + func IsImageUrl(url string) (bool, error) { resp, err := http.Head(url) if err != nil { @@ -44,9 +47,6 @@ func GetImageSizeFromUrl(url string) (width int, height int, err error) { } func GetImageFromUrl(url string) (mimeType string, data string, err error) { - // Regex to match data URL pattern - dataURLPattern := regexp.MustCompile(`data:image/([^;]+);base64,(.*)`) - // Check if the URL is a data URL matches := dataURLPattern.FindStringSubmatch(url) if len(matches) == 3 {