Преглед на файлове

修复OSS注入服务异常
阿里云OSS上传文件,外链显示

skywolf627 преди 3 години
родител
ревизия
57558dd7e7

+ 33 - 0
Admin.NET/Admin.NET.Core/Admin.NET.Core.xml

@@ -635,6 +635,11 @@
             文件大小信息-计算后的
             </summary>
         </member>
+        <member name="P:Admin.NET.Core.SysFile.Url">
+            <summary>
+            外链地址  OSS上传后直接生成外链地址  方便前端预览
+            </summary>
+        </member>
         <member name="T:Admin.NET.Core.SysLogAudit">
             <summary>
             系统审计日志表
@@ -3144,6 +3149,16 @@
             是否启用OSS存储
             </summary>
         </member>
+        <member name="P:Admin.NET.Core.OSSProviderOptions.Bucket">
+            <summary>
+            自定义桶名称 不能直接使用Provider来替代桶名称。例:阿里云 1.只能包括小写字母,数字,短横线(-)2.必须以小写字母或者数字开头 3.长度必须在3-63字节之间
+            </summary>
+        </member>
+        <member name="P:Admin.NET.Core.OSSProviderOptions.OptionName">
+            <summary>
+            配置名称
+            </summary>
+        </member>
         <member name="T:Admin.NET.Core.PayCallBackOptions">
             <summary>
             支付回调配置选项
@@ -3585,6 +3600,15 @@
             <param name="value"></param>
             <returns></returns>
         </member>
+        <member name="M:Admin.NET.Core.Service.SysCacheService.SetAsync(System.String,System.Object,System.TimeSpan)">
+            <summary>
+            增加对象缓存,并设置过期时间
+            </summary>
+            <param name="cacheKey"></param>
+            <param name="value"></param>
+            <param name="expire"></param>
+            <returns></returns>
+        </member>
         <member name="M:Admin.NET.Core.Service.SysCacheService.SetStringAsync(System.String,System.String)">
             <summary>
             增加字符串缓存
@@ -3593,6 +3617,15 @@
             <param name="value"></param>
             <returns></returns>
         </member>
+        <member name="M:Admin.NET.Core.Service.SysCacheService.SetStringAsync(System.String,System.String,System.TimeSpan)">
+            <summary>
+            增加字符串缓存,并设置过期时间
+            </summary>
+            <param name="cacheKey"></param>
+            <param name="value"></param>
+            <param name="expire"></param>
+            <returns></returns>
+        </member>
         <member name="M:Admin.NET.Core.Service.SysCacheService.GetStringAsync(System.String)">
             <summary>
             获取缓存

+ 3 - 1
Admin.NET/Admin.NET.Core/AdminNETConfig.json

@@ -93,7 +93,9 @@
     "AccessKey": "",
     "SecretKey": "",
     "IsEnableHttps": true,
-    "IsEnableCache": true
+    "IsEnableCache": true,
+    "Bucket": "",
+    "OptionName": "Aliyun"
   },
   "Email": {
     "Server": "smtp.163.com", // 主机

+ 7 - 0
Admin.NET/Admin.NET.Core/Entity/SysFile.cs

@@ -47,4 +47,11 @@ public class SysFile : EntityBase
     [SugarColumn(ColumnDescription = "文件大小信息", Length = 64)]
     [MaxLength(64)]
     public string SizeInfo { get; set; }
+
+    /// <summary>
+    /// 外链地址  OSS上传后直接生成外链地址  方便前端预览
+    /// </summary>
+    [SugarColumn(ColumnDescription = "外链地址", Length = 128)]
+    [MaxLength(128)]
+    public string Url { get; set; }
 }

+ 8 - 0
Admin.NET/Admin.NET.Core/Option/OSSProviderOptions.cs

@@ -11,4 +11,12 @@ public sealed class OSSProviderOptions : OSSOptions, IConfigurableOptions
     /// 是否启用OSS存储
     /// </summary>
     public bool IsEnable { get; set; }
+    /// <summary>
+    /// 自定义桶名称 不能直接使用Provider来替代桶名称。例:阿里云 1.只能包括小写字母,数字,短横线(-)2.必须以小写字母或者数字开头 3.长度必须在3-63字节之间
+    /// </summary>
+    public string Bucket { get; set; }
+    /// <summary>
+    /// 配置名称
+    /// </summary>
+    public string OptionName { get; set; }
 }

+ 20 - 7
Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs

@@ -25,7 +25,7 @@ public class SysFileService : IDynamicApiController, ITransient
         _uploadOptions = uploadOptions.Value;
         _commonService = commonService;
         if (_OSSProviderOptions.IsEnable)
-            _OSSService = ossServiceFactory.Create(_OSSProviderOptions.Provider.ToString());
+            _OSSService = ossServiceFactory.Create(_OSSProviderOptions.OptionName);
     }
 
     /// <summary>
@@ -67,7 +67,7 @@ public class SysFileService : IDynamicApiController, ITransient
         return new FileOutput
         {
             Id = sysFile.Id,
-            Url = _commonService.GetFileUrl(sysFile),
+            Url = string.IsNullOrWhiteSpace(sysFile.Url) ? _commonService.GetFileUrl(sysFile) : sysFile.Url,
             SizeKb = sysFile.SizeKb,
             Suffix = sysFile.Suffix,
             FilePath = sysFile.FilePath,
@@ -162,21 +162,33 @@ public class SysFileService : IDynamicApiController, ITransient
 
         var suffix = Path.GetExtension(file.FileName).ToLower(); // 后缀
 
-        // 先存库获取Id
-        var newFile = await _sysFileRep.AsInsertable(new SysFile
+        var newFile = new SysFile
         {
-            BucketName = _OSSProviderOptions.IsEnable ? _OSSProviderOptions.Provider.ToString() : "Local",
+            Id = Yitter.IdGenerator.YitIdHelper.NextId(),
+            //BucketName = _OSSProviderOptions.IsEnable ? _OSSProviderOptions.Provider.ToString() : "Local",
+            //阿里云对bucket名称有要求,1.只能包括小写字母,数字,短横线(-)2.必须以小写字母或者数字开头  3.长度必须在3-63字节之间
+            //无法使用Provider
+            BucketName = _OSSProviderOptions.IsEnable ? _OSSProviderOptions.Bucket : "Local",
             FileName = Path.GetFileNameWithoutExtension(file.FileName),
             Suffix = suffix,
             SizeKb = sizeKb.ToString(),
             FilePath = path
-        }).ExecuteReturnEntityAsync();
+        };
 
         var finalName = newFile.Id + suffix; // 文件最终名称
         if (_OSSProviderOptions.IsEnable)
         {
             var filePath = string.Concat(path, "/", finalName);
-            await _OSSService.PutObjectAsync(newFile.BucketName.ToString(), filePath, file.OpenReadStream());
+            await _OSSService.PutObjectAsync(newFile.BucketName, filePath, file.OpenReadStream());
+            //  http://hxmkedu.oss-cn-hangzhou.aliyuncs.com/idcardpic/upload/2022/08/04/317117690298693.jpg
+            //  http://<你的bucket名字>.oss.aliyuncs.com/<你的object名字>
+            //  生成外链地址 方便前端预览
+            switch (_OSSProviderOptions.Provider)
+            {
+                case OSSProvider.Aliyun:
+                    newFile.Url = $"{(_OSSProviderOptions.IsEnableHttps ? "https" : "http")}://{newFile.BucketName}.{_OSSProviderOptions.Endpoint}/{filePath}";
+                    break;
+            }
         }
         else
         {
@@ -186,6 +198,7 @@ public class SysFileService : IDynamicApiController, ITransient
             using var stream = File.Create(Path.Combine(filePath, finalName));
             await file.CopyToAsync(stream);
         }
+        await _sysFileRep.AsInsertable(newFile).ExecuteCommandAsync();
         return newFile;
     }
 }

+ 9 - 2
Admin.NET/Admin.NET.Web.Core/Startup.cs

@@ -61,9 +61,16 @@ public class Startup : AppStartup
             builder.AddSubscriber<LogEventSubscriber>();
         });
         // OSS对象存储
-        services.AddOSSService(options =>
+        var opt = App.GetOptions<OSSProviderOptions>();
+        services.AddOSSService(opt.OptionName, options =>
         {
-            options = App.GetOptions<OSSProviderOptions>();
+            options.Provider = opt.Provider;
+            options.Endpoint = opt.Endpoint;
+            options.AccessKey = opt.AccessKey;
+            options.SecretKey = opt.SecretKey;
+            options.Region = opt.Region;
+            options.IsEnableCache = opt.IsEnableCache;
+            options.IsEnableHttps = opt.IsEnableHttps;
         });
         // 电子邮件
         services.AddMailKit(options =>

+ 3 - 1
Vben2/src/views/sys/admin/file/file.data.ts

@@ -34,7 +34,9 @@ export const columns: BasicColumn[] = [
         record.suffix.indexOf('jpg') > 0 ||
         record.suffix.indexOf('bmp') > 0
       ) {
-        const filePath = uploadUrl + '/' + record.filePath + '/' + record.id + record.suffix;
+        const filePath = record.url
+          ? record.url
+          : uploadUrl + '/' + record.filePath + '/' + record.id + record.suffix;
         return h(TableImg, {
           size: 60,
           simpleShow: true,

+ 3 - 1
Vben2/src/views/sys/admin/file/index.vue

@@ -86,7 +86,9 @@
       });
 
       async function handleDownload(record: Recordable) {
-        const filePath = uploadUrl + '/' + record.filePath + '/' + record.id + record.suffix;
+        const filePath = record.url
+          ? record.url
+          : uploadUrl + '/' + record.filePath + '/' + record.id + record.suffix;
         downloadByUrl({ url: filePath });
       }