Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(media): add UploadImgFromReader api #802

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions work/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
return result, err
}

// UploadImgFromReader 从 io.Reader 上传图片
// @see https://developer.work.weixin.qq.com/document/path/90256
func (r *Client) UploadImgFromReader(filename string, reader io.Reader) (*UploadImgResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var byteData []byte
byteData, err = io.ReadAll(reader)
if err != nil {
return nil, err
}
var response []byte
if response, err = util.PostFileByStream("media", filename, fmt.Sprintf(uploadImgURL, accessToken), byteData); err != nil {
return nil, err
}
result := &UploadImgResponse{}
err = util.DecodeWithError(response, result, "UploadImg")
return result, err
}

// UploadTempFile 上传临时素材
// @see https://developer.work.weixin.qq.com/document/path/90253
// @mediaType 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
Expand Down Expand Up @@ -166,5 +190,13 @@ func (r *Client) GetTempFile(mediaID string) ([]byte, error) {
if err != nil {
return nil, err
}

// 检查响应是否为错误信息
err = util.DecodeWithCommonError(response, "GetTempFile")
if err != nil {
return nil, err
}

// 如果不是错误响应,则返回原始数据
return response, nil
}
Loading