-
Notifications
You must be signed in to change notification settings - Fork 3
/
save_img.go
42 lines (38 loc) · 1023 Bytes
/
save_img.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
package wechatminiprogram
import (
"context"
"github.com/dtapps/go-library/utils/gorequest"
"log"
"os"
)
type SaveImgResponse struct {
Path string
Name string
}
func (c *Client) SaveImg(ctx context.Context, resp gorequest.Response, dir, saveName string) SaveImgResponse {
// 返回是二进制图片,或者json错误
if resp.ResponseHeader.Get("Content-Type") == "image/jpeg" || resp.ResponseHeader.Get("Content-Type") == "image/png" {
// 保存在output目录
outputFileName := saveName
if resp.ResponseHeader.Get("Content-Type") == "image/jpeg" {
outputFileName = outputFileName + ".jpg"
} else {
outputFileName = outputFileName + ".png"
}
here:
log.Println(dir + outputFileName)
f, err := os.OpenFile(dir+outputFileName, os.O_CREATE|os.O_RDWR, 0666)
log.Println(err)
if err != nil {
os.Mkdir(dir, 0755)
goto here
}
f.Write(resp.ResponseBody)
f.Close()
return SaveImgResponse{
Path: dir + outputFileName,
Name: outputFileName,
}
}
return SaveImgResponse{}
}