Exemple #1
0
        /// <summary>
        /// Removes all parents form a file or folder
        /// </summary>
        /// <param name="fileOrFolderId">Id of file or folder</param>
        /// <returns>PGDriveResult contains file with removed parents in response body</returns>
        public PGDriveResult <File> RemoveAllParents(string fileOrFolderId)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                var getRequest = driveService.Files.Get(fileOrFolderId);
                getRequest.Fields = DefaultFileFieldsOnResponse;
                var file = getRequest.Execute();
                if (file.Parents == null)
                {
                    pGDriveResult.SetResponseBody(file);
                    return(pGDriveResult);
                }
                string previousParents = String.Join(",", file.Parents);
                var    updateRequest   = driveService.Files.Update(new File(), fileOrFolderId);
                updateRequest.Fields        = DefaultFileFieldsOnResponse;
                updateRequest.RemoveParents = previousParents;
                file = updateRequest.Execute();
                pGDriveResult.SetResponseBody(file);
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #2
0
        /// <summary>
        /// Updates a file
        /// </summary>
        /// <param name="fileId">Id of file</param>
        /// <param name="stream">Stream of a file which would be a content to update</param>
        /// <param name="contentType">Mime type of a file stream</param>
        /// <param name="body">Optional body with metadata file</param>
        /// <returns>PGDriveResult with updated file in response body</returns>
        public PGDriveResult <File> UpdateFile(string fileId, System.IO.Stream stream, string contentType, File body = null)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                var getFileRequest = driveService.Files.Get(fileId);
                getFileRequest.Fields = DefaultFileFieldsOnResponse;
                var file = getFileRequest.Execute();
                if (body == null)
                {
                    body = file;
                }
                using (stream)
                {
                    var request = driveService.Files.Update(body, fileId, stream, contentType);
                    var res     = request.Upload();
                }
                getFileRequest        = driveService.Files.Get(fileId);
                getFileRequest.Fields = DefaultFileFieldsOnResponse;
                file = getFileRequest.Execute();
                pGDriveResult.SetResponseBody(file);
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #3
0
        /// <summary>
        /// Updates a file
        /// </summary>
        /// <param name="fileId">Id of a file</param>
        /// <param name="filePath">Path to a file which would be a content to update</param>
        /// <param name="name">Future name of a file</param>
        /// <param name="mimeType">Mime type of a file</param>
        /// <param name="isStarred">Would be file trashed</param>
        /// <param name="isTrashed">Would be file starred</param>
        /// <returns>PGDriveResult with updated file in response body</returns>
        public PGDriveResult <File> UpdateFile(string fileId, string filePath = null, string name = null, string mimeType = null, bool?isStarred = null, bool?isTrashed = null)
        {
            File body = new File();

            if (name != null)
            {
                body.Name = name;
            }
            if (mimeType != null)
            {
                body.MimeType = mimeType;
            }
            if (isStarred != null)
            {
                body.Starred = isStarred;
            }
            if (isTrashed != null)
            {
                body.Trashed = isTrashed;
            }
            PGDriveResult <File> result = new PGDriveResult <File>();

            if (filePath != null)
            {
                filePath = System.IO.Path.GetFullPath(filePath);
                result   = UpdateFile(fileId, body, filePath);
            }
            else
            {
                result = UpdateFile(fileId, body);
            }
            return(result);
        }
Exemple #4
0
        /// <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="replyId">Id of reply to delete</param>
        /// <returns>PGDriveResult with bool which shows did reply was deleted or not</returns>
        public PGDriveResult <bool> DeleteReply(string fileId, string commentId, string replyId)
        {
            PGDriveResult <bool> pGDriveResult = new PGDriveResult <bool>();

            try
            {
                var createRequest = driveService.Replies.Delete(fileId, commentId, replyId);
                createRequest.Fields = DefaultReplyFields;
                var result = createRequest.Execute();
                if (string.IsNullOrEmpty(result))
                {
                    pGDriveResult.SetResponseBody(true);
                }
                else
                {
                    pGDriveResult.SetResponseBody(false);
                }
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #5
0
        /// <summary>
        /// Moves file from one folder to another
        /// </summary>
        /// <param name="fileOrFolderId">Id of file or folder which yu want to move</param>
        /// <param name="newFolderId">Id of folder to which you want to move a file</param>
        /// <returns>PGDriveResult with moved file in response body</returns>
        public PGDriveResult <File> MoveFileToAntherFolder(string fileOrFolderId, string newFolderId)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                var getRequest       = driveService.Files.Get(fileOrFolderId);
                var folderGetRequest = driveService.Files.Get(newFolderId);
                folderGetRequest.Fields = "mimeType";
                var folder = folderGetRequest.Execute();
                if (!folder.MimeType.Contains("folder"))
                {
                    throw CustomExceptions.isNotAFolder(newFolderId, driveService);
                }
                getRequest.Fields = "parents";
                var file            = getRequest.Execute();
                var previousParents = String.Join(",", file.Parents);
                var updateRequest   = driveService.Files.Update(new File(), fileOrFolderId);
                updateRequest.Fields        = "id, parents";
                updateRequest.AddParents    = newFolderId;
                updateRequest.RemoveParents = previousParents;
                file = updateRequest.Execute();
                pGDriveResult.SetResponseBody(file);
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #6
0
        protected override PGDriveResult <Permission> CreateFilePermission(string fileId, DriveService service, Permission permission)
        {
            var resultPermissions = GetFilePermissions(fileId, service);

            if (resultPermissions.isSuccess == false)
            {
                return(resultPermissions.CopyResult <Permission>());
            }
            //
            var existPermission = resultPermissions.ResponseBody.Any(p => p.Role == permission.Role && p.Type == permission.Type);
            PGDriveResult <Permission> pGDriveResult = new PGDriveResult <Permission>();

            try
            {
                var result = service.Permissions.Create(permission, fileId);
                result.Fields = "id, emailAddress, domain, type, role, displayName, kind";
                var Request = result.Execute();
                pGDriveResult.SetResponseBody(Request);
                return(pGDriveResult);
            }
            catch (GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #7
0
        /// <summary>
        /// Uploads a file to a drive
        /// </summary>
        /// <param name="stream">Stream of your file to upload</param>
        /// <param name="contentType">MimeType of a file</param>
        /// <param name="body">Metadata of future file</param>
        /// <param name="parentFolderId">Id of parent folder for a new file</param>
        /// <returns>PGDriveResult which contains uploaded file metadata in response body</returns>
        public PGDriveResult <File> CreateFile(System.IO.Stream stream, string contentType, File body, string parentFolderId = null)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                FilesResource.CreateMediaUpload request;


                if (parentFolderId != null)
                {
                    var folder = driveService.Files.Get(parentFolderId).Execute();
                    if (!folder.MimeType.Contains("folder"))
                    {
                        throw CustomExceptions.isNotAFolder(parentFolderId, driveService);
                    }
                    body.Parents = new List <string>()
                    {
                        parentFolderId
                    };
                }
                request        = driveService.Files.Create(body, stream, contentType);
                request.Fields = DefaultFileFieldsOnResponse;
                request.Upload();
                var file = request.ResponseBody;
                pGDriveResult.SetResponseBody(file);
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #8
0
        /// <summary>
        /// Creates a folder at the drive
        /// </summary>
        /// <param name="name">Name of folder</param>
        /// <param name="parentFolderId">Id of parent folder for new folder</param>
        /// <returns>PGDriveResult which contains created folder metadata in response body</returns>
        public PGDriveResult <File> CreateFolder(string name, string parentFolderId = null)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                FilesResource.CreateRequest request;
                var fileMetadata = new File();
                fileMetadata.Name     = name;
                fileMetadata.MimeType = "application/vnd.google-apps.folder";
                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
                    };
                }
                request        = driveService.Files.Create(fileMetadata);
                request.Fields = DefaultFileFieldsOnResponse;
                var file = request.Execute();

                pGDriveResult.SetResponseBody(file);
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #9
0
        /// <summary>
        /// Updates a file
        /// </summary>
        /// <param name="fileId">Id of file</param>
        /// <param name="bodyUpdates">Changes of file metadata</param>
        /// <returns>PGDriveResult with updated file in response body</returns>
        public PGDriveResult <File> UpdateFile(string fileId, File bodyUpdates)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                var getFileRequest = driveService.Files.Get(fileId);
                getFileRequest.Fields = DefaultFileFieldsOnResponse;
                var file = getFileRequest.Execute();
                if (bodyUpdates == null)
                {
                    bodyUpdates = file;
                }

                var request = driveService.Files.Update(bodyUpdates, fileId);

                var result = request.Execute();
                pGDriveResult.SetResponseBody(result);
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #10
0
        /// <summary>
        /// Untrashes specific file
        /// </summary>
        /// <param name="fileOrFolderId"></param>
        /// <returns>PGDriveResult with untrashed file in response body</returns>
        public PGDriveResult <File> UnTrashFile(string fileOrFolderId)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                var result = UpdateFile(fileOrFolderId, isTrashed: false);
                return(result);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #11
0
        /// <summary>
        /// Cleanes the trash from all files
        /// </summary>
        /// <param name="fileOrFolderId"></param>
        /// <returns>PGDriveResult with bool in response body which shows did trash was cleaned or not</returns>
        public PGDriveResult <bool> EmptyTrash(string fileOrFolderId)
        {
            PGDriveResult <bool> pGDriveResult = new PGDriveResult <bool>();

            try
            {
                driveService.Files.EmptyTrash().Execute();
                pGDriveResult.SetResponseBody(true);
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #12
0
        protected override PGDriveResult <bool> DeleteFilePermission(string fileId, DriveService service, Permission permission)
        {
            var resultPermissions = GetFilePermissions(fileId, service);

            if (resultPermissions.isSuccess == false)
            {
                return(resultPermissions.CopyResult <bool>());
            }

            var existPermission = resultPermissions.ResponseBody.Where(p => p.Role == permission.Role && p.Type == permission.Type).ToList();

            if (permission.EmailAddress != null)
            {
                existPermission = existPermission.Where(p => p.EmailAddress == permission.EmailAddress).ToList();
            }
            else if (permission.Domain != null)
            {
                existPermission = existPermission.Where(p => p.Domain == permission.Domain).ToList();
            }
            Permission           findedPermission = new Permission();
            PGDriveResult <bool> pGDriveResult    = new PGDriveResult <bool>();

            if (existPermission.Count != 1)
            {
                pGDriveResult.SetResponseBody(false);
                return(pGDriveResult);
            }
            try
            {
                if (findedPermission != null)
                {
                    var result = service.Permissions.Delete(fileId, existPermission.SingleOrDefault().Id).Execute();
                    pGDriveResult.SetResponseBody(true);
                    return(pGDriveResult);
                }
                pGDriveResult.SetResponseBody(false);
                return(pGDriveResult);
            }
            catch (GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                pGDriveResult.SetResponseBody(false);
                return(pGDriveResult);
            }
        }
Exemple #13
0
        /// <summary>
        /// Updates comment
        /// </summary>
        /// <param name="fileId">Id of file</param>
        /// <param name="commentId">Id of a comment</param>
        /// <param name="content">Text of a comment</param>
        /// <returns>PGDriveResult with updated comment in response body</returns>
        public PGDriveResult <Comment> UpdateComment(string fileId, string commentId, string content)
        {
            PGDriveResult <Comment> pGDriveResult = new PGDriveResult <Comment>();

            try
            {
                var result = UpdateComment(fileId, commentId, new Comment()
                {
                    Content = content
                });
                return(result);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #14
0
        /// <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);
            }
        }
Exemple #15
0
        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);
            }
        }
Exemple #16
0
        /// <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);
            }
        }
Exemple #17
0
        /// <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);
            }
        }
Exemple #18
0
        /// <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);
            }
        }
Exemple #19
0
        /// <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);
            }
        }
Exemple #20
0
        /// <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);
            }
        }
Exemple #21
0
        /// <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);
            }
        }
Exemple #22
0
        /// <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);
            }
        }
Exemple #23
0
        /// <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);
            }
        }
Exemple #24
0
        /// <summary>
        /// Updates a file
        /// </summary>
        /// <param name="fileId">Id of file</param>
        /// <param name="bodyUpdates">Changes of file metadata</param>
        /// <param name="filePath">Path to a file which would be a content to update</param>
        /// <returns>PGDriveResult with updated file in response body</returns>
        public PGDriveResult <File> UpdateFile(string fileId, File body, string filePath)
        {
            PGDriveResult <File> pGDriveResult = new PGDriveResult <File>();

            try
            {
                filePath = System.IO.Path.GetFullPath(filePath);
                var    stream   = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                string mimeType = MimeMapping.GetMimeMapping(filePath);
                if (body == null)
                {
                    body = new File();
                }
                var result = UpdateFile(fileId, stream, mimeType, body);

                return(result);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #25
0
        /// <summary>
        /// Downloads a file from a drive
        /// </summary>
        /// <param name="fileId">Id of file</param>
        /// <returns>PGDriveResult with a FileDownloadResult which contains memory stream of downloaded file, mimeType
        /// and it's extension
        /// </returns>
        public PGDriveResult <FileDownloadResult> DownloadFile(string fileId)
        {
            PGDriveResult <FileDownloadResult> pGDriveResult = new PGDriveResult <FileDownloadResult>();

            try
            {
                var fileRequest = driveService.Files.Get(fileId);
                fileRequest.Fields = DefaultFileFieldsOnResponse;
                var fileResponse = fileRequest.Execute();
                if (fileResponse.MimeType.Contains("folder"))
                {
                    throw CustomExceptions.isAFolder(fileId, driveService);
                }
                var request = driveService.Files.Get(fileId);
                var stream  = new System.IO.MemoryStream();
                request.Download(stream);
                if (stream.Length != 0)
                {
                    string             mimeType           = fileResponse.MimeType;
                    string             ext                = GetDefaultExtension(mimeType);
                    FileDownloadResult fileDownloadResult = new FileDownloadResult(stream, mimeType, ext);
                    pGDriveResult.SetResponseBody(fileDownloadResult);
                }
                else
                {
                    pGDriveResult.SetResponseBody(null);
                    pGDriveResult.SetIsSuccess(false);
                }
                return(pGDriveResult);
            }
            catch (Google.GoogleApiException exception)
            {
                pGDriveResult.InitializeError(exception);
                return(pGDriveResult);
            }
        }
Exemple #26
0
        /// <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);
            }
        }
Exemple #27
0
        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);
            }
        }