diff --git a/Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs b/Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs index c02a1f56..7d5308ac 100644 --- a/Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs +++ b/Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs @@ -6,6 +6,7 @@ using Aliyun.OSS.Util; using OnceMi.AspNetCore.OSS; +using System.IO; namespace Admin.NET.Core.Service; @@ -147,28 +148,20 @@ public class SysFileService : IDynamicApiController, ITransient public async Task GetPreview([FromRoute] long Id) { var file = await GetFile(new FileInput { Id = Id }); - //var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8")); + var contextType = GetContextType(file.Suffix); if (_OSSProviderOptions.IsEnable) { var filePath = string.Concat(file.FilePath, "/", file.Id.ToString() + file.Suffix); - var stream = await (await _OSSService.PresignedGetObjectAsync(file.BucketName.ToString(), filePath, 5)).GetAsStreamAsync(); - return new FileStreamResult(stream.Stream, "application/octet-stream"); - } - else if (App.Configuration["SSHProvider:IsEnable"].ToBoolean()) - { - var fullPath = string.Concat(file.FilePath, "/", file.Id + file.Suffix); - using (SSHHelper helper = new SSHHelper(App.Configuration["SSHProvider:Host"], - App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"])) - { - return new FileStreamResult(helper.OpenRead(fullPath), "application/octet-stream"); - } + var (stream, encoding, response) = await (await _OSSService.PresignedGetObjectAsync(file.BucketName.ToString(), filePath, 5)).GetAsStreamAsync(); + return new FileDownHelper().File(stream, contextType); } else { var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix); var path = Path.Combine(App.WebHostEnvironment.WebRootPath, filePath); - return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream"); + var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); + return new FileDownHelper().File(stream, contextType); } } @@ -413,23 +406,12 @@ public class SysFileService : IDynamicApiController, ITransient var host = CommonUtil.GetLocalhost(); if (!host.EndsWith('/')) host += "/"; - newFile.Url = $"{host}{newFile.FilePath}/{newFile.Id + newFile.Suffix}"; + newFile.Url = $"/sysFile/preview/{newFile.Id}"; } await _sysFileRep.AsInsertable(newFile).ExecuteCommandAsync(); return newFile; } - ///// - ///// 获取Minio文件的下载或者预览地址 - ///// - ///// 桶名 - ///// 文件名 - ///// - //private async Task GetMinioPreviewFileUrl(string bucketName, string fileName) - //{ - // return await _OSSService.PresignedGetObjectAsync(bucketName, fileName, 7); - //} - /// /// 上传头像 🔖 /// @@ -527,4 +509,42 @@ public class SysFileService : IDynamicApiController, ITransient }) .ToListAsync(); } -} \ No newline at end of file + + /// + /// 根据扩展名返回 ContextType + /// + /// + /// + private string GetContextType(string ext) + { + return ext switch + { + ".js" => "application/x-javascript", + ".css" => "text/css", + ".asp" => "text/asp", + ".htm" or ".html" or ".php" => "text/html", + ".cgi" => "text/cgi", + ".aspx" => "text/aspx", + ".jfif" => "image/jpeg", + ".jpg" => "image/jpeg", + ".webp" => "image/jpeg", + ".jpeg" => "image/jpeg", + ".gif" => "image/gif", + ".ico" => "image/x-icon", + ".png" => "image/png", + ".xls" or ".xlsx" or ".apk" or ".exe" or ".zip" or ".rar" or ".doc" or ".docx" => "application/x-download", + ".pdf" => "application/pdf", + ".json" => "application/json", + ".swf" => "application/x-shockwave-flash", + ".rss" => "application/rss+xml; charset=ISO-8859-1", + ".xml" => "text/xml", + //除了以上类型,都允许下载 + _ => "application/octet-stream", + }; + } +} + +//为了使用 ControllBase 中的方法,需要继承自 ControllBase 的对象 +internal class FileDownHelper : ControllerBase +{ +}