mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-30 15:35:01 +00:00
rf: centralize base64 image handling (#2595)
contains simple fixes to warnings and errors, removes a broken / outdated test, runs go mod tidy, and as the actual change, centralizes base64 image handling Signed-off-by: Dave Lee <dave@gray101.com>
This commit is contained in:
parent
4156a4f15f
commit
12513ebae0
12 changed files with 60 additions and 98 deletions
|
@ -42,9 +42,12 @@ func GetImageURLAsBase64(s string) (string, error) {
|
|||
return encoded, nil
|
||||
}
|
||||
|
||||
// if the string instead is prefixed with "data:image/jpeg;base64,", drop it
|
||||
if strings.HasPrefix(s, "data:image/jpeg;base64,") {
|
||||
return strings.ReplaceAll(s, "data:image/jpeg;base64,", ""), 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
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("not valid string")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,13 +7,20 @@ import (
|
|||
)
|
||||
|
||||
var _ = Describe("utils/base64 tests", func() {
|
||||
It("GetImageURLAsBase64 can strip data url prefixes", func() {
|
||||
It("GetImageURLAsBase64 can strip jpeg data url prefixes", func() {
|
||||
// This one doesn't actually _care_ that it's base64, so feed "bad" data in this test in order to catch a change in that behavior for informational purposes.
|
||||
input := "data:image/jpeg;base64,FOO"
|
||||
b64, err := GetImageURLAsBase64(input)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(b64).To(Equal("FOO"))
|
||||
})
|
||||
It("GetImageURLAsBase64 can strip png data url prefixes", func() {
|
||||
// This one doesn't actually _care_ that it's base64, so feed "bad" data in this test in order to catch a change in that behavior for informational purposes.
|
||||
input := "data:image/png;base64,BAR"
|
||||
b64, err := GetImageURLAsBase64(input)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(b64).To(Equal("BAR"))
|
||||
})
|
||||
It("GetImageURLAsBase64 returns an error for bogus data", func() {
|
||||
input := "FOO"
|
||||
b64, err := GetImageURLAsBase64(input)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue