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.
242 lines
7.7 KiB
242 lines
7.7 KiB
using System.Collections;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
using Google.Apis.Auth.OAuth2;
|
|
using Google.Apis.Drive.v3;
|
|
using Google.Apis.Services;
|
|
|
|
|
|
|
|
|
|
public class UploadToGoogleDrive : MonoBehaviour
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
public GameObject BackgroundText;
|
|
public GameObject BackgroundDefault;
|
|
|
|
public GameObject ImageDiscard;
|
|
public GameObject ImageGenerate;
|
|
|
|
public GameObject ImageDiscardPostcard;
|
|
public GameObject ImageGeneratePostcard;
|
|
// Start is called before the first frame update
|
|
|
|
|
|
public string GoogleDriveFolderId = "1m6qxzRrWfj3UcVxJNgACabS4aM8arbHq";
|
|
|
|
// DriveService service;
|
|
|
|
void Start()
|
|
{
|
|
setupService();
|
|
}
|
|
|
|
// 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 uploadDest = parmas[0].Trim();
|
|
string id = parmas[1].Trim();
|
|
string summary = parmas[2].Trim();
|
|
string filename = parmas.Length > 3 ? parmas[3].Trim() : System.DateTime.Now.ToString("yyyyMMdd_hhmmss");
|
|
bool discard = parmas.Length > 4 && (parmas[4].Trim().ToLower() == "discard");
|
|
bool default_image = parmas.Length > 5 && (parmas[5].Trim().ToLower() == "default");
|
|
|
|
if (discard)
|
|
{
|
|
if (ImageDiscard != null) ImageDiscard.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
if (ImageDiscard != null) ImageDiscard.SetActive(false);
|
|
}
|
|
|
|
Debug.Log("choice: " + discard + " default" + default_image + " summary:" + summary);
|
|
|
|
if (summary.Length > 0)
|
|
{
|
|
BackgroundDefault.SetActive(false);
|
|
BackgroundText.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
BackgroundDefault.SetActive(true);
|
|
BackgroundText.SetActive(false);
|
|
}
|
|
|
|
if (default_image)
|
|
{
|
|
ImageGenerate.SetActive(false);
|
|
if (!discard) ImageDiscard.SetActive(false);
|
|
|
|
ImageGeneratePostcard.SetActive(false);
|
|
if (!discard) ImageDiscardPostcard.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
ImageGenerate.SetActive(true);
|
|
ImageGeneratePostcard.SetActive(true);
|
|
}
|
|
|
|
TextNumber.SetText(id);
|
|
TextSummary.SetText(summary);
|
|
|
|
TextNumber.ForceMeshUpdate();
|
|
TextSummary.ForceMeshUpdate();
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(filename))
|
|
{
|
|
Debug.LogError("Filename cannot be null or empty.");
|
|
return;
|
|
}
|
|
Debug.Log("Saving image to: " + uploadDest + " filename=" + filename + " id=" + id);
|
|
|
|
string timestamp = System.DateTime.Now.ToString("yyyyMMdd");
|
|
|
|
if (!System.IO.Directory.Exists(OutputFolder + "/" + uploadDest))
|
|
{
|
|
System.IO.Directory.CreateDirectory(OutputFolder + "/" + uploadDest);
|
|
}
|
|
|
|
|
|
SaveRenderTextureToPNG(renderTexture_postcard, System.IO.Path.Combine(OutputFolder + "/" + uploadDest, filename + "_print.png"));
|
|
Debug.Log("Image saved to " + filename);
|
|
|
|
SaveRenderTextureToPNG(renderTexture_share, System.IO.Path.Combine(OutputFolder + "/" + uploadDest, filename + ".png"));
|
|
Debug.Log("Image saved to " + filename);
|
|
|
|
// Optionally, you can also log the full path
|
|
string print_fullPath = System.IO.Path.Combine(OutputFolder + "/" + uploadDest + "/", filename + "_print.png");
|
|
Debug.Log("print_path: " + print_fullPath);
|
|
|
|
upload(print_fullPath, id, "print");
|
|
|
|
string share_fullPath = System.IO.Path.Combine(OutputFolder + "/" + uploadDest + "/", filename + ".png");
|
|
Debug.Log("share_path: " + share_fullPath);
|
|
upload(share_fullPath, id, "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 setupService()
|
|
{
|
|
// Debug.Log("Setting up Google Drive service...");
|
|
// // Initialize Google Drive API service
|
|
// service = new DriveService(new BaseClientService.Initializer()
|
|
// {
|
|
// HttpClientInitializer = GoogleCredential.FromFile("../Material/tech-277909-8bd38efb7464.json").CreateScoped(DriveService.Scope.DriveFile),
|
|
// ApplicationName = "24070-Upload",
|
|
// });
|
|
|
|
GoogleDriveUtils.setupService();
|
|
}
|
|
|
|
|
|
async void upload(string _uploadFile, string _fileId, string type)
|
|
{
|
|
|
|
// if(service == null)
|
|
// {
|
|
// Debug.Log("service is null, setting up service again.");
|
|
// setupService();
|
|
// }
|
|
|
|
|
|
string path = _fileId;
|
|
int lastSlash = path.LastIndexOf('/');
|
|
if (lastSlash >= 0)
|
|
{
|
|
path = path.Substring(0, lastSlash);
|
|
}
|
|
Debug.Log("Uploading file to Google Drive: " + _uploadFile + " folder=" + path);
|
|
|
|
string folderId = GoogleDriveUtils.getFolder(GoogleDriveUtils.service, path, GoogleDriveFolderId);
|
|
await GoogleDriveUtils.upload(GoogleDriveUtils.service, _uploadFile, folderId, _fileId, type);
|
|
|
|
}
|
|
|
|
public void writeToFirebase(string collection, string id, string url, string type)
|
|
{
|
|
Debug.Log("Writing to Firebase: " + collection + "/" + 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|