| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- 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<SysAttach> repository, IAppFolders appFolders, IWebHostEnvironment env)
- {
- SettingManager = settingManager;
- Repository = repository;
- AppFolders = appFolders;
- _env = env;
- //ObjectMapper = SingletonDependency<AutoMapperObjectMapper>.Instance;
- }
- private IRepository<SysAttach> Repository { get; }
- private IAppFolders AppFolders { get; }
- private readonly IWebHostEnvironment _env;
- /// <summary>
- /// 查询附件
- /// </summary>
- /// <param name="codeKey"></param>
- /// <param name="sourceKey"></param>
- /// <returns></returns>
- public async Task<AttachDto> GetFile(string codeKey, string sourceKey)
- {
- var file =
- await Repository.FirstOrDefaultAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey);
- var dto = ObjectMapper.Map<AttachDto>(file);
- return dto;
- }
- /// <summary>
- /// 查询附件
- /// </summary>
- /// <param name="codeKey"></param>
- /// <param name="sourceKey"></param>
- /// <returns></returns>
- public async Task<List<AttachDto>> GetFiles(string codeKey, string sourceKey)
- {
- var files =
- await Repository.GetAllListAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey);
- var dtoList = files.Select(ObjectMapper.Map<AttachDto>).ToList();
- return dtoList;
- }
- /// <summary>
- /// 删除附件
- /// </summary>
- /// <param name="codeKey"></param>
- /// <param name="sourceKey"></param>
- /// <returns></returns>
- public Task DeleteFile(string codeKey, string sourceKey)
- {
- return Repository.DeleteAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey);
- }
- /// <summary>
- /// 删除附件
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public Task DeleteFile(int id)
- {
- return Repository.DeleteAsync(a => a.Id == id);
- }
- /// <summary>
- /// 上传附件
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [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<SysAttach>(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<SysAttach>(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));
- }
- /// <summary>
- /// 上传附件
- /// </summary>
- /// <returns></returns>
- [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));
- }
- /// <summary>
- /// 上传附件
- /// </summary>
- /// <returns></returns>
- [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<bool> IsValidFileType(AttachDto input)
- {
- return await IsValidFileType(input.FileExt, input.AllowExt, input.CheckAll);
- }
- private async Task<bool> 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;
- }
- }
|