ResourceFileAppService.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(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. return new FileContentResult(fbs, contentType);
  63. }
  64. throw Oops.Oh(ErrorCode.E1011);
  65. }
  66. }