PictureStorageHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using ET;
  9. using FairyGUI;
  10. using UnityEngine;
  11. using UnityEngine.Networking;
  12. namespace GFGGame
  13. {
  14. public static class PictureStorageHelper
  15. {
  16. //将图片上传到华为云
  17. public static async Task<bool> PushToHWCloud(string signUrl, byte[] buffer)
  18. {
  19. HttpContent content = new ByteArrayContent(buffer);
  20. HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseCookies = false });
  21. HttpResponseMessage response = await httpClient.PutAsync(signUrl, content);
  22. response.EnsureSuccessStatusCode();
  23. await response.Content.ReadAsStreamAsync();
  24. if (response.StatusCode != HttpStatusCode.OK)
  25. {
  26. ViewManager.Hide<ModalStatusView>();
  27. PromptController.Instance.ShowFloatTextPrompt("上传资源失败");
  28. return false;
  29. }
  30. else
  31. {
  32. return true;
  33. }
  34. }
  35. public static IEnumerator Download(List<long> list, int startIndex, Action callBack = null)
  36. {
  37. for (int i = startIndex; i < list.Count; i++)
  38. {
  39. PoemGalleryData data = PoemGalleryDataManager.Instance.GetGalleryDataById(list[i]);
  40. if (data.Ntexture != null) continue;
  41. int count = 0;
  42. yield return DownloadPicture(data, count);
  43. }
  44. if (callBack != null) callBack();
  45. PoemGalleryDataManager.Instance.waitRelease = false;
  46. ET.Log.Debug("Download finish!!!");
  47. }
  48. private static IEnumerator DownloadPicture(PoemGalleryData data, int count)
  49. {
  50. if (count >= 3)
  51. {
  52. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  53. ET.Log.Error("PoemGalleryData Download failed!!! data:" + JsonUtility.ToJson(data));
  54. data.Ntexture = null;
  55. ViewManager.Hide<ModalStatusView>();
  56. yield break;
  57. }
  58. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.PictureTempUrl))
  59. {
  60. yield return request.SendWebRequest();
  61. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  62. {
  63. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  64. count += 1;
  65. yield return DownloadPicture(data, count);
  66. }
  67. else
  68. {
  69. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  70. data.Ntexture = new NTexture(texture);
  71. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH_ONE);
  72. }
  73. }
  74. }
  75. public static IEnumerator Download(List<PoemPhotoData> list)
  76. {
  77. for (int i = 0; i < list.Count; i++)
  78. {
  79. PoemPhotoData data = list[i];
  80. if (data == null || data.Ntexture != null) continue;
  81. int count = 0;
  82. yield return DownloadPicture(data, count);
  83. }
  84. ET.Log.Debug("Download finish!!!");
  85. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  86. }
  87. private static IEnumerator DownloadPicture(PoemPhotoData data, int count)
  88. {
  89. if (count >= 3)
  90. {
  91. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  92. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  93. data.Ntexture = null;
  94. ViewManager.Hide<ModalStatusView>();
  95. yield break;
  96. }
  97. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.PictureTempUrl))
  98. {
  99. yield return request.SendWebRequest();
  100. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  101. {
  102. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  103. count += 1;
  104. yield return DownloadPicture(data, count);
  105. }
  106. else
  107. {
  108. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  109. data.Ntexture = new NTexture(texture);
  110. data.Bytes = texture.EncodeToJPG();
  111. }
  112. }
  113. }
  114. }
  115. }