123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using ET;
- using FairyGUI;
- using UnityEngine;
- using UnityEngine.Networking;
- namespace GFGGame
- {
- public static class MatchingPhotoHelper
- {
- //将图片上传到华为云
- public static async Task<bool> PushToHWCloud(string signUrl, byte[] buffer)
- {
- HttpContent content = new ByteArrayContent(buffer);
- HttpClient httpClient = new HttpClient(new HttpClientHandler() { UseCookies = false });
- HttpResponseMessage response = await httpClient.PutAsync(signUrl, content);
- response.EnsureSuccessStatusCode();
- await response.Content.ReadAsStreamAsync();
- if (response.StatusCode != HttpStatusCode.OK)
- {
- ViewManager.Hide<ModalStatusView>();
- PromptController.Instance.ShowFloatTextPrompt("上传资源失败");
- return false;
- }
- else
- {
- return true;
- }
- }
- public static IEnumerator Download(List<MatchingPhotoWorksData> list,string targetPath)
- {
- for (int i = 0; i < list.Count; i++)
- {
- List<string> imageList = MatchingCompetitionDataManager.Instance.GetImageFileNames(targetPath);
- string ImageName = list[i].JudgingInfo.WorksId.ToString() + ".jpg";
- //排行榜内图片是否存在本地,否就下载,是就赋值
- if (!MatchingCompetitionDataManager.Instance.RankIdisInLocal(ImageName, imageList))
- {
- MatchingPhotoWorksData data = list[i];
- if (data == null || data.Ntexture != null) continue;
- int count = 0;
- yield return DownloadPicture(data, count, targetPath);
- }
- else
- {
- list[i].Bytes = File.ReadAllBytes((targetPath+ "/" + ImageName));
- list[i].Ntexture = MatchingCompetitionDataManager.Instance.GetTargetImage(ImageName,targetPath);
- yield return null;
- }
- //本地图片是否存在排行榜,否就删除
- foreach (string item in imageList)
- {
- string getImagePath = targetPath +"/"+ list[i].JudgingInfo.WorksId.ToString()+".jpg";
- if (!MatchingCompetitionDataManager.Instance.ImageIdIsInRank(item))
- {
- MatchingCompetitionDataManager.Instance.DeleteImage(getImagePath);
- }
- }
- }
- ET.Log.Debug("Download finish!!!");
- EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
- }
- public static IEnumerator Download(List<MatchingWorksData> list)
- {
- for (int i = 0; i < list.Count; i++)
- {
- MatchingWorksData data = list[i];
- if (data == null || data.Ntexture != null) continue;
- int count = 0;
- yield return DownloadPicture(data, count);
- }
- ET.Log.Debug("Download finish!!!");
- EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
- }
- public static IEnumerator Download(MatchingPhotoWorksData list, string targetPath)
- {
- MatchingPhotoWorksData data = list;
- yield return DownloadPicture(data, 1,targetPath);
- ET.Log.Debug("Download finish!!!");
- EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
- }
- private static IEnumerator DownloadPicture(MatchingPhotoWorksData data, int count,string targetPath)
- {
- if (count >= 3)
- {
- PromptController.Instance.ShowFloatTextPrompt("下载失败");
- ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
- data.Ntexture = null;
- ViewManager.Hide<ModalStatusView>();
- yield break;
- }
- using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.JudgingInfo.PictureTempUrl))
- {
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
- {
- ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
- count += 1;
- yield return DownloadPicture(data, count, targetPath);
- }
- else
- {
- Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
- data.Ntexture = new NTexture(texture);
- data.Bytes = texture.EncodeToJPG();
- MatchingCompetitionDataManager.Instance.DownloadImageToLocal(data.Bytes,data.JudgingInfo.WorksId,targetPath);
- }
- }
- }
- private static IEnumerator DownloadPicture(MatchingWorksData data, int count)
- {
- if (count >= 3)
- {
- PromptController.Instance.ShowFloatTextPrompt("下载失败");
- ET.Log.Error("PoemPhotoData Download failed!!! data:" + JsonUtility.ToJson(data));
- data.Ntexture = null;
- ViewManager.Hide<ModalStatusView>();
- yield break;
- }
- using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(data.WorksInfo.PictureTempUrl))
- {
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
- {
- ET.Log.Error("Download failed, error code:" + request.result, ",data:" + JsonUtility.ToJson(data));
- count += 1;
- yield return DownloadPicture(data, count);
- }
- else
- {
- Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
- data.Ntexture = new NTexture(texture);
- data.Bytes = texture.EncodeToJPG();
- }
- }
- }
- public static IEnumerator DownloadMyself(string nTextture,string targetPath)
- {
- yield return DownloadMyPicture(nTextture, 1,targetPath);
- ET.Log.Debug("Download finish!!!");
- EventAgent.DispatchEvent(ConstMessage.DOWNLOAD_FINISH);
- }
- private static IEnumerator DownloadMyPicture(string nTextture, int count,string targetPath)
- {
- if (count >= 3)
- {
- PromptController.Instance.ShowFloatTextPrompt("下载失败");
- ViewManager.Hide<ModalStatusView>();
- yield break;
- }
- using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(nTextture))
- {
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
- {
- count += 1;
- yield return DownloadMyPicture(nTextture, count,targetPath);
- }
- else
- {
- Texture2D texture = (request.downloadHandler as DownloadHandlerTexture).texture;
- MatchingCompetitionDataManager.Instance.MyNtextture = new NTexture(texture);
- MatchingCompetitionDataManager.Instance.MyBytes = texture.EncodeToJPG();
- //MatchingCompetitionDataManager.Instance.DownloadImageToLocal(MatchingCompetitionDataManager.Instance.MyBytes, MatchingCompetitionDataManager.Instance.WorksID,targetPath);
- }
- }
- }
- }
- }
|