Skip to content

Commit

Permalink
feat(media): add UploadImgFromReader api (#802)
Browse files Browse the repository at this point in the history
* feat(media): add getTempFile api

add getTempFile api

* feat(media): handle error in GetTempFile

handle error in GetTempFile

* feat(media): add UploadImgFromReader api

add UploadImgFromReader api

* fix(media): fixed not return common error

fixed not return common error
  • Loading branch information
Lumiaqian authored Dec 20, 2024
1 parent 4a8371e commit 35af33f
Showing 1 changed file with 32 additions and 0 deletions.
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
}

0 comments on commit 35af33f

Please sign in to comment.