Parcourir la source

新增 文件上传支持文件自动重命名开关设置

zhontai il y a 2 ans
Parent
commit
d840338681

+ 6 - 0
src/platform/ZhonTai.Admin/Domain/File/FileEntity.cs

@@ -36,6 +36,12 @@ public partial class FileEntity : EntityBase
     [OrderGuid]
     public Guid FileGuid { get; set; }
 
+    /// <summary>
+    /// 保存文件名
+    /// </summary>
+    [Column(StringLength = 200)]
+    public string SaveFileName { get; set; }
+
     /// <summary>
     /// 文件名
     /// </summary>

+ 16 - 12
src/platform/ZhonTai.Admin/Services/File/FileService.cs

@@ -82,7 +82,7 @@ public class FileService : BaseService, IFileService, IDynamicApi
             return;
         }
 
-        var shareFile = await _fileRepository.Where(a=>a.Id != input.Id && a.FileGuid == file.FileGuid).AnyAsync();
+        var shareFile = await _fileRepository.Where(a=>a.Id != input.Id && a.LinkUrl == file.LinkUrl).AnyAsync();
         if (!shareFile)
         {
             if(file.Provider.HasValue)
@@ -92,14 +92,14 @@ public class FileService : BaseService, IFileService, IDynamicApi
                 var enableOss = oSSOptions != null && oSSOptions.Enable;
                 if (enableOss)
                 {
-                    var filePath = Path.Combine(file.FileDirectory, file.FileGuid + file.Extension).ToPath();
+                    var filePath = Path.Combine(file.FileDirectory, file.SaveFileName + file.Extension).ToPath();
                     await oSSService.RemoveObjectAsync(file.BucketName, filePath);
                 }
             }
             else
             {
                 var env = LazyGetRequiredService<IWebHostEnvironment>();
-                var filePath = Path.Combine(env.WebRootPath, file.FileDirectory, file.FileGuid + file.Extension).ToPath();
+                var filePath = Path.Combine(env.WebRootPath, file.FileDirectory, file.SaveFileName + file.Extension).ToPath();
                 if (System.IO.File.Exists(filePath))
                 {
                     System.IO.File.Delete(filePath);
@@ -113,10 +113,11 @@ public class FileService : BaseService, IFileService, IDynamicApi
     /// <summary>
     /// 上传文件
     /// </summary>
-    /// <param name="file"></param>
-    /// <param name="fileDirectory"></param>
+    /// <param name="file">文件</param>
+    /// <param name="fileDirectory">文件目录</param>
+    /// <param name="fileReName">文件重命名</param>
     /// <returns></returns>
-    public async Task<FileEntity> UploadFileAsync([Required] IFormFile file, string fileDirectory = "")
+    public async Task<FileEntity> UploadFileAsync([Required] IFormFile file, string fileDirectory = "", bool fileReName = true)
     {
         var localUploadConfig = _oSSConfig.LocalUploadConfig;
         var oSSOptions = _oSSConfig.OSSConfigs.Where(a => a.Enable && a.Provider == _oSSConfig.Provider).FirstOrDefault();
@@ -133,7 +134,8 @@ public class FileService : BaseService, IFileService, IDynamicApi
                 {
                     Provider = md5FileEntity.Provider,
                     BucketName = md5FileEntity.BucketName,
-                    FileGuid = md5FileEntity.FileGuid,
+                    FileGuid = FreeUtil.NewMongodbId(),
+                    SaveFileName = md5FileEntity.SaveFileName,
                     FileName = Path.GetFileNameWithoutExtension(file.FileName),
                     Extension = Path.GetExtension(file.FileName).ToLower(),
                     FileDirectory = md5FileEntity.FileDirectory,
@@ -169,8 +171,9 @@ public class FileService : BaseService, IFileService, IDynamicApi
             SizeFormat = fileSize.ToString(),
             Md5 = md5
         };
+        fileEntity.SaveFileName = fileReName ? fileEntity.FileGuid.ToString() : fileEntity.FileName;
 
-        var filePath = Path.Combine(fileDirectory, fileEntity.FileGuid + fileEntity.Extension).ToPath();
+        var filePath = Path.Combine(fileDirectory, fileEntity.SaveFileName + fileEntity.Extension).ToPath();
         var url = string.Empty;
         if (enableOss)
         {
@@ -226,15 +229,16 @@ public class FileService : BaseService, IFileService, IDynamicApi
     /// <summary>
     /// 上传多文件
     /// </summary>
-    /// <param name="files"></param>
-    /// <param name="fileDirectory"></param>
+    /// <param name="files">文件列表</param>
+    /// <param name="fileDirectory">文件目录</param>
+    /// <param name="fileReName">文件重命名</param>
     /// <returns></returns>
-    public async Task<List<FileEntity>> UploadFilesAsync([Required] IFormFileCollection files, string fileDirectory = "")
+    public async Task<List<FileEntity>> UploadFilesAsync([Required] IFormFileCollection files, string fileDirectory = "", bool fileReName = true)
     {
         var fileList = new List<FileEntity>();
         foreach (var file in files)
         {
-            fileList.Add(await UploadFileAsync(file, fileDirectory));
+            fileList.Add(await UploadFileAsync(file, fileDirectory, fileReName));
         }
         return fileList;
     }

+ 2 - 2
src/platform/ZhonTai.Admin/Services/File/IFileService.cs

@@ -18,7 +18,7 @@ public interface IFileService
 
     Task DeleteAsync(FileDeleteInput input);
 
-    Task<FileEntity> UploadFileAsync(IFormFile file, string fileDirectory = "");
+    Task<FileEntity> UploadFileAsync(IFormFile file, string fileDirectory = "", bool fileReName = true);
 
-    Task<List<FileEntity>> UploadFilesAsync([Required] IFormFileCollection files, string fileDirectory = "");
+    Task<List<FileEntity>> UploadFilesAsync([Required] IFormFileCollection files, string fileDirectory = "", bool fileReName = true);
 }

+ 2 - 2
src/platform/ZhonTai.Admin/ZhonTai.Admin.csproj

@@ -26,8 +26,8 @@
 		<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.1" />
 		<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
 		<PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
-		<PackageReference Include="NLog" Version="5.1.0" />
-		<PackageReference Include="NLog.Web.AspNetCore" Version="5.2.0" />
+		<PackageReference Include="NLog" Version="5.1.1" />
+		<PackageReference Include="NLog.Web.AspNetCore" Version="5.2.1" />
 		<PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
 		<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
 		<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />

+ 9 - 2
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -2182,6 +2182,11 @@
             文件Guid
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Domain.File.FileEntity.SaveFileName">
+            <summary>
+            保存文件名
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Domain.File.FileEntity.FileName">
             <summary>
             文件名
@@ -4458,20 +4463,22 @@
             <param name="input"></param>
             <returns></returns>
         </member>
-        <member name="M:ZhonTai.Admin.Services.File.FileService.UploadFileAsync(Microsoft.AspNetCore.Http.IFormFile,System.String)">
+        <member name="M:ZhonTai.Admin.Services.File.FileService.UploadFileAsync(Microsoft.AspNetCore.Http.IFormFile,System.String,System.Boolean)">
             <summary>
             上传文件
             </summary>
             <param name="file"></param>
             <param name="fileDirectory"></param>
+            <param name="fileReName"></param>
             <returns></returns>
         </member>
-        <member name="M:ZhonTai.Admin.Services.File.FileService.UploadFilesAsync(Microsoft.AspNetCore.Http.IFormFileCollection,System.String)">
+        <member name="M:ZhonTai.Admin.Services.File.FileService.UploadFilesAsync(Microsoft.AspNetCore.Http.IFormFileCollection,System.String,System.Boolean)">
             <summary>
             上传多文件
             </summary>
             <param name="files"></param>
             <param name="fileDirectory"></param>
+            <param name="fileReName"></param>
             <returns></returns>
         </member>
         <member name="T:ZhonTai.Admin.Services.File.IFileService">