From 0b7bb7719eb17dd0083805aca62b1a0d48fc63d1 Mon Sep 17 00:00:00 2001 From: Sugam Panthi Date: Wed, 16 Apr 2025 20:58:12 -0500 Subject: [PATCH 1/2] Add test case for local image path in ImageToBase64Encoding function --- image_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/image_test.go b/image_test.go index bf81423..a956137 100644 --- a/image_test.go +++ b/image_test.go @@ -231,6 +231,12 @@ func TestImageToBase64Encoding(t *testing.T) { want: "data:image/png;base64,", wantErr: false, }, + { + name: "Local image full path", + imgURL: "/Users/vein/Downloads/wallpapers/PINK-FLOYD.png", + want: "data:image/png;base64,", + wantErr: false, + }, { name: "WebP image from Google", imgURL: "https://www.gstatic.com/webp/gallery/1.webp", From beb0e518b2d56f74c371d1f7b349ef4599d4f1b0 Mon Sep 17 00:00:00 2001 From: Sugam Panthi Date: Wed, 16 Apr 2025 20:58:17 -0500 Subject: [PATCH 2/2] Refactor ImageToBase64 function to validate web URLs using prefix check --- image.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/image.go b/image.go index 1851b07..7786dcc 100644 --- a/image.go +++ b/image.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "net/http" - "net/url" "os" "path/filepath" "strings" @@ -196,9 +195,8 @@ func ImageToBase64(imageURL string) (string, error) { return "", fmt.Errorf("unsupported image format: %s", ext) } - //Check if the imageURL is a valid URL or a path to a local file - if _, err := url.ParseRequestURI(imageURL); err == nil { - // If it's a valid URL, download the image and convert it to base64 + // Check if the imageURL is a web URL by looking for http(s) prefix + if strings.HasPrefix(imageURL, "http://") || strings.HasPrefix(imageURL, "https://") { return handleImageFromURL(imageURL) }