Skip to content

Commit

Permalink
Merge pull request #575 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Matrix-X authored Oct 21, 2024
2 parents c95c51a + 41e904b commit 91691d2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/work/media/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,61 @@ func (comp *Client) Upload(ctx context.Context, mediaType string, path string, f

return outResponse, err
}

func (comp *Client) UploadMediaImageUrl(ctx context.Context, attachmentType string, url string, form *power.HashMap) (*response2.ResponseUploadMediaData, error) {
path, err := support.DownloadFileFromURL(url)
if err != nil {
return nil, errors.Wrap(err, "failed to download file from URL")
}
defer os.Remove(path)
return comp.UploadMedia(ctx, attachmentType, "image", path, form)
}

func (comp *Client) UploadMediaVideoUrl(ctx context.Context, attachmentType string, url string, form *power.HashMap) (*response2.ResponseUploadMediaData, error) {
path, err := support.DownloadFileFromURL(url)
if err != nil {
return nil, errors.Wrap(err, "failed to download file from URL")
}
defer os.Remove(path)
return comp.UploadMedia(ctx, attachmentType, "video", path, form)
}

func (comp *Client) UploadMediaImage(ctx context.Context, attachmentType string, path string, form *power.HashMap) (*response2.ResponseUploadMediaData, error) {
return comp.UploadMedia(ctx, attachmentType, "image", path, form)
}

func (comp *Client) UploadMediaVideo(ctx context.Context, attachmentType string, path string, form *power.HashMap) (*response2.ResponseUploadMediaData, error) {
return comp.UploadMedia(ctx, attachmentType, "video", path, form)
}

// 上传附件资源
// https://developer.work.weixin.qq.com/document/path/95178
func (comp *Client) UploadMedia(ctx context.Context, attachmentType string, mediaType string, path string, form *power.HashMap) (*response2.ResponseUploadMediaData, error) {
outResponse := &response2.ResponseUploadMediaData{}

var files *object.HashMap
if path != "" {
files = &object.HashMap{
"media": path,
}
}

var formData *kernel.UploadForm
if form != nil {
formData = &kernel.UploadForm{
Contents: []*kernel.UploadContent{
&kernel.UploadContent{
Name: (*form)["name"].(string),
Value: (*form)["value"],
},
},
}
}

_, err := comp.BaseClient.HttpUpload(ctx, "cgi-bin/media/upload_attachment", files, formData, &object.StringMap{
"media_type": mediaType,
"attachment_type": attachmentType,
}, nil, outResponse)

return outResponse, err
}
8 changes: 8 additions & 0 deletions src/work/media/response/responseUploadMedia.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ type ResponseUploadMedia struct {
MediaID string `json:"media_id"`
CreatedAt string `json:"created_at"`
}

type ResponseUploadMediaData struct {
response.ResponseWork

Type string `json:"type"`
MediaID string `json:"media_id"`
CreatedAt int `json:"created_at"`
}

0 comments on commit 91691d2

Please sign in to comment.