ResourceFileAppService.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(IResourceFileService resourceFileService, IOptions<EqmSiteOptions> options) : IDynamicApiController
  13. {
  14. private readonly IResourceFileService _resourceFileService = resourceFileService;
  15. private readonly EqmSiteOptions _eqmSiteOptions = options.Value;
  16. //private readonly string[] thumbImageTypes = { ".png", ".jpg", "jpeg", ".gif", ".bmp" };
  17. /// <summary>
  18. /// 上传资源文件
  19. /// </summary>
  20. /// <param name="input"></param>
  21. /// <returns></returns>
  22. [RequestSizeLimit(long.MaxValue)]
  23. [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  24. public async Task<ResourceFileOutput> Upload([FromForm] UploadResourceFileInput input)
  25. {
  26. return await _resourceFileService.Upload(input);
  27. }
  28. /// <summary>
  29. /// 根据文件ID下载文件
  30. /// </summary>
  31. /// <param name="id"></param>
  32. /// <returns></returns>
  33. [HttpGet, NonUnify]
  34. public async Task<IActionResult> Download([FromQuery][Required] long id)
  35. {
  36. var item = await _resourceFileService.GetById(id);
  37. var filePath = Path.Combine(_eqmSiteOptions.ResourceFileRoot, item.FilePath);
  38. return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream")
  39. {
  40. FileDownloadName = item.FileName,
  41. };
  42. }
  43. /// <summary>
  44. /// 通过URL
  45. /// </summary>
  46. /// <param name="id"></param>
  47. /// <returns></returns>
  48. [HttpGet, NonUnify, AllowAnonymous]
  49. public async Task<IActionResult> View([FromQuery][Required] long id)
  50. {
  51. var item = await _resourceFileService.GetById(id);
  52. var filePath = Path.Combine(_eqmSiteOptions.ResourceFileRoot, item.FilePath);
  53. if (File.Exists(filePath))
  54. {
  55. var fbs = await File.ReadAllBytesAsync(filePath);
  56. FS.TryGetContentType(filePath, out var contentType);
  57. var ret = new FileContentResult(fbs, contentType);
  58. //{
  59. // FileDownloadName = item.FileName
  60. //};
  61. return ret;
  62. }
  63. throw Oops.Oh(ErrorCode.E1011);
  64. }
  65. }