Ver código fonte

文件上传服务新增本地文件存储和本地文件删除功能

zhontai 2 anos atrás
pai
commit
2272b8c90d

+ 19 - 10
src/hosts/ZhonTai.Host/Configs/ossconfig.json

@@ -1,6 +1,15 @@
 {
+  //本地上传配置
+  "LocalUploadConfig": {
+    //上传目录
+    "Directory": "upload",
+    //日期目录
+    "DateTimeDirectory": "yyyy/MM/dd",
+    "Md5": false
+  },
   //文件存储供应商
   "Provider": "Minio",
+  //OSS配置列表
   "OSSConfigs": [
     //Minio
     {
@@ -13,8 +22,8 @@
       "IsEnableCache": true,
       "BucketName": "admin",
       "Url": "", //文件外链
-      "Enable": false,
-      "Md5":  false
+      "Md5": false,
+      "Enable": false
     },
     //阿里云
     {
@@ -27,8 +36,8 @@
       "IsEnableCache": true,
       "BucketName": "admin",
       "Url": "",
-      "Enable": false,
-      "Md5": false
+      "Md5": false,
+      "Enable": false
     },
     //腾讯云
     {
@@ -41,8 +50,8 @@
       "IsEnableCache": true,
       "BucketName": "admin",
       "Url": "",
-      "Enable": false,
-      "Md5": false
+      "Md5": false,
+      "Enable": false
     },
     //七牛
     {
@@ -55,8 +64,8 @@
       "IsEnableCache": true,
       "BucketName": "admin",
       "Url": "",
-      "Enable": false,
-      "Md5": false
+      "Md5": false,
+      "Enable": false
     },
     //华为云
     {
@@ -69,8 +78,8 @@
       "IsEnableCache": true,
       "BucketName": "admin",
       "Url": "",
-      "Enable": false,
-      "Md5": false
+      "Md5": false,
+      "Enable": false
     }
   ]
 }

+ 70 - 1
src/platform/ZhonTai.Admin/Core/Configs/OSSConfig.cs

@@ -3,19 +3,77 @@ using System.Collections.Generic;
 
 namespace ZhonTai.Admin.Core.Configs;
 
+/// <summary>
+/// OSS配置
+/// </summary>
 public class OSSOptions
 {
+    /// <summary>
+    /// 文件存储供应商
+    /// </summary>
     public OSSProvider Provider { get; set; } = OSSProvider.Minio;
+    /// <summary>
+    /// 域名
+    /// </summary>
     public string Endpoint { get; set; }
+    /// <summary>
+    /// 账号
+    /// </summary>
     public string AccessKey { get; set; }
+    /// <summary>
+    /// 密码
+    /// </summary>
     public string SecretKey { get; set; }
+    /// <summary>
+    /// 地区
+    /// </summary>
     public string Region { get; set; }
+    /// <summary>
+    /// 会话Token
+    /// </summary>
     public string SessionToken { get; set; }
-    public bool IsEnableHttps { get; set; } = true;
+    /// <summary>
+    /// 启用Https
+    /// </summary>
+    public bool IsEnableHttps { get; set; }
+    /// <summary>
+    /// 启用缓存
+    /// </summary>
     public bool IsEnableCache { get; set; }
+    /// <summary>
+    /// 存储桶
+    /// </summary>
     public string BucketName { get; set; } = "admin";
+    /// <summary>
+    /// 文件地址
+    /// </summary>
     public string Url { get; set; }
+    /// <summary>
+    /// 文件Md5码
+    /// </summary>
+    public bool Md5 { get; set; } = false;
+    /// <summary>
+    /// 启用
+    /// </summary>
     public bool Enable { get; set; } = false;
+}
+
+/// <summary>
+/// 本地上传配置
+/// </summary>
+public class LocalUploadConfig
+{
+    /// <summary>
+    /// 上传目录
+    /// </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;
 }
 
@@ -24,7 +82,18 @@ public class OSSOptions
 /// </summary>
 public class OSSConfig
 {
+    /// <summary>
+    /// 本地上传配置
+    /// </summary>
+    public LocalUploadConfig LocalUploadConfig { get; set; }
+
+    /// <summary>
+    /// 文件存储供应商
+    /// </summary>
     public OSSProvider Provider { get; set; } = OSSProvider.Minio;
 
+    /// <summary>
+    /// OSS配置列表
+    /// </summary>
     public List<OSSOptions> OSSConfigs { get; set; }
 }

+ 75 - 27
src/platform/ZhonTai.Admin/Services/File/FileService.cs

@@ -18,6 +18,8 @@ using ZhonTai.Common.Files;
 using ZhonTai.Common.Helpers;
 using System.ComponentModel.DataAnnotations;
 using System.Collections.Generic;
+using ZhonTai.Admin.Core.Helpers;
+using Microsoft.AspNetCore.Hosting;
 
 namespace ZhonTai.Admin.Services.File;
 
@@ -32,6 +34,8 @@ public class FileService : BaseService, IFileService, IDynamicApi
 
     private OSSConfig _oSSConfig => LazyGetRequiredService<IOptions<OSSConfig>>().Value;
 
+    private IHttpContextAccessor _httpContextAccessor => LazyGetRequiredService<IHttpContextAccessor>();
+
     public FileService()
     {
 
@@ -81,12 +85,25 @@ public class FileService : BaseService, IFileService, IDynamicApi
         var shareFile = await _fileRepository.Where(a=>a.Id != input.Id && a.FileGuid == file.FileGuid).AnyAsync();
         if (!shareFile)
         {
-            var oSSService = _oSSServiceFactory.Create(file.Provider.ToString());
-            var oSSOptions = _oSSConfig.OSSConfigs.Where(a => a.Enable && a.Provider == file.Provider).FirstOrDefault();
-            if (oSSOptions.Enable)
+            if(file.Provider.HasValue)
             {
-                var filePath = Path.Combine(file.FileDirectory, file.FileGuid + file.Extension).ToPath();
-                await oSSService.RemoveObjectAsync(file.BucketName, filePath);
+                var oSSService = _oSSServiceFactory.Create(file.Provider.ToString());
+                var oSSOptions = _oSSConfig.OSSConfigs.Where(a => a.Enable && a.Provider == file.Provider).FirstOrDefault();
+                var enableOss = oSSOptions != null && oSSOptions.Enable;
+                if (enableOss)
+                {
+                    var filePath = Path.Combine(file.FileDirectory, file.FileGuid + 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();
+                if (System.IO.File.Exists(filePath))
+                {
+                    System.IO.File.Delete(filePath);
+                }
             }
         }
 
@@ -101,14 +118,15 @@ public class FileService : BaseService, IFileService, IDynamicApi
     /// <returns></returns>
     public async Task<FileEntity> UploadFileAsync([Required] IFormFile file, string fileDirectory = "")
     {
-        var oSSService = _oSSServiceFactory.Create(_oSSConfig.Provider.ToString());
+        var localUploadConfig = _oSSConfig.LocalUploadConfig;
         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;
         var md5 = string.Empty;
-        if (oSSOptions.Md5)
+        if (enableMd5)
         {
             md5 = MD5Encrypt.GetHash(file.OpenReadStream());
-            var md5FileEntity = await _fileRepository.Where(a => a.Md5 == md5 && a.Provider == oSSOptions.Provider).FirstAsync();
+            var md5FileEntity = await _fileRepository.WhereIf(enableOss, a => a.Provider == oSSOptions.Provider).Where(a => a.Md5 == md5).FirstAsync();
             if (md5FileEntity != null)
             {
                 var sameFileEntity = new FileEntity
@@ -131,14 +149,18 @@ public class FileService : BaseService, IFileService, IDynamicApi
 
         if (fileDirectory.IsNull())
         {
-            fileDirectory = DateTime.Now.ToString("yyyy/MM/dd");
+            fileDirectory = localUploadConfig.Directory;
+            if (localUploadConfig.DateTimeDirectory.NotNull())
+            {
+                fileDirectory = Path.Combine(fileDirectory, DateTime.Now.ToString(localUploadConfig.DateTimeDirectory)).ToPath();
+            }
         }
 
         var fileSize = new FileSize(file.Length);
         var fileEntity = new FileEntity
         {
-            Provider = oSSOptions.Provider,
-            BucketName = oSSOptions.BucketName,
+            Provider = oSSOptions?.Provider,
+            BucketName = oSSOptions?.BucketName,
             FileGuid = FreeUtil.NewMongodbId(),
             FileName = Path.GetFileNameWithoutExtension(file.FileName),
             Extension = Path.GetExtension(file.FileName).ToLower(),
@@ -149,27 +171,53 @@ public class FileService : BaseService, IFileService, IDynamicApi
         };
 
         var filePath = Path.Combine(fileDirectory, fileEntity.FileGuid + fileEntity.Extension).ToPath();
-        var url = oSSOptions.Url;
-        if (url.IsNull())
+        var url = string.Empty;
+        if (enableOss)
         {
-            url = oSSOptions.Provider switch
+            url = oSSOptions.Url;
+            if (url.IsNull())
+            {
+                url = oSSOptions.Provider switch
+                {
+                    OSSProvider.Minio => $"{oSSOptions.Endpoint}/{oSSOptions.BucketName}",
+                    OSSProvider.Aliyun => $"{oSSOptions.BucketName}.{oSSOptions.Endpoint}",
+                    OSSProvider.QCloud => $"{oSSOptions.BucketName}-{oSSOptions.Endpoint}.cos.{oSSOptions.Region}.myqcloud.com",
+                    OSSProvider.Qiniu => $"{oSSOptions.BucketName}.{oSSOptions.Region}.qiniucs.com",
+                    OSSProvider.HuaweiCloud => $"{oSSOptions.BucketName}.{oSSOptions.Endpoint}",
+                    _ => ""
+                };
+            }
+            if (url.IsNull())
             {
-                OSSProvider.Minio => $"{oSSOptions.Endpoint}/{oSSOptions.BucketName}",
-                OSSProvider.Aliyun => $"{oSSOptions.BucketName}.{oSSOptions.Endpoint}",
-                OSSProvider.QCloud => $"{oSSOptions.BucketName}-{oSSOptions.Endpoint}.cos.{oSSOptions.Region}.myqcloud.com",
-                OSSProvider.Qiniu => $"{oSSOptions.BucketName}.{oSSOptions.Region}.qiniucs.com",
-                OSSProvider.HuaweiCloud => $"{oSSOptions.BucketName}.{oSSOptions.Endpoint}",
-                _ => ""
-            };
+                throw ResultOutput.Exception($"请配置{oSSOptions.Provider}的Url参数");
+            }
+
+            var urlProtocol = oSSOptions.IsEnableHttps ? "https" : "http";
+            fileEntity.LinkUrl = $"{urlProtocol}://{url}/{filePath}";
         }
-        if (url.IsNull())
+        else
         {
-            throw ResultOutput.Exception($"请配置{oSSOptions.Provider}的Url参数");
+            fileEntity.LinkUrl = $"{_httpContextAccessor.HttpContext.Request.Scheme}://{_httpContextAccessor.HttpContext.Request.Host.Value}/{filePath}";
         }
 
-        var urlProtocol = (oSSOptions.IsEnableHttps ? "https" : "http");
-        fileEntity.LinkUrl = $"{urlProtocol}://{url}/{filePath}";
-        await oSSService.PutObjectAsync(oSSOptions.BucketName, filePath, file.OpenReadStream());
+        if (enableOss)
+        {
+            var oSSService = _oSSServiceFactory.Create(_oSSConfig.Provider.ToString());
+            await oSSService.PutObjectAsync(oSSOptions.BucketName, filePath, file.OpenReadStream());
+        }
+        else
+        {
+            var uploadHelper = LazyGetRequiredService<UploadHelper>();
+            var env = LazyGetRequiredService<IWebHostEnvironment>();
+            fileDirectory = Path.Combine(env.WebRootPath, fileDirectory).ToPath();
+            if (!Directory.Exists(fileDirectory))
+            {
+                Directory.CreateDirectory(fileDirectory);
+            }
+            filePath = Path.Combine(env.WebRootPath, filePath).ToPath();
+            await uploadHelper.SaveAsync(file, filePath);
+        }
+       
         fileEntity = await _fileRepository.InsertAsync(fileEntity);
 
         return fileEntity;

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

@@ -656,11 +656,111 @@
             刷新有效期(分钟)
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Core.Configs.OSSOptions">
+            <summary>
+            OSS配置
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.Provider">
+            <summary>
+            文件存储供应商
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.Endpoint">
+            <summary>
+            域名
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.AccessKey">
+            <summary>
+            账号
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.SecretKey">
+            <summary>
+            密码
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.Region">
+            <summary>
+            地区
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.SessionToken">
+            <summary>
+            会话Token
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.IsEnableHttps">
+            <summary>
+            启用Https
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.IsEnableCache">
+            <summary>
+            启用缓存
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.BucketName">
+            <summary>
+            存储桶
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.Url">
+            <summary>
+            文件地址
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.Md5">
+            <summary>
+            文件Md5码
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSOptions.Enable">
+            <summary>
+            启用
+            </summary>
+        </member>
+        <member name="T:ZhonTai.Admin.Core.Configs.LocalUploadConfig">
+            <summary>
+            本地上传配置
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.LocalUploadConfig.Directory">
+            <summary>
+            上传目录
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.LocalUploadConfig.DateTimeDirectory">
+            <summary>
+            日期目录
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.LocalUploadConfig.Md5">
+            <summary>
+            文件Md5码
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Configs.OSSConfig">
             <summary>
             OSS配置
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSConfig.LocalUploadConfig">
+            <summary>
+            本地上传配置
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSConfig.Provider">
+            <summary>
+            文件存储供应商
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.OSSConfig.OSSConfigs">
+            <summary>
+            OSS配置列表
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Configs.UploadConfig">
             <summary>
             上传配置