前面我們完成了設定管理,接下來正好配合設定管理來實現檔案管理功能。
檔案管理自然包括檔案上傳,下載以及檔案儲存功能。設計要求可以支援擴充套件多種儲存服務,如本地檔案,雲端儲存等等。
首先當然是我們的資料庫表設計,用於管理檔案。建立一個檔案資訊儲存表。
using Wheel.Domain.Common;
using Wheel.Enums;
namespace Wheel.Domain.FileStorages
{
/// <summary>
/// 檔案資訊儲存表
/// </summary>
public class FileStorage : Entity, IHasCreationTime
{
/// <summary>
/// 檔名
/// </summary>
public string FileName { get; set; }
/// <summary>
/// 檔案型別ContentType
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// 檔案型別
/// </summary>
public FileStorageType FileStorageType { get; set; }
/// <summary>
/// 大小
/// </summary>
public long Size { get; set; }
/// <summary>
/// 儲存路徑
/// </summary>
public string Path { get; set; }
/// <summary>
/// 建立時間
/// </summary>
public DateTimeOffset CreationTime { get; set; }
/// <summary>
/// 儲存型別
/// </summary>
public string Provider { get; set; }
}
}
namespace Wheel.Enums
{
public enum FileStorageType
{
/// <summary>
/// 普通檔案
/// </summary>
File = 0,
/// <summary>
/// 圖片
/// </summary>
Image = 1,
/// <summary>
/// 視訊
/// </summary>
Video = 2,
/// <summary>
/// 音訊
/// </summary>
Audio = 3,
/// <summary>
/// 文字型別
/// </summary>
Text = 4,
}
}
FileStorageType是對ContentType型別的包裝。後面可根據需求再加上細分型別。
using Wheel.Enums;
namespace Wheel.Domain.FileStorages
{
public static class FileStorageTypeChecker
{
public static FileStorageType CheckFileType(string contentType)
{
return contentType switch
{
var _ when contentType.StartsWith("audio") => FileStorageType.Audio,
var _ when contentType.StartsWith("image") => FileStorageType.Image,
var _ when contentType.StartsWith("text") => FileStorageType.Text,
var _ when contentType.StartsWith("video") => FileStorageType.Video,
_ => FileStorageType.File
};
}
}
}
Provider對應不同的儲存服務。如Minio等。
在DbContext中新增程式碼:
#region FileStorage
public DbSet<FileStorage> FileStorages { get; set; }
#endregion
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
ConfigureIdentity(builder);
ConfigureLocalization(builder);
ConfigurePermissionGrants(builder);
ConfigureMenus(builder);
ConfigureSettings(builder);
ConfigureFileStorage(builder);
}
void ConfigureFileStorage(ModelBuilder builder)
{
builder.Entity<FileStorage>(b =>
{
b.HasKey(o => o.Id);
b.Property(o => o.FileName).HasMaxLength(256);
b.Property(o => o.Path).HasMaxLength(256);
b.Property(o => o.ContentType).HasMaxLength(32);
b.Property(o => o.Provider).HasMaxLength(32);
});
}
然後執行資料庫遷移操作即可完成表建立。
接下來就是實現我們的檔案儲存的Provider,首先建立一個IFileStorageProvider基礎介面。
using Wheel.DependencyInjection;
namespace Wheel.FileStorages
{
public interface IFileStorageProvider : ITransientDependency
{
string Name { get; }
Task<UploadFileResult> Upload(UploadFileArgs uploadFileArgs, CancellationToken cancellationToken = default);
Task<DownFileResult> Download(DownloadFileArgs downloadFileArgs, CancellationToken cancellationToken = default);
Task<object> GetClient();
void ConfigureClient<T>(Action<T> configure);
}
}
提供定義名稱,上傳下載,以及獲取Provider的Client和設定Provider中的Client的方法。
既然要對接各種儲存服務,那麼當然少不了對接的設定,那麼我們就基於前面設定管理。新增一個FileProviderSettingDefinition
using Wheel.Enums;
namespace Wheel.Settings.FileProvider
{
public class FileProviderSettingDefinition : ISettingDefinition
{
public string GroupName => "FileProvider";
public SettingScope SettingScope => SettingScope.Global;
public Dictionary<string, SettingValueParams> Define()
{
return new Dictionary<string, SettingValueParams>
{
{ "Minio.Endpoint", new(SettingValueType.String, "127.0.0.1:9000") },
{ "Minio.AccessKey", new(SettingValueType.String, "2QgNxo11uxgULRvkrdaT") },
{ "Minio.SecretKey", new(SettingValueType.String, "NvzXnh81UMwEcvLJc8BslA1GA0j0sCq0aXRgHSRJ") },
{ "Minio.Region", new(SettingValueType.String) },
{ "Minio.SessionToken", new(SettingValueType.String) }
};
}
}
}
這裡我暫時只實現對接Minio,所以只加上Minio的設定。
接下來實現一個MinioFileStorageProvider
using Minio;
using Minio.DataModel.Args;
using Minio.Exceptions;
using Wheel.Settings;
namespace Wheel.FileStorages.Providers
{
public class MinioFileStorageProvider : IFileStorageProvider
{
private readonly ISettingProvider _settingProvider;
private readonly ILogger<MinioFileStorageProvider> _logger;
public MinioFileStorageProvider(ISettingProvider settingProvider, ILogger<MinioFileStorageProvider> logger)
{
_settingProvider = settingProvider;
_logger = logger;
}
public string Name => "Minio";
internal Action<IMinioClient>? Configure { get; private set; }
public async Task<UploadFileResult> Upload(UploadFileArgs uploadFileArgs, CancellationToken cancellationToken = default)
{
var client = await GetMinioClient();
try
{
// Make a bucket on the server, if not already present.
var beArgs = new BucketExistsArgs()
.WithBucket(uploadFileArgs.BucketName);
bool found = await client.BucketExistsAsync(beArgs, cancellationToken).ConfigureAwait(false);
if (!found)
{
var mbArgs = new MakeBucketArgs()
.WithBucket(uploadFileArgs.BucketName);
await client.MakeBucketAsync(mbArgs, cancellationToken).ConfigureAwait(false);
}
// Upload a file to bucket.
var putObjectArgs = new PutObjectArgs()
.WithBucket(uploadFileArgs.BucketName)
.WithObject(uploadFileArgs.FileName)
.WithStreamData(uploadFileArgs.FileStream)
.WithObjectSize(uploadFileArgs.FileStream.Length)
.WithContentType(uploadFileArgs.ContentType);
await client.PutObjectAsync(putObjectArgs, cancellationToken).ConfigureAwait(false);
var path = BuildPath(uploadFileArgs.BucketName, uploadFileArgs.FileName);
_logger.LogInformation("Successfully Uploaded " + path);
return new UploadFileResult { FilePath = path, Success = true };
}
catch (MinioException e)
{
_logger.LogError("File Upload Error: {0}", e.Message);
return new UploadFileResult { Success = false };
}
}
public async Task<DownFileResult> Download(DownloadFileArgs downloadFileArgs, CancellationToken cancellationToken = default)
{
var client = await GetMinioClient();
try
{
var stream = new MemoryStream();
var args = downloadFileArgs.Path.Split("/");
var getObjectArgs = new GetObjectArgs()
.WithBucket(args[0])
.WithObject(downloadFileArgs.Path.RemovePreFix($"{args[0]}/"))
.WithCallbackStream(fs => fs.CopyTo(stream))
;
var response = await client.GetObjectAsync(getObjectArgs, cancellationToken).ConfigureAwait(false);
_logger.LogInformation("Successfully Download " + downloadFileArgs.Path);
stream.Position = 0;
return new DownFileResult { Stream = stream, Success = true, FileName = response.ObjectName, ContentType = response.ContentType };
}
catch (MinioException e)
{
_logger.LogError("File Download Error: {0}", e.Message);
return new DownFileResult { Success = false };
}
}
public async Task<object> GetClient()
{
return await GetMinioClient();
}
public void ConfigureClient<T>(Action<T> configure)
{
if (typeof(T) == typeof(IMinioClient))
Configure = configure as Action<IMinioClient>;
else
throw new Exception("MinioFileProvider ConfigureClient Only Can Configure Type With IMinioClient");
}
private async Task<IMinioClient> GetMinioClient()
{
var minioSetting = await GetSettings();
var client = new MinioClient()
.WithHttpClient(new HttpClient())
.WithEndpoint(minioSetting["Endpoint"])
.WithCredentials(minioSetting["AccessKey"], minioSetting["SecretKey"])
.WithSessionToken(minioSetting["SessionToken"]);
if (!string.IsNullOrWhiteSpace(minioSetting["Region"]))
{
client.WithRegion(minioSetting["Region"]);
}
if (Configure != null)
{
Configure.Invoke(client);
}
return client;
}
private async Task<Dictionary<string, string>> GetSettings()
{
var settings = await _settingProvider.GetGolbalSettings("FileProvider");
return settings.Where(a => a.Key.StartsWith("Minio")).ToDictionary(a => a.Key.RemovePreFix("Minio."), a => a.Value);
}
private string BuildPath(string bucketName, string fileName)
{
return string.Join('/', bucketName, fileName);
}
}
}
這裡定義MinioFileStorageProvider的Name是Minio用作標識。
Upload和Download則是正常的使用MinioClient的上傳下載操作。
GetClient()返回一個MinioClient範例,用於方便做其他「騷操作」。
ConfigureClient則是用來設定MinioClient範例,程式碼約定限制只支援IMinioClient的型別。
GetSettings則是從SettingProvider中獲取Minio的設定資訊。
基礎的對接搭好了,現在我們來實現我們的業務功能。很簡單,就三個功能,上傳下載,分頁查詢。
using Wheel.Core.Dto;
using Wheel.DependencyInjection;
using Wheel.Services.FileStorageManage.Dtos;
namespace Wheel.Services.FileStorageManage
{
public interface IFileStorageManageAppService : ITransientDependency
{
Task<Page<FileStorageDto>> GetFileStoragePageList(FileStoragePageRequest request);
Task<R<List<FileStorageDto>>> UploadFiles(UploadFileDto uploadFileDto);
Task<R<DownloadFileResonse>> DownloadFile(long id);
}
}
using Wheel.Const;
using Wheel.Core.Dto;
using Wheel.Core.Exceptions;
using Wheel.Domain;
using Wheel.Domain.FileStorages;
using Wheel.Enums;
using Wheel.FileStorages;
using Wheel.Services.FileStorageManage.Dtos;
using Path = System.IO.Path;
namespace Wheel.Services.FileStorageManage
{
public class FileStorageManageAppService : WheelServiceBase, IFileStorageManageAppService
{
private readonly IBasicRepository<FileStorage, long> _fileStorageRepository;
public FileStorageManageAppService(IBasicRepository<FileStorage, long> fileStorageRepository)
{
_fileStorageRepository = fileStorageRepository;
}
public async Task<Page<FileStorageDto>> GetFileStoragePageList(FileStoragePageRequest request)
{
var (items, total) = await _fileStorageRepository.GetPageListAsync(
_fileStorageRepository.BuildPredicate(
(!string.IsNullOrWhiteSpace(request.FileName), f => f.FileName.Contains(request.FileName!)),
(!string.IsNullOrWhiteSpace(request.ContentType), f => f.ContentType.Equals(request.ContentType)),
(!string.IsNullOrWhiteSpace(request.Path), f => f.Path.StartsWith(request.Path!)),
(!string.IsNullOrWhiteSpace(request.Provider), f => f.Provider.Equals(request.Provider)),
(request.FileStorageType.HasValue, f => f.FileStorageType.Equals(request.FileStorageType))
),
(request.PageIndex -1) * request.PageSize,
request.PageSize,
request.OrderBy
);
return new Page<FileStorageDto>(Mapper.Map<List<FileStorageDto>>(items), total);
}
public async Task<R<List<FileStorageDto>>> UploadFiles(UploadFileDto uploadFileDto)
{
var files = uploadFileDto.Files;
if (files.Count == 0)
return new R<List<FileStorageDto>>(new());
IFileStorageProvider? fileStorageProvider = null;
var fileStorageProviders = ServiceProvider.GetServices<IFileStorageProvider>();
if (string.IsNullOrWhiteSpace(uploadFileDto.Provider))
{
fileStorageProvider = fileStorageProviders.First();
}
else
{
fileStorageProvider = fileStorageProviders.First(a => a.Name == uploadFileDto.Provider);
}
var fileStorages = new List<FileStorage>();
foreach (var file in files)
{
var fileName = uploadFileDto.Cover ? file.FileName : $"{Path.GetFileNameWithoutExtension(file.FileName)}-{SnowflakeIdGenerator.Create()}{Path.GetExtension(file.FileName)}";
var fileStream = file.OpenReadStream();
var fileStorageType = FileStorageTypeChecker.CheckFileType(file.ContentType);
var uploadFileArgs = new UploadFileArgs
{
BucketName = fileStorageType switch
{
FileStorageType.Image => "images",
FileStorageType.Video => "videos",
FileStorageType.Audio => "audios",
FileStorageType.Text => "texts",
_ => "files"
},
ContentType = file.ContentType,
FileName = fileName,
FileStream = fileStream
};
var uploadFileResult = await fileStorageProvider.Upload(uploadFileArgs);
if (uploadFileResult.Success)
{
var fileStorage = await _fileStorageRepository.InsertAsync(new FileStorage
{
Id = SnowflakeIdGenerator.Create(),
ContentType = file.ContentType,
FileName = file.FileName,
FileStorageType = fileStorageType,
Path = uploadFileResult.FilePath,
Provider = fileStorageProvider.Name,
Size = fileStream.Length
});
await _fileStorageRepository.SaveChangeAsync();
fileStorages.Add(fileStorage);
}
}
return new R<List<FileStorageDto>>(Mapper.Map<List<FileStorageDto>>(fileStorages));
}
public async Task<R<DownloadFileResonse>> DownloadFile(long id)
{
var fileStorage = await _fileStorageRepository.FindAsync(id);
if(fileStorage == null)
{
throw new BusinessException(ErrorCode.FileNotExist, "FileNotExist")
.WithMessageDataData(id.ToString());
}
var fileStorageProvider = ServiceProvider.GetServices<IFileStorageProvider>().First(a=>a.Name == fileStorage.Provider);
var downloadResult = await fileStorageProvider.Download(new DownloadFileArgs { Path = fileStorage.Path });
if (downloadResult.Success)
{
return new R<DownloadFileResonse>(new DownloadFileResonse { ContentType = downloadResult.ContentType, FileName = downloadResult.FileName, Stream = downloadResult.Stream });
}
else
{
throw new BusinessException(ErrorCode.FileDownloadFail, "FileDownloadFail")
.WithMessageDataData(id.ToString());
}
}
}
}
UploadFiles時如果沒有指定Provider則預設取依賴注入第一個Provider,如果指定則取Provider。
using Microsoft.AspNetCore.Mvc;
namespace Wheel.Services.FileStorageManage.Dtos
{
public class UploadFileDto
{
[FromQuery]
public bool Cover { get; set; } = false;
[FromQuery]
public string? Provider { get; set; }
[FromForm]
public IFormFileCollection Files { get; set; }
}
}
這裡上傳引數定義,Cover表示是否覆蓋原檔案,Provider表示指定那種儲存服務。Files則是從Form表單中讀取檔案流。
接下來就是把Service包成API對外。
using Microsoft.AspNetCore.Mvc;
using Wheel.Core.Dto;
using Wheel.Services.FileStorageManage;
using Wheel.Services.FileStorageManage.Dtos;
namespace Wheel.Controllers
{
/// <summary>
/// 檔案管理
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class FileController : WheelControllerBase
{
private readonly IFileStorageManageAppService _fileStorageManageAppService;
public FileController(IFileStorageManageAppService fileStorageManageAppService)
{
_fileStorageManageAppService = fileStorageManageAppService;
}
/// <summary>
/// 分頁查詢列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet]
public Task<Page<FileStorageDto>> GetFileStoragePageList([FromQuery] FileStoragePageRequest request)
{
return _fileStorageManageAppService.GetFileStoragePageList(request);
}
/// <summary>
/// 上傳檔案
/// </summary>
/// <param name="uploadFileDto"></param>
/// <returns></returns>
[HttpPost]
public Task<R<List<FileStorageDto>>> UploadFiles(UploadFileDto uploadFileDto)
{
return _fileStorageManageAppService.UploadFiles(uploadFileDto);
}
/// <summary>
/// 下載檔案
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<IActionResult> DownloadFile(long id)
{
var result = await _fileStorageManageAppService.DownloadFile(id);
return File(result.Data.Stream, result.Data.ContentType, result.Data.FileName);
}
}
}
DownloadFile返回一個FileResult,瀏覽器會自動下載。
這裡我使用原生的Minio服務進行測試。
查詢
上傳
可以看到我們FileName和Path不一樣,預設不覆蓋的情況,所有檔案在後面自動拼接雪花Id。
下載檔案
這裡swagger可以看到有個Download file,點選即可下載出來
測試順利完成,到這我們就完成了我們簡單的檔案管理功能了。
輪子倉庫地址https://github.com/Wheel-Framework/Wheel
歡迎進群催更。