FileController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Hotline.Repository.SqlSugar.Extensions;
  2. using Hotline.Settings;
  3. using Hotline.Share.Dtos;
  4. using Hotline.Share.Dtos.File;
  5. using MapsterMapper;
  6. using Microsoft.AspNetCore.Mvc;
  7. using XF.Domain.Authentications;
  8. using XF.Domain.Exceptions;
  9. using XF.Domain.Repository;
  10. using Hotline.Caching.Interfaces;
  11. using Microsoft.AspNetCore.Authorization;
  12. namespace Hotline.Api.Controllers
  13. {
  14. public class FileController : BaseController
  15. {
  16. private readonly ISessionContext _sessionContext;
  17. private readonly IMapper _mapper;
  18. private readonly ISystemDicDataCacheManager _sysDicDataCacheManager;
  19. private readonly IRepository<File.File> _fileRepository;
  20. private readonly ILogger<FileController> _logger;
  21. public FileController(
  22. ISessionContext sessionContext,
  23. IMapper mapper,
  24. ISystemDicDataCacheManager sysDicDataCacheManager,
  25. IRepository<File.File> fileRepository,
  26. ILogger<FileController> logger)
  27. {
  28. _sessionContext = sessionContext;
  29. _mapper = mapper;
  30. _sysDicDataCacheManager = sysDicDataCacheManager;
  31. _fileRepository = fileRepository;
  32. _logger = logger;
  33. }
  34. #region 附件管理
  35. /// <summary>
  36. /// 新增附件
  37. /// </summary>
  38. /// <param name="dtos"></param>
  39. /// <returns></returns>
  40. [HttpPost]
  41. public async Task Add([FromBody] List<FileDto> dtos)
  42. {
  43. List<File.File> files = new List<File.File>();
  44. foreach (var dto in dtos)
  45. {
  46. if (string.IsNullOrEmpty(dto.Key))
  47. throw UserFriendlyException.SameMessage("请上传附件关联Key");
  48. var model = _mapper.Map<File.File>(dto);
  49. model.OrgName = _sessionContext.OrgName;
  50. model.OrgId = _sessionContext.OrgId;
  51. model.UserId = _sessionContext.UserId;
  52. model.UserName = _sessionContext.UserName;
  53. files.Add(model);
  54. }
  55. await _fileRepository.AddRangeAsync(files, HttpContext.RequestAborted);
  56. }
  57. /// <summary>
  58. /// 删除附件
  59. /// </summary>
  60. /// <param name="dto"></param>
  61. /// <returns></returns>
  62. [HttpDelete]
  63. public async Task Delete([FromBody] DeleteFileDto dto)
  64. {
  65. foreach (var Id in dto.Ids)
  66. {
  67. await _fileRepository.RemoveAsync(x => x.Id == Id);
  68. }
  69. }
  70. /// <summary>
  71. /// 更新附件
  72. /// </summary>
  73. /// <param name="dto"></param>
  74. /// <returns></returns>
  75. [HttpPut]
  76. public async Task Update([FromBody] UpdateFileDto dto)
  77. {
  78. //验证工单是否可以申请
  79. var file = await _fileRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
  80. if (file is null)
  81. throw UserFriendlyException.SameMessage("无效附件");
  82. _mapper.Map(dto, file);
  83. file.OrgName = _sessionContext.OrgName;
  84. file.OrgId = _sessionContext.OrgId;
  85. file.UserId = _sessionContext.UserId;
  86. file.UserName = _sessionContext.UserName;
  87. await _fileRepository.UpdateAsync(file, HttpContext.RequestAborted);
  88. }
  89. /// <summary>
  90. /// 获取附件列表
  91. /// </summary>
  92. /// <param name="dto"></param>
  93. /// <returns></returns>
  94. [HttpGet("list")]
  95. public async Task<PagedDto<File.File>> List([FromQuery] FileListDto dto)
  96. {
  97. var (total, items) = await _fileRepository.Queryable()
  98. .WhereIF(!string.IsNullOrEmpty(dto.Name), x => x.Name.Contains(dto.Name))
  99. .WhereIF(!string.IsNullOrEmpty(dto.Key), x => x.Key == dto.Key)
  100. .WhereIF(!string.IsNullOrEmpty(dto.Type), x => x.Type == dto.Type)
  101. .WhereIF(!string.IsNullOrEmpty(dto.OrgName), x => x.OrgName.Contains(dto.OrgName))
  102. .WhereIF(dto.Publicity > 0, x => x.Publicity == dto.Publicity)
  103. .WhereIF(!string.IsNullOrEmpty(dto.Classify), x => x.Classify == dto.Classify)
  104. .WhereIF(dto.CreationTimeStart.HasValue, d => d.CreationTime >= dto.CreationTimeStart)
  105. .WhereIF(dto.CreationTimeEnd.HasValue, d => d.CreationTime <= dto.CreationTimeEnd)
  106. .OrderByDescending(x => x.CreationTime)
  107. .ToPagedListAsync(dto.PageIndex, dto.PageSize, HttpContext.RequestAborted);
  108. return new PagedDto<File.File>(total, items);
  109. }
  110. /// <summary>
  111. /// 获取附件实体
  112. /// </summary>
  113. /// <param name="id"></param>
  114. /// <returns></returns>
  115. [HttpGet("{id}")]
  116. public async Task<File.File> Entity(string id)
  117. {
  118. return await _fileRepository.Queryable()
  119. .FirstAsync(x => x.Id == id);
  120. }
  121. /// <summary>
  122. /// 获取附件分类
  123. /// </summary>
  124. /// <returns></returns>
  125. [HttpGet("classify")]
  126. public Task<object> FileClassify()
  127. {
  128. var rsp = new
  129. {
  130. FileClassify = _sysDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.FileClassify),
  131. };
  132. return Task.FromResult<object>(rsp);
  133. }
  134. #endregion
  135. /// <summary>
  136. /// 下载(代理模式)
  137. /// </summary>
  138. [HttpGet("download-proxy")]
  139. public async Task<IActionResult> DownloadProxy([FromServices] IHttpClientFactory clientFactory, string path)
  140. {
  141. using var client = clientFactory.CreateClient();
  142. var responseMessage = await client.GetAsync(path, HttpContext.RequestAborted);
  143. responseMessage.EnsureSuccessStatusCode();
  144. var stream = await responseMessage.Content.ReadAsStreamAsync(HttpContext.RequestAborted);
  145. return File(stream, responseMessage?.Content?.Headers?.ContentType?.MediaType);
  146. }
  147. }
  148. }