From 01c46e7bfe9aa8fb20b29f70c83c03a307af548a Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期二, 22 十二月 2020 16:35:16 +0800 Subject: [PATCH] 2020-12-22 1.更新。 --- HDL_ON/Common/ImageUtlis.cs | 198 +++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 164 insertions(+), 34 deletions(-) diff --git a/HDL_ON/Common/ImageUtlis.cs b/HDL_ON/Common/ImageUtlis.cs old mode 100644 new mode 100755 index c85b61e..f2b6cca --- a/HDL_ON/Common/ImageUtlis.cs +++ b/HDL_ON/Common/ImageUtlis.cs @@ -1,6 +1,7 @@ 锘縰sing System; using Shared; using HDL_ON.DAL.Server; +using System.IO; namespace HDL_ON { @@ -36,6 +37,107 @@ } #endregion + + #region 鏂囦欢璇诲啓 + /// <summary> + /// 鏍圭洰褰� + /// </summary> + string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/"; + /// <summary> + /// 鏂囦欢鏄惁瀛樺湪 + /// </summary> + /// <param name="filePath">File path.</param> + public bool Exists(string filePath) + { + return File.Exists(Path.Combine(RootPath, filePath)); + } + + /// <summary> + /// Writes the file by bytes. + /// </summary> + /// <returns><c>true</c>, if file by bytes was writed, <c>false</c> otherwise.</returns> + /// <param name="fileName">File name.</param> + /// <param name="bytes">Bytes.</param> + public bool WriteFileByBytes(string fileName, byte[] bytes) + { + if (fileName == null || (fileName = fileName.Trim()) == ("")) + { + return false; + } + + FileStream fs = null; + + try + { + fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write); + fs.Write(bytes, 0, bytes.Length); + fs.Flush(); + MainPage.Log("SaveFile:" + fileName); + return true; + } + catch (Exception ex) + { + MainPage.Log("FileUtiles Code 113:" + ex.ToString()); + return false; + } + finally + { + try + { + if (fs != null) + { + fs.Close(); + } + } + catch (Exception ex) + { + MainPage.Log("FileUtils Code 121 :" + ex.ToString()); + } + } + } + public byte[] ReadFile(string fileName) + { + FileStream fs = null; + try + { + if (File.Exists(Path.Combine(RootPath, fileName))) + { + fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read); + } + else if (File.Exists(fileName)) + { + fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); + } + else + { + return new byte[0]; + } + byte[] bytes = new byte[fs.Length]; + fs.Read(bytes, 0, bytes.Length); + return bytes; + } + catch + { + return new byte[0]; + } + finally + { + try + { + if (fs != null) + { + fs.Close(); + } + } + catch + { + + } + } + } + + #endregion + #region 鎴块棿銆佸満鏅� 鍥剧墖涓婁紶鍜屼笅杞� /// <summary> /// 涓婁紶鍥剧墖 @@ -60,6 +162,7 @@ /// <summary> /// 閫氳繃鍥剧墖璺緞涓嬭浇鍥剧墖 + /// 閽堝浣忓畢锛屼娇鐢ㄤ綇瀹呭煙鍚嶆湇鍔″櫒 /// </summary> public byte[] GetImageDownloadUrl(string imageKey) { @@ -68,6 +171,31 @@ imageKey = imageKey, }); var revertObj = HttpUtil.RequestHttpsPostFroHome(NewAPI.API_POST_GetImageUrl, requestJson); + if (revertObj.Code == StateCode.SUCCESS) + { + if (revertObj.Data != null) + { + if (!string.IsNullOrEmpty(revertObj.Data.ToString())) + { + var url = revertObj.Data.ToString(); + return HttpUtil.HttpsDownload(url); + } + } + } + return null; + } + + /// <summary> + /// 閫氳繃鍥剧墖璺緞涓嬭浇鍥剧墖 + /// 閽堝鐢ㄦ埛澶村儚鎸囧畾鐢ㄦ埛璺緞绛� + /// </summary> + public byte[] GetImageDownloadUrlWithRegionUrl(string imageKey, string regionUrl) + { + var requestJson = HttpUtil.GetSignRequestJson(new GetImageUrlObj() + { + imageKey = imageKey, + }); + var revertObj = HttpUtil.RequestHttpsPost(NewAPI.API_POST_GetImageUrl, requestJson, regionUrl); if (revertObj.Code == StateCode.SUCCESS) { if (revertObj.Data != null) @@ -107,12 +235,12 @@ else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (imageView != null) { - imageView.ImageBytes = Common.FileUtlis.Files.ReadFile(imageKey); + imageView.ImageBytes = ReadFile(imageKey); //Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); } } @@ -124,7 +252,7 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { @@ -178,7 +306,7 @@ else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (frameLayout != null) @@ -195,7 +323,7 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { @@ -224,54 +352,56 @@ } } - /// <summary> - /// 鑾峰彇鎷兼帴鐢ㄦ埛澶村儚鐨処mageKey瑙勫垯 - /// </summary> - /// <returns></returns> - public string GetHeadImageKey(string userId) - { - return "ON" + "_" + userId + "_" + HEADIMAGE; - } + // /// <summary> + ///// 鑾峰彇鎷兼帴鐢ㄦ埛澶村儚鐨処mageKey瑙勫垯 + ///// </summary> + ///// <returns></returns> + //public string GetHeadImageKey(string userId) + // { + // return "ON" + "_" + userId + "_" + HEADIMAGE; + // } - /// <summary> - /// 鐢熸垚澶村儚涓婁紶瑙勫垯鍙傛暟 - /// </summary> - /// <returns></returns> - public UploadImageObj GetHeadImageUploadKeyObj(string userId) - { - return new UploadImageObj() - { - prefix = "ON", - uid = userId, - fileName = HEADIMAGE, - }; - } + ///// <summary> + ///// 鐢熸垚澶村儚涓婁紶瑙勫垯鍙傛暟 + ///// </summary> + ///// <returns></returns> + //public UploadImageObj GetHeadImageUploadKeyObj(string userId) + //{ + // return new UploadImageObj() + // { + // prefix = "Icon" + Utlis.GetTimestamp(), + // uid = userId, + // fileName = HEADIMAGE, + // }; + //} /// <summary> /// 涓嬭浇鐢ㄦ埛澶村儚 /// </summary> - /// <param name="userId"></param> - /// <param name="ignoreCache">鏄惁蹇界暐缂撳瓨</param> + /// <param name="imageKey"></param> + /// <param name="ignoreCache">蹇界暐缂撳瓨</param> + /// <param name="regionUrl">鎸囧畾鏈嶅姟鍣ㄥ煙鍚�,浼犵┖涓轰娇鐢ㄥ綋鍓嶇敤鎴峰湴鍧�</param> /// <returns></returns> - public byte[] DownHeadImage(string userId, bool ignoreCache = false) + public byte[] DownHeadImageByImageKey(string imageKey, bool ignoreCache = false, string regionUrl = "") { try { - var imageKey = GetHeadImageKey(userId); + if (string.IsNullOrEmpty(imageKey)) return null; + byte[] imageBytes; - if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //鏈湴鏈夌紦瀛� - imageBytes = Common.FileUtlis.Files.ReadFile(imageKey); + imageBytes = ReadFile(imageKey); } else { //浜戠涓嬭浇 - imageBytes = GetImageDownloadUrl(imageKey); + imageBytes = GetImageDownloadUrlWithRegionUrl(imageKey, regionUrl); if(imageBytes != null) { //淇濆瓨鍒版湰鍦� - Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); } } -- Gitblit v1.8.0