ResourceFileAppService.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Furion.VirtualFileServer;
  2. using Microsoft.Extensions.Options;
  3. using SixLabors.ImageSharp;
  4. using SixLabors.ImageSharp.Processing;
  5. using YBEE.EQM.Core;
  6. namespace YBEE.EQM.Application;
  7. /// <summary>
  8. /// 资源文件管理服务
  9. /// </summary>
  10. [ApiDescriptionSettings(Name = "file")]
  11. [Route("file")]
  12. public class ResourceFileAppService : IDynamicApiController
  13. {
  14. private readonly IResourceFileService _resourceFileService;
  15. private readonly EqmSiteOptions _eqmSiteOptions;
  16. private readonly string[] thumbImageTypes = { ".png", ".jpg", "jpeg", ".gif", ".bmp" };
  17. public ResourceFileAppService(IResourceFileService resourceFileService, IOptions<EqmSiteOptions> options)
  18. {
  19. _resourceFileService = resourceFileService;
  20. _eqmSiteOptions = options.Value;
  21. }
  22. /// <summary>
  23. /// 上传资源文件
  24. /// </summary>
  25. /// <param name="input"></param>
  26. /// <returns></returns>
  27. [RequestSizeLimit(long.MaxValue)]
  28. [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  29. public async Task<ResourceFileOutput> Upload([FromForm] UploadResourceFileInput input)
  30. {
  31. return await _resourceFileService.Upload(input);
  32. }
  33. /// <summary>
  34. /// 根据文件ID下载文件
  35. /// </summary>
  36. /// <param name="id"></param>
  37. /// <returns></returns>
  38. [HttpGet, NonUnify]
  39. public async Task<IActionResult> Download([FromQuery][Required] long id)
  40. {
  41. var item = await _resourceFileService.GetById(id);
  42. var filePath = Path.Combine(_eqmSiteOptions.ResourceFileRoot, item.FilePath);
  43. return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream")
  44. {
  45. FileDownloadName = item.FileName,
  46. };
  47. }
  48. /// <summary>
  49. /// 通过URL
  50. /// </summary>
  51. /// <param name="id"></param>
  52. /// <returns></returns>
  53. [HttpGet, NonUnify, AllowAnonymous]
  54. public async Task<IActionResult> View([FromQuery][Required] long id)
  55. {
  56. var item = await _resourceFileService.GetById(id);
  57. var filePath = Path.Combine(_eqmSiteOptions.ResourceFileRoot, item.FilePath);
  58. if (File.Exists(filePath))
  59. {
  60. var fbs = await File.ReadAllBytesAsync(filePath);
  61. FS.TryGetContentType(filePath, out var contentType);
  62. var ret = new FileContentResult(fbs, contentType);
  63. //{
  64. // FileDownloadName = item.FileName
  65. //};
  66. return ret;
  67. }
  68. throw Oops.Oh(ErrorCode.E1011);
  69. }
  70. }