AttachManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using Abp.Auditing;
  2. using Abp.Configuration;
  3. using Abp.Dependency;
  4. using Abp.Domain.Repositories;
  5. using Abp.UI;
  6. using VberZero.AppService.Attaches.Dto;
  7. using VberZero.BaseSystem;
  8. using VberZero.Folders;
  9. using VberZero.Settings;
  10. using VberZero.Tools.FileHelpers;
  11. namespace VberZero.DomainService.Attaches;
  12. public class AttachManager : VzDomainServiceBase, IAttachManager, ISingletonDependency
  13. {
  14. public AttachManager(ISettingManager settingManager, IRepository<SysAttach> repository, IAppFolders appFolders, IWebHostEnvironment env)
  15. {
  16. SettingManager = settingManager;
  17. Repository = repository;
  18. AppFolders = appFolders;
  19. _env = env;
  20. //ObjectMapper = SingletonDependency<AutoMapperObjectMapper>.Instance;
  21. }
  22. private IRepository<SysAttach> Repository { get; }
  23. private IAppFolders AppFolders { get; }
  24. private readonly IWebHostEnvironment _env;
  25. /// <summary>
  26. /// 查询附件
  27. /// </summary>
  28. /// <param name="codeKey"></param>
  29. /// <param name="sourceKey"></param>
  30. /// <returns></returns>
  31. public async Task<AttachDto> GetFile(string codeKey, string sourceKey)
  32. {
  33. var file =
  34. await Repository.FirstOrDefaultAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey);
  35. var dto = ObjectMapper.Map<AttachDto>(file);
  36. return dto;
  37. }
  38. /// <summary>
  39. /// 查询附件
  40. /// </summary>
  41. /// <param name="codeKey"></param>
  42. /// <param name="sourceKey"></param>
  43. /// <returns></returns>
  44. public async Task<List<AttachDto>> GetFiles(string codeKey, string sourceKey)
  45. {
  46. var files =
  47. await Repository.GetAllListAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey);
  48. var dtoList = files.Select(ObjectMapper.Map<AttachDto>).ToList();
  49. return dtoList;
  50. }
  51. /// <summary>
  52. /// 删除附件
  53. /// </summary>
  54. /// <param name="codeKey"></param>
  55. /// <param name="sourceKey"></param>
  56. /// <returns></returns>
  57. public Task DeleteFile(string codeKey, string sourceKey)
  58. {
  59. return Repository.DeleteAsync(a => a.CodeKey == codeKey && a.SourceKey == sourceKey);
  60. }
  61. /// <summary>
  62. /// 删除附件
  63. /// </summary>
  64. /// <param name="id"></param>
  65. /// <returns></returns>
  66. public Task DeleteFile(int id)
  67. {
  68. return Repository.DeleteAsync(a => a.Id == id);
  69. }
  70. /// <summary>
  71. /// 上传附件
  72. /// </summary>
  73. /// <param name="input"></param>
  74. /// <returns></returns>
  75. [DisableAuditing]
  76. public async Task FileUpload(AttachDto input)
  77. {
  78. if (await IsValidFileType(input))
  79. {
  80. string filePath = string.IsNullOrEmpty(input.FilePath)
  81. ? $"{AppFolders.DownloadFolder}/{input.CodeKey.Replace(".", "/")}"
  82. : input.FilePath;
  83. var lcRetVal = Base64ToFile(input.FileInfo, $"{input.FileName}-{DateTime.Now:yyMMddHHmmssfff}", input.FileExt, filePath);
  84. if (lcRetVal.StartsWith("error@"))
  85. {
  86. throw new UserFriendlyException(lcRetVal.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1]);
  87. }
  88. input.FilePath = lcRetVal;
  89. if (input.IsUpdate)
  90. {
  91. var entity = await Repository.FirstOrDefaultAsync(a =>
  92. a.CodeKey == input.CodeKey &&
  93. a.SourceKey == input.SourceKey);
  94. if (entity == null)
  95. {
  96. entity = ObjectMapper.Map<SysAttach>(input);
  97. await Repository.InsertAsync(entity);
  98. }
  99. else
  100. {
  101. entity.FileExt = input.FileExt;
  102. entity.FileName = input.FileName;
  103. entity.FileTitle = input.FileTitle;
  104. entity.FilePath = lcRetVal;
  105. entity.FileType = input.FileType;
  106. await Repository.UpdateAsync(entity);
  107. }
  108. }
  109. else
  110. {
  111. var entity = ObjectMapper.Map<SysAttach>(input);
  112. await Repository.InsertAsync(entity);
  113. }
  114. return;
  115. }
  116. var allowExt = string.IsNullOrEmpty(input.AllowExt)
  117. ? await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt)
  118. : input.AllowExt;
  119. throw new UserFriendlyException(L("FileUploadFileTypeInvalidFormatter", $"{input.FileName}.{input.FileExt}", allowExt));
  120. }
  121. /// <summary>
  122. /// 上传附件
  123. /// </summary>
  124. /// <returns></returns>
  125. [DisableAuditing]
  126. public async Task FileUpload(string fileInfo, string filePath, string fileName, string fileExt)
  127. {
  128. if (await IsValidFileType(fileExt))
  129. {
  130. var lcRetVal = Base64ToFile(fileInfo, fileName, fileExt, filePath);
  131. if (lcRetVal.StartsWith("error@"))
  132. {
  133. throw new UserFriendlyException(lcRetVal.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1]);
  134. }
  135. return;
  136. }
  137. var allowExt = await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt);
  138. throw new UserFriendlyException(L("FileUploadFileTypeInvalidFormatter", $"{fileName}.{fileExt}", allowExt));
  139. }
  140. /// <summary>
  141. /// 上传附件
  142. /// </summary>
  143. /// <returns></returns>
  144. [DisableAuditing]
  145. public async Task FileUpload(string fileInfo, string filePath, string fileName, string fileExt, string allowExt, bool checkAll = true)
  146. {
  147. checkAll = string.IsNullOrEmpty(allowExt) || checkAll;
  148. if (await IsValidFileType(fileExt, allowExt, checkAll))
  149. {
  150. var lcRetVal = Base64ToFile(fileInfo, fileName, fileExt, filePath);
  151. if (lcRetVal.StartsWith("error@"))
  152. {
  153. throw new UserFriendlyException(lcRetVal.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1]);
  154. }
  155. Logger.Info("文件保存路径:" + lcRetVal);
  156. return;
  157. }
  158. allowExt = string.IsNullOrEmpty(allowExt)
  159. ?
  160. await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt)
  161. : checkAll
  162. ? await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt) + "," + allowExt
  163. : allowExt;
  164. throw new UserFriendlyException(L("FileUploadFileTypeInvalidFormatter", $"{fileName}.{fileExt}", allowExt));
  165. }
  166. private async Task<bool> IsValidFileType(AttachDto input)
  167. {
  168. return await IsValidFileType(input.FileExt, input.AllowExt, input.CheckAll);
  169. }
  170. private async Task<bool> IsValidFileType(string fileName, string allowExt = null, bool checkAll = true, bool isName = false)
  171. {
  172. string ext = isName ? GetFileExt(fileName) : fileName;
  173. allowExt = checkAll
  174. ? $"{allowExt ?? ""},{await SettingManager.GetSettingValueAsync(VzSettingNames.UploadFileExt)}"
  175. : allowExt;
  176. if (string.IsNullOrEmpty(allowExt))
  177. return true;
  178. string[] extList = allowExt.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  179. foreach (var e in extList)
  180. {
  181. if (ext.ToLower() == e.ToLower())
  182. return true;
  183. }
  184. Logger.Error("上传的文件非法:" + fileName);
  185. return false;
  186. }
  187. private string GetFileExt(string fileName)
  188. {
  189. string fileExt = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal) + 1, fileName.Length - fileName.LastIndexOf(".", StringComparison.Ordinal) - 1);
  190. return fileExt.ToLower();
  191. }
  192. private string Base64ToFile(string base64Str, string fileName, string fileExt, string filePath)
  193. {
  194. //string lcRetVal = "error@";
  195. //try
  196. //{
  197. // fileName = $"{fileName}.{fileExt}";
  198. // filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
  199. // string path = Path.Combine(Env.WebRootPath, filePath);
  200. // base64Str.SaveBase64File(path,fileName);
  201. // lcRetVal = filePath + fileName;
  202. //}
  203. //catch (Exception e)
  204. //{
  205. // Logger.Error(e.Message,e);
  206. // lcRetVal += "FileUploadException";
  207. //}
  208. string lcRetVal = base64Str.Base64ToFile(fileName, filePath, fileExt, _env);
  209. return lcRetVal;
  210. }
  211. }