Skip to content

Commit

Permalink
新增支持流的形式上传方式
Browse files Browse the repository at this point in the history
  • Loading branch information
w-art-creater committed Jul 11, 2024
1 parent 1138a7d commit f302486
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 21 deletions.
5 changes: 4 additions & 1 deletion miniprogram/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ func (content *Content) CheckImage(media string) error {
if err != nil {
return err
}
var directory = media
response, err := util.PostFile(
"media",
media,
nil,
"",
directory,
fmt.Sprintf(checkImageURL, accessToken),
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion miniprogram/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func (security *Security) ImageCheckV1(filename string) (err error) {
}

uri := fmt.Sprintf(imageCheckURL, accessToken)
response, err := util.PostFile("media", filename, uri)
var directory = filename
response, err := util.PostFile("media", nil, "", directory, uri)
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion officialaccount/customerservice/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ func (csm *Manager) UploadHeadImg(kfAccount, fileName string) (err error) {
}
uri := fmt.Sprintf("%s?access_token=%s&kf_account=%s", customerServiceUploadHeadImg, accessToken, kfAccount)
var response []byte
response, err = util.PostFile("media", fileName, uri)
var directory = fileName
response, err = util.PostFile("media", nil, "", directory, uri)
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion officialaccount/material/material.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func (material *Material) AddMaterial(mediaType MediaType, filename string) (med

uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
var response []byte
response, err = util.PostFile("media", filename, uri)
var directory = filename
response, err = util.PostFile("media", nil, "", directory, uri)
if err != nil {
return
}
Expand Down
34 changes: 27 additions & 7 deletions officialaccount/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package material
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"

"github.com/silenceper/wechat/v2/util"
)
Expand Down Expand Up @@ -38,16 +41,32 @@ type Media struct {
}

// MediaUpload 临时素材上传
func (material *Material) MediaUpload(mediaType MediaType, filename string) (media Media, err error) {
func (material *Material) MediaUpload(mediaType MediaType, url string) (media Media, err error) {
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
if accessToken, err = material.GetAccessToken(); err != nil {
return
}

uri := fmt.Sprintf("%s?access_token=%s&type=%s", mediaUploadURL, accessToken, mediaType)
// 使用strings.LastIndex函数找到最后一个斜杠的位置
lastSlashIndex := strings.LastIndex(url, "/")
// 从最后一个斜杠的位置截取到最后,获取文件名
filename := url[lastSlashIndex+1:]
// 获取图片
resp, err := http.Get(url)
if err != nil {
err = fmt.Errorf("get image error: %v", err)
return
}
// 读取响应到内存
var imageData []byte
imageData, err = io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
err = fmt.Errorf("read image error: %v", err)
return
}
var response []byte
response, err = util.PostFile("media", filename, uri)
response, err = util.PostFile("media", imageData, filename, "", uri)
if err != nil {
return
}
Expand All @@ -56,7 +75,7 @@ func (material *Material) MediaUpload(mediaType MediaType, filename string) (med
return
}
if media.ErrCode != 0 {
err = fmt.Errorf("MediaUpload error : errcode=%v , errmsg=%v", media.ErrCode, media.ErrMsg)
err = fmt.Errorf("MediaUpload error : errcode=%v, errmsg=%v", media.ErrCode, media.ErrMsg)
return
}
return
Expand Down Expand Up @@ -91,7 +110,8 @@ func (material *Material) ImageUpload(filename string) (url string, err error) {

uri := fmt.Sprintf("%s?access_token=%s", mediaUploadImageURL, accessToken)
var response []byte
response, err = util.PostFile("media", filename, uri)
var directory = filename
response, err = util.PostFile("media", nil, "", directory, uri)
if err != nil {
return
}
Expand Down
25 changes: 18 additions & 7 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,23 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
return responseData, contentType, err
}

// PostFile 上传文件
func PostFile(fieldName, filename, uri string) ([]byte, error) {
// PostFile 支持流或文件形式上传
func PostFile(fieldName string, data []byte, fileName string, directory string, uri string) ([]byte, error) {
var fileContent []byte
var isFile bool
// 数据为空且文件目录不为空则按文件形式上传
if len(data) == 0 && directory != "" {
isFile = true
} else {
fileContent = data
}
fields := []MultipartFormField{
{
IsFile: true,
IsFile: isFile,
Fieldname: fieldName,
Filename: filename,
Value: fileContent,
Filename: fileName,
Directory: directory,
},
}
return PostMultipartForm(fields, uri)
Expand All @@ -164,6 +174,7 @@ type MultipartFormField struct {
Fieldname string
Value []byte
Filename string
Directory string
}

// PostMultipartForm 上传文件或其他多个字段
Expand All @@ -176,13 +187,13 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte

for _, field := range fields {
if field.IsFile {
fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename)
fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Directory)
if e != nil {
err = fmt.Errorf("error writing to buffer , err=%v", e)
return
}

fh, e := os.Open(field.Filename)
fh, e := os.Open(field.Directory)
if e != nil {
err = fmt.Errorf("error opening file , err=%v", e)
return
Expand All @@ -193,7 +204,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
return
}
} else {
partWriter, e := bodyWriter.CreateFormField(field.Fieldname)
partWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename)
if e != nil {
err = e
return
Expand Down
9 changes: 6 additions & 3 deletions work/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadImgURL, accessToken)); err != nil {
var directory = filename
if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadImgURL, accessToken)); err != nil {
return nil, err
}
result := &UploadImgResponse{}
Expand All @@ -68,7 +69,8 @@ func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempF
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil {
var directory = filename
if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil {
return nil, err
}
result := &UploadTempFileResponse{}
Expand All @@ -89,7 +91,8 @@ func (r *Client) UploadAttachment(filename string, mediaType string, attachmentT
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType)); err != nil {
var directory = filename
if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil {
return nil, err
}
result := &UploadAttachmentResponse{}
Expand Down

0 comments on commit f302486

Please sign in to comment.