diff --git a/Attributes/YYAuthAttribute.cs b/Attributes/YYAuthAttribute.cs
index 08876d2..08752b2 100644
--- a/Attributes/YYAuthAttribute.cs
+++ b/Attributes/YYAuthAttribute.cs
@@ -1,4 +1,5 @@
-using System.Linq;
+using System;
+using System.Linq;
using Microsoft.AspNetCore.Mvc.Filters;
using YYApi.Communications;
using YYApi.Exceptions;
@@ -8,6 +9,7 @@ namespace YYApi.Attributes
///
/// 检测登录中间件
///
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class YYAuthAttribute : ActionFilterAttribute
{
private string[] Types { get; }
diff --git a/Attributes/YYDocAttribute.cs b/Attributes/YYDocAttribute.cs
index b6cc66a..21972e2 100644
--- a/Attributes/YYDocAttribute.cs
+++ b/Attributes/YYDocAttribute.cs
@@ -1,10 +1,12 @@
-using Swashbuckle.AspNetCore.Annotations;
+using System;
+using Swashbuckle.AspNetCore.Annotations;
namespace YYApi.Attributes
{
///
/// 快捷自定义接口文档类
///
+ [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class YYDocAttribute : SwaggerOperationAttribute
{
///
diff --git a/Controllers/YYBaseController.cs b/Controllers/YYBaseController.cs
index 750c7cd..35d8ef9 100644
--- a/Controllers/YYBaseController.cs
+++ b/Controllers/YYBaseController.cs
@@ -46,7 +46,7 @@ namespace YYApi.Controllers
/// 错误代码
/// 错误描述(若是常规错误,代码可自动带取描述)
///
- protected static void Error(int code, string message = "")
+ protected static void Error(int code = 500, string message = "")
{
throw new YYApiException(code, message);
}
@@ -57,7 +57,7 @@ namespace YYApi.Controllers
/// 检测条件
/// 错误代码
/// 错误描述
- protected static void ErrorWhen(bool condition, int code, string message = "")
+ protected static void ErrorWhen(bool condition, int code = 500, string message = "")
{
if (condition)
{
@@ -71,7 +71,7 @@ namespace YYApi.Controllers
/// 检测条件
/// 错误代码
/// 错误描述
- protected static void ErrorWhenNull(object condition, int code, string message = "")
+ protected static void ErrorWhenNull(object condition, int code = 404, string message = "请求的资源不存在")
{
ErrorWhen(condition == null, code, message);
}
diff --git a/Extensions.cs b/Extensions.cs
index a5c2e47..9e249e6 100644
--- a/Extensions.cs
+++ b/Extensions.cs
@@ -25,20 +25,20 @@ namespace YYApi
public static IServiceCollection AddYYApi(this IServiceCollection builder, string title,
string description = null)
{
- return builder.AddYYAuth().AddYYDoc(title, description).AddCors();
+ return builder.AddYYAuth().AddYYDoc(title, description).AddCors().AddYYUpload();
}
///
- /// 使用所有YYApi自定义中间件
+ /// 使用所有YYApi自定义中间件
///
///
///
+ ///
///
///
- public static IApplicationBuilder UseYYApi(this IApplicationBuilder builder, string title = "API文档",
- params SubmitMethod[] submitMethods)
+ public static IApplicationBuilder UseYYApi(this IApplicationBuilder builder, string title = "API文档", params SubmitMethod[] submitMethods)
{
- return builder.UseYYException().UseYYDoc(title, submitMethods).UseMiddleware().UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
+ return builder.UseYYException().UseYYDoc(title, submitMethods).UseMiddleware().UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()).UseYYUpload();
}
@@ -55,6 +55,16 @@ namespace YYApi
return builder.AddScoped();
}
+ ///
+ /// 添加Base64上传组件
+ ///
+ ///
+ ///
+ public static IServiceCollection AddYYUpload(this IServiceCollection builder)
+ {
+ return builder.AddSingleton();
+ }
+
///
/// 添加Api文档服务
///
@@ -95,6 +105,16 @@ namespace YYApi
return builder.UseMiddleware();
}
+ ///
+ /// 配置上传组件必须
+ ///
+ ///
+ ///
+ public static IApplicationBuilder UseYYUpload(this IApplicationBuilder builder)
+ {
+ return builder.UseStaticFiles();
+ }
+
///
/// 使用文档UI
///
diff --git a/Helpers/YYUpload.cs b/Helpers/YYUpload.cs
new file mode 100644
index 0000000..79c80a3
--- /dev/null
+++ b/Helpers/YYUpload.cs
@@ -0,0 +1,82 @@
+using System;
+using System.IO;
+using Microsoft.AspNetCore.Hosting;
+
+namespace YYApi.Helpers
+{
+ public class YYUpload
+ {
+ private string FilePathFolder = "/uploads/";
+ public string FilePath
+ {
+ set
+ {
+ if (!value.StartsWith("/"))
+ {
+ FilePathFolder = $"/{value}";
+ }
+ if (!FilePathFolder.EndsWith("/"))
+ {
+ FilePathFolder += "/";
+ }
+ }
+ get
+ {
+ return FilePathFolder;
+ }
+ }
+
+ private IWebHostEnvironment Env { get; }
+
+ public YYUpload(IWebHostEnvironment env)
+ {
+ Env = env;
+ }
+
+ ///
+ /// 保存base64文件,如果有标头按照标头自动识别
+ ///
+ ///
+ /// 扩展名
+ /// 存放路径
+ /// 文件名
+ ///
+ public string SaveFile(string base64, string ext = null, string path = null, string fileName = null)
+ {
+ byte[] bt;
+ var filePath = FilePath + path;
+ if (fileName == null)
+ {
+ fileName = Guid.NewGuid().ToString();
+ }
+ var baseArr = base64.Split(";");
+ if (baseArr.Length == 2)
+ {
+ var info = baseArr[0].Split(":")[1].Split("/");
+ if (path == null)
+ {
+ filePath += $"{info[0]}/";
+ }
+ if (ext == null)
+ {
+ ext = info[1];
+ }
+ var data = baseArr[1].Split(",");
+ bt = Convert.FromBase64String(data[1]);
+ }
+ else
+ {
+ bt = Convert.FromBase64String(base64);
+ }
+
+ var realPath = Directory.GetCurrentDirectory() + "/wwwroot" + filePath;
+ if (!Directory.Exists(realPath))
+ {
+ Directory.CreateDirectory(filePath);
+ }
+ var fn = fileName + (string.IsNullOrEmpty(ext) ? string.Empty : $".{ext}");
+ File.WriteAllBytes(realPath + fn, bt);
+ return filePath + fn;
+ }
+ }
+}