Эх сурвалжийг харах

新增文件上传包含文件拓展名和排除文件拓展名校验
修复不添加OSS服务时,本地上传异常的问题

zhontai 2 жил өмнө
parent
commit
c5bb7f33a8

+ 3 - 1
src/hosts/ZhonTai.Host/Configs/appconfig.json

@@ -93,5 +93,7 @@
     "formatResult": true
   },
   //实现标准标识密码哈希
-  "passwordHasher": false
+  "passwordHasher": false,
+  //最大请求大小
+  "maxRequestBodySize": 104857600
 }

+ 7 - 1
src/hosts/ZhonTai.Host/Configs/ossconfig.json

@@ -5,7 +5,13 @@
     "Directory": "upload",
     //日期目录
     "DateTimeDirectory": "yyyy/MM/dd",
-    "Md5": false
+    "Md5": false,
+    //文件最大大小
+    "MaxSize": 104857600,
+    //包含文件拓展名
+    "IncludeExtension": [],
+    //排除文件拓展名
+    "ExcludeExtension": [ ".exe", ".dll", ".jar" ]
   },
   //文件存储供应商
   "Provider": "Minio",

+ 1 - 1
src/hosts/ZhonTai.Host/Program.cs

@@ -39,7 +39,7 @@ new HostApp(new HostAppOptions
             options.TaskHandler = new TaskHandler(options.FreeSql);
         });
 
-        context.Services.AddOSS();
+        //context.Services.AddOSS();
     },
 
     //配置mvc

+ 5 - 0
src/platform/ZhonTai.Admin/Core/Configs/AppConfig.cs

@@ -94,6 +94,11 @@ public class AppConfig
     /// 实现标准标识密码哈希
     /// </summary>
     public bool PasswordHasher { get; set; } = false;
+
+    /// <summary>
+    /// 最大请求大小
+    /// </summary>
+    public long MaxRequestBodySize { get; set; } = 104857600;
 }
 
 /// <summary>

+ 17 - 0
src/platform/ZhonTai.Admin/Core/Configs/OSSConfig.cs

@@ -67,14 +67,31 @@ public class LocalUploadConfig
     /// 上传目录
     /// </summary>
     public string Directory { get; set; } = "upload";
+
     /// <summary>
     /// 日期目录
     /// </summary>
     public string DateTimeDirectory { get; set; } = "yyyy/MM/dd";
+
     /// <summary>
     /// 文件Md5码
     /// </summary>
     public bool Md5 { get; set; } = false;
+
+    /// <summary>
+    /// 文件大小
+    /// </summary>
+    public long MaxSize { get; set; } = 104857600;
+
+    /// <summary>
+    /// 包含文件拓展名
+    /// </summary>
+    public string[] IncludeExtension { get; set; }
+
+    /// <summary>
+    /// 排除文件拓展名
+    /// </summary>
+    public string[] ExcludeExtension { get; set; }
 }
 
 /// <summary>

+ 3 - 9
src/platform/ZhonTai.Admin/Core/Extensions/ServiceCollectionExtensions.cs

@@ -1,10 +1,8 @@
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Options;
 using Minio;
 using OnceMi.AspNetCore.OSS;
 using ZhonTai.Admin.Core.Configs;
-using ZhonTai.Common.Helpers;
 using OSSOptions = ZhonTai.Admin.Core.Configs.OSSOptions;
 
 namespace ZhonTai.Admin.Core.Extensions;
@@ -44,11 +42,7 @@ public static class ServiceCollectionExtensions
 
     public static IServiceCollection AddOSS(this IServiceCollection services)
     {
-        var env = services.BuildServiceProvider().GetRequiredService<IWebHostEnvironment>();
-        var oSSConfigRoot = ConfigHelper.Load("ossconfig", env.EnvironmentName, true);
-        services.Configure<OSSConfig>(oSSConfigRoot);
-
-        var oSSConfig = oSSConfigRoot.Get<OSSConfig>();
+        var oSSConfig = services.BuildServiceProvider().GetRequiredService<IOptions<OSSConfig>>().Value;
         if (oSSConfig.OSSConfigs?.Count > 0)
         {
             foreach (var oSSOptions in oSSConfig.OSSConfigs)

+ 4 - 2
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -53,7 +53,6 @@ using ZhonTai.Admin.Core.Dto;
 using ZhonTai.DynamicApi.Attributes;
 using System.Text.RegularExpressions;
 using Swashbuckle.AspNetCore.SwaggerGen;
-using Newtonsoft.Json.Converters;
 using System.Text.Json.Serialization;
 
 namespace ZhonTai.Admin.Core;
@@ -104,6 +103,9 @@ public class HostApp
             builder.Configuration.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
         }
 
+        var oSSConfigRoot = ConfigHelper.Load("ossconfig", env.EnvironmentName, true);
+        services.Configure<OSSConfig>(oSSConfigRoot);
+
         //应用配置
         services.AddSingleton(appConfig);
 
@@ -126,7 +128,7 @@ public class HostApp
         builder.WebHost.ConfigureKestrel((context, options) =>
         {
             //设置应用服务器Kestrel请求体最大为100MB
-            options.Limits.MaxRequestBodySize = 1024 * 1024 * 100;
+            options.Limits.MaxRequestBodySize = appConfig.MaxRequestBodySize;
         });
 
         //访问地址

+ 22 - 3
src/platform/ZhonTai.Admin/Services/File/FileService.cs

@@ -119,6 +119,25 @@ public class FileService : BaseService, IFileService, IDynamicApi
     public async Task<FileEntity> UploadFileAsync([Required] IFormFile file, string fileDirectory = "", bool fileReName = true)
     {
         var localUploadConfig = _oSSConfig.LocalUploadConfig;
+
+        var extention = Path.GetExtension(file.FileName).ToLower();
+        var hasIncludeExtension = localUploadConfig.IncludeExtension?.Length > 0;
+        if(hasIncludeExtension && !localUploadConfig.IncludeExtension.Contains(extention))
+        {
+            throw new Exception($"不允许上传{extention}文件格式");
+        }
+        var hasExcludeExtension = localUploadConfig.ExcludeExtension?.Length > 0;
+        if (hasExcludeExtension && localUploadConfig.ExcludeExtension.Contains(extention))
+        {
+            throw new Exception($"不允许上传{extention}文件格式");
+        }
+
+        var fileLenth = file.Length;
+        if(fileLenth > localUploadConfig.MaxSize) 
+        {
+            throw new Exception($"文件大小不能超过{new FileSize(localUploadConfig.MaxSize)}");
+        }
+       
         var oSSOptions = _oSSConfig.OSSConfigs.Where(a => a.Enable && a.Provider == _oSSConfig.Provider).FirstOrDefault();
         var enableOss = oSSOptions != null && oSSOptions.Enable;
         var enableMd5 = enableOss ? oSSOptions.Md5 : localUploadConfig.Md5;
@@ -136,7 +155,7 @@ public class FileService : BaseService, IFileService, IDynamicApi
                     FileGuid = FreeUtil.NewMongodbId(),
                     SaveFileName = md5FileEntity.SaveFileName,
                     FileName = Path.GetFileNameWithoutExtension(file.FileName),
-                    Extension = Path.GetExtension(file.FileName).ToLower(),
+                    Extension = extention,
                     FileDirectory = md5FileEntity.FileDirectory,
                     Size = md5FileEntity.Size,
                     SizeFormat = md5FileEntity.SizeFormat,
@@ -157,14 +176,14 @@ public class FileService : BaseService, IFileService, IDynamicApi
             }
         }
 
-        var fileSize = new FileSize(file.Length);
+        var fileSize = new FileSize(fileLenth);
         var fileEntity = new FileEntity
         {
             Provider = oSSOptions?.Provider,
             BucketName = oSSOptions?.BucketName,
             FileGuid = FreeUtil.NewMongodbId(),
             FileName = Path.GetFileNameWithoutExtension(file.FileName),
-            Extension = Path.GetExtension(file.FileName).ToLower(),
+            Extension = extention,
             FileDirectory = fileDirectory,
             Size = fileSize.Size,
             SizeFormat = fileSize.ToString(),

+ 20 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -376,6 +376,11 @@
             实现标准标识密码哈希
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.AppConfig.MaxRequestBodySize">
+            <summary>
+            最大请求大小
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Configs.SwaggerConfig">
             <summary>
             Swagger配置
@@ -781,6 +786,21 @@
             文件Md5码
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.LocalUploadConfig.MaxSize">
+            <summary>
+            文件大小
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.LocalUploadConfig.IncludeExtension">
+            <summary>
+            包含文件拓展名
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.LocalUploadConfig.ExcludeExtension">
+            <summary>
+            排除文件拓展名
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Configs.OSSConfig">
             <summary>
             OSS配置