internal static PGDriveResult <IEnumerable <Permission> > GetFilePermissions(string fileId, DriveService service) { PGDriveResult <IEnumerable <Permission> > driveResult = new PGDriveResult <IEnumerable <Permission> >(); try { var result = service.Permissions.List(fileId); result.Fields = "permissions(id, emailAddress, domain, type, role, displayName, kind)"; var Request = result.Execute(); driveResult.SetResponseBody(Request.Permissions); return(driveResult); } catch (GoogleApiException exception) { driveResult.InitializeError(exception); return(driveResult); } }
/// <summary> /// Creates a comment to a file /// </summary> /// <param name="fileId">Id of a file</param> /// <param name="body">Metadata of a comment</param> /// <returns>PGDriveResult with a created comment in response body</returns> public PGDriveResult <Comment> CreateComment(string fileId, Comment body) { PGDriveResult <Comment> pGDriveResult = new PGDriveResult <Comment>(); try { var createRequest = driveService.Comments.Create(body, fileId); createRequest.Fields = DefaultCommentFields; var result = createRequest.Execute(); pGDriveResult.SetResponseBody(result); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Creates a comment to a file /// </summary> /// <param name="fileId">Id of a file</param> /// <param name="commentId">Id of a comment</param> /// <param name="body">Body of future reply</param> /// <returns>PGDriveResult with create reply in response body</returns> public PGDriveResult <Reply> CreateReply(string fileId, string commentId, Reply body) { PGDriveResult <Reply> pGDriveResult = new PGDriveResult <Reply>(); try { var createRequest = driveService.Replies.Create(body, fileId, commentId); createRequest.Fields = DefaultReplyFields; var result = createRequest.Execute(); pGDriveResult.SetResponseBody(result); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Gets reply by id /// </summary> /// <param name="fileId">Id of a fiile</param> /// <param name="commentId">Id of a comment</param> /// <param name="replyId">Id of a reply</param> /// <returns>PGDriveResult with getted reply</returns> public PGDriveResult <Reply> GetReplyById(string fileId, string commentId, string replyId) { PGDriveResult <Reply> pGDriveResult = new PGDriveResult <Reply>(); try { var getReplyRequest = driveService.Replies.Get(fileId, commentId, replyId); getReplyRequest.Fields = DefaultReplyFields; var reply = getReplyRequest.Execute(); pGDriveResult.SetResponseBody(reply); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Gets comment by id /// </summary> /// <param name="fileId">Id of a file</param> /// <param name="commentId">Id of a comment</param> /// <returns>PGDriveResult with a comment in response body</returns> public PGDriveResult <Comment> GetCommentById(string fileId, string commentId) { PGDriveResult <Comment> pGDriveResult = new PGDriveResult <Comment>(); try { var getCommentRequest = driveService.Comments.Get(fileId, commentId); getCommentRequest.Fields = DefaultCommentFields; var comment = getCommentRequest.Execute(); pGDriveResult.SetResponseBody(comment); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Uploads a fiile to a drive /// </summary> /// <param name="filePath">Path to file which you want to upload</param> /// <param name="name">Future name of the file</param> /// <param name="parentFolderId"></param> /// <returns>PGDriveResult which contains created folder metadata in response body</returns> public PGDriveResult <File> CreateFile(string filePath, string name = null, string parentFolderId = null) { PGDriveResult <File> pGDriveResult = new PGDriveResult <File>(); try { FilesResource.CreateMediaUpload request; filePath = System.IO.Path.GetFullPath(filePath); using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { var fileMetadata = new File(); if (name != null) { fileMetadata.Name = name; } if (parentFolderId != null) { var folder = driveService.Files.Get(parentFolderId).Execute(); if (!folder.MimeType.Contains("folder")) { throw CustomExceptions.isNotAFolder(parentFolderId, driveService); } fileMetadata.Parents = new List <string>() { parentFolderId }; } string mimeType = MimeMapping.GetMimeMapping(filePath); request = driveService.Files.Create(fileMetadata, stream, mimeType); request.Fields = DefaultFileFieldsOnResponse; request.Upload(); } var file = request.ResponseBody; pGDriveResult.SetResponseBody(file); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Gets file by it id /// </summary> /// <param name="fileOrFolderId">Id of file or folder</param> /// <returns></returns> public PGDriveResult <File> GetFileById(string fileOrFolderId) { List <File> files = new List <File>(); PGDriveResult <File> pGDriveResult = new PGDriveResult <File>(); FilesResource.ListRequest listRequest = driveService.Files.List(); try { var request = driveService.Files.Get(fileOrFolderId); request.Fields = DefaultFileFieldsOnResponse; var result = request.Execute(); pGDriveResult.SetResponseBody(result); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Updates a reply /// </summary> /// <param name="fileId">Id of a fiile</param> /// <param name="commentId">Id of a comment</param> /// <param name="replyId">Id of a reply</param> /// <param name="content">Future text of a reply to update</param> /// <returns>PGDriveResult with updated reply</returns> public PGDriveResult <Reply> UpdateReply(string fileId, string commentId, string replyId, string content) { PGDriveResult <Reply> pGDriveResult = new PGDriveResult <Reply>(); try { var updateReplyRequest = driveService.Replies.Update(new Reply() { Content = content }, fileId, commentId, replyId); updateReplyRequest.Fields = DefaultReplyFields; var reply = updateReplyRequest.Execute(); pGDriveResult.SetResponseBody(reply); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Copy file and optional change it's metadata /// </summary> /// <param name="fileId">Id of file to copy</param> /// <param name="bodyChanges">Changed file metadata</param> /// <returns>PGDriveResult with a bool which shows did file was copied in response body</returns> public PGDriveResult <bool> CopyFile(string fileId, File bodyChanges = null) { PGDriveResult <bool> pGDriveResult = new PGDriveResult <bool>(); try { if (bodyChanges == null) { bodyChanges = new File(); } var request = driveService.Files.Copy(bodyChanges, fileId); request.Execute(); pGDriveResult.SetResponseBody(true); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Adds parents to a file /// </summary> /// <param name="fileOrFolderId">Id of a file or folder to adds parents to</param> /// <param name="parents">Collection of folders Id's which would be added as parents to a file</param> /// <returns>PGDriveResult contains file with added parents in response body</returns> public PGDriveResult <File> AddParents(string fileOrFolderId, IList <string> parents) { PGDriveResult <File> pGDriveResult = new PGDriveResult <File>(); try { var getRequest = driveService.Files.Get(fileOrFolderId); getRequest.Fields = DefaultFileFieldsOnResponse; var file = getRequest.Execute(); string parentsToDelete = String.Join(",", parents); var updateRequest = driveService.Files.Update(new File(), fileOrFolderId); updateRequest.Fields = DefaultFileFieldsOnResponse; updateRequest.AddParents = parentsToDelete; file = updateRequest.Execute(); pGDriveResult.SetResponseBody(file); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
/// <summary> /// Get replies from specific comment /// </summary> /// <param name="fileId">Id of a file</param> /// <param name="commentId">Id of a comment</param> /// <param name="maxRepliesCount">Maximum count of replies you need</param> /// <returns>PGDriveResult with collection of getted replies in response body</returns> public PGDriveResult <IList <Reply> > GetReplies(string fileId, string commentId, int maxRepliesCount = DefaultMaxRepliesCount) { if (maxRepliesCount <= 0 && maxRepliesCount != DefaultMaxRepliesCount) { maxRepliesCount = DefaultMaxRepliesCount; } List <Reply> replies = new List <Reply>(); PGDriveResult <IList <Reply> > pGDriveResult = new PGDriveResult <IList <Reply> >(); var listRequest = driveService.Replies.List(fileId, commentId); try { listRequest.Fields = DefaultGetFieldsOnResponse; listRequest.PageSize = DefaultReplySizePerRequest; if (maxRepliesCount != DefaultMaxRepliesCount) { if (maxRepliesCount < DefaultReplySizePerRequest) { listRequest.PageSize = maxRepliesCount; maxRepliesCount = 0; } else { maxRepliesCount -= DefaultReplySizePerRequest; } } var result = listRequest.Execute(); if (result.Replies != null) { replies.AddRange(result.Replies); if (result.Replies.Count < DefaultReplySizePerRequest) { maxRepliesCount = 0; } } while (maxRepliesCount != 0) { if (!string.IsNullOrWhiteSpace(result.NextPageToken)) { listRequest = driveService.Replies.List(fileId, commentId); listRequest.PageToken = result.NextPageToken; listRequest.PageSize = DefaultReplySizePerRequest; listRequest.Fields = DefaultGetFieldsOnResponse; if (maxRepliesCount != DefaultMaxRepliesCount) { if (maxRepliesCount < DefaultReplySizePerRequest) { listRequest.PageSize = maxRepliesCount; maxRepliesCount = 0; } else { maxRepliesCount -= DefaultReplySizePerRequest; } } result = listRequest.Execute(); if (result.Replies != null) { replies.AddRange(result.Replies); } } else { break; } } pGDriveResult.SetResponseBody(replies); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }
private PGDriveResult <IList <File> > GetFiles(string Q = null, int maxFilesCount = DefaultMaxFilesCount) { if (maxFilesCount <= 0 && maxFilesCount != DefaultMaxFilesCount) { maxFilesCount = DefaultMaxFilesCount; } List <File> files = new List <File>(); PGDriveResult <IList <File> > pGDriveResult = new PGDriveResult <IList <File> >(); FilesResource.ListRequest listRequest = driveService.Files.List(); try { listRequest.Fields = DefaultGetFieldsOnResponse; listRequest.PageSize = DefaultPageSizePerRequest; if (Q != null) { listRequest.Q = Q; } if (maxFilesCount != DefaultMaxFilesCount) { if (maxFilesCount < DefaultPageSizePerRequest) { listRequest.PageSize = maxFilesCount; maxFilesCount = 0; } else { maxFilesCount -= DefaultPageSizePerRequest; } } var result = listRequest.Execute(); if (result.Files != null) { files.AddRange(result.Files); if (result.Files.Count < DefaultPageSizePerRequest) { maxFilesCount = 0; } } while (maxFilesCount != 0) { if (!string.IsNullOrWhiteSpace(result.NextPageToken)) { listRequest = driveService.Files.List(); listRequest.PageToken = result.NextPageToken; listRequest.PageSize = DefaultPageSizePerRequest; listRequest.Fields = DefaultGetFieldsOnResponse; if (Q != null) { listRequest.Q = Q; } if (maxFilesCount != DefaultMaxFilesCount) { if (maxFilesCount < DefaultPageSizePerRequest) { listRequest.PageSize = maxFilesCount; maxFilesCount = 0; } else { maxFilesCount -= DefaultPageSizePerRequest; } } result = listRequest.Execute(); if (result.Files != null) { files.AddRange(result.Files); } } else { break; } } pGDriveResult.SetResponseBody(files); return(pGDriveResult); } catch (Google.GoogleApiException exception) { pGDriveResult.InitializeError(exception); return(pGDriveResult); } }