From 17308f6494c81fb5a5ee035724a414ec7da11936 Mon Sep 17 00:00:00 2001 From: username@email.com <yzy2002yzy@163.com> Date: 星期四, 22 八月 2024 08:38:45 +0800 Subject: [PATCH] Merge branch 'master' of http://47.108.235.38:8080/r/cylsg --- cylsg/EzUpFile/EzFileUploadService.cs | 531 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 531 insertions(+), 0 deletions(-) diff --git a/cylsg/EzUpFile/EzFileUploadService.cs b/cylsg/EzUpFile/EzFileUploadService.cs new file mode 100644 index 0000000..da61bfd --- /dev/null +++ b/cylsg/EzUpFile/EzFileUploadService.cs @@ -0,0 +1,531 @@ +锘� +using Aliyun.OSS; +using Aliyun.OSS.Util; +using cylsg.utility; +using cylsg.utility.Extend; +using EzTencentCloud; +using Furion; +using Furion.DependencyInjection; +using Furion.FriendlyException; +using Microsoft.AspNetCore.Http; + +using SqlSugar; +using System; +using System.Buffers.Text; +using System.Drawing; +using System.Globalization; +using System.Security.Policy; +using TencentCloud.Ocr.V20181119.Models; +using TencentCloud.Teo.V20220901.Models; +using Task = System.Threading.Tasks.Task; +namespace EzUpFile +{ + /// <summary> + /// 闄勪欢鏈嶅姟绋嬪簭 + /// </summary> + public class EzFileUploadService : IEzFileUploadService, IScoped + { + + private readonly HttpRequest? _request; + private readonly ISqlSugarClient _sqlSugarClient; + private readonly ITencentCloudService _tcs; + public EzFileUploadService(IHttpContextAccessor httpContext, ISqlSugarClient sqlSugarClient,ITencentCloudService tencentCloudService) + { + + _request = httpContext.HttpContext?.Request ?? null; + _sqlSugarClient = sqlSugarClient; + _tcs= tencentCloudService; + } + + /// <summary> + /// 涓婁紶闄勪欢 + /// </summary> + /// <returns></returns> + public async Task<string> UploadFiles() + { + + + var maxSize = 1024 * 1024 * 5; //涓婁紶澶у皬5M + + var file = _request?.Form?.Files["file"]; + if (file == null) + { + throw Oops.Oh("浣犳病鏈夐�夋嫨鏂囦欢"); + } + + var fileName = file.FileName; + var fileExt = Path.GetExtension(fileName).ToLowerInvariant(); + + //妫�鏌ュぇ灏� + if (file.Length > maxSize) + { + throw Oops.Oh("鏂囦欢澶т簬璁剧疆鏂囦欢"); + } + + ////妫�鏌ユ枃浠舵墿灞曞悕 + //if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(op.AttachmentSaveFileExtName?.Split(',') ?? new string[] { "" }, fileExt.Substring(1).ToLower()) == -1) + //{ + // throw Oops.Oh("涓婁紶鏂囦欢鎵╁睍鍚嶆槸涓嶅厑璁哥殑鎵╁睍鍚�,璇蜂笂浼犲悗缂�鍚嶄负锛�" + op.AttachmentSaveFileExtName); + + //} + + string url = string.Empty; + + + url = await UpLoadFileForAliYunOSS(fileExt, file); + + + return url; + + + } + + /// <summary> + /// 涓婁紶base64 + /// </summary> + /// <param name="base64"></param> + /// <returns></returns> + public async Task<string> UploadFilesFByBase64(string base64) + { + + if (string.IsNullOrEmpty(base64)) + { + throw Oops.Oh("娌℃湁鍐呭"); + } + + //妫�鏌ヤ笂浼犲ぇ灏� + if (!CommonHelper.CheckBase64Size(base64, 5)) + { + throw Oops.Oh("涓婁紶鏂囦欢澶у皬瓒呰繃闄愬埗锛屾渶澶у厑璁镐笂浼�" + "5" + "M"); + + } + + base64 = base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//灏哹ase64澶撮儴淇℃伅鏇挎崲 + byte[] bytes = Convert.FromBase64String(base64); + MemoryStream memStream = new MemoryStream(bytes); + + string url = string.Empty; + + url = await UpLoadBase64ForAliYunOSS(memStream); + + + + return url; + } + + /// <summary> + /// 鍒犻櫎鏂囦欢 + /// </summary> + /// <param name="Path"></param> + /// <returns></returns> + public async Task<bool> DelFile(string Path) + { + + + var ret = await DelFileForAliYunOSS(Path); + + + + return ret; + } + + + + #region 闃块噷浜戜笂浼犳柟娉曪紙File锛� + /// <summary> + /// 闃块噷浜戜笂浼犳柟娉曪紙File锛� + /// </summary> + /// <param name="options"></param> + /// <param name="fileExt"></param> + /// <param name="file"></param> + /// <returns></returns> + public async Task<string> UpLoadFileForAliYunOSS(string fileExt, IFormFile file) + { + + + var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt; + var today = DateTime.Now.ToString("yyyyMMdd"); + + //涓婁紶鍒伴樋閲屼簯 + await using var fileStream = file.OpenReadStream(); + var md5 = OssUtils.ComputeContentMd5(fileStream, file.Length); + + var filePath = App.Configuration["FileUploadOptions:SavePath"] + today + "/" + newFileName; //浜戞枃浠朵繚瀛樿矾寰� + //鍒濆鍖栭樋閲屼簯閰嶇疆--澶栫綉Endpoint銆佽闂甀D銆佽闂畃assword + var aliYun = new OssClient(App.Configuration["FileUploadOptions:AliOSSEndpoint"], App.Configuration["FileUploadOptions:AliOSSAccessKeyID"], App.Configuration["FileUploadOptions:AliOSSAccessKeySecret"]); + //灏嗘枃浠秏d5鍊艰祴鍊肩粰meat澶翠俊鎭紝鏈嶅姟鍣ㄩ獙璇佹枃浠禡D5 + var objectMeta = new ObjectMetadata + { + ContentMd5 = md5 + }; + + + var task = Task.Run(() => + //鏂囦欢涓婁紶--绌洪棿鍚嶃�佹枃浠朵繚瀛樿矾寰勩�佹枃浠舵祦銆乵eta澶翠俊鎭�(鏂囦欢md5) //杩斿洖meta澶翠俊鎭�(鏂囦欢md5) + aliYun.PutObject(App.Configuration["FileUploadOptions:AliOSSBucketName"], filePath, fileStream, objectMeta) + + + + ); + //绛夊緟瀹屾垚 + try + { + task.Wait(); + } + catch (AggregateException ex) + { + + throw Oops.Oh(ex.Message); + } + + //杩斿洖缁橴Editor鐨勬彃鍏ョ紪杈戝櫒鐨勫浘鐗囩殑src + + return App.Configuration["FileUploadOptions:AliOSSSaveBaseUrl"] + filePath; + } + + #endregion + + + + + + #region 闃块噷浜戜笂浼犳柟娉曪紙Base64锛� + /// <summary> + /// 闃块噷浜戜笂浼犳柟娉曪紙Base64锛� + /// </summary> + /// <param name="options"></param> + /// <param name="memStream"></param> + /// <returns></returns> + public async Task<string> UpLoadBase64ForAliYunOSS(MemoryStream memStream) + { + + + var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + ".jpg"; + var today = DateTime.Now.ToString("yyyyMMdd"); + + // 璁剧疆褰撳墠娴佺殑浣嶇疆涓烘祦鐨勫紑濮� + memStream.Seek(0, SeekOrigin.Begin); + + await using var fileStream = memStream; + var md5 = OssUtils.ComputeContentMd5(fileStream, memStream.Length); + + var filePath = App.Configuration["FileUploadOptions:SavePath"] + today + "/" + newFileName; //浜戞枃浠朵繚瀛樿矾寰� + //鍒濆鍖栭樋閲屼簯閰嶇疆--澶栫綉Endpoint銆佽闂甀D銆佽闂畃assword + var aliYun = new OssClient(App.Configuration["FileUploadOptions:AliOSSEndpoint"], App.Configuration["FileUploadOptions:AliOSSAccessKeyID"], App.Configuration["FileUploadOptions:AliOSSAccessKeySecret"]); + //灏嗘枃浠秏d5鍊艰祴鍊肩粰meat澶翠俊鎭紝鏈嶅姟鍣ㄩ獙璇佹枃浠禡D5 + var objectMeta = new ObjectMetadata + { + ContentMd5 = md5 + }; + try + { + //鏂囦欢涓婁紶--绌洪棿鍚嶃�佹枃浠朵繚瀛樿矾寰勩�佹枃浠舵祦銆乵eta澶翠俊鎭�(鏂囦欢md5) //杩斿洖meta澶翠俊鎭�(鏂囦欢md5) + aliYun.PutObject(App.Configuration["FileUploadOptions:AliOSSBucketName"], filePath, fileStream, objectMeta); + } + catch (AggregateException ex) + { + + throw Oops.Oh(ex.Message); + } + + + //杩斿洖缁橴Editor鐨勬彃鍏ョ紪杈戝櫒鐨勫浘鐗囩殑src + + return App.Configuration["FileUploadOptions:AliOSSSaveBaseUrl"] + filePath; + + } + + #endregion + + + + + + #region 鍒犻櫎鏂囦欢 + + /// <summary> + /// 闃块噷浜戝垹闄� + /// </summary> + /// <param name="options"></param> + /// <param name="fileUrl">甯xx.xx鐨勯摼鎺ュ湴鍧�</param> + /// <returns></returns> + public async Task<bool> DelFileForAliYunOSS(string fileUrl) + { + + + + + + + + + + + //鍒濆鍖栭樋閲屼簯閰嶇疆--澶栫綉Endpoint銆佽闂甀D銆佽闂畃assword + var aliYun = new OssClient(App.Configuration["FileUploadOptions:AliOSSEndpoint"], App.Configuration["FileUploadOptions:AliOSSAccessKeyID"], App.Configuration["FileUploadOptions:AliOSSAccessKeySecret"]); + + try + { + var task = Task.Run(() => aliYun.DeleteObject(App.Configuration["FileUploadOptions:AliOSSBucketName"], (App.Configuration["FileUploadOptions:SavePath"]?.RemoveStartWithStr("/") ?? "") + fileUrl.GetFileName())); + + + task.Wait(); + + } + catch (Exception ex) + { + + throw Oops.Oh(ex.Message); + } + + + //杩斿洖缁橴Editor鐨勬彃鍏ョ紪杈戝櫒鐨勫浘鐗囩殑src + + return true; + + } + + + + + #endregion + + #region 璇嗗埆涓婁紶 + public async Task<(IDCardOCRResponse, string)> UpIdCord(string PageName = "FRONT") + { + try + { + + var maxSize = 1024 * 1024 * 5; //涓婁紶澶у皬5M + + var FileData = _request?.Form?.Files["file"]; + + if (FileData.Length > maxSize) + { + throw Oops.Oh(" 涓婁紶鏂囦欢涓嶅彲瓒呭嚭500K"); + } + + + //澶勭悊鍥惧舰 + // var FileData = Request.Form.Files[0]; + + Image oimage = Image.FromStream(FileData.OpenReadStream()); + if (oimage == null) + { + throw Oops.Oh(" 涓婁紶澶辫触"); + } + + MemoryStream ms = new MemoryStream(); + if (oimage.Width > 600) + + { + if (oimage.Width > oimage.Height) + oimage.GetThumbnailImage(600, 400, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png); + else + oimage.GetThumbnailImage(400, 600, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png); + } + else + oimage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); + ms.Position = 0; + + var arr = ms.ToArray(); + string img64 = Convert.ToBase64String(arr); + + IDCardOCRResponse idcordinfo = null; + string url = ""; + + try + { + + + idcordinfo = _tcs.IdCord(img64, PageName == "FRONT"); + + url = await UploadFilesFByBase64(_tcs.GetIdCordImg()); + idcordinfo.AdvancedInfo = null; + + return (idcordinfo, url); + + } + catch (Exception e) + { + + throw Oops.Oh(e.Message); + } + } + catch (Exception e) + { + + throw Oops.Oh(e.Message); + } + + } + + public async Task<(BizLicenseOCRResponse, string)> UpBizLicense() + { + try + { + + var maxSize = 1024 * 1024 * 5; //涓婁紶澶у皬5M + + var FileData = _request?.Form?.Files["file"]; + + if (FileData.Length > maxSize) + { + throw Oops.Oh(" 涓婁紶鏂囦欢涓嶅彲瓒呭嚭500K"); + } + + + //澶勭悊鍥惧舰 + // var FileData = Request.Form.Files[0]; + + Image oimage = Image.FromStream(FileData.OpenReadStream()); + if (oimage == null) + { + throw Oops.Oh(" 涓婁紶澶辫触"); + } + + MemoryStream ms = new MemoryStream(); + if (oimage.Width > 600) + + { + if (oimage.Width > oimage.Height) + oimage.GetThumbnailImage(600, 400, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png); + else + oimage.GetThumbnailImage(400, 600, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png); + } + else + oimage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); + ms.Position = 0; + + var arr = ms.ToArray(); + string img64 = Convert.ToBase64String(arr); + + BizLicenseOCRResponse info = null; + string url = ""; + + try + { + info = _tcs.BizLicenseOCR(img64); + + url = await UploadFilesFByBase64(img64); + + return (info, url); + + } + catch (Exception e) + { + + throw Oops.Oh(e.Message); + } + } + catch (Exception e) + { + + throw Oops.Oh(e.Message); + } + } + + + public async Task<(bool,string)> IaiAddPerso(string CoredID,string Name,int PersonGender) + { + try + { + + var maxSize = 1024 * 1024 * 5; //涓婁紶澶у皬5M + + var FileData = _request?.Form?.Files["file"]; + + if (FileData.Length > maxSize) + { + throw Oops.Oh(" 涓婁紶鏂囦欢涓嶅彲瓒呭嚭500K"); + } + + + //澶勭悊鍥惧舰 + // var FileData = Request.Form.Files[0]; + + Image oimage = Image.FromStream(FileData.OpenReadStream()); + if (oimage == null) + { + throw Oops.Oh(" 涓婁紶澶辫触"); + } + + MemoryStream ms = new MemoryStream(); + if (oimage.Width > 600) + + { + if (oimage.Width > oimage.Height) + oimage.GetThumbnailImage(600, 400, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png); + else + oimage.GetThumbnailImage(400, 600, null, IntPtr.Zero).Save(ms, System.Drawing.Imaging.ImageFormat.Png); + } + else + oimage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); + ms.Position = 0; + + var arr = ms.ToArray(); + string img64 = Convert.ToBase64String(arr); + + bool info = false; + string url = ""; + + try + { + info = _tcs.IaiAddPerso(img64, CoredID, Name, PersonGender); + + url = await UploadFilesFByBase64(img64); + + return (info, url); + + } + catch (Exception e) + { + + throw Oops.Oh(e.Message); + } + } + catch (Exception e) + { + + throw Oops.Oh(e.Message); + } + } +/// <inheritdoc/> + + public async Task<(bool, string)> IaiAddPerso(string imgBase64, string CoredID, string Name, int PersonGender) + { + if (string.IsNullOrEmpty(imgBase64)) + { + throw Oops.Oh("娌℃湁鍐呭"); + } + + //妫�鏌ヤ笂浼犲ぇ灏� + if (!CommonHelper.CheckBase64Size(imgBase64, 5)) + { + throw Oops.Oh("涓婁紶鏂囦欢澶у皬瓒呰繃闄愬埗锛屾渶澶у厑璁镐笂浼�" + "5" + "M"); + } + + imgBase64 = imgBase64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//灏哹ase64澶撮儴淇℃伅鏇挎崲 + bool info = false; + string url = ""; + + try + { + info = _tcs.IaiAddPerso(imgBase64, CoredID, Name, PersonGender); + + url = await UploadFilesFByBase64(imgBase64); + + return (info, url); + + } + catch (Exception e) + { + + throw Oops.Oh(e.Message); + } + } + + + #endregion + } +} -- Gitblit v1.9.1