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 { /// /// 文件帮助类 /// public class FileStorage : IStorage { /// /// 保存文件 /// /// 文件保存路径 /// 文件二进制数据 /// 保存文件是否成功! 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); } /// /// 保存文件 /// /// 文件保存路径 /// 文件名 /// 文件二进制数据 /// 保存后文件的路径,为空时,表示上传不成功! 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; } /// /// 获取保存的路径 /// /// /// /// 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; } /// /// 获取图片保存地址 /// /// /// /// 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; } /// /// 删除指定的文件 /// /// 文件路径 /// 删除是否成功 public bool Delete(string path) { if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("文件路径不能为空"); if (File.Exists(path)) File.Delete(path); return true; } /// /// 判断文件是否存在 /// /// 文件路径 /// 是否存在 public bool Exists(string path) { if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("文件路径不能为空"); return File.Exists(path); } /// /// 读取文件 /// /// 文件路径 /// 文件的二进制数据 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; } } }