/// <summary> /// Used to download the file at the given path from Google Drive. /// </summary> /// <param name="filePath">The Drive path of the file to download</param> /// <returns>A stream for the downloaded file or null if the file couldn't be found</returns> public MemoryStream GetGoogleDriveFileStream(string filePath) { // Get the file with the given path GoogleDriveData.File file = GetGoogleDriveFile(filePath); if (file == null) { return(null); } // Return the stream for the file return(GetGoogleDriveFileStream(file)); }
/// <summary> /// Used to download the given file from Google Drive. /// </summary> /// <param name="file">The file to download</param> /// <returns>A stream for the downloaded file or null if the file couldn't be found</returns> public MemoryStream GetGoogleDriveFileStream(GoogleDriveData.File file) { // Get the Google Drive service DriveService driveService = GetDriveService(); // Prepare the download request FilesResource.GetRequest getRequest = driveService.Files.Get(file.Id); getRequest.MediaDownloader.ProgressChanged += GetGoogleDriveFileStream_ProgressChanged; // Download the file MemoryStream downloadStream = new MemoryStream(); getRequest.DownloadWithStatus(downloadStream); // Go back to the start of the stream downloadStream.Seek(0, SeekOrigin.Begin); return(downloadStream); }
/// <summary> /// Used to get the Google Drive file at the specified path. /// </summary> /// <param name="filePath">The path of a file on Google Drive</param> /// <returns>The requested file or null if it couldn't be found</returns> public GoogleDriveData.File GetGoogleDriveFile(string filePath) { // Get the Drive service DriveService service = GetDriveService(); // Escape any apostrophes in the file path filePath = filePath.Replace("'", "\\'"); // Split the file path at forward slashes string[] fileSections = filePath.Split('/'); // Go through each file section, working our way down the file path string parentID = null; GoogleDriveData.File file = null; for (int sectionIndex = 0; sectionIndex < fileSections.Length; sectionIndex++) { string section = fileSections[sectionIndex]; // If this is the last file section then that means it's the file name bool sectionIsFile = (sectionIndex == fileSections.Length - 1); // Create the request to find the folder/file FilesResource.ListRequest listRequest = service.Files.List(); // Set the query for the list request if (sectionIsFile) { listRequest.Q = "name = '" + section + "'"; } else { listRequest.Q = "mimeType = 'application/vnd.google-apps.folder' and name = '" + section + "'"; } // If the parent ID isn't null, then add it to the list request query if (parentID != null) { listRequest.Q += " and '" + parentID + "' in parents"; } // Do the request GoogleDriveData.FileList fileList = listRequest.Execute(); // If no files/folders were found then that means the file must not exist if (fileList.Files == null || fileList.Files.Count == 0) { break; } // If the file returned is a file then we have found what we are looking for GoogleDriveData.File currentFileOrFolder = fileList.Files.First(); if (sectionIsFile) { file = currentFileOrFolder; break; } else { // Get the ID of the current folder parentID = currentFileOrFolder.Id; } } return(file); }