FileStorage.cs 5.7 KB

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