Skip to content

Commit

Permalink
worker: Add ReadImageB64DataUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
yondonfu committed Jan 22, 2024
1 parent dc36f66 commit e9f1bf7
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions worker/b64.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"fmt"
"image"
"image/png"
"io"
"os"

"github.com/vincent-petithory/dataurl"
)

func SaveImageB64DataUrl(url, outputPath string) error {
func ReadImageB64DataUrl(url string, w io.Writer) error {
dataURL, err := dataurl.DecodeString(url)
if err != nil {
return err
Expand All @@ -21,19 +22,23 @@ func SaveImageB64DataUrl(url, outputPath string) error {
return err
}

file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()

switch dataURL.MediaType.ContentType() {
case "image/png":
err = png.Encode(file, img)
err = png.Encode(w, img)
// Add cases for other image formats if necessary
default:
return fmt.Errorf("unsupported image format: %s", dataURL.MediaType.ContentType())
}

return err
}

func SaveImageB64DataUrl(url, outputPath string) error {
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()

return ReadImageB64DataUrl(url, file)
}

0 comments on commit e9f1bf7

Please sign in to comment.