public static void downloadFromDrive(string filename, string fileId, string savePath, string mimeType, frmMain parentForm) { frmMain parent = parentForm; long totalSize = 100000; parent.updateStatusBar(0, "Downloading..."); try { if (Path.HasExtension(filename)) { var request = driveService.Files.Get(fileId); var stream = new System.IO.MemoryStream(); System.Diagnostics.Debug.WriteLine(fileId); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { System.Diagnostics.Debug.WriteLine(progress.BytesDownloaded); parent.updateStatusBar((progress.BytesDownloaded * 100) / totalSize, "Downloading..."); break; } case DownloadStatus.Completed: { parent.updateStatusBar(100, "Download complete."); System.Diagnostics.Debug.WriteLine("Download complete."); break; } case DownloadStatus.Failed: { parent.updateStatusBar(0, "Download failed."); System.Diagnostics.Debug.WriteLine("Download failed."); MessageBox.Show("File failed to download!!!", "Download Message", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } } }; request.Download(stream); convertMemoryStreamToFileStream(stream, savePath + @"\" + @filename); stream.Dispose(); } else { string extension = "", converter = ""; foreach (MimeTypeConvert obj in MimeConverter.mimeList()) { if (mimeType == obj.mimeType) { extension = obj.extension; converter = obj.converterType; } } System.Diagnostics.Debug.WriteLine("{0} {1} {2}", fileId, extension, mimeType); var request = driveService.Files.Export(fileId, converter); var stream = new System.IO.MemoryStream(); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { Console.WriteLine(progress.BytesDownloaded); break; } case DownloadStatus.Completed: { parent.updateStatusBar(100, "Download Complete!"); Console.WriteLine("Download complete."); break; } case DownloadStatus.Failed: { parent.updateStatusBar(0, "Download failed."); Console.WriteLine("Download failed."); MessageBox.Show("File failed to download!!!", "Download Message", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } } }; request.Download(stream); convertMemoryStreamToFileStream(stream, savePath + @"\" + @filename + extension); stream.Dispose(); } } catch (Exception exc) { System.Diagnostics.Debug.WriteLine(exc.Message + " Download From Drive Error"); Gtools.writeToFile(frmMain.errorLog, Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + exc.Message + " Download From Drive.\n"); } }
public static void directoryUpload(string path, string parentId, bool onlyNew, frmMain parentForm) { try { // Get the subdirectories for the specified directory. string folderId = createFolderToDrive( Path.GetFileName(path), parentId); System.Diagnostics.Debug.WriteLine(folderId); DirectoryInfo dir = new DirectoryInfo(path); if (!dir.Exists) { throw new DirectoryNotFoundException( "Source directory does not exist or could not be found: " + path); } FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { uploadFileToDrive( folderId, file.Name, Path.Combine(path, file.Name), getMimeType(file.Name), onlyNew, parentForm); } DirectoryInfo[] dirs = dir.GetDirectories(); foreach (DirectoryInfo subdir in dirs) { directoryUpload(subdir.FullName, folderId, onlyNew, parentForm); } } catch (Exception exc) { System.Diagnostics.Debug.WriteLine(exc.Message + " Directory upload Error"); Gtools.writeToFile(frmMain.errorLog, Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + exc.Message + " Directory upload Error.\n"); } }
private static bool uploadFileToDrive(string folderId, string fileName, string filePath, string fileType, bool onlyNew, frmMain parentForm) { if (onlyNew) { if (!compareHash(Gtools.hashGenerator(filePath))) { uploadFileToDrive(folderId, fileName, filePath, fileType, parentForm); return(true); } else { return(false); } } else { uploadFileToDrive(folderId, fileName, filePath, fileType, parentForm); return(true); } }
public static void uploadToDrive(string path, string name, string parentId, bool onlyNew, frmMain parentForm) { if (Path.HasExtension(path)) { uploadFileToDrive( parentId, name, path, getMimeType(Path.GetFileName(path)), onlyNew, parentForm); } else { directoryUpload(path, parentId, onlyNew, parentForm); } }
private static bool uploadFileToDrive(string folderId, string fileName, string filePath, string fileType, frmMain parentForm) { frmMain parent = parentForm; parent.updateStatusBar(0, "Uploading..."); long totalSize = 100000; try { FileInfo fi = new FileInfo(filePath); totalSize = fi.Length; var fileMetadata = new Google.Apis.Drive.v3.Data.File() { Name = fileName }; if (folderId != null) { fileMetadata.Parents = new List <string> { folderId }; } FilesResource.CreateMediaUpload request; using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open)) { request = driveService.Files.Create( fileMetadata, stream, fileType); request.ChunkSize = FilesResource.CreateMediaUpload.MinimumChunkSize; request.ProgressChanged += (IUploadProgress progress) => { switch (progress.Status) { case UploadStatus.Uploading: { parent.updateStatusBar((progress.BytesSent * 100) / totalSize, "Uploading..."); System.Diagnostics.Debug.WriteLine(progress.BytesSent); break; } case UploadStatus.Completed: { parent.updateStatusBar(100, "Upload complete."); System.Diagnostics.Debug.WriteLine("Upload complete."); break; } case UploadStatus.Failed: { parent.updateStatusBar(0, "Upload failed."); System.Diagnostics.Debug.WriteLine("Upload failed."); //MessageBox.Show("File failed to upload!!!", "Upload Message", MessageBoxButtons.OK, MessageBoxIcon.Error); Gtools.writeToFile(frmMain.errorLog, Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + "Upload failed.\n"); break; } } }; request.Fields = "id"; request.Upload(); } var file = request.ResponseBody; System.Diagnostics.Debug.WriteLine("File ID:{0} \n FileName {1} ", file.Id, file.Name); return(true); } catch (Exception exc) { System.Diagnostics.Debug.WriteLine(exc.Message + " Upload file to Drive Error"); Gtools.writeToFile(frmMain.errorLog, Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + exc.Message + " Upload file to Drive Error.\n"); return(false); } }