/// <summary> /// Updates a file on Google Drive: GooglDriveService.Files.Update /// Requirements: A Valid authenticated DriveService path to the file to upload /// If upload succeeded returns the File resource of the uploaded file /// If the upload fails returns null /// </summary> /// <param name="service"></param> /// <param name="uploadFile"></param> /// <param name="parentId"></param> /// <param name="fileId">The resource id for the file we would like to update</param> /// <param name="fileDescription"></param> /// <returns></returns> public GoogleDriveManagerResult UpdateFile(DriveService service, string uploadFile, string parentId, string fileId, string fileDescription) { result = new GoogleDriveManagerResult(); try { if (System.IO.File.Exists(uploadFile)) { var mimeType = GoogleDriveMimeService.GetFileMimeType(uploadFile); Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File() { Name = System.IO.Path.GetFileName(uploadFile), Description = fileDescription, MimeType = mimeType, Parents = new List <string> { parentId } }; byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile); MemoryStream stream = new MemoryStream(byteArray); FilesResource.UpdateMediaUpload request = service.Files.Update(body, fileId, stream, mimeType); request.ProgressChanged += Request_ProgressChanged; request.ResponseReceived += Request_ResponseReceived; request.Upload(); Google.Apis.Drive.v3.Data.File response = request.ResponseBody; if (response != null) { result.GoogleDriveFileId = response.Id; result.IsGoogleDriveFileUpdated = true; } else { result.IsGoogleDriveFileUpdated = false; result.Errors.Add(GoogleDriveManagementConstants.FileNotUpdatedError); } } else { result.Errors.Add(GoogleDriveManagementConstants.FileOnDiskDoesNotExistError); } return(result); } catch (Exception e) { WriteToConsole(GoogleDriveManagementConstants.UpdateFileException + e.Message); result.Exceptions.Add(e); return(result); } }
private Google.Apis.Drive.v3.Data.File GetFileMetaData(LocalGoogleDriveFile localGoogleDriveFile, IList <string> parents = null) { var fileMetaData = new Google.Apis.Drive.v3.Data.File(); fileMetaData.Name = Path.GetFileName(Path.GetFileName(localGoogleDriveFile.FilePath)); if (parents != null) { fileMetaData.Parents = parents; } var mimeType = System.Web.MimeMapping.GetMimeMapping(localGoogleDriveFile.FilePath); if (string.IsNullOrEmpty(mimeType)) { fileMetaData.MimeType = GoogleDriveMimeService.GetFileMimeType(localGoogleDriveFile.FilePath); } Dictionary <string, string> properties = new Dictionary <string, string>(); properties.Add("LocalId", localGoogleDriveFile.LocalId); properties.Add("AppVersion", localGoogleDriveFile.AppVersion); properties.Add("DisplayName", localGoogleDriveFile.DisplayName); fileMetaData.AppProperties = properties; return(fileMetaData); }
/// <summary> /// Upload a batch load of files in a google drive folder /// </summary> /// <param name="service"></param> /// <param name="folder"></param> /// <param name="files"></param> /// <returns></returns> public GoogleDriveManagerResult UploadBatchFilesToFolder(DriveService service, GoogleDriveFile folder, List <LocalGoogleDriveFile> files) { result = new GoogleDriveManagerResult(); try { GoogleDriveManagerResult googleDriveManagerResult = GetFolderById(service, folder.Id); if ((googleDriveManagerResult != null) && (googleDriveManagerResult.IsValidGoogleDriveManagerResult)) { List <LocalGoogleDriveFile> uploadedLocalGoogleDriveFiles = new List <LocalGoogleDriveFile>(); var parents = new List <string>(); parents.Add(googleDriveManagerResult.GoogleDriveFileId); foreach (LocalGoogleDriveFile localGoogleDriveFile in files) { FilesResource.CreateMediaUpload request; using (var stream = new System.IO.FileStream(localGoogleDriveFile.FilePath, System.IO.FileMode.Open)) { var fileMetadata = GetFileMetaData(localGoogleDriveFile, parents); request = service.Files.Create(fileMetadata, stream, GoogleDriveMimeService.GetFileMimeType(localGoogleDriveFile.FilePath)); request.Fields = "id"; request.Upload(); } var uploadedFile = request.ResponseBody; localGoogleDriveFile.CloudId = uploadedFile.Id; uploadedLocalGoogleDriveFiles.Add(localGoogleDriveFile); WriteToConsole(GoogleDriveManagementConstants.FileUploadedMessage + uploadedFile.Id); } result.LocalGoogleDriveFiles = uploadedLocalGoogleDriveFiles; } else { WriteToConsole(GoogleDriveManagementConstants.FolderNotFound + folder.Id); result.Errors.Add(GoogleDriveManagementConstants.FolderNotFound + folder.Id); } return(result); } catch (Exception e) { WriteToConsole(GoogleDriveManagementConstants.UploadingBatchFilesToFolderException + e.Message); result.Exceptions.Add(e); return(result); } }