2021-05-30 23:13:46 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-07-11 00:55:58 +08:00
|
|
|
|
using System.Web;
|
2021-05-30 23:13:46 +08:00
|
|
|
|
using Flurl;
|
|
|
|
|
using Flurl.Http;
|
|
|
|
|
|
|
|
|
|
namespace SKIT.FlurlHttpClient.Wechat.Work
|
|
|
|
|
{
|
|
|
|
|
public static class WechatWorkClientExecuteCgibinMediaExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/media/upload 接口。</para>
|
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90000/90135/90253 </para>
|
2021-06-03 10:38:41 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90001/90143/90389 </para>
|
2021-06-10 16:23:04 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90002/90151/90871 </para>
|
2021-05-30 23:13:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMediaUploadResponse> ExecuteCgibinMediaUploadAsync(this WechatWorkClient client, Models.CgibinMediaUploadRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
2021-07-11 00:55:58 +08:00
|
|
|
|
const string TYPE_IMAGE = "image";
|
|
|
|
|
const string TYPE_VOICE = "voice";
|
|
|
|
|
const string TYPE_VIDEO = "video";
|
|
|
|
|
|
2021-05-30 23:13:46 +08:00
|
|
|
|
if (string.IsNullOrEmpty(request.FileName))
|
|
|
|
|
{
|
|
|
|
|
string ext = "";
|
2021-07-11 00:55:58 +08:00
|
|
|
|
if (TYPE_IMAGE.Equals(request.Type))
|
2021-05-30 23:13:46 +08:00
|
|
|
|
ext = ".png";
|
2021-07-11 00:55:58 +08:00
|
|
|
|
else if (TYPE_VOICE.Equals(request.Type))
|
2021-05-30 23:13:46 +08:00
|
|
|
|
ext = ".mp3";
|
2021-07-11 00:55:58 +08:00
|
|
|
|
else if (TYPE_VIDEO.Equals(request.Type))
|
2021-05-30 23:13:46 +08:00
|
|
|
|
ext = ".mp4";
|
|
|
|
|
|
|
|
|
|
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.FileContentType))
|
|
|
|
|
{
|
2021-07-11 00:55:58 +08:00
|
|
|
|
if (TYPE_IMAGE.Equals(request.Type))
|
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
|
|
|
|
|
else if (TYPE_VOICE.Equals(request.Type))
|
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForVoice(request.FileName!) ?? "audio/mp3";
|
|
|
|
|
else if (TYPE_VIDEO.Equals(request.Type))
|
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForVideo(request.FileName!) ?? "video/mp4";
|
2021-05-30 23:13:46 +08:00
|
|
|
|
else
|
|
|
|
|
request.FileContentType = "application/octet-stream";
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 00:55:58 +08:00
|
|
|
|
IFlurlRequest flurlReq = client
|
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "media", "upload")
|
|
|
|
|
.SetQueryParam("access_token", request.AccessToken)
|
|
|
|
|
.SetQueryParam("type", request.Type);
|
|
|
|
|
|
2021-05-30 23:13:46 +08:00
|
|
|
|
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
|
|
|
|
using var fileContent = new ByteArrayContent(request.FileBytes ?? new byte[0]);
|
|
|
|
|
using var httpContent = new MultipartFormDataContent(boundary);
|
2021-07-11 00:55:58 +08:00
|
|
|
|
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
2021-05-30 23:13:46 +08:00
|
|
|
|
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
|
|
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
|
|
|
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
|
|
|
|
|
2021-07-11 00:55:58 +08:00
|
|
|
|
return await client.SendRequestAsync<Models.CgibinMediaUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
2021-05-30 23:13:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [POST] /cgi-bin/media/uploadimg 接口。</para>
|
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90000/90135/90256 </para>
|
2021-06-03 10:38:41 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90001/90143/90392 </para>
|
2021-06-10 16:23:04 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90002/90151/90874 </para>
|
2021-05-30 23:13:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMediaUploadImageResponse> ExecuteCgibinMediaUploadImageAsync(this WechatWorkClient client, Models.CgibinMediaUploadImageRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.FileName))
|
|
|
|
|
request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".png";
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(request.FileContentType))
|
2021-07-11 00:55:58 +08:00
|
|
|
|
request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png";
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
|
|
|
|
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "media", "uploadimg")
|
|
|
|
|
.SetQueryParam("access_token", request.AccessToken);
|
2021-05-30 23:13:46 +08:00
|
|
|
|
|
|
|
|
|
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
|
|
|
|
|
using var fileContent = new ByteArrayContent(request.FileBytes ?? new byte[0]);
|
|
|
|
|
using var httpContent = new MultipartFormDataContent(boundary);
|
2021-07-11 00:55:58 +08:00
|
|
|
|
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
|
2021-05-30 23:13:46 +08:00
|
|
|
|
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
|
|
|
|
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
|
|
|
|
|
fileContent.Headers.ContentLength = request.FileBytes?.Length;
|
|
|
|
|
|
2021-07-11 00:55:58 +08:00
|
|
|
|
return await client.SendRequestAsync<Models.CgibinMediaUploadImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
|
2021-05-30 23:13:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [GET] /cgi-bin/media/get 接口。</para>
|
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90000/90135/90254 </para>
|
2021-06-03 10:38:41 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90001/90143/90390 </para>
|
2021-06-10 16:23:04 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90002/90151/90872 </para>
|
2021-05-30 23:13:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMediaGetResponse> ExecuteCgibinMediaGetAsync(this WechatWorkClient client, Models.CgibinMediaGetRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:58 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "media", "get")
|
2021-05-30 23:13:46 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken)
|
|
|
|
|
.SetQueryParam("media_id", request.MediaId);
|
|
|
|
|
|
2021-07-11 00:55:58 +08:00
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMediaGetResponse>(flurlReq, cancellationToken: cancellationToken);
|
2021-05-30 23:13:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>异步调用 [GET] /cgi-bin/media/get/jssdk 接口。</para>
|
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90000/90135/90255 </para>
|
2021-06-03 10:38:41 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90001/90143/90391 </para>
|
2021-06-10 16:23:04 +08:00
|
|
|
|
/// <para>REF: https://open.work.weixin.qq.com/api/doc/90002/90151/90873 </para>
|
2021-05-30 23:13:46 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task<Models.CgibinMediaGetJssdkResponse> ExecuteCgibinMediaGetJssdkAsync(this WechatWorkClient client, Models.CgibinMediaGetJssdkRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (client is null) throw new ArgumentNullException(nameof(client));
|
|
|
|
|
if (request is null) throw new ArgumentNullException(nameof(request));
|
|
|
|
|
|
|
|
|
|
IFlurlRequest flurlReq = client
|
2021-07-11 00:55:58 +08:00
|
|
|
|
.CreateRequest(request, HttpMethod.Get, "cgi-bin", "media", "get", "jssdk")
|
2021-05-30 23:13:46 +08:00
|
|
|
|
.SetQueryParam("access_token", request.AccessToken)
|
|
|
|
|
.SetQueryParam("media_id", request.MediaId);
|
|
|
|
|
|
2021-07-11 00:55:58 +08:00
|
|
|
|
return await client.SendRequestWithJsonAsync<Models.CgibinMediaGetJssdkResponse>(flurlReq, cancellationToken: cancellationToken);
|
2021-05-30 23:13:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|