FileController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. [AllowAnonymous]
  46. [HttpPost("upload")]
  47. public async Task<ResultPath> Upload(IFormFile file)
  48. {
  49. if (file == null) throw new BusinessException("请选择需要上传的文件");
  50. byte[] bytes;
  51. await using (var ms = new MemoryStream())
  52. {
  53. file.CopyTo(ms);
  54. bytes = ms.ToArray();
  55. }
  56. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), file.FileName,
  57. bytes);
  58. saveResult.AbsolutePath = Request.Scheme + "://" + Request.Host.Host +
  59. (Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +
  60. 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. public async Task<List<ResultPath>> Uploads(List<IFormFile> file)
  93. {
  94. if (file == null) throw new BusinessException("请选择需要上传的文件");
  95. List<ResultPath> result = new List<ResultPath>();
  96. foreach (var item in file)
  97. {
  98. byte[] bytes;
  99. await using (var ms = new MemoryStream())
  100. {
  101. item.CopyTo(ms);
  102. bytes = ms.ToArray();
  103. }
  104. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), item.FileName,
  105. bytes);
  106. saveResult.AbsolutePath = Request.Scheme + "://" + Request.Host.Host +
  107. (Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +
  108. 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,Request.Scheme + "://" + Request.Host.Host +
  120. (Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty));
  121. saveResult.FileType = "image";
  122. }
  123. //添加
  124. var fileLibrary = new FileLibrary
  125. {
  126. FileName = saveResult.FileName,
  127. FileType = saveResult.FileType,
  128. FileUrl = saveResult.RelativePath,
  129. UserId = 0,
  130. Size = item.Length
  131. };
  132. saveResult.Size = item.Length;
  133. saveResult.FileId = await _fileLibraryRepository.InsertAsync(fileLibrary);
  134. result.Add(saveResult);
  135. }
  136. return result;
  137. }
  138. /// <summary>
  139. /// 根据ID查询文件数据
  140. /// </summary>
  141. /// <param name="id"></param>
  142. /// <returns></returns>
  143. [HttpGet("{id}")]
  144. public async Task<FileLibraryResult> GetFileLibraryById(int id)
  145. {
  146. return await _fileLibraryRepository.GetFileLibraryByIdAsync(id);
  147. }
  148. /// <summary>
  149. /// 根据ID获取文件数据
  150. /// </summary>
  151. /// <param name="ids"></param>
  152. /// <returns></returns>
  153. [HttpPost("find")]
  154. public async Task<IEnumerable<FileLibraryResult>> GetFileLibraryByIds([FromBody] GetFileLibraryByIdsRequest ids)
  155. {
  156. return await _fileLibraryRepository.GetFileLibraryByIdsAsync(ids.FileLibraryByIds);
  157. }
  158. /// <summary>
  159. /// App多文件上传
  160. /// </summary>
  161. /// <param name="file"></param>
  162. /// <param name="code"></param>
  163. /// <returns></returns>
  164. [HttpPost("upload-files")]
  165. [AllowAnonymous]
  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 = Request.Scheme + "://" + Request.Host.Host +
  190. (Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +
  191. saveResult.RelativePath;
  192. saveResult.FileName = item.FileName;
  193. saveResult.FileType = item.FileName.Split('.')[item.FileName.Split('.').Length - 1];
  194. //图片种类
  195. string imageType =
  196. "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp";
  197. var imageTypeList = StringUtils.StringCollectionToStringList(imageType);
  198. if (imageTypeList.Contains(saveResult.FileType.ToLower()))
  199. {
  200. saveResult.FileType = "image";
  201. saveResult.MinAbsolutePath = StringUtils.AddDomainMin(saveResult.RelativePath);
  202. }
  203. //
  204. //添加
  205. var fileLibrary = new FileLibrary
  206. {
  207. FileName = saveResult.FileName,
  208. FileType = saveResult.FileType,
  209. FileUrl = saveResult.RelativePath,
  210. UserId = verificationCode.UserId,
  211. Size = item.Length
  212. };
  213. saveResult.Size = item.Length;
  214. saveResult.FileId = await fileLibraryService.InsertAsync(fileLibrary);
  215. result.Add(saveResult);
  216. }
  217. return result;
  218. }
  219. }
  220. }