refactor: 完成文件模块优化重构

This commit is contained in:
橙子
2024-12-21 23:00:43 +08:00
parent 6c409bfa00
commit c00ada5aee
6 changed files with 61 additions and 26 deletions

View File

@@ -11,10 +11,10 @@ namespace Yi.Framework.Core.Enums
/// </summary>
public enum FileTypeEnum
{
File,
Image,
Thumbnail,
Excel,
Temp
file,
image,
thumbnail,
excel,
temp
}
}

View File

@@ -45,8 +45,8 @@ namespace Yi.Framework.Core.Helper
{
var extension = Path.GetExtension(fileName);
if (ImageType.Contains(extension.ToLower()))
return FileTypeEnum.Image;
return FileTypeEnum.File;
return FileTypeEnum.image;
return FileTypeEnum.file;
}

View File

@@ -103,7 +103,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
/// <returns></returns>
public string GetAndCheakThumbnailSavePath(bool isCheak=false)
{
string thumbnailPath = $"wwwroot/{FileTypeEnum.Thumbnail}";
string thumbnailPath = $"wwwroot/{FileTypeEnum.thumbnail}";
if (isCheak)
{
if (!Directory.Exists(thumbnailPath))

View File

@@ -1,8 +1,10 @@
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using Volo.Abp.Guids;
using Volo.Abp.Imaging;
using Yi.Framework.Core.Enums;
using Yi.Framework.Core.Helper;
using Yi.Framework.Rbac.Domain.Entities;
@@ -13,11 +15,14 @@ public class FileManager : DomainService, IFileManager
{
private IGuidGenerator _guidGenerator;
private readonly IRepository<FileAggregateRoot> _repository;
public FileManager(IGuidGenerator guidGenerator, IRepository<FileAggregateRoot> repository)
private readonly IImageCompressor _imageCompressor;
public FileManager(IGuidGenerator guidGenerator, IRepository<FileAggregateRoot> repository,
IImageCompressor imageCompressor)
{
_guidGenerator = guidGenerator;
_repository = repository;
_imageCompressor = imageCompressor;
}
/// <summary>
@@ -51,7 +56,7 @@ public class FileManager : DomainService, IFileManager
/// </summary>
/// <param name="file"></param>
/// <param name="fileStream"></param>
public async Task SaveFileAsync(FileAggregateRoot file,Stream fileStream)
public async Task SaveFileAsync(FileAggregateRoot file, Stream fileStream)
{
var filePath = file.GetSaveFilePath();
@@ -59,25 +64,52 @@ public class FileManager : DomainService, IFileManager
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
{
await fileStream.CopyToAsync(stream);
fileStream.Position = 0;
}
//如果是图片类型,还需要生成缩略图
//这里根据自己需求变更,我们的需求是:原始文件与缩略图文件,都要一份
var fileType=file.GetFileType();;
//如果文件类型是图片,尝试进行压缩
if (FileTypeEnum.Image.Equals(fileType))
//如果是图片类型,还需要生成缩略图
//这里根据自己需求变更,我们的需求是:原始文件与缩略图文件,都要一份
var fileType = file.GetFileType();
//如果文件类型是图片,尝试进行压缩
if (FileTypeEnum.image==fileType)
{
var thumbnailSavePath = file.GetAndCheakThumbnailSavePath(true);
Stream compressImageStream=null;
try
{
var thumbnailSavePath= file.GetAndCheakThumbnailSavePath(true);
try
//压缩图片
var compressResult = await _imageCompressor.CompressAsync(fileStream, file.GetMimeMapping());
if (compressResult.State == ImageProcessState.Done)
{
// _imageSharpManager.ImageCompress(f.FileName, f.OpenReadStream(), thumbnailFilePath);
compressImageStream =
(await _imageCompressor.CompressAsync(fileStream, file.GetMimeMapping())).Result;
}
catch
else if (compressResult.State == ImageProcessState.Canceled)
{
//如果失败了,直接复制一份到缩略图上即可
var result = new byte[stream.Length];
await stream.ReadAsync(result, 0, result.Length);
await File.WriteAllBytesAsync(thumbnailSavePath, result);
throw new NotSupportedException($"当前图片无法再进行压缩,文件id{file.Id}");
}
else
{
throw new NotSupportedException($"当前图片不支持压缩,文件id{file.Id}");
}
}
catch (Exception exception) when (exception is NotSupportedException)
{
this.LoggerFactory.CreateLogger<FileManager>().LogInformation(exception, exception.Message);
}
catch (Exception exception)
{
//如果失败了,直接复制一份到缩略图上即可
compressImageStream = fileStream;
this.LoggerFactory.CreateLogger<FileManager>().LogError(exception, exception.Message);
}
using (var stream = new FileStream(thumbnailSavePath, FileMode.CreateNew, FileAccess.ReadWrite))
{
await compressImageStream.CopyToAsync(stream);
compressImageStream.Position = 0;
}
}
}

View File

@@ -16,7 +16,8 @@
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Imaging.ImageSharp" Version="$(AbpVersion)" />
</ItemGroup>

View File

@@ -2,6 +2,7 @@
using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.Caching;
using Volo.Abp.Domain;
using Volo.Abp.Imaging;
using Volo.Abp.Modularity;
using Yi.Framework.Caching.FreeRedis;
using Yi.Framework.Mapster;
@@ -18,7 +19,8 @@ namespace Yi.Framework.Rbac.Domain
typeof(AbpAspNetCoreSignalRModule),
typeof(AbpDddDomainModule),
typeof(AbpCachingModule)
typeof(AbpCachingModule),
typeof(AbpImagingImageSharpModule)
)]
public class YiFrameworkRbacDomainModule : AbpModule
{