write to firebase

main
reng 4 months ago
parent 6cbfe9506d
commit 5ed288a7c7
  1. 2
      Assets/Scenes/SampleScene.unity
  2. 78
      Assets/Scripts/SaveImage.cs

@ -3248,7 +3248,7 @@ GameObject:
- component: {fileID: 1146279351}
- component: {fileID: 1146279350}
m_Layer: 0
m_Name: uploader
m_Name: Uploader
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

@ -1,12 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;
using Amazon.S3.Model;
using UltraCombos.Upload;
using OscJack;
using System.Collections;
[System.Serializable]
public class S3Tag
@ -24,6 +20,8 @@ public class SaveImage : MonoBehaviour
public RenderTexture renderTexture;
public string OutputFolder = "output";
public string FirebaseUrl = "https://firestore.googleapis.com/v1/projects/uc-24070-thegreattipsy/databases/(default)/documents/";
// Start is called before the first frame update
void Start()
@ -84,14 +82,17 @@ public class SaveImage : MonoBehaviour
Destroy(tex);
}
void upload(string filename){
void upload(string filename)
{
if (uploader != null)
{
uploader.Upload(filename, (response) =>
{
if (response.success)
{
Debug.Log("Upload successful: " + response);
Debug.Log("Upload successful: " + response.message);
writeToFirebase(filename, response.message);
}
else
{
@ -105,5 +106,66 @@ public class SaveImage : MonoBehaviour
}
}
public void writeToFirebase(string filename, string url)
{
Debug.Log("Writing to Firebase: " + filename);
StartCoroutine(WriteToFirebaseCoroutine(filename, url));
IEnumerator WriteToFirebaseCoroutine(string filename, string url)
{
string id=filename.Split('/')[2].Split('.')[0];
Debug.Log("Extracted ID: " + id);
string firebaseUrl = FirebaseUrl + "prints/" + id;
string jsonData = "{ \"fields\": { " +
"\"url\": { \"stringValue\": \"" + url + "\" }, " +
"\"timestamp\": { \"timestampValue\": \"" + System.DateTime.UtcNow.ToString("o") + "\" } " +
"} }";
using (var www = new UnityEngine.Networking.UnityWebRequest(firebaseUrl, "PATCH"))
{
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
www.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(bodyRaw);
www.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
yield return www.SendWebRequest();
#if UNITY_2020_1_OR_NEWER
if (www.result != UnityEngine.Networking.UnityWebRequest.Result.Success)
#else
if (www.isNetworkError || www.isHttpError)
#endif
{
Debug.LogError("Error writing to Firebase: " + www.error);
Debug.LogError("Response: " + www.downloadHandler.text);
}
else
{
Debug.Log("Successfully wrote to Firebase: " + www.downloadHandler.text);
}
}
}
}
public void sendOscMessage(string message)
{
// Implement your OSC message sending logic here
Debug.Log("Sending OSC message: " + message);
StartCoroutine(SendOscMessageCoroutine(message));
}
public IEnumerator SendOscMessageCoroutine(string message){
using (var client = new OscClient("127.0.0.1", 9000))
{
// Send two-component float values ten times.
for (var i = 0; i < 1; i++)
{
yield return new WaitForSeconds(0.5f);
client.Send("/uploaded", // OSC address
message); // Message content
}
}
}
}
Loading…
Cancel
Save