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.
171 lines
6.1 KiB
171 lines
6.1 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
using Google.Apis.Auth.OAuth2;
|
|
using Google.Apis.Drive.v3;
|
|
using Google.Apis.Drive.v3.Data;
|
|
using Google.Apis.Services;
|
|
using Google.Apis.Util.Store;
|
|
using System;
|
|
using System.IO;
|
|
using Google.Apis.Upload;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
|
|
|
|
|
|
public class GoogleDriveUtils
|
|
{
|
|
|
|
static string[] Scopes = { DriveService.Scope.Drive }; // or DriveService.Scope.Drive for full access
|
|
static string ApplicationName = "MyDriveUploader";
|
|
|
|
public static DriveService service;
|
|
|
|
public static void setupService()
|
|
{
|
|
UserCredential credential;
|
|
using (var stream = new FileStream("../Material/client_secret_30989913678-hk8sipecu98lp5vdsk0ictf5gufmn33p.apps.googleusercontent.com.json", FileMode.Open, FileAccess.Read))
|
|
{
|
|
// credential = GoogleCredential.FromStream(stream)
|
|
// .CreateScoped(Scopes);
|
|
string credPath = "../Material/token.json";
|
|
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
|
|
GoogleClientSecrets.FromStream(stream).Secrets,
|
|
Scopes,
|
|
"user",
|
|
CancellationToken.None,
|
|
new FileDataStore(credPath, true)).Result;
|
|
}
|
|
|
|
service = new DriveService(new BaseClientService.Initializer()
|
|
{
|
|
HttpClientInitializer = credential,
|
|
ApplicationName = ApplicationName,
|
|
});
|
|
}
|
|
|
|
|
|
public static string getFolder(DriveService service, string path, string parentFolderId)
|
|
{
|
|
Debug.Log("Getting/Creating folder path: " + path);
|
|
|
|
List<string> folders = new List<string>(path.Split('/'));
|
|
string currentFolderId = parentFolderId;
|
|
|
|
foreach (string folder in folders)
|
|
{
|
|
var request = service.Files.List();
|
|
request.Q = $"mimeType='application/vnd.google-apps.folder' and name='{folder}' and '{currentFolderId}' in parents and trashed=false";
|
|
request.Fields = "files(id, name)";
|
|
request.SupportsAllDrives = true;
|
|
request.IncludeItemsFromAllDrives = true;
|
|
|
|
var result = request.Execute();
|
|
if (result.Files.Count > 0)
|
|
{
|
|
currentFolderId = result.Files[0].Id;
|
|
Debug.Log("Found folder: " + folder + " with ID: " + currentFolderId);
|
|
}
|
|
else
|
|
{
|
|
// Folder does not exist, create it
|
|
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
|
|
{
|
|
Name = folder,
|
|
MimeType = "application/vnd.google-apps.folder",
|
|
Parents = new List<string> { currentFolderId }
|
|
};
|
|
var createRequest = service.Files.Create(fileMetadata);
|
|
createRequest.Fields = "id";
|
|
createRequest.SupportsAllDrives = true;
|
|
|
|
var file = createRequest.Execute();
|
|
currentFolderId = file.Id;
|
|
}
|
|
}
|
|
|
|
return currentFolderId;
|
|
}
|
|
|
|
public static async Task upload(DriveService service, string _uploadFile, string folderId, string _fileId, string type)
|
|
{
|
|
Debug.Log("Uploading file to Google Drive: " + _uploadFile);
|
|
|
|
if (System.IO.File.Exists(_uploadFile))
|
|
{
|
|
// File body = new File();
|
|
// body.Name = System.IO.Path.GetFileName(_uploadFile);
|
|
// body.Description = "File updated by Diamto Drive Sample";
|
|
// body.MimeType = "image/png";
|
|
|
|
// byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
|
|
// System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
|
|
try
|
|
{
|
|
|
|
// Replace with your folder ID
|
|
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
|
|
{
|
|
Name = Path.GetFileName(_uploadFile),
|
|
Parents = new List<string>
|
|
{
|
|
folderId
|
|
}
|
|
};
|
|
|
|
Debug.Log("metadata prepared." + fileMetadata);
|
|
// FilesResource.CreateMediaUpload request;
|
|
// Create a new file on drive.
|
|
using (var stream = new FileStream(_uploadFile, FileMode.Open))
|
|
{
|
|
// Create a new file, with metadata and stream.
|
|
|
|
var request = service.Files.Create(fileMetadata, stream, "image/png");
|
|
request.Fields = "id, name";
|
|
request.SupportsAllDrives = true;
|
|
|
|
request.ProgressChanged += (progress) => Debug.Log($"Upload Progress: {progress.Status} - Bytes Sent: {progress.BytesSent} {progress.Exception?.Message}");
|
|
request.ResponseReceived += (file) => Debug.Log($"File uploaded successfully: {file.Name} (ID: {file.Id})");
|
|
|
|
|
|
var results = await request.UploadAsync();
|
|
|
|
if (results.Status == UploadStatus.Failed)
|
|
{
|
|
// TODO: handle a file upload error.
|
|
Console.WriteLine($"Error uploading file: {results.Exception.Message}");
|
|
}
|
|
// Debug.Log("request prepared." + request);
|
|
|
|
// var file = request.ResponseBody;
|
|
|
|
// if(file==null)
|
|
// {
|
|
// Debug.LogError("Upload failed, file is null.");
|
|
// return;
|
|
// }
|
|
// string fileId= file.Id;
|
|
// string fileUrl = $"https://drive.google.com/file/d/{fileId}/view";
|
|
// Debug.Log("File URL: " + fileUrl);
|
|
// Debug.Log("File ID: " + fileId);
|
|
}
|
|
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError("An error occurred: " + e.Message + " " + e.StackTrace);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("File does not exist: " + _uploadFile);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|