PictureStorageHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ET.Log.Debug("Download finish!!!");
  46. }
  47. private static IEnumerator DownloadPicture(PoemGalleryData data, int count)
  48. {
  49. if (count >= 3)
  50. {
  51. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  52. ET.Log.Error("PoemGalleryData Download failed!!! data:" + JsonUtility.ToJson(data));
  53. data.Ntexture = null;
  54. ViewManager.Hide<ModalStatusView>();
  55. yield break;
  56. }
  57. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.PictureTempUrl))
  58. {
  59. yield return request.SendWebRequest();
  60. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  61. {
  62. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  63. count += 1;
  64. yield return DownloadPicture(data, count);
  65. }
  66. else
  67. {
  68. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  69. data.Ntexture = new NTexture(texture);
  70. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH_ONE);
  71. }
  72. }
  73. }
  74. public static IEnumerator Download(List<PoemPhotoData> list)
  75. {
  76. for (int i = 0; i < list.Count; i++)
  77. {
  78. PoemPhotoData data = list[i];
  79. if (data == null || data.Ntexture != null) continue;
  80. int count = 0;
  81. yield return DownloadPicture(data, count);
  82. }
  83. ET.Log.Debug("Download finish!!!");
  84. EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
  85. }
  86. private static IEnumerator DownloadPicture(PoemPhotoData data, int count)
  87. {
  88. if (count >= 3)
  89. {
  90. PromptController.Instance.ShowFloatTextPrompt("下载失败");
  91. ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
  92. data.Ntexture = null;
  93. ViewManager.Hide<ModalStatusView>();
  94. yield break;
  95. }
  96. using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.PictureTempUrl))
  97. {
  98. yield return request.SendWebRequest();
  99. if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
  100. {
  101. ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
  102. count += 1;
  103. yield return DownloadPicture(data, count);
  104. }
  105. else
  106. {
  107. Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
  108. data.Ntexture = new NTexture(texture);
  109. data.Bytes = texture.EncodeToJPG();
  110. }
  111. }
  112. }
  113. }
  114. }