Files
monde-usage-unity/Assets/Scripts/FirebaseStorageDownload.cs
2024-09-22 21:45:37 +02:00

69 lines
2.0 KiBLFS
C#

#nullable enable
#if !UNITY_WEBGL || UNITY_EDITOR
using Firebase.Storage;
using System;
using System.Threading;
using UnityEngine;
#endif
public partial class FirebaseFileDownloader
{
#if !UNITY_WEBGL || UNITY_EDITOR
public static void DownloadFileFirebaseVanilla(
string targetStorageUrl,
string localUrl,
Action<float>? progressCallBack = null,
Action? finishCallback = null,
Action<string>? errorCallback = null
)
{
Debug.Log("Downloading file... from " + targetStorageUrl);
FirebaseStorage storage = FirebaseStorage.DefaultInstance;
StorageReference storageRef = storage.GetReferenceFromUrl(targetStorageUrl);
storageRef.GetFileAsync(localUrl, new StorageProgress<DownloadState>(state =>
{
progressCallBack?.Invoke((float)state.BytesTransferred / state.TotalByteCount);
}), CancellationToken.None).ContinueWith(task =>
{
if (!task.IsFaulted && !task.IsCanceled)
{
Debug.Log("File downloaded.");
}
else
{
Debug.LogError("Error downloading file: " + task.Exception);
errorCallback?.Invoke(task.Exception.ToString());
}
finishCallback?.Invoke();
});
}
public static void DownloadFileDownloadUrl(
string targetStorageUrl,
Action<string>? finishCallback = null,
Action<string>? errorCallback = null
)
{
Console.WriteLine("Getting download URL... from " + targetStorageUrl);
FirebaseStorage storage = FirebaseStorage.DefaultInstance;
StorageReference storageRef = storage.GetReferenceFromUrl(targetStorageUrl);
storageRef.GetDownloadUrlAsync().ContinueWith(task =>
{
if (!task.IsFaulted && !task.IsCanceled)
{
string downloadUrl = task.Result.ToString();
Debug.Log("Download URL: " + downloadUrl);
finishCallback?.Invoke(downloadUrl);
}
else
{
Debug.LogError("Error getting download URL: " + task.Exception);
errorCallback?.Invoke(task.Exception.ToString());
}
});
}
#endif
}