FileController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using GxPress.Auth;
  7. using GxPress.Common.Exceptions;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.App.FileLibrary;
  12. using GxPress.Result.App.FileLibrary;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Microsoft.AspNetCore.Hosting;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using GxPress.Service.Interface.FileLibrary;
  18. using GxPress.Repository.Interface.VerificationCode;
  19. using Datory;
  20. namespace GxPress.Api.AppControllers
  21. {
  22. [Route("api/app/file")]
  23. [ApiController]
  24. [Authorize]
  25. public class FileController : ControllerBase
  26. {
  27. private readonly IWebHostEnvironment _environment;
  28. private readonly IFileLibraryRepository _fileLibraryRepository;
  29. private readonly IFileLibraryService fileLibraryService;
  30. private readonly ILoginContext _loginContext;
  31. private readonly IVerificationCodeRepository verificationCodeRepository;
  32. public FileController(IWebHostEnvironment environment, IFileLibraryRepository fileLibraryRepository, ILoginContext loginContext, IFileLibraryService fileLibraryService, IVerificationCodeRepository verificationCodeRepository)
  33. {
  34. _environment = environment;
  35. _fileLibraryRepository = fileLibraryRepository;
  36. _loginContext = loginContext;
  37. this.fileLibraryService = fileLibraryService;
  38. this.verificationCodeRepository = verificationCodeRepository;
  39. }
  40. /// <summary>
  41. /// App上传
  42. /// </summary>
  43. /// <param name="file"></param>
  44. /// <returns></returns>
  45. [HttpPost("upload")]
  46. [AllowAnonymous]
  47. [DisableRequestSizeLimit]
  48. public async Task<ResultPath> Upload(IFormFile file)
  49. {
  50. if (file == null) throw new BusinessException("请选择需要上传的文件");
  51. byte[] bytes;
  52. await using (var ms = new MemoryStream())
  53. {
  54. file.CopyTo(ms);
  55. bytes = ms.ToArray();
  56. }
  57. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), file.FileName,
  58. bytes);
  59. //saveResult.AbsolutePath = "https://" + Request.Host.Host +(Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +saveResult.RelativePath;
  60. saveResult.AbsolutePath = StringUtils.AddDomain(saveResult.RelativePath);
  61. saveResult.FileName = file.FileName;
  62. saveResult.FileType = saveResult.FileName.Split('.')[saveResult.FileName.Split('.').Length - 1];
  63. //图片种类
  64. string imageType =
  65. "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp";
  66. var imageTypeList = StringUtils.StringCollectionToStringList(imageType);
  67. if (imageTypeList.Contains(saveResult.FileType.ToLower()))
  68. {
  69. saveResult.FileType = "image";
  70. saveResult.MinAbsolutePath = StringUtils.AddDomainMin(saveResult.RelativePath);
  71. }
  72. //添加
  73. var fileLibrary = new FileLibrary
  74. {
  75. FileName = saveResult.FileName,
  76. FileType = saveResult.FileType,
  77. FileUrl = saveResult.RelativePath,
  78. UserId = 0,
  79. Size = file.Length
  80. };
  81. saveResult.Size = file.Length;
  82. saveResult.FileId = await fileLibraryService.InsertAsync(fileLibrary);
  83. return saveResult;
  84. }
  85. /// <summary>
  86. /// App多文件上传
  87. /// </summary>
  88. /// <param name="file"></param>
  89. /// <returns></returns>
  90. [HttpPost("uploads")]
  91. [AllowAnonymous]
  92. [DisableRequestSizeLimit]
  93. public async Task<List<ResultPath>> Uploads(List<IFormFile> file)
  94. {
  95. if (file == null) throw new BusinessException("请选择需要上传的文件");
  96. List<ResultPath> result = new List<ResultPath>();
  97. foreach (var item in file)
  98. {
  99. byte[] bytes;
  100. await using (var ms = new MemoryStream())
  101. {
  102. item.CopyTo(ms);
  103. bytes = ms.ToArray();
  104. }
  105. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), item.FileName,
  106. bytes);
  107. //saveResult.AbsolutePath = "https://" + Request.Host.Host +(Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +saveResult.RelativePath;
  108. saveResult.AbsolutePath = StringUtils.AddDomain(saveResult.RelativePath);
  109. saveResult.FileName = item.FileName;
  110. saveResult.FileType = item.FileName.Split('.')[item.FileName.Split('.').Length - 1];
  111. //图片种类
  112. string imageType =
  113. "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp";
  114. var imageTypeList = StringUtils.StringCollectionToStringList(imageType);
  115. if (imageTypeList.Contains(saveResult.FileType.ToLower()))
  116. {
  117. saveResult.MinAbsolutePath = StringUtils.AddDomainMin(saveResult.RelativePath);
  118. if (saveResult.FileType.ToLower() == "webp")
  119. saveResult = Common.Tools.FileHelper.WebPToImage(StringUtils.GetWebRootPath(_environment.WebRootPath), item.FileName, saveResult, StringUtils.AddDomain(saveResult.RelativePath));
  120. saveResult.FileType = "image";
  121. }
  122. //添加
  123. var fileLibrary = new FileLibrary
  124. {
  125. FileName = saveResult.FileName,
  126. FileType = saveResult.FileType,
  127. FileUrl = saveResult.RelativePath,
  128. UserId = 0,
  129. Size = item.Length
  130. };
  131. saveResult.Size = item.Length;
  132. saveResult.FileId = await _fileLibraryRepository.InsertAsync(fileLibrary);
  133. result.Add(saveResult);
  134. }
  135. return result;
  136. }
  137. /// <summary>
  138. /// 根据ID查询文件数据
  139. /// </summary>
  140. /// <param name="id"></param>
  141. /// <returns></returns>
  142. [HttpGet("{id}")]
  143. public async Task<FileLibraryResult> GetFileLibraryById(int id)
  144. {
  145. return await _fileLibraryRepository.GetFileLibraryByIdAsync(id);
  146. }
  147. /// <summary>
  148. /// 根据ID获取文件数据
  149. /// </summary>
  150. /// <param name="ids"></param>
  151. /// <returns></returns>
  152. [HttpPost("find")]
  153. public async Task<IEnumerable<FileLibraryResult>> GetFileLibraryByIds([FromBody] GetFileLibraryByIdsRequest ids)
  154. {
  155. return await _fileLibraryRepository.GetFileLibraryByIdsAsync(ids.FileLibraryByIds);
  156. }
  157. /// <summary>
  158. /// App多文件上传
  159. /// </summary>
  160. /// <param name="file"></param>
  161. /// <param name="code"></param>
  162. /// <returns></returns>
  163. [HttpPost("upload-files")]
  164. [AllowAnonymous]
  165. [DisableRequestSizeLimit]
  166. public async Task<List<ResultPath>> UploadFiles(List<IFormFile> file, [FromForm] string code)
  167. {
  168. if (string.IsNullOrWhiteSpace(code))
  169. throw new BusinessException("code无效或为空值");
  170. if (file == null) throw new BusinessException("请选择需要上传的文件");
  171. //获取verificationCode
  172. var verificationCode = await verificationCodeRepository.GetAsync(Q.Where(nameof(Entity.VerificationCode.VerificationCode.Code), code).OrderByDesc(nameof(Entity.VerificationCode.VerificationCode.CreatedDate)));
  173. if (verificationCode == null)
  174. throw new BusinessException("验证码无效");
  175. var verificationCodeTime = Convert.ToDateTime(verificationCode.CreatedDate).AddMinutes(verificationCode.ExpireTime);
  176. if (verificationCodeTime < DateTime.Now)
  177. throw new BusinessException("验证码已经过期");
  178. List<ResultPath> result = new List<ResultPath>();
  179. foreach (var item in file)
  180. {
  181. byte[] bytes;
  182. await using (var ms = new MemoryStream())
  183. {
  184. item.CopyTo(ms);
  185. bytes = ms.ToArray();
  186. }
  187. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), item.FileName,
  188. bytes);
  189. saveResult.AbsolutePath = StringUtils.AddDomain(saveResult.RelativePath) +
  190. saveResult.RelativePath;
  191. saveResult.FileName = item.FileName;
  192. saveResult.FileType = item.FileName.Split('.')[item.FileName.Split('.').Length - 1];
  193. //图片种类
  194. string imageType =
  195. "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp";
  196. var imageTypeList = StringUtils.StringCollectionToStringList(imageType);
  197. if (imageTypeList.Contains(saveResult.FileType.ToLower()))
  198. {
  199. saveResult.FileType = "image";
  200. saveResult.MinAbsolutePath = StringUtils.AddDomainMin(saveResult.RelativePath);
  201. }
  202. //
  203. //添加
  204. var fileLibrary = new FileLibrary
  205. {
  206. FileName = saveResult.FileName,
  207. FileType = saveResult.FileType,
  208. FileUrl = saveResult.RelativePath,
  209. UserId = verificationCode.UserId,
  210. Size = item.Length
  211. };
  212. saveResult.Size = item.Length;
  213. saveResult.FileId = await fileLibraryService.InsertAsync(fileLibrary);
  214. result.Add(saveResult);
  215. }
  216. return result;
  217. }
  218. }
  219. }