Merge branch 'refs/heads/abp' into digital-collectibles
This commit is contained in:
@@ -7,6 +7,8 @@ using SqlSugar;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
using Yi.Framework.Core.Enums;
|
||||
using Yi.Framework.Core.Helper;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Entities
|
||||
{
|
||||
@@ -17,28 +19,140 @@ namespace Yi.Framework.Rbac.Domain.Entities
|
||||
{
|
||||
}
|
||||
|
||||
public FileAggregateRoot(Guid fileId)
|
||||
/// <summary>
|
||||
/// 创建文件
|
||||
/// </summary>
|
||||
/// <param name="fileId">文件标识id</param>
|
||||
/// <param name="fileName">文件名</param>
|
||||
/// <param name="fileSize">文件大小</param>
|
||||
public FileAggregateRoot(Guid fileId, string fileName, decimal fileSize)
|
||||
{
|
||||
this.Id = fileId;
|
||||
this.FileSize = fileSize;
|
||||
this.FileName = fileName;
|
||||
|
||||
var type = GetFileType();
|
||||
|
||||
var savePath = GetSaveFilePath();
|
||||
var filePath = Path.Combine(savePath, this.FileName);
|
||||
this.FilePath = filePath;
|
||||
}
|
||||
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public override Guid Id { get; protected set; }
|
||||
/// <summary>
|
||||
/// 检测目录是否存在,不存在便创建
|
||||
/// </summary>
|
||||
public void CheckDirectoryOrCreate()
|
||||
{
|
||||
var savePath = GetSaveDirPath();
|
||||
if (!Directory.Exists(savePath))
|
||||
{
|
||||
Directory.CreateDirectory(savePath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件类型
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public FileTypeEnum GetFileType()
|
||||
{
|
||||
return MimeHelper.GetFileType(this.FileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件mime
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetMimeMapping()
|
||||
{
|
||||
return MimeHelper.GetMimeMapping(this.FileName)??@"text/plain";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 落库目录路径
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetSaveDirPath()
|
||||
{
|
||||
return $"wwwroot/{GetFileType()}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 落库文件路径
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetSaveFilePath()
|
||||
{
|
||||
string savefileName = GetSaveFileName();
|
||||
return Path.Combine(GetSaveDirPath(), savefileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取保存的文件名
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetSaveFileName()
|
||||
{
|
||||
return this.Id.ToString() + Path.GetExtension(this.FileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测,并且返回缩略图的保存路径
|
||||
/// </summary>
|
||||
/// <param name="saveFileName"></param>
|
||||
/// <returns></returns>
|
||||
public string GetAndCheakThumbnailSavePath(bool isCheak=false)
|
||||
{
|
||||
string thumbnailPath = $"wwwroot/{FileTypeEnum.thumbnail}";
|
||||
if (isCheak)
|
||||
{
|
||||
if (!Directory.Exists(thumbnailPath))
|
||||
{
|
||||
Directory.CreateDirectory(thumbnailPath);
|
||||
}
|
||||
}
|
||||
return Path.Combine(thumbnailPath, GetSaveFileName());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询的的文件路径
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="isThumbnail"></param>
|
||||
/// <returns></returns>
|
||||
public string? GetQueryFileSavePath(bool? isThumbnail)
|
||||
{
|
||||
string fileSavePath;
|
||||
//如果为缩略图,需要修改路径
|
||||
if (isThumbnail is true)
|
||||
{
|
||||
fileSavePath = this.GetAndCheakThumbnailSavePath();
|
||||
}
|
||||
else
|
||||
{
|
||||
fileSavePath = this.GetSaveFilePath();
|
||||
}
|
||||
return fileSavePath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件大小
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "FileSize")]
|
||||
public decimal FileSize { get; set; }
|
||||
public decimal FileSize { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件名
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "FileName")]
|
||||
public string FileName { get; set; }
|
||||
public string FileName { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件路径
|
||||
///</summary>
|
||||
[SugarColumn(ColumnName = "FilePath")]
|
||||
public string FilePath { get; set; }
|
||||
public string FilePath { get; internal set; }
|
||||
|
||||
public DateTime CreationTime { get; set; }
|
||||
public Guid? CreatorId { get; set; }
|
||||
@@ -46,8 +160,5 @@ namespace Yi.Framework.Rbac.Domain.Entities
|
||||
public Guid? LastModifierId { get; set; }
|
||||
|
||||
public DateTime? LastModificationTime { get; set; }
|
||||
|
||||
[SugarColumn(IsIgnore=true)]
|
||||
public override ExtraPropertyDictionary ExtraProperties { get; protected set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
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;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Managers;
|
||||
|
||||
public class FileManager : DomainService, IFileManager
|
||||
{
|
||||
private IGuidGenerator _guidGenerator;
|
||||
private readonly IRepository<FileAggregateRoot> _repository;
|
||||
private readonly IImageCompressor _imageCompressor;
|
||||
public FileManager(IGuidGenerator guidGenerator, IRepository<FileAggregateRoot> repository,
|
||||
|
||||
IImageCompressor imageCompressor)
|
||||
{
|
||||
_guidGenerator = guidGenerator;
|
||||
_repository = repository;
|
||||
_imageCompressor = imageCompressor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入数据库
|
||||
/// </summary>
|
||||
/// <param name="files"></param>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public async Task<List<FileAggregateRoot>> CreateAsync(IEnumerable<IFormFile> files)
|
||||
{
|
||||
if (files.Count() == 0)
|
||||
{
|
||||
throw new ArgumentException("文件上传为空!");
|
||||
}
|
||||
|
||||
//批量插入
|
||||
List<FileAggregateRoot> entities = new();
|
||||
foreach (var file in files)
|
||||
{
|
||||
FileAggregateRoot data = new(_guidGenerator.Create(), file.FileName, (decimal)file.Length / 1024);
|
||||
data.CheckDirectoryOrCreate();
|
||||
entities.Add(data);
|
||||
}
|
||||
|
||||
await _repository.InsertManyAsync(entities);
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 保存文件
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="fileStream"></param>
|
||||
public async Task SaveFileAsync(FileAggregateRoot file, Stream fileStream)
|
||||
{
|
||||
var filePath = file.GetSaveFilePath();
|
||||
|
||||
//生成文件
|
||||
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
|
||||
{
|
||||
await fileStream.CopyToAsync(stream);
|
||||
fileStream.Position = 0;
|
||||
}
|
||||
|
||||
|
||||
//如果是图片类型,还需要生成缩略图
|
||||
//这里根据自己需求变更,我们的需求是:原始文件与缩略图文件,都要一份
|
||||
var fileType = file.GetFileType();
|
||||
//如果文件类型是图片,尝试进行压缩
|
||||
if (FileTypeEnum.image==fileType)
|
||||
{
|
||||
var thumbnailSavePath = file.GetAndCheakThumbnailSavePath(true);
|
||||
Stream compressImageStream=null;
|
||||
try
|
||||
{
|
||||
//压缩图片
|
||||
var compressResult = await _imageCompressor.CompressAsync(fileStream, file.GetMimeMapping());
|
||||
if (compressResult.State == ImageProcessState.Done)
|
||||
{
|
||||
compressImageStream =
|
||||
(await _imageCompressor.CompressAsync(fileStream, file.GetMimeMapping())).Result;
|
||||
}
|
||||
else if (compressResult.State == ImageProcessState.Canceled)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Yi.Framework.Rbac.Domain.Managers;
|
||||
|
||||
public interface IFileManager
|
||||
{
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user