MinIOStorage.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Drawing;
  3. using System.DrawingCore.Imaging;
  4. using System.Globalization;
  5. using System.IO;
  6. using GxPress.Common.Exceptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Result;
  9. using GxPress.Service.Interface.Storage;
  10. using QRCoder;
  11. using Bitmap = System.DrawingCore.Bitmap;
  12. using Minio;
  13. namespace GxPress.Service.Implement
  14. {
  15. /// <summary>
  16. /// 文件帮助类
  17. /// </summary>
  18. public class MinIOStorage : IStorage
  19. {
  20. private readonly MinioClient _client;
  21. public MinIOStorage(MinioClient client)
  22. {
  23. _client = client;
  24. }
  25. /// <summary>
  26. /// 保存文件
  27. /// </summary>
  28. /// <param name="path">文件保存路径</param>
  29. /// <param name="bytes">文件二进制数据</param>
  30. /// <returns>保存文件是否成功!</returns>
  31. public void Save(string path, byte[] bytes)
  32. {
  33. if (string.IsNullOrWhiteSpace(path))
  34. throw new ArgumentException("文件保存路径不能为空");
  35. if (bytes == null || bytes.Length == 0)
  36. throw new ArgumentException("文件二进制数据不能为空");
  37. var info = new FileInfo(path);
  38. if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
  39. info.Delete();
  40. File.WriteAllBytes(info.FullName, bytes);
  41. }
  42. /// <summary>
  43. /// 保存文件
  44. /// </summary>
  45. /// <param name="path">文件保存路径</param>
  46. /// <param name="fileName">文件名</param>
  47. /// <param name="bytes">文件二进制数据</param>
  48. /// <returns>保存后文件的路径,为空时,表示上传不成功!</returns>
  49. public PathResult SaveFile(string path, string fileName, byte[] bytes)
  50. {
  51. if (string.IsNullOrWhiteSpace(path))
  52. throw new BusinessException("文件保存路径不能为空");
  53. if (bytes == null || bytes.Length == 0)
  54. throw new BusinessException("上传文件不能为空");
  55. fileName = $"{FileHelper.FileNewName()}{Path.GetExtension(fileName)}";
  56. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  57. var resultPath = new PathResult
  58. {
  59. AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
  60. RelativePath = $"/cache/{saveDirectory}/{fileName}",
  61. };
  62. Save(resultPath.AbsolutePath, bytes);
  63. return resultPath;
  64. }
  65. /// <summary>
  66. /// 获取保存的路径
  67. /// </summary>
  68. /// <param name="path"></param>
  69. /// <param name="fileName"></param>
  70. /// <returns></returns>
  71. public PathResult GetSavePath(string path, string fileName)
  72. {
  73. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  74. fileName = $"{FileHelper.FileNewName()}{Path.GetExtension(fileName)}";
  75. var resultPath = new PathResult
  76. {
  77. AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
  78. RelativePath = $"/cache/{saveDirectory}/{fileName}",
  79. };
  80. return resultPath;
  81. }
  82. /// <summary>
  83. /// 获取图片保存地址
  84. /// </summary>
  85. /// <param name="path"></param>
  86. /// <param name="content"></param>
  87. /// <returns></returns>
  88. public PathResult SaveQRCodeFile(string path, string content)
  89. {
  90. //创建二维码
  91. QRCodeGenerator qrGenerator = new QRCodeGenerator();
  92. QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);
  93. var qrCode = new QRCode(qrCodeData);
  94. Bitmap qrCodeImage = qrCode.GetGraphic(20);
  95. if (string.IsNullOrWhiteSpace(path))
  96. throw new BusinessException("文件保存路径不能为空");
  97. var fileName = $"{FileHelper.FileNewName()}.jpeg";
  98. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  99. var resultPath = new PathResult
  100. {
  101. AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
  102. RelativePath = $"/cache/{saveDirectory}/{fileName}",
  103. };
  104. var info = new FileInfo(resultPath.AbsolutePath);
  105. if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
  106. info.Delete();
  107. //保存位置
  108. qrCodeImage.Save(resultPath.AbsolutePath, ImageFormat.Jpeg);
  109. return resultPath;
  110. }
  111. /// <summary>
  112. /// 删除指定的文件
  113. /// </summary>
  114. /// <param name="path">文件路径</param>
  115. /// <returns>删除是否成功</returns>
  116. public bool Delete(string path)
  117. {
  118. if (string.IsNullOrWhiteSpace(path))
  119. throw new ArgumentException("文件路径不能为空");
  120. if (File.Exists(path)) File.Delete(path);
  121. return true;
  122. }
  123. /// <summary>
  124. /// 判断文件是否存在
  125. /// </summary>
  126. /// <param name="path">文件路径</param>
  127. /// <returns>是否存在</returns>
  128. public bool Exists(string path)
  129. {
  130. if (string.IsNullOrWhiteSpace(path))
  131. throw new ArgumentException("文件路径不能为空");
  132. return File.Exists(path);
  133. }
  134. /// <summary>
  135. /// 读取文件
  136. /// </summary>
  137. /// <param name="path">文件路径</param>
  138. /// <returns>文件的二进制数据</returns>
  139. public byte[] Read(string path)
  140. {
  141. if (string.IsNullOrWhiteSpace(path))
  142. throw new ArgumentException("文件路径不能为空");
  143. byte[] result = null;
  144. if (File.Exists(path))
  145. result = File.ReadAllBytes(path);
  146. return result;
  147. }
  148. }
  149. }