using Abp.Auditing; using Abp.Configuration; using Abp.Dependency; using Abp.Domain.Repositories; using Abp.UI; using VberZero.AppService.Attaches.Dto; using VberZero.BaseSystem; using VberZero.Folders; using VberZero.Settings; using VberZero.Tools.FileHelpers; namespace VberZero.DomainService.Attaches; public class AttachManager : VzDomainServiceBase, IAttachManager, ISingletonDependency { public AttachManager(ISettingManager settingManager, IRepository repository, IAppFolders appFolders, IWebHostEnvironment env) { SettingManager = settingManager; Repository = repository; AppFolders = appFolders; _env = env; //ObjectMapper = SingletonDependency.Instance; } private IRepository Repository { get; } private IAppFolders AppFolders { get; } private readonly IWebHostEnvironment _env; /// /// 查询附件 /// /// /// /// public async Task GetFile(string codeKey, string sourceKey) { var file = await Repository.FirstOrDefaultAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey); var dto = ObjectMapper.Map(file); return dto; } /// /// 查询附件 /// /// /// /// public async Task> GetFiles(string codeKey, string sourceKey) { var files = await Repository.GetAllListAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey); var dtoList = files.Select(ObjectMapper.Map).ToList(); return dtoList; } /// /// 删除附件 /// /// /// /// public Task DeleteFile(string codeKey, string sourceKey) { return Repository.DeleteAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey); } /// /// 删除附件 /// /// /// public Task DeleteFile(int id) { return Repository.DeleteAsync(a => a.Id == id); } /// /// 上传附件 /// /// /// [DisableAuditing] public async Task FileUpload(AttachDto input) { if (await IsValidFileType(input)) { string filePath = string.IsNullOrEmpty(input.FilePath) ? $"{AppFolders.DownloadFolder}/{input.CodeKey.Replace(".", "/")}" : input.FilePath; var lcRetVal = Base64ToFile(input.FileInfo, $"{input.FileName}-{DateTime.Now:yyMMddHHmmssfff}", input.FileExt, filePath); if (lcRetVal.StartsWith("error@")) { throw new UserFriendlyException(lcRetVal.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1]); } input.FilePath = lcRetVal; if (input.IsUpdate) { var entity = await Repository.FirstOrDefaultAsync(a => a.CodeKey == input.CodeKey && a.SourceKey == input.SourceKey); if (entity == null) { entity = ObjectMapper.Map(input); await Repository.InsertAsync(entity); } else { entity.FileExt = input.FileExt; entity.FileName = input.FileName; entity.FileTitle = input.FileTitle; entity.FilePath = lcRetVal; entity.FileType = input.FileType; await Repository.UpdateAsync(entity); } } else { var entity = ObjectMapper.Map(input); await Repository.InsertAsync(entity); } return; } var allowExt = string.IsNullOrEmpty(input.AllowExt) ? await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt) : input.AllowExt; throw new UserFriendlyException(L("FileUploadFileTypeInvalidFormatter", $"{input.FileName}.{input.FileExt}", allowExt)); } /// /// 上传附件 /// /// [DisableAuditing] public async Task FileUpload(string fileInfo, string filePath, string fileName, string fileExt) { if (await IsValidFileType(fileExt)) { var lcRetVal = Base64ToFile(fileInfo, fileName, fileExt, filePath); if (lcRetVal.StartsWith("error@")) { throw new UserFriendlyException(lcRetVal.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1]); } return; } var allowExt = await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt); throw new UserFriendlyException(L("FileUploadFileTypeInvalidFormatter", $"{fileName}.{fileExt}", allowExt)); } /// /// 上传附件 /// /// [DisableAuditing] public async Task FileUpload(string fileInfo, string filePath, string fileName, string fileExt, string allowExt, bool checkAll = true) { checkAll = string.IsNullOrEmpty(allowExt) || checkAll; if (await IsValidFileType(fileExt, allowExt, checkAll)) { var lcRetVal = Base64ToFile(fileInfo, fileName, fileExt, filePath); if (lcRetVal.StartsWith("error@")) { throw new UserFriendlyException(lcRetVal.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1]); } Logger.Info("文件保存路径:" + lcRetVal); return; } allowExt = string.IsNullOrEmpty(allowExt) ? await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt) : checkAll ? await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt) + "," + allowExt : allowExt; throw new UserFriendlyException(L("FileUploadFileTypeInvalidFormatter", $"{fileName}.{fileExt}", allowExt)); } private async Task IsValidFileType(AttachDto input) { return await IsValidFileType(input.FileExt, input.AllowExt, input.CheckAll); } private async Task IsValidFileType(string fileName, string allowExt = null, bool checkAll = true, bool isName = false) { string ext = isName ? GetFileExt(fileName) : fileName; allowExt = checkAll ? $"{allowExt ?? ""},{await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt)}" : allowExt; if (string.IsNullOrEmpty(allowExt)) return true; string[] extList = allowExt.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var e in extList) { if (ext.ToLower() == e.ToLower()) return true; } Logger.Error("上传的文件非法:" + fileName); return false; } private string GetFileExt(string fileName) { string fileExt = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal) + 1, fileName.Length - fileName.LastIndexOf(".", StringComparison.Ordinal) - 1); return fileExt.ToLower(); } private string Base64ToFile(string base64Str, string fileName, string fileExt, string filePath) { //string lcRetVal = "error@"; //try //{ // fileName = $"{fileName}.{fileExt}"; // filePath = filePath.EndsWith("/") ? filePath : (filePath + "/"); // string path = Path.Combine(Env.WebRootPath, filePath); // base64Str.SaveBase64File(path,fileName); // lcRetVal = filePath + fileName; //} //catch (Exception e) //{ // Logger.Error(e.Message,e); // lcRetVal += "FileUploadException"; //} string lcRetVal = base64Str.Base64ToFile(fileName, filePath, fileExt, _env); return lcRetVal; } }