feat(storagemanager): correctly convert GS url to http url

This commit is contained in:
Fangh 2024-01-28 22:07:34 +01:00
parent c952783c36
commit 878a8d18fa
3 changed files with 51 additions and 37 deletions

View File

@ -28,44 +28,23 @@ public void Initialize(Proposition _proposition)
DisplayPicture(proposition.photoUrl);
}
private void DisplayPicture(string _url)
/// <summary>
/// Url is Google bucket URL. We
/// </summary>
/// <param name="_gsUrl"></param>
private void DisplayPicture(string _gsUrl)
{
Debug.Log($"Downloading {_url}");
StorageReference imageRef = FirebaseStorage.DefaultInstance.GetReferenceFromUrl(_url);
imageRef.GetDownloadUrlAsync().ContinueWithOnMainThread(task =>
Debug.Log($"Google Storage URL : {_gsUrl}");
StorageManager.ConvertGoogleStorageURLToHttpsUrl(_gsUrl, _url =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.LogException(task.Exception);
}
else
{
Debug.Log("Download URL: " + task.Result);
StartCoroutine(DownloadImage(task.Result.AbsolutePath, _texture =>
StartCoroutine(StorageManager.DownloadImage_Coroutine(_url, _texture =>
{
Debug.Log("Set texture in the raw image");
picture.texture = _texture;
}));
}
});
}
private IEnumerator DownloadImage(string _url, Action<Texture> callback_OnTextureDownloaded)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(_url);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.LogError(request.error);
}
else
{
callback_OnTextureDownloaded?.Invoke(((DownloadHandlerTexture)request.downloadHandler).texture);
}
}
public void UpdateVoters(List<Player> _newVoters)
{
Debug.Log($"There are some new voters for {proposition.owner}'s proposition", this);
@ -80,4 +59,15 @@ public void UpdateVoters(List<Player> _newVoters)
}
}
}
[ContextMenu("Debug Download image")]
private void Debug_DownloadImage()
{
StartCoroutine(DownloadImage("https://firebasestorage.googleapis.com/v0/b/ggj2024-5cb41.appspot.com/o/0278%2F63ae32b0-8371-4a7a-84a0-e1a97328fc09%2F10a5127b-12d4-48ca-b94c-1cb920a43529.png?alt=media&token=d7e53d8a-d5b4-40f9-847d-224ed2d683dc",
texture =>
{
Debug.Log(texture.dimension);
}));
}
}

View File

@ -27,11 +27,14 @@ void OnEnable()
for (int i = 0; i < 2; i++)
{
StartCoroutine(storageManager.DownloadImage(props[i].photoUrl, (Texture texture) =>
StorageManager.ConvertGoogleStorageURLToHttpsUrl(props[i].photoUrl, _httpURL =>
{
StartCoroutine(StorageManager.DownloadImage_Coroutine(_httpURL, (Texture texture) =>
{
Sprite sprite = Sprite.Create(texture as Texture2D, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
btns[i].sprite = sprite;
}));
});
}
}

View File

@ -1,8 +1,11 @@
using Firebase.Database;
using Firebase.Extensions;
using Firebase.Storage;
using System;
using System.Collections;
using System.Security.Policy;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
@ -68,7 +71,7 @@ public void UploadPhoto(string roomCode, string playerId, int question, int prop
});
}
public IEnumerator DownloadImage(string _url, Action<Texture> callback_OnTextureDownloaded)
public static IEnumerator DownloadImage_Coroutine(string _url, Action<Texture> callback_OnTextureDownloaded)
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(_url);
yield return request.SendWebRequest();
@ -82,4 +85,22 @@ public IEnumerator DownloadImage(string _url, Action<Texture> callback_OnTexture
callback_OnTextureDownloaded?.Invoke(((DownloadHandlerTexture)request.downloadHandler).texture);
}
}
public static void ConvertGoogleStorageURLToHttpsUrl(string _gsURL, Action<string> callback_OnURLFound)
{
StorageReference imageRef = FirebaseStorage.DefaultInstance.GetReferenceFromUrl(_gsURL);
imageRef.GetDownloadUrlAsync().ContinueWithOnMainThread(task =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.LogException(task.Exception);
}
else
{
Debug.Log("Image HTTPS URL: " + task.Result);
callback_OnURLFound?.Invoke(task.Result.ToString());
}
});
}
}