|
|
|
|
@ -15,12 +15,13 @@ 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"; |
|
|
|
|
// static string GoogleRawFileUrl = "https://drive.google.com/uc?id="; |
|
|
|
|
static string GoogleRawFileUrl = "https://drive.usercontent.google.com/download?id="; |
|
|
|
|
|
|
|
|
|
public static DriveService service; |
|
|
|
|
|
|
|
|
|
@ -90,82 +91,67 @@ public class GoogleDriveUtils |
|
|
|
|
return currentFolderId; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static async Task upload(DriveService service, string _uploadFile, string folderId, string _fileId, string type) |
|
|
|
|
public static IEnumerator UploadCoroutine(DriveService service, string _uploadFile, string folderId, string _fileId, string type, System.Action<string> onComplete) |
|
|
|
|
{ |
|
|
|
|
Debug.Log("Uploading file to Google Drive: " + _uploadFile); |
|
|
|
|
|
|
|
|
|
if (System.IO.File.Exists(_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 |
|
|
|
|
{ |
|
|
|
|
Debug.LogError("File does not exist: " + _uploadFile); |
|
|
|
|
onComplete?.Invoke(null); |
|
|
|
|
yield break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Replace with your folder ID |
|
|
|
|
var fileMetadata = new Google.Apis.Drive.v3.Data.File() |
|
|
|
|
{ |
|
|
|
|
Name = Path.GetFileName(_uploadFile), |
|
|
|
|
Parents = new List<string> |
|
|
|
|
{ |
|
|
|
|
folderId |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
Google.Apis.Drive.v3.Data.File 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. |
|
|
|
|
Debug.Log("Metadata prepared: " + fileMetadata.Name); |
|
|
|
|
|
|
|
|
|
var request = service.Files.Create(fileMetadata, stream, "image/png"); |
|
|
|
|
request.Fields = "id, name"; |
|
|
|
|
request.SupportsAllDrives = true; |
|
|
|
|
using (var stream = new FileStream(_uploadFile, FileMode.Open)) |
|
|
|
|
{ |
|
|
|
|
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})"); |
|
|
|
|
|
|
|
|
|
request.ProgressChanged += (progress) => |
|
|
|
|
{ |
|
|
|
|
Debug.Log($"Upload Progress: {progress.Status} - Bytes Sent: {progress.BytesSent} {progress.Exception?.Message}"); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var results = await request.UploadAsync(); |
|
|
|
|
request.ResponseReceived += (file) => |
|
|
|
|
{ |
|
|
|
|
Debug.Log($"File uploaded successfully: {file.Name} (ID: {file.Id})"); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
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 uploadTask = request.UploadAsync(); |
|
|
|
|
|
|
|
|
|
// var file = request.ResponseBody; |
|
|
|
|
// Wait for the upload to complete |
|
|
|
|
while (!uploadTask.IsCompleted) |
|
|
|
|
{ |
|
|
|
|
yield return null; // pause and continue next frame |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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); |
|
|
|
|
} |
|
|
|
|
if (uploadTask.IsFaulted || uploadTask.IsCanceled) |
|
|
|
|
{ |
|
|
|
|
Debug.LogError("Upload failed: " + uploadTask.Exception?.Message); |
|
|
|
|
onComplete?.Invoke(null); |
|
|
|
|
yield break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var fileResult = request.ResponseBody; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
catch (Exception e) |
|
|
|
|
if (fileResult == null) |
|
|
|
|
{ |
|
|
|
|
Debug.LogError("An error occurred: " + e.Message + " " + e.StackTrace); |
|
|
|
|
Debug.LogError("Upload failed, file is null."); |
|
|
|
|
onComplete?.Invoke(null); |
|
|
|
|
yield break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
Debug.LogError("File does not exist: " + _uploadFile); |
|
|
|
|
|
|
|
|
|
string fileUrl = $"{GoogleRawFileUrl}{fileResult.Id}"; |
|
|
|
|
onComplete?.Invoke(fileUrl); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|