refactor: 完成文件模块优化重构
This commit is contained in:
@@ -11,10 +11,10 @@ namespace Yi.Framework.Core.Enums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public enum FileTypeEnum
|
public enum FileTypeEnum
|
||||||
{
|
{
|
||||||
File,
|
file,
|
||||||
Image,
|
image,
|
||||||
Thumbnail,
|
thumbnail,
|
||||||
Excel,
|
excel,
|
||||||
Temp
|
temp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,8 +45,8 @@ namespace Yi.Framework.Core.Helper
|
|||||||
{
|
{
|
||||||
var extension = Path.GetExtension(fileName);
|
var extension = Path.GetExtension(fileName);
|
||||||
if (ImageType.Contains(extension.ToLower()))
|
if (ImageType.Contains(extension.ToLower()))
|
||||||
return FileTypeEnum.Image;
|
return FileTypeEnum.image;
|
||||||
return FileTypeEnum.File;
|
return FileTypeEnum.file;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string GetAndCheakThumbnailSavePath(bool isCheak=false)
|
public string GetAndCheakThumbnailSavePath(bool isCheak=false)
|
||||||
{
|
{
|
||||||
string thumbnailPath = $"wwwroot/{FileTypeEnum.Thumbnail}";
|
string thumbnailPath = $"wwwroot/{FileTypeEnum.thumbnail}";
|
||||||
if (isCheak)
|
if (isCheak)
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(thumbnailPath))
|
if (!Directory.Exists(thumbnailPath))
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Volo.Abp.Domain.Repositories;
|
using Volo.Abp.Domain.Repositories;
|
||||||
using Volo.Abp.Domain.Services;
|
using Volo.Abp.Domain.Services;
|
||||||
using Volo.Abp.Guids;
|
using Volo.Abp.Guids;
|
||||||
|
using Volo.Abp.Imaging;
|
||||||
using Yi.Framework.Core.Enums;
|
using Yi.Framework.Core.Enums;
|
||||||
using Yi.Framework.Core.Helper;
|
using Yi.Framework.Core.Helper;
|
||||||
using Yi.Framework.Rbac.Domain.Entities;
|
using Yi.Framework.Rbac.Domain.Entities;
|
||||||
@@ -13,11 +15,14 @@ public class FileManager : DomainService, IFileManager
|
|||||||
{
|
{
|
||||||
private IGuidGenerator _guidGenerator;
|
private IGuidGenerator _guidGenerator;
|
||||||
private readonly IRepository<FileAggregateRoot> _repository;
|
private readonly IRepository<FileAggregateRoot> _repository;
|
||||||
|
private readonly IImageCompressor _imageCompressor;
|
||||||
public FileManager(IGuidGenerator guidGenerator, IRepository<FileAggregateRoot> repository)
|
public FileManager(IGuidGenerator guidGenerator, IRepository<FileAggregateRoot> repository,
|
||||||
|
|
||||||
|
IImageCompressor imageCompressor)
|
||||||
{
|
{
|
||||||
_guidGenerator = guidGenerator;
|
_guidGenerator = guidGenerator;
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
_imageCompressor = imageCompressor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -51,7 +56,7 @@ public class FileManager : DomainService, IFileManager
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="file"></param>
|
/// <param name="file"></param>
|
||||||
/// <param name="fileStream"></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();
|
var filePath = file.GetSaveFilePath();
|
||||||
|
|
||||||
@@ -59,25 +64,52 @@ public class FileManager : DomainService, IFileManager
|
|||||||
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
|
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
|
||||||
{
|
{
|
||||||
await fileStream.CopyToAsync(stream);
|
await fileStream.CopyToAsync(stream);
|
||||||
|
fileStream.Position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
//如果是图片类型,还需要生成缩略图
|
|
||||||
//这里根据自己需求变更,我们的需求是:原始文件与缩略图文件,都要一份
|
//如果是图片类型,还需要生成缩略图
|
||||||
var fileType=file.GetFileType();;
|
//这里根据自己需求变更,我们的需求是:原始文件与缩略图文件,都要一份
|
||||||
//如果文件类型是图片,尝试进行压缩
|
var fileType = file.GetFileType();
|
||||||
if (FileTypeEnum.Image.Equals(fileType))
|
//如果文件类型是图片,尝试进行压缩
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
//如果失败了,直接复制一份到缩略图上即可
|
throw new NotSupportedException($"当前图片无法再进行压缩,文件id:{file.Id}");
|
||||||
var result = new byte[stream.Length];
|
|
||||||
await stream.ReadAsync(result, 0, result.Length);
|
|
||||||
await File.WriteAllBytesAsync(thumbnailSavePath, result);
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,8 @@
|
|||||||
|
|
||||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
||||||
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
|
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
|
||||||
|
<PackageReference Include="Volo.Abp.Imaging.ImageSharp" Version="$(AbpVersion)" />
|
||||||
|
|
||||||
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using Volo.Abp.AspNetCore.SignalR;
|
using Volo.Abp.AspNetCore.SignalR;
|
||||||
using Volo.Abp.Caching;
|
using Volo.Abp.Caching;
|
||||||
using Volo.Abp.Domain;
|
using Volo.Abp.Domain;
|
||||||
|
using Volo.Abp.Imaging;
|
||||||
using Volo.Abp.Modularity;
|
using Volo.Abp.Modularity;
|
||||||
using Yi.Framework.Caching.FreeRedis;
|
using Yi.Framework.Caching.FreeRedis;
|
||||||
using Yi.Framework.Mapster;
|
using Yi.Framework.Mapster;
|
||||||
@@ -18,7 +19,8 @@ namespace Yi.Framework.Rbac.Domain
|
|||||||
|
|
||||||
typeof(AbpAspNetCoreSignalRModule),
|
typeof(AbpAspNetCoreSignalRModule),
|
||||||
typeof(AbpDddDomainModule),
|
typeof(AbpDddDomainModule),
|
||||||
typeof(AbpCachingModule)
|
typeof(AbpCachingModule),
|
||||||
|
typeof(AbpImagingImageSharpModule)
|
||||||
)]
|
)]
|
||||||
public class YiFrameworkRbacDomainModule : AbpModule
|
public class YiFrameworkRbacDomainModule : AbpModule
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user