FileHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 QRCoder;
  8. using Bitmap = System.DrawingCore.Bitmap;
  9. using Imazen.WebP;
  10. namespace GxPress.Common.Tools
  11. {
  12. /// <summary>
  13. /// 文件帮助类
  14. /// </summary>
  15. public class FileHelper
  16. {
  17. /// <summary>
  18. /// 得到图片的 Size 值
  19. /// </summary>
  20. /// <param name="bytes">图片的二进制数据</param>
  21. /// <returns>图片的 Size 值</returns>
  22. public static Size GetSize(byte[] bytes)
  23. {
  24. var size = Size.Empty;
  25. if (bytes != null && bytes.Length > 0)
  26. {
  27. using var ms = new MemoryStream(bytes);
  28. size = GetSize(ms);
  29. }
  30. return size;
  31. }
  32. /// <summary>
  33. /// 得到图片的 Size 值
  34. /// </summary>
  35. /// <param name="stream">图片的文件数据流</param>
  36. /// <returns>图片的 Size 值</returns>
  37. public static Size GetSize(Stream stream)
  38. {
  39. Size size;
  40. using (var originalImage = Image.FromStream(stream))
  41. {
  42. size = originalImage.Size;
  43. }
  44. return size;
  45. }
  46. /// <summary>
  47. /// 保存文件
  48. /// </summary>
  49. /// <param name="path">文件保存路径</param>
  50. /// <param name="bytes">文件二进制数据</param>
  51. /// <returns>保存文件是否成功!</returns>
  52. public static void Save(string path, byte[] bytes)
  53. {
  54. if (string.IsNullOrWhiteSpace(path))
  55. throw new ArgumentException("文件保存路径不能为空");
  56. if (bytes == null || bytes.Length == 0)
  57. throw new ArgumentException("文件二进制数据不能为空");
  58. var info = new FileInfo(path);
  59. if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
  60. info.Delete();
  61. File.WriteAllBytes(info.FullName, bytes);
  62. }
  63. /// <summary>
  64. /// 保存文件
  65. /// </summary>
  66. /// <param name="path">文件保存路径</param>
  67. /// <param name="fileName">文件名</param>
  68. /// <param name="bytes">文件二进制数据</param>
  69. /// <returns>保存后文件的路径,为空时,表示上传不成功!</returns>
  70. public static ResultPath SaveFile(string path, string fileName, byte[] bytes)
  71. {
  72. if (string.IsNullOrWhiteSpace(path))
  73. throw new BusinessException("文件保存路径不能为空");
  74. if (bytes == null || bytes.Length == 0)
  75. throw new BusinessException("上传文件不能为空");
  76. fileName = $"{FileNewName()}{Path.GetExtension(fileName)}";
  77. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  78. var resultPath = new ResultPath
  79. {
  80. AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
  81. RelativePath = $"/cache/{saveDirectory}/{fileName}",
  82. };
  83. Save(resultPath.AbsolutePath, bytes);
  84. //压缩图片
  85. CompressImage.Execute(resultPath.AbsolutePath, 50);
  86. CompressImage.Execute(resultPath.AbsolutePath, 100);
  87. return resultPath;
  88. }
  89. /// <summary>
  90. /// 获取新的图片路径
  91. /// </summary>
  92. /// <returns></returns>
  93. public static ResultPath GetNewFilePath(string extension)
  94. {
  95. var result = new ResultPath();
  96. var fileName = $"{FileNewName()}{extension}";
  97. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  98. var startPath = Directory.GetCurrentDirectory() + "\\wwwroot";
  99. var path = Path.Combine(startPath + $"\\cache\\{saveDirectory}\\{fileName}");
  100. var info = new FileInfo(path);
  101. if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
  102. result.AbsolutePath = path;
  103. result.RelativePath = path.Replace(startPath, "").Replace("\\", "/");
  104. return result;
  105. }
  106. /// <summary>
  107. /// 获取保存的路径
  108. /// </summary>
  109. /// <param name="path"></param>
  110. /// <param name="fileName"></param>
  111. /// <returns></returns>
  112. public static ResultPath GetSavePath(string path, string fileName)
  113. {
  114. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  115. fileName = $"{FileNewName()}{Path.GetExtension(fileName)}";
  116. var resultPath = new ResultPath
  117. {
  118. AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
  119. RelativePath = $"/cache/{saveDirectory}/{fileName}",
  120. };
  121. return resultPath;
  122. }
  123. /// <summary>
  124. /// 获取图片保存地址
  125. /// </summary>
  126. /// <param name="path"></param>
  127. /// <param name="content"></param>
  128. /// <returns></returns>
  129. public static ResultPath SaveQRCodeFile(string path, string content)
  130. {
  131. //创建二维码
  132. QRCodeGenerator qrGenerator = new QRCodeGenerator();
  133. QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);
  134. var qrCode = new QRCode(qrCodeData);
  135. Bitmap qrCodeImage = qrCode.GetGraphic(20);
  136. if (string.IsNullOrWhiteSpace(path))
  137. throw new BusinessException("文件保存路径不能为空");
  138. var fileName = $"{FileNewName()}.jpeg";
  139. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  140. var resultPath = new ResultPath
  141. {
  142. AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
  143. RelativePath = $"/cache/{saveDirectory}/{fileName}",
  144. };
  145. var info = new FileInfo(resultPath.AbsolutePath);
  146. if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
  147. info.Delete();
  148. //保存位置
  149. qrCodeImage.Save(resultPath.AbsolutePath, ImageFormat.Jpeg);
  150. return resultPath;
  151. }
  152. /// <summary>
  153. /// 删除指定的文件
  154. /// </summary>
  155. /// <param name="path">文件路径</param>
  156. /// <returns>删除是否成功</returns>
  157. public static bool Delete(string path)
  158. {
  159. if (string.IsNullOrWhiteSpace(path))
  160. throw new ArgumentException("文件路径不能为空");
  161. if (File.Exists(path)) File.Delete(path);
  162. return true;
  163. }
  164. /// <summary>
  165. /// webp
  166. /// </summary>
  167. /// <param name="path"></param>
  168. /// <param name="fileName"></param>
  169. /// <param name="webpUrl"></param>
  170. /// <returns></returns>
  171. public static ResultPath WebPToImage(string path, string fileName, ResultPath resultPath, string absolutePathUrl)
  172. {
  173. // using (Bitmap image = WebPFormat.LoadFromStream(new FileStream("image.webp", FileMode.Open, FileAccess.Read)))
  174. // {
  175. // image.Save("image.png", ImageFormat.Png);
  176. // }
  177. var dim = System.IO.File.ReadAllBytes("wwwroot/" + resultPath.RelativePath);
  178. //Imazen.WebP.Extern.LoadLibrary.LoadWebPOrFail();
  179. var simpleDecoder = new SimpleDecoder();
  180. var bitmap = simpleDecoder.DecodeFromBytes(dim, dim.Length);
  181. var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
  182. fileName = fileName.Split('.')[0] + ".jpg";
  183. resultPath.AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName);
  184. resultPath.RelativePath = $"/cache/{saveDirectory}/{fileName}";
  185. using (MemoryStream stream = new MemoryStream())
  186. {
  187. bitmap.Save(resultPath.AbsolutePath, System.Drawing.Imaging.ImageFormat.Jpeg); // 坑点:格式选Bmp时,不带透明度
  188. stream.Close();
  189. }
  190. resultPath.AbsolutePath = absolutePathUrl + resultPath.RelativePath;
  191. resultPath.FileName = fileName;
  192. resultPath.MinAbsolutePath = StringUtils.AddDomainMin(resultPath.AbsolutePath);
  193. //压缩图片
  194. CompressImage.Execute(resultPath.AbsolutePath, 50);
  195. CompressImage.Execute(resultPath.AbsolutePath, 100);
  196. return resultPath;
  197. }
  198. /// <summary>
  199. /// 判断文件是否存在
  200. /// </summary>
  201. /// <param name="path">文件路径</param>
  202. /// <returns>是否存在</returns>
  203. public static bool Exists(string path)
  204. {
  205. if (string.IsNullOrWhiteSpace(path))
  206. throw new ArgumentException("文件路径不能为空");
  207. return File.Exists(path);
  208. }
  209. /// <summary>
  210. /// 读取文件
  211. /// </summary>
  212. /// <param name="path">文件路径</param>
  213. /// <returns>文件的二进制数据</returns>
  214. public static byte[] Read(string path)
  215. {
  216. if (string.IsNullOrWhiteSpace(path))
  217. throw new ArgumentException("文件路径不能为空");
  218. byte[] result = null;
  219. if (File.Exists(path))
  220. result = File.ReadAllBytes(path);
  221. return result;
  222. }
  223. /// <summary>
  224. /// 得到文件新名称
  225. /// </summary>
  226. /// <returns></returns>
  227. public static string FileNewName()
  228. {
  229. return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
  230. }
  231. }
  232. /// <summary>
  233. /// 文件结果
  234. /// </summary>
  235. public class ResultPath
  236. {
  237. /// <summary>相对路径</summary>
  238. public string RelativePath { get; set; }
  239. /// <summary>绝对路径</summary>
  240. public string AbsolutePath { get; set; }
  241. /// <summary>
  242. /// 文件名称
  243. /// </summary>
  244. public string FileName { get; set; }
  245. /// <summary>
  246. /// 文件类型
  247. /// </summary>
  248. public string FileType { get; set; }
  249. public int FileId { get; set; }
  250. public long Size { get; set; }
  251. /// <summary>
  252. /// 缩略图
  253. /// </summary>
  254. /// <value></value>
  255. public string MinAbsolutePath { get; set; }
  256. }
  257. }