fix(work): 修复上传素材文件接口不支持 Unicode 文件名的问题

This commit is contained in:
Fu Diwei
2022-04-22 18:08:53 +08:00
parent 75529a4115
commit 170a5dd2d2
6 changed files with 78 additions and 37 deletions

View File

@@ -1,9 +1,7 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Flurl;
using Flurl.Http;
@@ -60,14 +58,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
.SetQueryParam("access_token", request.AccessToken)
.SetQueryParam("type", request.Type);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
using var httpContent = new MultipartFormDataContent(boundary);
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
fileContent.Headers.ContentLength = request.FileBytes?.Length;
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, formDataName: "media");
return await client.SendRequestAsync<Models.CgibinMediaUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
}
@@ -96,14 +87,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
.CreateRequest(request, HttpMethod.Post, "cgi-bin", "media", "uploadimg")
.SetQueryParam("access_token", request.AccessToken);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
using var httpContent = new MultipartFormDataContent(boundary);
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
fileContent.Headers.ContentLength = request.FileBytes?.Length;
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, formDataName: "media");
return await client.SendRequestAsync<Models.CgibinMediaUploadImageResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
}
@@ -156,14 +140,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
.SetQueryParam("media_type", request.Type)
.SetQueryParam("attachment_type", request.AttachmentType);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
using var httpContent = new MultipartFormDataContent(boundary);
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
fileContent.Headers.ContentLength = request.FileBytes?.Length;
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, formDataName: "media");
return await client.SendRequestAsync<Models.CgibinMediaUploadAttachmentResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
}

View File

@@ -1,9 +1,7 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Flurl;
using Flurl.Http;
@@ -316,14 +314,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
.SetQueryParam("provider_access_token", request.ProviderAccessToken)
.SetQueryParam("type", request.Type);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
using var fileContent = new ByteArrayContent(request.FileBytes ?? Array.Empty<byte>());
using var httpContent = new MultipartFormDataContent(boundary);
httpContent.Add(fileContent, "\"media\"", $"\"{HttpUtility.UrlEncode(request.FileName)}\"");
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(request.FileContentType);
fileContent.Headers.ContentLength = request.FileBytes?.Length;
using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.FileName, fileBytes: request.FileBytes, fileContentType: request.FileContentType, formDataName: "media");
return await client.SendRequestAsync<Models.CgibinServiceMediaUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken);
}
#endregion

View File

@@ -0,0 +1,45 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
{
internal static class FileHttpContentBuilder
{
public static MultipartFormDataContent Build(string fileName, byte[] fileBytes, string fileContentType, string formDataName)
{
return Build(fileName: fileName, fileBytes: fileBytes, fileContentType: fileContentType, formDataName: formDataName, (_) => { });
}
public static MultipartFormDataContent Build(string fileName, byte[] fileBytes, string fileContentType, string formDataName, Action<HttpContent> configureFileHttpContent)
{
if (fileName == null) throw new ArgumentNullException(nameof(fileName));
if (formDataName == null) throw new ArgumentNullException(nameof(formDataName));
if (configureFileHttpContent == null) throw new ArgumentNullException(nameof(configureFileHttpContent));
fileName = fileName.Replace("\"", "");
fileBytes = fileBytes ?? Array.Empty<byte>();
fileContentType = string.IsNullOrEmpty(fileContentType) ? "application/octet-stream" : fileContentType;
formDataName = formDataName.Replace("\"", "");
// HACKED: 默认不支持 Unicode 文件名 https://github.com/dotnet/runtime/issues/22996
byte[] bytesFileName = Encoding.UTF8.GetBytes(fileName);
char[] bytesHackedFileName = new char[bytesFileName.Length];
Array.Copy(bytesFileName, 0, bytesHackedFileName, 0, bytesFileName.Length);
string hackedFileName = new string(bytesHackedFileName);
ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse($"form-data; name=\"{formDataName}\"; filename=\"{hackedFileName}\"");
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(fileContentType);
fileContent.Headers.ContentLength = fileBytes.Length;
configureFileHttpContent(fileContent);
string boundary = "--BOUNDARY--" + DateTimeOffset.Now.Ticks.ToString("x");
MultipartFormDataContent httpContent = new MultipartFormDataContent(boundary);
httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/form-data; boundary={boundary}");
httpContent.Add(fileContent);
return httpContent;
}
}
}