Snaparazzi/Assets/Scripts/StorageManager.cs
2024-01-28 13:25:17 +01:00

69 lines
1.8 KiB
C#

using Firebase.Database;
using Firebase.Storage;
using System;
using System.Threading.Tasks;
using UnityEngine;
public class StorageManager : MonoBehaviour
{
private StorageReference storage;
private DatabaseReference realtimeDB;
void Awake()
{
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
}
void Initialize()
{
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
storage = FirebaseStorage.DefaultInstance.RootReference;
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void UploadPhoto(string roomCode, string playerId, int question, string proposition)
{
Texture2D photo = gameObject.GetComponent<CameraManager>().GetPhoto();
byte[] photoBytes = ImageConversion.EncodeToJPG(photo);
GameManager game = gameObject.GetComponent<GameManager>();
string imageUuid = Guid.NewGuid().ToString();
StorageReference imageRef = storage.Child($"{roomCode}/{playerId}/{imageUuid}.png");
imageRef.PutBytesAsync(photoBytes).ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.LogException(task.Exception);
// Uh-oh, an error occurred!
}
else
{
realtimeDB
.Child("rooms")
.Child(roomCode)
.Child("questions")
.Child(question.ToString())
.Child("propositions")
.Child(proposition)
.Child("photoUrl")
.SetValueAsync(imageRef.Path);
}
});
}
}