FileController.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = "https://" + Request.Host.Host +(Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +saveResult.RelativePath;
  59. saveResult.AbsolutePath = StringUtils.AddDomain(saveResult.RelativePath);
  60. saveResult.FileName = file.FileName;
  61. saveResult.FileType = saveResult.FileName.Split('.')[saveResult.FileName.Split('.').Length - 1];
  62. //图片种类
  63. string imageType =
  64. "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp";
  65. var imageTypeList = StringUtils.StringCollectionToStringList(imageType);
  66. if (imageTypeList.Contains(saveResult.FileType.ToLower()))
  67. {
  68. saveResult.FileType = "image";
  69. saveResult.MinAbsolutePath = StringUtils.AddDomainMin(saveResult.RelativePath);
  70. }
  71. //添加
  72. var fileLibrary = new FileLibrary
  73. {
  74. FileName = saveResult.FileName,
  75. FileType = saveResult.FileType,
  76. FileUrl = saveResult.RelativePath,
  77. UserId = 0,
  78. Size = file.Length
  79. };
  80. saveResult.Size = file.Length;
  81. saveResult.FileId = await fileLibraryService.InsertAsync(fileLibrary);
  82. return saveResult;
  83. }
  84. /// <summary>
  85. /// App多文件上传
  86. /// </summary>
  87. /// <param name="file"></param>
  88. /// <returns></returns>
  89. [HttpPost("uploads")]
  90. [AllowAnonymous]
  91. public async Task<List<ResultPath>> Uploads(List<IFormFile> file)
  92. {
  93. if (file == null) throw new BusinessException("请选择需要上传的文件");
  94. List<ResultPath> result = new List<ResultPath>();
  95. foreach (var item in file)
  96. {
  97. byte[] bytes;
  98. await using (var ms = new MemoryStream())
  99. {
  100. item.CopyTo(ms);
  101. bytes = ms.ToArray();
  102. }
  103. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), item.FileName,
  104. bytes);
  105. //saveResult.AbsolutePath = "https://" + Request.Host.Host +(Request.Host.Port > 0 ? $":{Request.Host.Port}" : string.Empty) +saveResult.RelativePath;
  106. saveResult.AbsolutePath = StringUtils.AddDomain(saveResult.RelativePath);
  107. saveResult.FileName = item.FileName;
  108. saveResult.FileType = item.FileName.Split('.')[item.FileName.Split('.').Length - 1];
  109. //图片种类
  110. string imageType =
  111. "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp";
  112. var imageTypeList = StringUtils.StringCollectionToStringList(imageType);
  113. if (imageTypeList.Contains(saveResult.FileType.ToLower()))
  114. {
  115. saveResult.MinAbsolutePath = StringUtils.AddDomainMin(saveResult.RelativePath);
  116. if (saveResult.FileType.ToLower() == "webp")
  117. saveResult = Common.Tools.FileHelper.WebPToImage(StringUtils.GetWebRootPath(_environment.WebRootPath), item.FileName, saveResult, StringUtils.AddDomain(saveResult.RelativePath));
  118. saveResult.FileType = "image";
  119. }
  120. //添加
  121. var fileLibrary = new FileLibrary
  122. {
  123. FileName = saveResult.FileName,
  124. FileType = saveResult.FileType,
  125. FileUrl = saveResult.RelativePath,
  126. UserId = 0,
  127. Size = item.Length
  128. };
  129. saveResult.Size = item.Length;
  130. saveResult.FileId = await _fileLibraryRepository.InsertAsync(fileLibrary);
  131. result.Add(saveResult);
  132. }
  133. return result;
  134. }
  135. /// <summary>
  136. /// 根据ID查询文件数据
  137. /// </summary>
  138. /// <param name="id"></param>
  139. /// <returns></returns>
  140. [HttpGet("{id}")]
  141. public async Task<FileLibraryResult> GetFileLibraryById(int id)
  142. {
  143. return await _fileLibraryRepository.GetFileLibraryByIdAsync(id);
  144. }
  145. /// <summary>
  146. /// 根据ID获取文件数据
  147. /// </summary>
  148. /// <param name="ids"></param>
  149. /// <returns></returns>
  150. [HttpPost("find")]
  151. public async Task<IEnumerable<FileLibraryResult>> GetFileLibraryByIds([FromBody] GetFileLibraryByIdsRequest ids)
  152. {
  153. return await _fileLibraryRepository.GetFileLibraryByIdsAsync(ids.FileLibraryByIds);
  154. }
  155. /// <summary>
  156. /// App多文件上传
  157. /// </summary>
  158. /// <param name="file"></param>
  159. /// <param name="code"></param>
  160. /// <returns></returns>
  161. [HttpPost("upload-files")]
  162. [AllowAnonymous]
  163. public async Task<List<ResultPath>> UploadFiles(List<IFormFile> file, [FromForm] string code)
  164. {
  165. if (string.IsNullOrWhiteSpace(code))
  166. throw new BusinessException("code无效或为空值");
  167. if (file == null) throw new BusinessException("请选择需要上传的文件");
  168. //获取verificationCode
  169. var verificationCode = await verificationCodeRepository.GetAsync(Q.Where(nameof(Entity.VerificationCode.VerificationCode.Code), code).OrderByDesc(nameof(Entity.VerificationCode.VerificationCode.CreatedDate)));
  170. if (verificationCode == null)
  171. throw new BusinessException("验证码无效");
  172. var verificationCodeTime = Convert.ToDateTime(verificationCode.CreatedDate).AddMinutes(verificationCode.ExpireTime);
  173. if (verificationCodeTime < DateTime.Now)
  174. throw new BusinessException("验证码已经过期");
  175. List<ResultPath> result = new List<ResultPath>();
  176. foreach (var item in file)
  177. {
  178. byte[] bytes;
  179. await using (var ms = new MemoryStream())
  180. {
  181. item.CopyTo(ms);
  182. bytes = ms.ToArray();
  183. }
  184. var saveResult = FileHelper.SaveFile(StringUtils.GetWebRootPath(_environment.WebRootPath), item.FileName,
  185. bytes);
  186. saveResult.AbsolutePath =StringUtils.AddDomain(saveResult.RelativePath) +
  187. saveResult.RelativePath;
  188. saveResult.FileName = item.FileName;
  189. saveResult.FileType = item.FileName.Split('.')[item.FileName.Split('.').Length - 1];
  190. //图片种类
  191. string imageType =
  192. "bmp,jpg,jpeg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,wmf,webp";
  193. var imageTypeList = StringUtils.StringCollectionToStringList(imageType);
  194. if (imageTypeList.Contains(saveResult.FileType.ToLower()))
  195. {
  196. saveResult.FileType = "image";
  197. saveResult.MinAbsolutePath = StringUtils.AddDomainMin(saveResult.RelativePath);
  198. }
  199. //
  200. //添加
  201. var fileLibrary = new FileLibrary
  202. {
  203. FileName = saveResult.FileName,
  204. FileType = saveResult.FileType,
  205. FileUrl = saveResult.RelativePath,
  206. UserId = verificationCode.UserId,
  207. Size = item.Length
  208. };
  209. saveResult.Size = item.Length;
  210. saveResult.FileId = await fileLibraryService.InsertAsync(fileLibrary);
  211. result.Add(saveResult);
  212. }
  213. return result;
  214. }
  215. }
  216. }