using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web; using Flurl.Http; namespace SKIT.FlurlHttpClient.Wechat.TenpayV3 { /// /// 为 提供媒体文件上传相关的 API 扩展方法。 /// public static class WechatTenpayClientExecuteMerchantMediaExtensions { /// /// 异步调用 [POST] /merchant/media/upload 接口。 /// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_1.shtml /// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter2_1_1.shtml /// /// /// /// /// public static async Task ExecuteUploadMerchantMediaImageAsync(this WechatTenpayClient client, Models.UploadMerchantMediaImageRequest request, CancellationToken cancellationToken = default) { if (client is null) throw new ArgumentNullException(nameof(client)); if (request is null) throw new ArgumentNullException(nameof(request)); if (request.FileName == null) request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".png"; if (request.FileHash == null) request.FileHash = Utilities.SHA256Utility.Hash(request.FileBytes).ToLower(); if (request.FileContentType == null) request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.FileName!) ?? "image/png"; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "merchant", "media", "upload"); string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x"); using var fileContent = new ByteArrayContent(request.FileBytes); using var metaContent = new StringContent(client.JsonSerializer.Serialize(request), Encoding.UTF8, "application/json"); using var httpContent = new MultipartFormDataContent(boundary); httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\""); httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\""); httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary); metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType); return await client.SendRequestAsync(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken); } /// /// 异步调用 [POST] /merchant/media/video_upload 接口。 /// REF: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter2_1_2.shtml /// REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter2_1_2.shtml /// /// /// /// /// public static async Task ExecuteUploadMerchantMediaVideoAsync(this WechatTenpayClient client, Models.UploadMerchantMediaVideoRequest request, CancellationToken cancellationToken = default) { if (client is null) throw new ArgumentNullException(nameof(client)); if (request is null) throw new ArgumentNullException(nameof(request)); if (request.FileName == null) request.FileName = Guid.NewGuid().ToString("N").ToLower() + ".mp4"; if (request.FileHash == null) request.FileHash = Utilities.SHA256Utility.Hash(request.FileBytes).ToLower(); if (request.FileContentType == null) request.FileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForVideo(request.FileName!) ?? "video/mp4"; IFlurlRequest flurlReq = client .CreateRequest(request, HttpMethod.Post, "merchant", "media", "video_upload"); string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x"); using var fileContent = new ByteArrayContent(request.FileBytes); using var metaContent = new ByteArrayContent(Encoding.UTF8.GetBytes(client.JsonSerializer.Serialize(request))); using var httpContent = new MultipartFormDataContent(boundary); httpContent.Add(metaContent, $"\"{Constants.FormDataFields.FORMDATA_META}\""); httpContent.Add(fileContent, "\"file\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\""); httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary); metaContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType); return await client.SendRequestAsync(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken); } } }