定义ICustomFileProvider(自定义文件提供器接口),分别实现SSH和OSS,减少文件服务代码耦合

This commit is contained in:
hans 2025-02-09 09:37:00 +08:00
parent 0e69c32d70
commit 213a9a5555
5 changed files with 244 additions and 132 deletions

View File

@ -0,0 +1,59 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core.Service;
public class DefaultFileProvider : ICustomFileProvider, ITransient
{
public Task DeleteFileAsync(SysFile sysFile)
{
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, sysFile.FilePath ?? "", sysFile.Id + sysFile.Suffix);
if (File.Exists(filePath)) File.Delete(filePath);
return Task.CompletedTask;
}
public async Task<string> DownloadFileBase64Async(SysFile sysFile)
{
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, sysFile.FilePath);
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
var realFile = Path.Combine(filePath, $"{sysFile.Id}{sysFile.Suffix}");
if (!File.Exists(realFile))
{
Log.Error($"DownloadFileBase64:文件[{realFile}]不存在");
throw Oops.Oh($"文件[{sysFile.FilePath}]不存在");
}
byte[] fileBytes = await File.ReadAllBytesAsync(realFile);
return Convert.ToBase64String(fileBytes);
}
public Task<FileStreamResult> GetFileStreamResultAsync(SysFile sysFile, string fileName)
{
var filePath = Path.Combine(sysFile.FilePath ?? "", sysFile.Id + sysFile.Suffix);
var path = Path.Combine(App.WebHostEnvironment.WebRootPath, filePath);
return Task.FromResult(new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName + sysFile.Suffix });
}
public async Task<SysFile> UploadFileAsync(IFormFile file, SysFile newFile, string path, string finalName)
{
newFile.Provider = ""; // 本地存储 Provider 显示为空
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, path);
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
var realFile = Path.Combine(filePath, finalName);
await using (var stream = File.Create(realFile))
{
await file.CopyToAsync(stream);
}
newFile.Url = $"{newFile.FilePath}/{newFile.Id + newFile.Suffix}";
return newFile;
}
}

View File

@ -0,0 +1,42 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core.Service;
/// <summary>
/// 自定义文件提供器接口
/// </summary>
public interface ICustomFileProvider
{
/// <summary>
/// 获取文件流
/// </summary>
/// <param name="sysFile"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public Task<FileStreamResult> GetFileStreamResultAsync(SysFile sysFile, string fileName);
/// <summary>
/// 下载指定文件Base64格式
/// </summary>
/// <param name="sysFile"></param>
/// <returns></returns>
public Task<string> DownloadFileBase64Async(SysFile sysFile);
/// <summary>
/// 删除文件
/// </summary>
/// <param name="sysFile"></param>
/// <returns></returns>
public Task DeleteFileAsync(SysFile sysFile);
/// <summary>
/// 上传文件
/// </summary>
/// <param name="file">文件</param>
/// <param name="sysFile"></param>
/// <param name="path">文件存储位置</param>
/// <param name="finalName">文件最终名称</param>
/// <returns></returns>
public Task<SysFile> UploadFileAsync(IFormFile file, SysFile sysFile, string path, string finalName);
}

View File

@ -0,0 +1,78 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using OnceMi.AspNetCore.OSS;
namespace Admin.NET.Core.Service;
public class OSSFileProvider : ICustomFileProvider,ITransient
{
private readonly IOSSService _OSSService;
private readonly OSSProviderOptions _OSSProviderOptions;
public OSSFileProvider(IOptions<OSSProviderOptions> oSSProviderOptions, IOSSServiceFactory ossServiceFactory) {
_OSSProviderOptions = oSSProviderOptions.Value;
if (_OSSProviderOptions.Enabled)
_OSSService = ossServiceFactory.Create(Enum.GetName(_OSSProviderOptions.Provider));
}
public async Task DeleteFileAsync(SysFile file)
{
await _OSSService.RemoveObjectAsync(file.BucketName, string.Concat(file.FilePath, "/", $"{file.Id}{file.Suffix}"));
}
public async Task<string> DownloadFileBase64Async(SysFile file)
{
using var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(file.Url);
if (response.IsSuccessStatusCode)
{
// 读取文件内容并将其转换为 Base64 字符串
byte[] fileBytes = await response.Content.ReadAsByteArrayAsync();
return Convert.ToBase64String(fileBytes);
}
throw new HttpRequestException($"Request failed with status code: {response.StatusCode}");
}
public async Task<FileStreamResult> GetFileStreamResultAsync(SysFile file, string fileName)
{
var filePath = Path.Combine(file.FilePath ?? "", file.Id + file.Suffix);
var httpRemoteService = App.GetRequiredService<IHttpRemoteService>();
var stream = await httpRemoteService.GetAsStreamAsync(await _OSSService.PresignedGetObjectAsync(file.BucketName, filePath, 5));
return new FileStreamResult(stream, "application/octet-stream") { FileDownloadName = fileName + file.Suffix };
}
public async Task<SysFile> UploadFileAsync(IFormFile file,SysFile sysFile,string path,string finalName)
{
sysFile.Provider = Enum.GetName(_OSSProviderOptions.Provider);
var filePath = string.Concat(path, "/", finalName);
await _OSSService.PutObjectAsync(sysFile.BucketName, filePath, file.OpenReadStream());
// http://<你的bucket名字>.oss.aliyuncs.com/<你的object名字>
// 生成外链地址 方便前端预览
switch (_OSSProviderOptions.Provider)
{
case OSSProvider.Aliyun:
sysFile.Url = $"{(_OSSProviderOptions.IsEnableHttps ? "https" : "http")}://{sysFile.BucketName}.{_OSSProviderOptions.Endpoint}/{filePath}";
break;
case OSSProvider.QCloud:
var protocol = _OSSProviderOptions.IsEnableHttps ? "https" : "http";
sysFile.Url = !string.IsNullOrWhiteSpace(_OSSProviderOptions.CustomHost)
? $"{protocol}://{_OSSProviderOptions.CustomHost}/{filePath}"
: $"{protocol}://{sysFile.BucketName}-{_OSSProviderOptions.Endpoint}.cos.{_OSSProviderOptions.Region}.myqcloud.com/{filePath}";
break;
case OSSProvider.Minio:
// 获取Minio文件的下载或者预览地址
// newFile.Url = await GetMinioPreviewFileUrl(newFile.BucketName, filePath);// 这种方法生成的Url是有7天有效期的不能这样使用
// 需要在MinIO中的Buckets开通对 Anonymous 的readonly权限
var customHost = _OSSProviderOptions.CustomHost;
if (string.IsNullOrWhiteSpace(customHost))
customHost = _OSSProviderOptions.Endpoint;
sysFile.Url = $"{(_OSSProviderOptions.IsEnableHttps ? "https" : "http")}://{customHost}/{sysFile.BucketName}/{filePath}";
break;
}
return sysFile;
}
}

View File

@ -0,0 +1,44 @@
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core.Service;
public class SSHFileProvider : ICustomFileProvider, ITransient
{
public Task DeleteFileAsync(SysFile sysFile)
{
var fullPath = string.Concat(sysFile.FilePath, "/", sysFile.Id + sysFile.Suffix);
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
helper.DeleteFile(fullPath);
return Task.CompletedTask;
}
public Task<string> DownloadFileBase64Async(SysFile sysFile)
{
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
return Task.FromResult(Convert.ToBase64String(helper.ReadAllBytes(sysFile.FilePath)));
}
public Task<FileStreamResult> GetFileStreamResultAsync(SysFile sysFile, string fileName)
{
var filePath = Path.Combine(sysFile.FilePath ?? "", sysFile.Id + sysFile.Suffix);
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
return Task.FromResult( new FileStreamResult(helper.OpenRead(filePath), "application/octet-stream") { FileDownloadName = fileName + sysFile.Suffix });
}
public Task<SysFile> UploadFileAsync(IFormFile file, SysFile sysFile, string path, string finalName)
{
var fullPath = string.Concat(path.StartsWith('/') ? path : "/" + path, "/", finalName);
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
helper.UploadFile(file.OpenReadStream(), fullPath);
return Task.FromResult(sysFile);
}
}

View File

@ -5,7 +5,6 @@
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Aliyun.OSS.Util;
using OnceMi.AspNetCore.OSS;
namespace Admin.NET.Core.Service;
@ -19,21 +18,31 @@ public class SysFileService : IDynamicApiController, ITransient
private readonly SqlSugarRepository<SysFile> _sysFileRep;
private readonly OSSProviderOptions _OSSProviderOptions;
private readonly UploadOptions _uploadOptions;
private readonly IOSSService _OSSService;
private readonly string _imageType = ".jpeg.jpg.png.bmp.gif.tif";
private readonly INamedServiceProvider<ICustomFileProvider> _namedServiceProvider;
private readonly ICustomFileProvider _customFileProvider;
public SysFileService(UserManager userManager,
SqlSugarRepository<SysFile> sysFileRep,
IOptions<OSSProviderOptions> oSSProviderOptions,
IOptions<UploadOptions> uploadOptions,
IOSSServiceFactory ossServiceFactory)
IOptions<UploadOptions> uploadOptions, INamedServiceProvider<ICustomFileProvider> namedServiceProvider)
{
_namedServiceProvider = namedServiceProvider;
_userManager = userManager;
_sysFileRep = sysFileRep;
_OSSProviderOptions = oSSProviderOptions.Value;
_uploadOptions = uploadOptions.Value;
if (_OSSProviderOptions.Enabled)
_OSSService = ossServiceFactory.Create(Enum.GetName(_OSSProviderOptions.Provider));
{
_customFileProvider = _namedServiceProvider.GetService<ITransient>(nameof(OSSFileProvider));
}
else if (App.Configuration["SSHProvider:Enabled"].ToBoolean())
{
_customFileProvider = _namedServiceProvider.GetService<ITransient>(nameof(SSHFileProvider));
}
else {
_customFileProvider = _namedServiceProvider.GetService<ITransient>(nameof(DefaultFileProvider));
}
}
/// <summary>
@ -135,23 +144,7 @@ public class SysFileService : IDynamicApiController, ITransient
/// <returns></returns>
private async Task<IActionResult> GetFileStreamResult(SysFile file, string fileName)
{
var filePath = Path.Combine(file.FilePath ?? "", file.Id + file.Suffix);
if (_OSSProviderOptions.Enabled)
{
var httpRemoteService = App.GetRequiredService<IHttpRemoteService>();
var stream = await httpRemoteService.GetAsStreamAsync(await _OSSService.PresignedGetObjectAsync(file.BucketName, filePath, 5));
return new FileStreamResult(stream, "application/octet-stream") { FileDownloadName = fileName + file.Suffix };
}
if (App.Configuration["SSHProvider:Enabled"].ToBoolean())
{
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
return new FileStreamResult(helper.OpenRead(filePath), "application/octet-stream") { FileDownloadName = fileName + file.Suffix };
}
var path = Path.Combine(App.WebHostEnvironment.WebRootPath, filePath);
return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName + file.Suffix };
return await _customFileProvider.GetFileStreamResultAsync(file, fileName);
}
/// <summary>
@ -162,42 +155,8 @@ public class SysFileService : IDynamicApiController, ITransient
[DisplayName("下载指定文件Base64格式")]
public async Task<string> DownloadFileBase64([FromBody] string url)
{
if (_OSSProviderOptions.Enabled)
{
using var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
// 读取文件内容并将其转换为 Base64 字符串
byte[] fileBytes = await response.Content.ReadAsByteArrayAsync();
return Convert.ToBase64String(fileBytes);
}
throw new HttpRequestException($"Request failed with status code: {response.StatusCode}");
}
if (App.Configuration["SSHProvider:Enabled"].ToBoolean())
{
var sysFile = await _sysFileRep.CopyNew().GetFirstAsync(u => u.Url == url) ?? throw Oops.Oh($"文件不存在");
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
return Convert.ToBase64String(helper.ReadAllBytes(sysFile.FilePath));
}
else
{
var sysFile = await _sysFileRep.CopyNew().GetFirstAsync(u => u.Url == url) ?? throw Oops.Oh($"文件不存在");
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, sysFile.FilePath);
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
var realFile = Path.Combine(filePath, $"{sysFile.Id}{sysFile.Suffix}");
if (!File.Exists(realFile))
{
Log.Error($"DownloadFileBase64:文件[{realFile}]不存在");
throw Oops.Oh($"文件[{sysFile.FilePath}]不存在");
}
byte[] fileBytes = await File.ReadAllBytesAsync(realFile);
return Convert.ToBase64String(fileBytes);
}
var sysFile = await _sysFileRep.CopyNew().GetFirstAsync(u => u.Url == url) ?? throw Oops.Oh($"文件不存在");
return await _customFileProvider.DownloadFileBase64Async(sysFile);
}
/// <summary>
@ -209,28 +168,9 @@ public class SysFileService : IDynamicApiController, ITransient
[DisplayName("删除文件")]
public async Task DeleteFile(DeleteFileInput input)
{
var file = await _sysFileRep.GetByIdAsync(input.Id);
if (file != null)
{
await _sysFileRep.DeleteAsync(file);
if (_OSSProviderOptions.Enabled)
{
await _OSSService.RemoveObjectAsync(file.BucketName, string.Concat(file.FilePath, "/", $"{input.Id}{file.Suffix}"));
}
else if (App.Configuration["SSHProvider:Enabled"].ToBoolean())
{
var fullPath = string.Concat(file.FilePath, "/", file.Id + file.Suffix);
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
helper.DeleteFile(fullPath);
}
else
{
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, file.FilePath ?? "", input.Id + file.Suffix);
if (File.Exists(filePath)) File.Delete(filePath);
}
}
var file = await _sysFileRep.GetByIdAsync(input.Id) ?? throw Oops.Oh($"文件不存在");
await _sysFileRep.DeleteAsync(file);
await _customFileProvider.DeleteFileAsync(file);
}
/// <summary>
@ -349,59 +289,8 @@ public class SysFileService : IDynamicApiController, ITransient
newFile.FileMd5 = fileMd5;
var finalName = newFile.Id + suffix; // 文件最终名称
if (_OSSProviderOptions.Enabled)
{
newFile.Provider = Enum.GetName(_OSSProviderOptions.Provider);
var filePath = string.Concat(path, "/", finalName);
await _OSSService.PutObjectAsync(newFile.BucketName, filePath, input.File.OpenReadStream());
// http://<你的bucket名字>.oss.aliyuncs.com/<你的object名字>
// 生成外链地址 方便前端预览
switch (_OSSProviderOptions.Provider)
{
case OSSProvider.Aliyun:
newFile.Url = $"{(_OSSProviderOptions.IsEnableHttps ? "https" : "http")}://{newFile.BucketName}.{_OSSProviderOptions.Endpoint}/{filePath}";
break;
case OSSProvider.QCloud:
var protocol = _OSSProviderOptions.IsEnableHttps ? "https" : "http";
newFile.Url = !string.IsNullOrWhiteSpace(_OSSProviderOptions.CustomHost)
? $"{protocol}://{_OSSProviderOptions.CustomHost}/{filePath}"
: $"{protocol}://{newFile.BucketName}-{_OSSProviderOptions.Endpoint}.cos.{_OSSProviderOptions.Region}.myqcloud.com/{filePath}";
break;
case OSSProvider.Minio:
// 获取Minio文件的下载或者预览地址
// newFile.Url = await GetMinioPreviewFileUrl(newFile.BucketName, filePath);// 这种方法生成的Url是有7天有效期的不能这样使用
// 需要在MinIO中的Buckets开通对 Anonymous 的readonly权限
var customHost = _OSSProviderOptions.CustomHost;
if (string.IsNullOrWhiteSpace(customHost))
customHost = _OSSProviderOptions.Endpoint;
newFile.Url = $"{(_OSSProviderOptions.IsEnableHttps ? "https" : "http")}://{customHost}/{newFile.BucketName}/{filePath}";
break;
}
}
else if (App.Configuration["SSHProvider:Enabled"].ToBoolean())
{
var fullPath = string.Concat(path.StartsWith('/') ? path : "/" + path, "/", finalName);
using SSHHelper helper = new(App.Configuration["SSHProvider:Host"],
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]);
helper.UploadFile(input.File.OpenReadStream(), fullPath);
}
else
{
newFile.Provider = ""; // 本地存储 Provider 显示为空
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, path);
if (!Directory.Exists(filePath))
Directory.CreateDirectory(filePath);
var realFile = Path.Combine(filePath, finalName);
await using (var stream = File.Create(realFile))
{
await input.File.CopyToAsync(stream);
}
newFile.Url = $"{newFile.FilePath}/{newFile.Id + newFile.Suffix}";
}
newFile = await _customFileProvider.UploadFileAsync(input.File, newFile, path,finalName);
await _sysFileRep.AsInsertable(newFile).ExecuteCommandAsync();
return newFile;
}