ResourceFileService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Furion.DatabaseAccessor.Extensions;
  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. public class ResourceFileService : IResourceFileService, ITransient
  11. {
  12. private readonly IRepository<ResourceFile> _rep;
  13. private readonly EqmSiteOptions _eqmSiteOptions;
  14. private readonly string[] _thumbImageTypes = { ".png", ".jpg", "jpeg", ".gif", ".bmp" };
  15. public ResourceFileService(IRepository<ResourceFile> rep, IOptions<EqmSiteOptions> options)
  16. {
  17. _rep = rep;
  18. _eqmSiteOptions = options.Value;
  19. }
  20. /// <summary>
  21. /// 上传文件
  22. /// </summary>
  23. /// <param name="input"></param>
  24. /// <returns></returns>
  25. public async Task<ResourceFileOutput> Upload([FromForm] UploadResourceFileInput input)
  26. {
  27. var savePath = Path.Combine(input.Type.ToString(), input.SourceId.ToString());
  28. var saveRoot = Path.Combine(_eqmSiteOptions.ResourceFileRoot, savePath);
  29. if (!Directory.Exists(saveRoot))
  30. {
  31. Directory.CreateDirectory(saveRoot);
  32. }
  33. var extName = Path.GetExtension(input.File.FileName);
  34. var fileNameGuid = Guid.NewGuid().ToString("N");
  35. var fileName = fileNameGuid + extName;
  36. using var stream = File.Create(Path.Combine(saveRoot, fileName));
  37. await input.File.CopyToAsync(stream);
  38. await stream.FlushAsync();
  39. var reqFileName = !string.IsNullOrEmpty(input.FileName?.Trim());
  40. var saveFileName = reqFileName ? input.FileName : input.File.FileName;
  41. // 生成缩略图
  42. AddResourceFileInput thumbItem = null;
  43. if (_thumbImageTypes.Any(t => t == extName.ToLower()))
  44. {
  45. BinaryReader r = new(stream);
  46. r.BaseStream.Seek(0, SeekOrigin.Begin);
  47. var bs = r.ReadBytes((int)r.BaseStream.Length);
  48. using MemoryStream ms = new(bs);
  49. Image imageFrom = Image.Load(ms);
  50. var thumbFileName = fileNameGuid + "_thumb" + extName;
  51. // 源图宽度及高度
  52. int imageFromWidth = imageFrom.Width;
  53. int imageFromHeight = imageFrom.Height;
  54. int width = 240;
  55. int height = (int)Math.Ceiling(width * (imageFromHeight * 1.0 / imageFromWidth));
  56. if (imageFromHeight > imageFromWidth)
  57. {
  58. height = 240;
  59. width = (int)Math.Ceiling(height * (imageFromWidth * 1.0 / imageFromHeight));
  60. }
  61. var thumbPath = Path.Combine(saveRoot, thumbFileName);
  62. imageFrom.Mutate(x => x.Resize(width, height));
  63. imageFrom.Save(thumbPath);
  64. imageFrom.Dispose();
  65. thumbItem = input.Adapt<AddResourceFileInput>();
  66. thumbItem.FileName = saveFileName.Replace(extName, $"_thumb{extName}");
  67. thumbItem.FilePath = Path.Combine(savePath, thumbFileName);
  68. thumbItem.FileSize = new FileInfo(thumbPath).Length;
  69. thumbItem.FileExtName = extName;
  70. }
  71. AddResourceFileInput item = input.Adapt<AddResourceFileInput>();
  72. item.FileName = saveFileName;
  73. item.FilePath = Path.Combine(savePath, fileName);
  74. item.FileSize = input.File.Length;
  75. item.FileExtName = extName;
  76. return await Add(item, thumbItem);
  77. }
  78. /// <summary>
  79. /// 添加资源文件
  80. /// </summary>
  81. /// <param name="input"></param>
  82. /// <param name="thumbInput">缩略图</param>
  83. /// <returns></returns>
  84. public async Task<ResourceFileOutput> Add(AddResourceFileInput input, AddResourceFileInput thumbInput = null)
  85. {
  86. var item = input.Adapt<ResourceFile>();
  87. await _rep.InsertAsync(item);
  88. var ret = item.Adapt<ResourceFileOutput>();
  89. if (thumbInput != null)
  90. {
  91. var thumbItem = thumbInput.Adapt<ResourceFile>();
  92. await _rep.InsertAsync(thumbItem);
  93. ret.ThumbResourceFile = thumbItem.Adapt<ResourceFileOutput>();
  94. }
  95. return ret;
  96. }
  97. /// <summary>
  98. /// 删除资源文件
  99. /// </summary>
  100. /// <param name="input"></param>
  101. /// <returns></returns>
  102. public async Task Del(BaseId<long> input)
  103. {
  104. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  105. var fpath = Path.Combine(_eqmSiteOptions.ResourceFileRoot, item.FilePath);
  106. if (File.Exists(fpath))
  107. {
  108. File.Delete(fpath);
  109. }
  110. await item.DeleteAsync();
  111. }
  112. /// <summary>
  113. /// 根据ID获取资源详细
  114. /// </summary>
  115. /// <param name="id"></param>
  116. /// <returns></returns>
  117. public async Task<ResourceFileOutput> GetById(long id)
  118. {
  119. var item = await _rep.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  120. return item.Adapt<ResourceFileOutput>();
  121. }
  122. /// <summary>
  123. /// 根据类型和源ID获取资源列表
  124. /// </summary>
  125. /// <param name="type"></param>
  126. /// <param name="sourceId"></param>
  127. /// <returns></returns>
  128. public async Task<List<ResourceFileOutput>> GetListBySourceId(ResourceFileType type, long sourceId)
  129. {
  130. var items = await _rep.DetachedEntities.Where(t => t.Type == type && t.SourceId == sourceId).ProjectToType<ResourceFileOutput>().ToListAsync();
  131. return items;
  132. }
  133. }