Skip to content

feat: support aliyun oss download link generation #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.2"/>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ public CqrsInjector AddFileProvider<TProvider>()
return this;
}

/// <summary>
/// Use given implementation of <see cref="IFileDeliveryProvider"/>.
/// </summary>
/// <typeparam name="TProvider">The type of implementation.</typeparam>
/// <returns></returns>
public CqrsInjector AddFileDeliveryProvider<TProvider>()
where TProvider : class, IFileDeliveryProvider
{
Services.AddScoped<IFileDeliveryProvider, TProvider>();
return this;
}

/// <summary>
/// 添加自定义随机数提供器。
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Cnblogs.Architecture.Ddd.Cqrs.AspNetCore\CqrsHeaderNames.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task PublishAsync(string eventName, IntegrationEvent @event)
eventName,
@event,
@event.TraceId ?? @event.Id);
object data = @event; // do not provide type information to serializer since it's base class.
object data = @event; // do not provide type information to serializer since it's base class.
await _daprClient.PublishEventAsync(
DaprOptions.PubSubName,
DaprUtils.GetDaprTopicName(_daprOptions.AppName, eventName),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;

/// <summary>
/// File provider that can create public url for user to download
/// </summary>
public interface IFileDeliveryProvider
{
/// <summary>
/// Get public url to download with validate time.
/// </summary>
/// <param name="filename">The file filename.</param>
/// <param name="duration">Duration of url availability.</param>
/// <returns></returns>
public Task<string> GetDownloadUrlAsync(string filename, TimeSpan duration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ClickHouse.Client" Version="6.8.1" />
<PackageReference Include="ClickHouse.Client" Version="7.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;
using Cuiliang.AliyunOssSdk;
using Microsoft.Extensions.Options;

namespace Cnblogs.Architecture.Ddd.Infrastructure.FileProviders.AliyunOss;

/// <summary>
/// Aliyun OSS implementation of <see cref="IFileDeliveryProvider"/>.
/// </summary>
public class AliyunOssFileDeliveryProvider : IFileDeliveryProvider
{
private readonly OssClient _client;
private readonly AliyunOssOptions _options;

/// <summary>
/// Create a <see cref="AliyunOssFileDeliveryProvider"/>.
/// </summary>
/// <param name="client">The oss client.</param>
/// <param name="options">The options for oss client.</param>
public AliyunOssFileDeliveryProvider(OssClient client, IOptions<AliyunOssOptions> options)
{
_client = client;
_options = options.Value;
}

/// <inheritdoc />
public async Task<string> GetDownloadUrlAsync(string filename, TimeSpan duration)
{
var meta = await _client.GetObjectMetaAsync(_options.BucketInfo, filename);
if (meta.IsSuccess == false)
{
throw new FileNotFoundException(meta.ErrorMessage, filename, meta.InnerException);
}

return _client.GetFileDownloadLink(
_options.BucketInfo,
filename,
(int)Math.Ceiling(duration.TotalSeconds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Cnblogs.Architecture.Ddd.Infrastructure.FileProviders.AliyunOss;
public static class CqrsInjectorExtensions
{
/// <summary>
/// Use aliyun oss as default implementation of <see cref="IFileProvider"/>.
/// Use aliyun oss as default implementation of <see cref="IFileProvider"/> and <see cref="IFileDeliveryProvider"/>.
/// </summary>
/// <param name="injector"></param>
/// <param name="configuration"></param>
Expand All @@ -24,6 +24,7 @@ public static CqrsInjector UseAliyunOssFileProvider(
{
injector.Services.AddOssClient(configuration, configurationSectionName);
injector.Services.Configure<AliyunOssOptions>(configuration.GetSection(configurationSectionName));
return injector.AddFileProvider<AliyunOssFileProvider>();
return injector.AddFileProvider<AliyunOssFileProvider>()
.AddFileDeliveryProvider<AliyunOssFileDeliveryProvider>();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Cnblogs.Serilog.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public async Task GetItem_MapHeadAndGet_SuccessAsync()
// Act
var uris = new[]
{
"/api/v1/apps/-/strings/-/value", "/api/v1/apps/-/strings/1/value",
"/api/v1/apps/someApp/strings/-/value", "/api/v1/apps/someApp/strings/1/value"
"/api/v1/apps/-/strings/-/value", "/api/v1/apps/-/strings/1/value", "/api/v1/apps/someApp/strings/-/value", "/api/v1/apps/someApp/strings/1/value"
}.Select(x => new HttpRequestMessage(HttpMethod.Head, x));
var responses = new List<HttpResponseMessage>();
foreach (var uri in uris)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
Expand Down