You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
6.1 KiB
191 lines
6.1 KiB
using UnityEngine;
|
|
using Amazon.S3.Model;
|
|
using UltraCombos.Upload;
|
|
using OscJack;
|
|
using System.Collections;
|
|
using TMPro;
|
|
using System;
|
|
|
|
[System.Serializable]
|
|
public class S3Tag
|
|
{
|
|
public string key;
|
|
public string value;
|
|
public static implicit operator Tag(S3Tag t) => new Tag { Key = t.key, Value = t.value };
|
|
}
|
|
|
|
public class SaveImage : MonoBehaviour
|
|
{
|
|
|
|
|
|
public Uploader uploader;
|
|
public RenderTexture renderTexture_postcard;
|
|
|
|
public RenderTexture renderTexture_share;
|
|
public string OutputFolder = "output";
|
|
|
|
public string FirebaseUrl = "https://firestore.googleapis.com/v1/projects/uc-24070-thegreattipsy/databases/(default)/documents/";
|
|
|
|
public TextMeshProUGUI TextNumber;
|
|
public TextMeshProUGUI TextSummary;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
uploader = GetComponent<Uploader>();
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Save(string input_text)
|
|
{
|
|
|
|
var parmas = input_text.Split('#');
|
|
if (parmas.Length < 2)
|
|
{
|
|
Debug.LogError("Input text must contain at least two parts separated by '#'.");
|
|
return;
|
|
}
|
|
|
|
string id = parmas[0].Trim();
|
|
string summary = parmas[1].Trim();
|
|
string password = parmas.Length > 2 ? parmas[2].Trim() : string.Empty;
|
|
|
|
TextNumber.text = id;
|
|
TextSummary.text = summary;
|
|
|
|
|
|
|
|
string filename = $"{password}_{id}.png"; // Use the ID as the filename
|
|
|
|
if (string.IsNullOrEmpty(filename))
|
|
{
|
|
Debug.LogError("Filename cannot be null or empty.");
|
|
return;
|
|
}
|
|
Debug.Log("Saving image to: " + filename + " with password: " + password);
|
|
|
|
string timestamp = System.DateTime.Now.ToString("yyyyMMdd");
|
|
|
|
if (!System.IO.Directory.Exists(OutputFolder + "/print/" + timestamp))
|
|
{
|
|
System.IO.Directory.CreateDirectory(OutputFolder + "/print/" + timestamp);
|
|
}
|
|
if (!System.IO.Directory.Exists(OutputFolder + "/share/" + timestamp))
|
|
{
|
|
System.IO.Directory.CreateDirectory(OutputFolder + "/share/" + timestamp);
|
|
}
|
|
|
|
|
|
SaveRenderTextureToPNG(renderTexture_postcard, System.IO.Path.Combine(OutputFolder + "/print/" + timestamp, filename));
|
|
Debug.Log("Image saved to " + filename);
|
|
|
|
SaveRenderTextureToPNG(renderTexture_share, System.IO.Path.Combine(OutputFolder + "/share/" + timestamp, filename));
|
|
Debug.Log("Image saved to " + filename);
|
|
|
|
// Optionally, you can also log the full path
|
|
string print_fullPath = System.IO.Path.Combine(OutputFolder + "/print/" + timestamp + "/", filename);
|
|
Debug.Log("print_path: " + print_fullPath);
|
|
|
|
upload(id, print_fullPath, "print");
|
|
|
|
string share_fullPath = System.IO.Path.Combine(OutputFolder + "/share/" + timestamp + "/", filename);
|
|
Debug.Log("share_path: " + share_fullPath);
|
|
upload(id, share_fullPath, "share");
|
|
}
|
|
|
|
void SaveRenderTextureToPNG(RenderTexture rt, string filePath)
|
|
{
|
|
RenderTexture currentRT = RenderTexture.active;
|
|
RenderTexture.active = rt;
|
|
|
|
Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
|
|
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
|
|
tex.Apply();
|
|
|
|
byte[] bytes = tex.EncodeToPNG();
|
|
System.IO.File.WriteAllBytes(filePath, bytes);
|
|
|
|
Debug.Log("Saved RenderTexture to PNG at " + filePath + " with size: " + bytes.Length + " bytes");
|
|
|
|
RenderTexture.active = currentRT;
|
|
Destroy(tex);
|
|
}
|
|
|
|
void upload(string id, string filename, string type)
|
|
{
|
|
if (uploader != null)
|
|
{
|
|
uploader.Upload(filename, (response) =>
|
|
{
|
|
if (response.success)
|
|
{
|
|
Debug.Log("Upload successful: " + response.message);
|
|
|
|
if(type=="print") writeToFirebase("prints", id, response.message, type);
|
|
writeToFirebase("records", id, response.message, type);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Upload failed: " + response);
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Uploader is not assigned.");
|
|
}
|
|
}
|
|
|
|
public void writeToFirebase(string collection, string id, string url, string type)
|
|
{
|
|
Debug.Log("Writing to Firebase: " + id);
|
|
StartCoroutine(WriteToFirebaseCoroutine(id, url));
|
|
|
|
IEnumerator WriteToFirebaseCoroutine(string id, string url)
|
|
{
|
|
// string[] tags=filename.Split('/');
|
|
// string id=tags[tags.Length - 1].Split('.')[0];
|
|
Debug.Log("Extracted ID: " + id);
|
|
|
|
string firebaseUrl = FirebaseUrl + collection + "/" + id;
|
|
string jsonData = "{ \"fields\": { " +
|
|
"\""+type+"_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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} |