Compare commits

..
5 Commits
Author SHA1 Message Date
xrain 62ddd6698e now simapiauth can use , for role 2022-09-02 09:47:41 +08:00
xrain b2a6e5f40f make storage bucket public 2022-08-17 20:26:25 +08:00
xrain 801f698dcf add storage get url 2022-08-17 19:37:39 +08:00
xrain 9c939426ba fix logger 2022-08-05 02:33:47 +08:00
xrain e45cc4d28d fix logger 2022-08-05 02:20:27 +08:00
3 changed files with 47 additions and 36 deletions
+9 -3
View File
@@ -19,13 +19,16 @@ namespace SimApi.Attributes
//默认是user登录类型 //默认是user登录类型
public SimApiAuthAttribute() public SimApiAuthAttribute()
{ {
Types = new[] { "user" }; Types = new[]
{
"user"
};
} }
//只检测一种用户类型的快捷方式 //只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type) public SimApiAuthAttribute(string type)
{ {
Types = new[] { type }; Types = type.Split(",");
} }
//设定特定类型的检测 //设定特定类型的检测
@@ -37,7 +40,10 @@ namespace SimApi.Attributes
//只检测一种用户类型的快捷方式 //只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type, string url) public SimApiAuthAttribute(string type, string url)
{ {
Types = new[] { type }; Types = new[]
{
type
};
new HttpPostAttribute(url); new HttpPostAttribute(url);
} }
+27 -24
View File
@@ -15,7 +15,7 @@ namespace SimApi.Helpers
private string ServeUrl { get; } private string ServeUrl { get; }
private string Bucket { get; } public string Bucket { get; }
private IHttpContextAccessor HttpContextAccessor { get; } private IHttpContextAccessor HttpContextAccessor { get; }
@@ -49,40 +49,43 @@ namespace SimApi.Helpers
} }
Mc = mcb.Build(); Mc = mcb.Build();
bool found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result; var found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result;
if (!found) if (!found)
{ {
Mc.MakeBucketAsync(new MakeBucketArgs().WithBucket(Bucket)).Wait(); Mc.MakeBucketAsync(new MakeBucketArgs().WithBucket(Bucket)).Wait();
} }
} }
/// <summary>
/// 获取上传的URL
/// </summary>
/// <param name="path"></param>
/// <param name="expire"></param>
/// <returns></returns>
public string GetUploadUrl(string path, int expire = 7200) public string GetUploadUrl(string path, int expire = 7200)
{ {
try return Mc.PresignedPutObjectAsync(new PresignedPutObjectArgs().WithBucket(Bucket)
{ .WithObject(path).WithExpiry(expire)).Result;
return Mc.PresignedPutObjectAsync(new PresignedPutObjectArgs().WithBucket(Bucket)
.WithObject(path).WithExpiry(expire)).Result;
}
catch (MinioException e)
{
Console.WriteLine("Error occurred: " + e);
return e.Message;
}
} }
public string UploadFile(string path, Stream stream,string contentType = "image/png") /// <summary>
/// 获取下载的URL
/// </summary>
/// <param name="path"></param>
/// <param name="expire"></param>
/// <returns></returns>
public string GetDownloadUrl(string path, int expire = 600)
{ {
try return Mc.PresignedGetObjectAsync(new PresignedGetObjectArgs().WithBucket(Bucket).WithObject(path)
{ .WithExpiry(expire)).Result;
Mc.PutObjectAsync(new PutObjectArgs().WithBucket(Bucket).WithObject(path). }
WithObjectSize(stream.Length).WithStreamData(stream).WithContentType(contentType)).Wait();
return null;
} public string UploadFile(string path, Stream stream, string contentType = "image/png")
catch (MinioException e) {
{ Mc.PutObjectAsync(new PutObjectArgs().WithBucket(Bucket).WithObject(path).WithObjectSize(stream.Length)
Console.WriteLine("Error occurred: " + e); .WithStreamData(stream).WithContentType(contentType)).Wait();
return e.Message; return null;
}
} }
public string FullUrl(string path) public string FullUrl(string path)
+9 -7
View File
@@ -22,7 +22,14 @@ namespace SimApi
{ {
var simApiOptions = new SimApiOptions(); var simApiOptions = new SimApiOptions();
options?.Invoke(simApiOptions); options?.Invoke(simApiOptions);
if (simApiOptions.EnableLogger)
{
builder.AddLogging(logger =>
{
logger.ClearProviders();
logger.AddProvider(new SimApiLoggerProvider());
});
}
// 是否使用 AUTH // 是否使用 AUTH
if (simApiOptions.EnableSimApiAuth) if (simApiOptions.EnableSimApiAuth)
{ {
@@ -190,13 +197,8 @@ namespace SimApi
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder) public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
{ {
var options = builder.ApplicationServices.GetRequiredService<SimApiOptions>(); var options = builder.ApplicationServices.GetRequiredService<SimApiOptions>();
if (options.EnableLogger)
{
builder.ApplicationServices.GetRequiredService<ILoggerFactory>()
.AddProvider(new SimApiLoggerProvider());
}
var logger = builder.ApplicationServices.GetRequiredService<ILogger<SimApiLogger>>(); var logger = builder.ApplicationServices.GetRequiredService<ILogger<SimApiOptions>>();
if (options.EnableForwardHeaders) if (options.EnableForwardHeaders)
{ {