You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wechatopen/save_img.go

43 lines
994 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package wechatopen
import (
"context"
"go.dtapp.net/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{}
}