Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5e4d57772 | ||
|
|
239cd8bcbb | ||
|
|
62ddd6698e | ||
|
|
b2a6e5f40f | ||
|
|
801f698dcf |
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +56,7 @@ namespace SimApi.Attributes
|
|||||||
throw new SimApiException(401);
|
throw new SimApiException(401);
|
||||||
}
|
}
|
||||||
//检测用户类型
|
//检测用户类型
|
||||||
if (Types.Intersect(loginInfo.Type).Count() == 0)
|
if (!Types.Intersect(loginInfo.Type).Any())
|
||||||
{
|
{
|
||||||
throw new SimApiException(403);
|
throw new SimApiException(403);
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-24
@@ -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)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace SimApi.Middlewares
|
|||||||
public class SimApiExceptionMiddleware
|
public class SimApiExceptionMiddleware
|
||||||
{
|
{
|
||||||
private RequestDelegate Next { get; }
|
private RequestDelegate Next { get; }
|
||||||
|
|
||||||
private ILogger<SimApiExceptionMiddleware> Log { get; }
|
private ILogger<SimApiExceptionMiddleware> Log { get; }
|
||||||
|
|
||||||
public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
|
||||||
@@ -35,7 +36,10 @@ namespace SimApi.Middlewares
|
|||||||
await Next(context);
|
await Next(context);
|
||||||
if (context.Response.StatusCode != 200)
|
if (context.Response.StatusCode != 200)
|
||||||
{
|
{
|
||||||
if (!new[] {301, 302}.Contains(context.Response.StatusCode))
|
if (!new[]
|
||||||
|
{
|
||||||
|
301, 302
|
||||||
|
}.Contains(context.Response.StatusCode))
|
||||||
{
|
{
|
||||||
throw new SimApiException(context.Response.StatusCode);
|
throw new SimApiException(context.Response.StatusCode);
|
||||||
}
|
}
|
||||||
@@ -70,9 +74,12 @@ namespace SimApi.Middlewares
|
|||||||
/// <param name="response"></param>
|
/// <param name="response"></param>
|
||||||
private void ErrorResponse(HttpContext context, SimApiBaseResponse response)
|
private void ErrorResponse(HttpContext context, SimApiBaseResponse response)
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = 200;
|
if (!context.Response.HasStarted)
|
||||||
context.Response.Headers.Add("Content-Type", "application/json");
|
{
|
||||||
context.Response.WriteAsync(response.ToString());
|
context.Response.StatusCode = 200;
|
||||||
|
context.Response.Headers.Add("Content-Type", "application/json");
|
||||||
|
context.Response.WriteAsync(response.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+4
-4
@@ -14,7 +14,7 @@
|
|||||||
<SynchReleaseVersion>false</SynchReleaseVersion>
|
<SynchReleaseVersion>false</SynchReleaseVersion>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<PackageVersion>5.0.2</PackageVersion>
|
<PackageVersion>5.0.2</PackageVersion>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -27,9 +27,9 @@
|
|||||||
<Folder Include="Configurations\" />
|
<Folder Include="Configurations\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Minio" Version="4.0.4" />
|
<PackageReference Include="Minio" Version="4.0.7" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.3.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<MonoDevelop>
|
<MonoDevelop>
|
||||||
|
|||||||
Reference in New Issue
Block a user