Skip to content

Commit

Permalink
add stream_upload_source
Browse files Browse the repository at this point in the history
  • Loading branch information
w-art-creater committed Aug 5, 2024
1 parent 05ac714 commit e55faeb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 19 deletions.
21 changes: 13 additions & 8 deletions officialaccount/material/material.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path"

"github.com/silenceper/wechat/v2/officialaccount/context"
"github.com/silenceper/wechat/v2/util"
Expand Down Expand Up @@ -163,7 +164,7 @@ type resAddMaterial struct {
}

// AddMaterialFromReader 上传永久性素材(处理视频需要单独上传),从 io.Reader 中读取
func (material *Material) AddMaterialFromReader(mediaType MediaType, filename string, reader io.Reader) (mediaID string, url string, err error) {
func (material *Material) AddMaterialFromReader(mediaType MediaType, directory string, reader io.Reader) (mediaID string, url string, err error) {
if mediaType == MediaTypeVideo {
err = errors.New("永久视频素材上传使用 AddVideo 方法")
return
Expand All @@ -175,8 +176,10 @@ func (material *Material) AddMaterialFromReader(mediaType MediaType, filename st
}

uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
// 获取文件名
filename := path.Base(directory)
var response []byte
response, err = util.PostFileFromReader("media", filename, uri, reader)
response, err = util.PostFileFromReader("media", directory, filename, uri, reader)
if err != nil {
return
}
Expand Down Expand Up @@ -211,7 +214,7 @@ type reqVideo struct {
}

// AddVideoFromReader 永久视频素材文件上传,从 io.Reader 中读取
func (material *Material) AddVideoFromReader(filename, title, introduction string, reader io.Reader) (mediaID string, url string, err error) {
func (material *Material) AddVideoFromReader(directory, title, introduction string, reader io.Reader) (mediaID string, url string, err error) {
var accessToken string
accessToken, err = material.GetAccessToken()
if err != nil {
Expand All @@ -229,17 +232,19 @@ func (material *Material) AddVideoFromReader(filename, title, introduction strin
if err != nil {
return
}

fileName := path.Base(directory)
fields := []util.MultipartFormField{
{
IsFile: true,
Fieldname: "media",
Filename: filename,
Directory: directory,
Filename: fileName,
FileReader: reader,
},
{
IsFile: false,
Fieldname: "description",
Filename: fileName,
Value: fieldValue,
},
}
Expand All @@ -265,14 +270,14 @@ func (material *Material) AddVideoFromReader(filename, title, introduction strin
}

// AddVideo 永久视频素材文件上传
func (material *Material) AddVideo(filename, title, introduction string) (mediaID string, url string, err error) {
f, err := os.Open(filename)
func (material *Material) AddVideo(directory, title, introduction string) (mediaID string, url string, err error) {
f, err := os.Open(directory)
if err != nil {
return "", "", err
}
defer func() { _ = f.Close() }()

return material.AddVideoFromReader(filename, title, introduction, f)
return material.AddVideoFromReader(directory, title, introduction, f)
}

type reqDeleteMaterial struct {
Expand Down
8 changes: 6 additions & 2 deletions officialaccount/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ 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 {
return
}

uri := fmt.Sprintf("%s?access_token=%s&type=%s", mediaUploadURL, accessToken, mediaType)
filename, byteData, err := util.GetFileSourceByURL(url)
if err != nil {
return
}
var response []byte
response, err = util.PostFile("media", filename, uri)
response, err = util.PostFileByStream("media", filename, uri, byteData)
if err != nil {
return
}
Expand Down
45 changes: 40 additions & 5 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"mime/multipart"
"net/http"
"os"
"path"

"golang.org/x/crypto/pkcs12"
)
Expand Down Expand Up @@ -146,24 +147,57 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
return responseData, contentType, err
}

// PostFileByStream 上传文件
func PostFileByStream(fieldName, fileName, uri string, byteData []byte) ([]byte, error) {
fields := []MultipartFormField{
{
IsFile: false,
Fieldname: fieldName,
Filename: fileName,
Value: byteData,
},
}
return PostMultipartForm(fields, uri)
}

// GetFileSourceByURL 从给定的 URL 获取文件名和文件字节数据
func GetFileSourceByURL(url string) (filename string, byteData []byte, err error) {
// 获取文件名
filename = path.Base(url)
// 获取资源
resp, err := http.Get(url)
if err != nil {
err = fmt.Errorf("get resp error: %v", err)
return
}
byteData, err = io.ReadAll(resp.Body)
defer func() { _ = resp.Body.Close() }()
if err != nil {
err = fmt.Errorf("read resp error: %v", err)
return
}
return
}

// PostFile 上传文件
func PostFile(fieldName, filename, uri string) ([]byte, error) {
func PostFile(fieldName, directory, uri string) ([]byte, error) {
fields := []MultipartFormField{
{
IsFile: true,
Fieldname: fieldName,
Filename: filename,
Directory: directory,
},
}
return PostMultipartForm(fields, uri)
}

// PostFileFromReader 上传文件,从 io.Reader 中读取
func PostFileFromReader(filedName, fileName, uri string, reader io.Reader) ([]byte, error) {
func PostFileFromReader(filedName, directory, fileName, uri string, reader io.Reader) ([]byte, error) {
fields := []MultipartFormField{
{
IsFile: true,
Fieldname: filedName,
Directory: directory,
Filename: fileName,
FileReader: reader,
},
Expand All @@ -176,6 +210,7 @@ type MultipartFormField struct {
IsFile bool
Fieldname string
Value []byte
Directory string
Filename string
FileReader io.Reader
}
Expand All @@ -197,7 +232,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
}

if field.FileReader == nil {
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 @@ -213,7 +248,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
}
}
} else {
partWriter, e := bodyWriter.CreateFormField(field.Fieldname)
partWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename)
if e != nil {
err = e
return
Expand Down
17 changes: 13 additions & 4 deletions work/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
// UploadTempFile 上传临时素材
// @see https://developer.work.weixin.qq.com/document/path/90253
// @mediaType 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempFileResponse, error) {
// 临时素材一般都是存储在oss上的,可以直接传递url
func (r *Client) UploadTempFile(url string, mediaType string) (*UploadTempFileResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
filename, byteData, err := util.GetFileSourceByURL(url)
if err != nil {
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil {
if response, err = util.PostFileByStream("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType), byteData); err != nil {
return nil, err
}
result := &UploadTempFileResponse{}
Expand All @@ -80,16 +85,20 @@ func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempF
// @see https://developer.work.weixin.qq.com/document/path/95098
// @mediaType 媒体文件类型,分别有图片(image)、视频(video)、普通文件(file)
// @attachment_type 附件类型,不同的附件类型用于不同的场景。1:朋友圈;2:商品图册
func (r *Client) UploadAttachment(filename string, mediaType string, attachmentType int) (*UploadAttachmentResponse, error) {
func (r *Client) UploadAttachment(url string, mediaType string, attachmentType int) (*UploadAttachmentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
filename, byteData, err := util.GetFileSourceByURL(url)
if err != nil {
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType)); err != nil {
if response, err = util.PostFileByStream("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType), byteData); err != nil {
return nil, err
}
result := &UploadAttachmentResponse{}
Expand Down

0 comments on commit e55faeb

Please sign in to comment.