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 Jul 23, 2024
1 parent 05ac714 commit 0ba62d8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 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
9 changes: 6 additions & 3 deletions officialaccount/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package material
import (
"encoding/json"
"fmt"

"github.com/silenceper/wechat/v2/util"
)

Expand Down Expand Up @@ -38,16 +37,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)

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16)

undefined: util.GetFileSourceByUrl (typecheck)

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.20)

undefined: util.GetFileSourceByUrl

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.22)

undefined: util.GetFileSourceByUrl

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.19)

undefined: util.GetFileSourceByUrl

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.16)

undefined: util.GetFileSourceByUrl

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.17)

undefined: util.GetFileSourceByUrl

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.18)

undefined: util.GetFileSourceByUrl

Check failure on line 48 in officialaccount/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.21)

undefined: util.GetFileSourceByUrl
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
48 changes: 41 additions & 7 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"encoding/pem"
"encoding/xml"
"fmt"
"golang.org/x/crypto/pkcs12"

Check failure on line 11 in util/http.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16)

File is not `goimports`-ed (goimports)
"io"
"log"
"mime/multipart"
"net/http"
"os"

"golang.org/x/crypto/pkcs12"
"path"
)

// URIModifier URI修改器
Expand Down Expand Up @@ -146,24 +146,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 获取文件名和文件字节数据

Check failure on line 162 in util/http.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16)

comment on exported function `GetFileSourceByURL` should be of the form `GetFileSourceByURL ...` (golint)
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 +209,7 @@ type MultipartFormField struct {
IsFile bool
Fieldname string
Value []byte
Directory string
Filename string
FileReader io.Reader
}
Expand All @@ -197,7 +231,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 +247,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
18 changes: 13 additions & 5 deletions work/material/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package material

import (
"fmt"

"github.com/silenceper/wechat/v2/util"
)

Expand Down Expand Up @@ -59,16 +58,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)

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16)

undefined: util.GetFileSourceByUrl (typecheck)

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.20)

undefined: util.GetFileSourceByUrl

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.22)

undefined: util.GetFileSourceByUrl

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.19)

undefined: util.GetFileSourceByUrl

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.16)

undefined: util.GetFileSourceByUrl

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.17)

undefined: util.GetFileSourceByUrl

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.18)

undefined: util.GetFileSourceByUrl

Check failure on line 70 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.21)

undefined: util.GetFileSourceByUrl
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 +84,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)

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.16)

undefined: util.GetFileSourceByUrl (typecheck)

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.20)

undefined: util.GetFileSourceByUrl

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.22)

undefined: util.GetFileSourceByUrl

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.19)

undefined: util.GetFileSourceByUrl

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.16)

undefined: util.GetFileSourceByUrl

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.17)

undefined: util.GetFileSourceByUrl

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.18)

undefined: util.GetFileSourceByUrl

Check failure on line 95 in work/material/media.go

View workflow job for this annotation

GitHub Actions / Test (1.21)

undefined: util.GetFileSourceByUrl
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 0ba62d8

Please sign in to comment.