2024-01-27 20:07:49 +00:00
|
|
|
using Firebase.Database;
|
2024-01-28 21:07:34 +00:00
|
|
|
using Firebase.Extensions;
|
2024-01-27 18:58:02 +00:00
|
|
|
using Firebase.Storage;
|
|
|
|
using System;
|
2024-01-28 16:07:54 +00:00
|
|
|
using System.Collections;
|
2024-02-01 21:38:34 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2024-01-28 21:07:34 +00:00
|
|
|
using System.Security.Policy;
|
2024-01-27 18:58:02 +00:00
|
|
|
using System.Threading.Tasks;
|
2024-01-28 21:07:34 +00:00
|
|
|
using UnityEditor;
|
2024-01-27 18:58:02 +00:00
|
|
|
using UnityEngine;
|
2024-01-28 16:07:54 +00:00
|
|
|
using UnityEngine.Networking;
|
2024-01-27 18:58:02 +00:00
|
|
|
|
|
|
|
public class StorageManager : MonoBehaviour
|
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
public static StorageManager Instance;
|
|
|
|
|
2024-01-28 15:36:37 +00:00
|
|
|
public CameraManager cameraManager;
|
2024-01-27 22:47:06 +00:00
|
|
|
private StorageReference storage;
|
2024-01-27 20:07:49 +00:00
|
|
|
private DatabaseReference realtimeDB;
|
2024-01-27 18:58:02 +00:00
|
|
|
|
|
|
|
void Awake()
|
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
Instance = this;
|
2024-01-27 18:58:02 +00:00
|
|
|
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize()
|
|
|
|
{
|
|
|
|
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
|
2024-01-27 22:47:06 +00:00
|
|
|
storage = FirebaseStorage.DefaultInstance.RootReference;
|
2024-01-27 20:07:49 +00:00
|
|
|
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
|
2024-01-27 18:58:02 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
public void DeleteFileOfRoom(Room _room, Action callBack_OnFinish)
|
2024-01-27 18:58:02 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
List<Task> deleteTasks = new List<Task>();
|
|
|
|
foreach(Player player in _room.players.Values.ToList())
|
|
|
|
{
|
|
|
|
foreach(Proposition proposition in _room.GetPropositionsForPlayer(player))
|
|
|
|
{
|
|
|
|
if (proposition != null)
|
|
|
|
{
|
|
|
|
if(proposition.photoUrl != null)
|
|
|
|
{
|
|
|
|
Debug.Log($"Adding a task to delete {proposition.photoUrl}", this);
|
|
|
|
deleteTasks.Add(Task.Run(async () =>
|
|
|
|
{
|
|
|
|
string temp = proposition.photoUrl;
|
|
|
|
StorageReference imageRef = FirebaseStorage.DefaultInstance.GetReferenceFromUrl(temp);
|
|
|
|
Debug.Log($"deleting {temp}", this);
|
|
|
|
await imageRef.DeleteAsync();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(deleteTasks.Count > 0)
|
|
|
|
{
|
|
|
|
RunDeleteTasks(deleteTasks, callBack_OnFinish);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Log($"There is no files to delete for room {_room.code}", this);
|
|
|
|
callBack_OnFinish.Invoke();
|
|
|
|
}
|
2024-01-27 18:58:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
private void RunDeleteTasks(List<Task> deleteTasks, Action callback_OnFinish)
|
2024-01-27 18:58:02 +00:00
|
|
|
{
|
2024-02-01 21:38:34 +00:00
|
|
|
int taskCount = deleteTasks.Count;
|
|
|
|
Debug.Log($"There are {taskCount} files to delete", this);
|
2024-01-27 18:58:02 +00:00
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
Task.WhenAll(deleteTasks).ContinueWith(_ =>
|
|
|
|
{
|
|
|
|
Debug.Log($"all delete tasks are done", this);
|
|
|
|
callback_OnFinish?.Invoke();
|
|
|
|
});
|
2024-01-27 18:58:02 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 21:38:34 +00:00
|
|
|
/*
|
|
|
|
private IEnumerator RunDeleteTasks(List<Task> deleteTasks, Action callback_OnFinish)
|
|
|
|
{
|
|
|
|
int taskCount = deleteTasks.Count;
|
|
|
|
Debug.Log($"There are {taskCount} files to delete", this);
|
|
|
|
foreach (Task task in deleteTasks)
|
|
|
|
{
|
|
|
|
task.ContinueWith((completedTask) =>
|
|
|
|
{
|
|
|
|
// This code will be executed when the task is completed.
|
|
|
|
taskCount--;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start the task
|
|
|
|
task.Start();
|
|
|
|
}
|
|
|
|
while (taskCount > 0)
|
|
|
|
{
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
callback_OnFinish?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task DeletePhoto(string _gsURL)
|
|
|
|
{
|
|
|
|
string temp = _gsURL;
|
|
|
|
StorageReference imageRef = FirebaseStorage.DefaultInstance.GetReferenceFromUrl(temp);
|
|
|
|
Debug.Log($"deleting {temp}", this);
|
|
|
|
return imageRef.DeleteAsync();
|
|
|
|
}*/
|
|
|
|
|
2024-01-29 21:10:52 +00:00
|
|
|
public void UploadPhoto(string roomCode, string playerId, int question, int proposition, Action<bool> callback_OnPhotoUploaded)
|
2024-01-27 18:58:02 +00:00
|
|
|
{
|
2024-01-28 15:36:37 +00:00
|
|
|
Texture2D photo = cameraManager.GetPhoto();
|
2024-01-27 18:58:02 +00:00
|
|
|
byte[] photoBytes = ImageConversion.EncodeToJPG(photo);
|
|
|
|
string imageUuid = Guid.NewGuid().ToString();
|
|
|
|
|
2024-01-28 16:54:04 +00:00
|
|
|
string root = "gs://ggj2024-5cb41.appspot.com/";
|
|
|
|
string path = $"{roomCode}/{playerId}/{imageUuid}.png";
|
|
|
|
StorageReference imageRef = storage.Child(path);
|
2024-01-27 18:58:02 +00:00
|
|
|
|
2024-01-29 21:19:22 +00:00
|
|
|
imageRef.PutBytesAsync(photoBytes).ContinueWithOnMainThread(task =>
|
2024-01-27 18:58:02 +00:00
|
|
|
{
|
|
|
|
if (task.IsFaulted || task.IsCanceled)
|
|
|
|
{
|
2024-01-29 21:10:52 +00:00
|
|
|
callback_OnPhotoUploaded?.Invoke(false);
|
2024-01-27 20:07:49 +00:00
|
|
|
Debug.LogException(task.Exception);
|
2024-01-27 18:58:02 +00:00
|
|
|
// Uh-oh, an error occurred!
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-29 21:10:52 +00:00
|
|
|
callback_OnPhotoUploaded?.Invoke(true);
|
2024-01-28 12:25:17 +00:00
|
|
|
realtimeDB
|
|
|
|
.Child("rooms")
|
|
|
|
.Child(roomCode)
|
|
|
|
.Child("questions")
|
|
|
|
.Child(question.ToString())
|
|
|
|
.Child("propositions")
|
2024-01-28 14:42:32 +00:00
|
|
|
.Child(proposition.ToString())
|
2024-01-28 12:25:17 +00:00
|
|
|
.Child("photoUrl")
|
2024-01-28 16:54:04 +00:00
|
|
|
.SetValueAsync(root + path);
|
2024-01-27 18:58:02 +00:00
|
|
|
}
|
|
|
|
});
|
2024-01-28 16:07:54 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 21:07:34 +00:00
|
|
|
public static IEnumerator DownloadImage_Coroutine(string _url, Action<Texture> callback_OnTextureDownloaded)
|
2024-01-28 16:07:54 +00:00
|
|
|
{
|
|
|
|
UnityWebRequest request = UnityWebRequestTexture.GetTexture(_url);
|
|
|
|
yield return request.SendWebRequest();
|
2024-01-27 18:58:02 +00:00
|
|
|
|
2024-01-28 16:07:54 +00:00
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
|
|
{
|
|
|
|
Debug.LogError(request.error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callback_OnTextureDownloaded?.Invoke(((DownloadHandlerTexture)request.downloadHandler).texture);
|
|
|
|
}
|
2024-01-27 18:58:02 +00:00
|
|
|
}
|
2024-01-28 21:07:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2024-01-29 22:00:10 +00:00
|
|
|
//Debug.Log("Image HTTPS URL: " + task.Result);
|
2024-01-28 21:07:34 +00:00
|
|
|
callback_OnURLFound?.Invoke(task.Result.ToString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2024-01-27 18:58:02 +00:00
|
|
|
}
|