Skip to content

Commit

Permalink
feat: 升级公共组件
Browse files Browse the repository at this point in the history
  • Loading branch information
fudiwei committed Jul 14, 2024
1 parent cf00f67 commit 09c066b
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class DouyinOpenClientExecuteApiDouyinVideoExtensions
if (request.VideoContentType is null)
request.VideoContentType = "video/mp4";

using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.VideoFileName, fileBytes: request.VideoFileBytes, fileContentType: request.VideoContentType, formDataName: "video");
using var httpContent = Utilities.HttpContentBuilder.BuildWithFile(fileName: request.VideoFileName, fileBytes: request.VideoFileBytes, fileContentType: request.VideoContentType, formDataName: "video");
return await client.SendFlurlRequestAsync<Models.DouyinVideoUploadVideoV1Response>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand Down Expand Up @@ -218,7 +218,7 @@ public static class DouyinOpenClientExecuteApiDouyinVideoExtensions
if (request.ImageContentType is null)
request.ImageContentType = "image/jpeg";

using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.ImageFileName, fileBytes: request.ImageFileBytes, fileContentType: request.ImageContentType, formDataName: "image");
using var httpContent = Utilities.HttpContentBuilder.BuildWithFile(fileName: request.ImageFileName, fileBytes: request.ImageFileBytes, fileContentType: request.ImageContentType, formDataName: "image");
return await client.SendFlurlRequestAsync<Models.DouyinVideoUploadImageV1Response>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand Down Expand Up @@ -301,7 +301,7 @@ public static class DouyinOpenClientExecuteApiDouyinVideoExtensions
if (request.VideoContentType is null)
request.VideoContentType = "video/mp4";

using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.VideoFileName, fileBytes: request.VideoFileBytes, fileContentType: request.VideoContentType, formDataName: "video");
using var httpContent = Utilities.HttpContentBuilder.BuildWithFile(fileName: request.VideoFileName, fileBytes: request.VideoFileBytes, fileContentType: request.VideoContentType, formDataName: "video");
return await client.SendFlurlRequestAsync<Models.DouyinVideoUploadVideoPartV1Response>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class DouyinOpenClientExecuteEnterpriseExtensions
if (request.MediaContentType is null)
request.MediaContentType = "image/jpeg";

using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.MediaFileName, fileBytes: request.MediaFileBytes, fileContentType: request.MediaContentType, formDataName: "media");
using var httpContent = Utilities.HttpContentBuilder.BuildWithFile(fileName: request.MediaFileName, fileBytes: request.MediaFileBytes, fileContentType: request.MediaContentType, formDataName: "media");
return await client.SendFlurlRequestAsync<Models.EnterpriseMediaUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand Down Expand Up @@ -68,7 +68,7 @@ public static class DouyinOpenClientExecuteEnterpriseExtensions
if (request.MediaContentType is null)
request.MediaContentType = MimeTypes.Binary;

using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.MediaFileName, fileBytes: request.MediaFileBytes, fileContentType: request.MediaContentType, formDataName: "media");
using var httpContent = Utilities.HttpContentBuilder.BuildWithFile(fileName: request.MediaFileName, fileBytes: request.MediaFileBytes, fileContentType: request.MediaContentType, formDataName: "media");
return await client.SendFlurlRequestAsync<Models.EnterpriseMediaTempUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class DouyinOpenClientExecuteImageExtensions
.WithHeader("access-token", request.AccessToken)
.SetQueryParam("open_id", request.OpenId);

using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.ImageFileName, fileBytes: request.ImageFileBytes, fileContentType: request.ImageContentType, formDataName: "image");
using var httpContent = Utilities.HttpContentBuilder.BuildWithFile(fileName: request.ImageFileName, fileBytes: request.ImageFileBytes, fileContentType: request.ImageContentType, formDataName: "image");
return await client.SendFlurlRequestAsync<Models.ImageUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;

namespace SKIT.FlurlHttpClient.ByteDance.DouyinOpen
Expand Down Expand Up @@ -36,7 +35,7 @@ public static class DouyinOpenClientExecuteToolExtensions
.CreateFlurlRequest(request, HttpMethod.Post, "tool", "imagex", "client_upload/")
.WithHeader("access-token", request.AccessToken);

using var httpContent = Utilities.FileHttpContentBuilder.Build(fileName: request.ImageFileName, fileBytes: request.ImageFileBytes, fileContentType: request.ImageContentType, formDataName: "image");
using var httpContent = Utilities.HttpContentBuilder.BuildWithFile(fileName: request.ImageFileName, fileBytes: request.ImageFileBytes, fileContentType: request.ImageContentType, formDataName: "image");
return await client.SendFlurlRequestAsync<Models.ToolImagexClientUploadResponse>(flurlReq, httpContent: httpContent, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="3.0.0" />
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="3.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
using System;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;

namespace SKIT.FlurlHttpClient.ByteDance.DouyinOpen.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, (_) => { });
}
using SKIT.FlurlHttpClient;

public static MultipartFormDataContent Build(string fileName, byte[] fileBytes, string fileContentType, string formDataName, Action<HttpContent> configureFileHttpContent)
internal static class HttpContentBuilder
{
public static MultipartFormDataContent BuildWithFile(string fileName, byte[] fileBytes, string fileContentType, string formDataName, Action<HttpContent>? configureFileHttpContent = null)
{
if (fileName is null) throw new ArgumentNullException(nameof(fileName));
if (formDataName is null) throw new ArgumentNullException(nameof(formDataName));
if (configureFileHttpContent is null) throw new ArgumentNullException(nameof(configureFileHttpContent));

fileName = fileName.Replace("\"", "");
fileBytes = fileBytes ?? Array.Empty<byte>();
fileContentType = string.IsNullOrEmpty(fileContentType) ? "application/octet-stream" : fileContentType;
fileContentType = string.IsNullOrEmpty(fileContentType) ? MimeTypes.Binary : fileContentType;
formDataName = formDataName.Replace("\"", "");

ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(fileContentType);
configureFileHttpContent(fileContent);
configureFileHttpContent?.Invoke(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.Headers.ContentType = MediaTypeHeaderValue.Parse($"{MimeTypes.FormData}; boundary={boundary}");
httpContent.Add(fileContent, $"\"{formDataName}\"", $"\"{HttpUtility.UrlEncode(fileName)}\"");
return httpContent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppOpenApiClientExecuteAppsFileExtensions
{
/// <summary>
Expand All @@ -29,7 +31,7 @@ public static class DouyinMicroAppOpenApiClientExecuteAppsFileExtensions
request.MaterialFileName = Guid.NewGuid().ToString("N").ToLower() + (request.MaterialType == 8 ? ".pdf" : ".jpg");

if (request.MaterialFileContentType is null)
request.MaterialFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForMaterial(request.MaterialFileName) ?? (request.MaterialType == 8 ? "application/pdf" : "image/jpeg");
request.MaterialFileContentType = MimeTypes.GetMimeMapping(request.MaterialFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v2", "file", "upload_material")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppOpenApiClientExecuteAppsICPRecordExtensions
{
/// <summary>
Expand All @@ -29,7 +31,7 @@ public static class DouyinMicroAppOpenApiClientExecuteAppsICPRecordExtensions
request.ImageFileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";

if (request.ImageFileContentType is null)
request.ImageFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.ImageFileName) ?? "image/jpeg";
request.ImageFileContentType = MimeTypes.GetMimeMapping(request.ImageFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "apps", "v1", "icp_record", "upload_image/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppOpenApiClientExecuteLegacyMicroAppExtensions
{
/// <summary>
Expand Down Expand Up @@ -59,7 +61,7 @@ public static class DouyinMicroAppOpenApiClientExecuteLegacyMicroAppExtensions
request.MaterialFileName = Guid.NewGuid().ToString("N").ToLower() + (request.MaterialType == 8 ? ".pdf" : ".jpg");

if (request.MaterialFileContentType is null)
request.MaterialFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForMaterial(request.MaterialFileName) ?? (request.MaterialType == 8 ? "application/pdf" : "image/jpeg");
request.MaterialFileContentType = MimeTypes.GetMimeMapping(request.MaterialFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v1", "microapp", "upload_material")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppOpenApiClientExecuteLegacyThirdPartyExtensions
{
/// <summary>
Expand Down Expand Up @@ -58,7 +60,7 @@ public static class DouyinMicroAppOpenApiClientExecuteLegacyThirdPartyExtensions
request.MaterialFileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";

if (request.MaterialFileContentType is null)
request.MaterialFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForMaterial(request.MaterialFileName) ?? "image/jpeg";
request.MaterialFileContentType = MimeTypes.GetMimeMapping(request.MaterialFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v1", "tp", "upload_pic_material")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.OpenApi
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppOpenApiClientExecuteTpAppFileExtensions
{
/// <summary>
Expand All @@ -29,7 +31,7 @@ public static class DouyinMicroAppOpenApiClientExecuteTpAppFileExtensions
request.MaterialFileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";

if (request.MaterialFileContentType is null)
request.MaterialFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForMaterial(request.MaterialFileName) ?? "image/jpeg";
request.MaterialFileContentType = MimeTypes.GetMimeMapping(request.MaterialFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "tpapp", "v2", "file", "upload_material")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.RoleApi
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppRoleApiClientExecuteRoleExtensions
{
/// <summary>
Expand All @@ -31,7 +33,7 @@ public static class DouyinMicroAppRoleApiClientExecuteRoleExtensions
request.MaterialFileName = Guid.NewGuid().ToString("N").ToLower() + (request.MaterialType == 1010 ? ".pdf" : ".jpg");

if (request.MaterialFileContentType is null)
request.MaterialFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForMaterial(request.MaterialFileName) ?? "image/jpeg";
request.MaterialFileContentType = MimeTypes.GetMimeMapping(request.MaterialFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "upload_material");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.ExtendedSDK.Webcast
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppWebcastClientExecuteFileExtensions
{
/// <summary>
Expand All @@ -29,7 +31,7 @@ public static class DouyinMicroAppWebcastClientExecuteFileExtensions
request.ImageFileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";

if (request.ImageFileContentType is null)
request.ImageFileContentType = "image/jpeg";
request.ImageFileContentType = MimeTypes.GetMimeMapping(request.ImageFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "file", "card_image", "upload")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppClientExecuteLegacyAppsExtensions
{
/// <summary>
Expand Down Expand Up @@ -93,7 +95,7 @@ public static class DouyinMicroAppClientExecuteLegacyAppsExtensions
request.MaterialFileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";

if (request.MaterialFileContentType is null)
request.MaterialFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForMaterial(request.MaterialFileName) ?? "image/jpeg";
request.MaterialFileContentType = MimeTypes.GetMimeMapping(request.MaterialFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "api", "apps", "v1", "capacity", "upload_material")
Expand Down Expand Up @@ -518,7 +520,7 @@ public static class DouyinMicroAppClientExecuteLegacyAppsExtensions
request.ImageFileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";

if (request.ImageFileContentType is null)
request.ImageFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForMaterial(request.ImageFileName) ?? "image/jpeg";
request.ImageFileContentType = MimeTypes.GetMimeMapping(request.ImageFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "api", "apps", "upload_live_image")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp
{
using SKIT.FlurlHttpClient;

public static class DouyinMicroAppClientExecutePromotionExtensions
{
#region Activity
Expand Down Expand Up @@ -76,7 +78,7 @@ public static class DouyinMicroAppClientExecutePromotionExtensions
request.ImageFileName = Guid.NewGuid().ToString("N").ToLower() + ".jpg";

if (request.ImageFileContentType is null)
request.ImageFileContentType = Utilities.FileNameToContentTypeMapper.GetContentTypeForImage(request.ImageFileName) ?? "image/jpeg";
request.ImageFileContentType = MimeTypes.GetMimeMapping(request.ImageFileName);

IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "api", "promotion", "v2", "activity", "upload_popup_screenshot/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="3.0.0" />
<PackageReference Include="SKIT.FlurlHttpClient.Common" Version="3.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
using System;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;

namespace SKIT.FlurlHttpClient.ByteDance.MicroApp.Utilities
{
using SKIT.FlurlHttpClient;

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)
public static MultipartFormDataContent Build(string fileName, byte[] fileBytes, string? fileContentType, string formDataName, Action<HttpContent>? configureFileHttpContent = null)
{
if (fileName is null) throw new ArgumentNullException(nameof(fileName));
if (formDataName is null) throw new ArgumentNullException(nameof(formDataName));
if (configureFileHttpContent is null) throw new ArgumentNullException(nameof(configureFileHttpContent));

fileName = fileName.Replace("\"", "");
fileBytes = fileBytes ?? Array.Empty<byte>();
fileContentType = string.IsNullOrEmpty(fileContentType) ? "application/octet-stream" : fileContentType;
fileContentType = string.IsNullOrEmpty(fileContentType) ? MimeTypes.Binary : fileContentType;
formDataName = formDataName.Replace("\"", "");

ByteArrayContent fileContent = new ByteArrayContent(fileBytes);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(fileContentType);
configureFileHttpContent(fileContent);
configureFileHttpContent?.Invoke(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.Headers.ContentType = MediaTypeHeaderValue.Parse($"{MimeTypes.FormData}; boundary={boundary}");
httpContent.Add(fileContent, $"\"{formDataName}\"", $"\"{HttpUtility.UrlEncode(fileName)}\"");
return httpContent;
}
Expand Down
Loading

0 comments on commit 09c066b

Please sign in to comment.