123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System;
- using System.Drawing;
- using System.DrawingCore.Imaging;
- using System.Globalization;
- using System.IO;
- using GxPress.Common.Exceptions;
- using GxPress.Common.Tools;
- using GxPress.Result;
- using GxPress.Service.Interface.Storage;
- using QRCoder;
- using Bitmap = System.DrawingCore.Bitmap;
- namespace GxPress.Service.Implement
- {
- /// <summary>
- /// 文件帮助类
- /// </summary>
- public class FileStorage : IStorage
- {
- /// <summary>
- /// 保存文件
- /// </summary>
- /// <param name="path">文件保存路径</param>
- /// <param name="bytes">文件二进制数据</param>
- /// <returns>保存文件是否成功!</returns>
- public void Save(string path, byte[] bytes)
- {
- if (string.IsNullOrWhiteSpace(path))
- throw new ArgumentException("文件保存路径不能为空");
- if (bytes == null || bytes.Length == 0)
- throw new ArgumentException("文件二进制数据不能为空");
- var info = new FileInfo(path);
- if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
- info.Delete();
- File.WriteAllBytes(info.FullName, bytes);
- }
- /// <summary>
- /// 保存文件
- /// </summary>
- /// <param name="path">文件保存路径</param>
- /// <param name="fileName">文件名</param>
- /// <param name="bytes">文件二进制数据</param>
- /// <returns>保存后文件的路径,为空时,表示上传不成功!</returns>
- public PathResult SaveFile(string path, string fileName, byte[] bytes)
- {
- if (string.IsNullOrWhiteSpace(path))
- throw new BusinessException("文件保存路径不能为空");
- if (bytes == null || bytes.Length == 0)
- throw new BusinessException("上传文件不能为空");
- fileName = $"{FileHelper.FileNewName()}{Path.GetExtension(fileName)}";
- var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
- var resultPath = new PathResult
- {
- AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
- RelativePath = $"/cache/{saveDirectory}/{fileName}",
- };
- Save(resultPath.AbsolutePath, bytes);
- return resultPath;
- }
- /// <summary>
- /// 获取保存的路径
- /// </summary>
- /// <param name="path"></param>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public PathResult GetSavePath(string path,string fileName)
- {
- var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
- fileName = $"{FileHelper.FileNewName()}{Path.GetExtension(fileName)}";
- var resultPath = new PathResult
- {
- AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
- RelativePath = $"/cache/{saveDirectory}/{fileName}",
- };
-
- return resultPath;
- }
- /// <summary>
- /// 获取图片保存地址
- /// </summary>
- /// <param name="path"></param>
- /// <param name="content"></param>
- /// <returns></returns>
- public PathResult SaveQRCodeFile(string path, string content)
- {
- //创建二维码
- QRCodeGenerator qrGenerator = new QRCodeGenerator();
- QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q);
- var qrCode = new QRCode(qrCodeData);
- Bitmap qrCodeImage = qrCode.GetGraphic(20);
- if (string.IsNullOrWhiteSpace(path))
- throw new BusinessException("文件保存路径不能为空");
- var fileName = $"{FileHelper.FileNewName()}.jpeg";
- var saveDirectory = DateTime.Now.ToString("yyyyMMdd");
- var resultPath = new PathResult
- {
- AbsolutePath = Path.Combine(path, "cache", saveDirectory, fileName),
- RelativePath = $"/cache/{saveDirectory}/{fileName}",
- };
- var info = new FileInfo(resultPath.AbsolutePath);
- if (info.Directory != null && !info.Directory.Exists) info.Directory.Create();
- info.Delete();
- //保存位置
- qrCodeImage.Save(resultPath.AbsolutePath, ImageFormat.Jpeg);
- return resultPath;
- }
- /// <summary>
- /// 删除指定的文件
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns>删除是否成功</returns>
- public bool Delete(string path)
- {
- if (string.IsNullOrWhiteSpace(path))
- throw new ArgumentException("文件路径不能为空");
- if (File.Exists(path)) File.Delete(path);
- return true;
- }
- /// <summary>
- /// 判断文件是否存在
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns>是否存在</returns>
- public bool Exists(string path)
- {
- if (string.IsNullOrWhiteSpace(path))
- throw new ArgumentException("文件路径不能为空");
- return File.Exists(path);
- }
- /// <summary>
- /// 读取文件
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <returns>文件的二进制数据</returns>
- public byte[] Read(string path)
- {
- if (string.IsNullOrWhiteSpace(path))
- throw new ArgumentException("文件路径不能为空");
- byte[] result = null;
- if (File.Exists(path))
- result = File.ReadAllBytes(path);
- return result;
- }
- }
- }
|