using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 { [SugarTable("File")] public class FileAggregateRoot : AggregateRoot, IAuditedObject { public FileAggregateRoot() { } /// /// 创建文件 /// /// 文件标识id /// 文件名 /// 文件大小 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; } /// /// 检测目录是否存在,不存在便创建 /// public void CheckDirectoryOrCreate() { var savePath = GetSaveDirPath(); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } } /// /// 文件类型 /// /// public FileTypeEnum GetFileType() { return MimeHelper.GetFileType(this.FileName); } /// /// 获取文件mime /// /// public string GetMimeMapping() { return MimeHelper.GetMimeMapping(this.FileName)??@"text/plain"; } /// /// 落库目录路径 /// /// public string GetSaveDirPath() { return $"wwwroot/{GetFileType()}"; } /// /// 落库文件路径 /// /// public string GetSaveFilePath() { string savefileName = GetSaveFileName(); return Path.Combine(GetSaveDirPath(), savefileName); } /// /// 获取保存的文件名 /// /// public string GetSaveFileName() { return this.Id.ToString() + Path.GetExtension(this.FileName); } /// /// 检测,并且返回缩略图的保存路径 /// /// /// 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()); } /// /// 获取查询的的文件路径 /// /// /// /// public string? GetQueryFileSavePath(bool? isThumbnail) { string fileSavePath; //如果为缩略图,需要修改路径 if (isThumbnail is true) { fileSavePath = this.GetAndCheakThumbnailSavePath(); } else { fileSavePath = this.GetSaveFilePath(); } return fileSavePath; } /// /// 文件大小 /// [SugarColumn(ColumnName = "FileSize")] public decimal FileSize { get; internal set; } /// /// 文件名 /// [SugarColumn(ColumnName = "FileName")] public string FileName { get; internal set; } /// /// 文件路径 /// [SugarColumn(ColumnName = "FilePath")] public string FilePath { get; internal set; } public DateTime CreationTime { get; set; } public Guid? CreatorId { get; set; } public Guid? LastModifierId { get; set; } public DateTime? LastModificationTime { get; set; } } }