write to firebase

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

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

@ -1,12 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;
using Amazon.S3.Model; using Amazon.S3.Model;
using UltraCombos.Upload; using UltraCombos.Upload;
using OscJack;
using System.Collections;
[System.Serializable] [System.Serializable]
public class S3Tag public class S3Tag
@ -19,11 +15,13 @@ public class S3Tag
public class SaveImage : MonoBehaviour public class SaveImage : MonoBehaviour
{ {
public Uploader uploader; public Uploader uploader;
public RenderTexture renderTexture; public RenderTexture renderTexture;
public string OutputFolder = "output"; 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 // Start is called before the first frame update
void Start() void Start()
@ -35,20 +33,20 @@ public class SaveImage : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
} }
public void save(string filename) public void save(string filename)
{ {
if(string.IsNullOrEmpty(filename)) if (string.IsNullOrEmpty(filename))
{ {
Debug.LogError("Filename cannot be null or empty."); Debug.LogError("Filename cannot be null or empty.");
return; return;
} }
Debug.Log("Saving image to: " + filename); Debug.Log("Saving image to: " + filename);
string timestamp = System.DateTime.Now.ToString("yyyyMMdd"); string timestamp = System.DateTime.Now.ToString("yyyyMMdd");
if (!System.IO.Directory.Exists(OutputFolder + "/" + timestamp)) if (!System.IO.Directory.Exists(OutputFolder + "/" + timestamp))
{ {
@ -78,20 +76,23 @@ public class SaveImage : MonoBehaviour
byte[] bytes = tex.EncodeToPNG(); byte[] bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes(filePath, bytes); System.IO.File.WriteAllBytes(filePath, bytes);
Debug.Log("Saved RenderTexture to PNG at " + filePath+ " with size: " + bytes.Length + " bytes"); Debug.Log("Saved RenderTexture to PNG at " + filePath + " with size: " + bytes.Length + " bytes");
RenderTexture.active = currentRT; RenderTexture.active = currentRT;
Destroy(tex); Destroy(tex);
} }
void upload(string filename){ void upload(string filename)
{
if (uploader != null) if (uploader != null)
{ {
uploader.Upload(filename, (response) => uploader.Upload(filename, (response) =>
{ {
if (response.success) if (response.success)
{ {
Debug.Log("Upload successful: " + response); Debug.Log("Upload successful: " + response.message);
writeToFirebase(filename, response.message);
} }
else else
{ {
@ -104,6 +105,67 @@ public class SaveImage : MonoBehaviour
Debug.LogError("Uploader is not assigned."); Debug.LogError("Uploader is not assigned.");
} }
} }
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