-
Notifications
You must be signed in to change notification settings - Fork 27
/
b64.go
61 lines (50 loc) · 1.08 KB
/
b64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package worker
import (
"bytes"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"github.com/vincent-petithory/dataurl"
)
func ReadImageB64DataUrl(url string, w io.Writer) error {
dataURL, err := dataurl.DecodeString(url)
if err != nil {
return err
}
img, _, err := image.Decode(bytes.NewReader(dataURL.Data))
if err != nil {
return err
}
switch dataURL.MediaType.ContentType() {
case "image/png":
err = png.Encode(w, img)
case "image/jpg", "image/jpeg":
err = jpeg.Encode(w, img, nil)
case "image/gif":
err = gif.Encode(w, img, nil)
// 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)
}
func ReadAudioB64DataUrl(url string, w io.Writer) error {
dataURL, err := dataurl.DecodeString(url)
if err != nil {
return err
}
w.Write(dataURL.Data)
return nil
}