Exemplo n.º 1
0
        public async Task <ActionResult> UpdateGoogleFileAsync(string id, CancellationToken cancellationToken)
        {
            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
                         AuthorizeAsync(cancellationToken);

            if (result.Credential != null)
            {
                var service = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName       = "WorkCard.vn"
                });
                var list = await service.Files.List().ExecuteAsync();

                var _item = list.Items.Where(t => t.Id == id).FirstOrDefault();

                try
                {
                    if (!_item.Description.Contains("WorkCard.vn"))
                    {
                        _item.Description += "WorkCard.vn";
                    }
                    _item.Shared     = true;
                    _item.CanComment = true;
                    _item.Capabilities.CanDownload = true;
                    _item.Capabilities.CanShare    = true;
                    FilesResource.UpdateRequest request = service.Files.Update(_item, _item.Id);
                    request.NewRevision = true;
                    await request.ExecuteAsync();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                if (!_item.DownloadUrl.IsNullOrEmptyOrWhiteSpace())
                {
                    using (ApplicationDbContext context = new ApplicationDbContext())
                    {
                        var file = context.Documents.Where(t => t.GDriveId == id).FirstOrDefault();
                        file.DownloadUrl          = _item.DownloadUrl;
                        context.Entry(file).State = EntityState.Modified;
                        await context.SaveChangesAsync();
                    }
                }
                if (Request.IsAjaxRequest())
                {
                    return(PartialView("_NotifyMessage", "Downloaded"));
                }
                return(View("_NotifyMessage", "Downloaded"));
            }
            else
            {
                return(new RedirectResult(result.RedirectUri));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This is the method that can be used to copy a file from one folder into another in folder
        /// in Google Drive.
        /// </summary>
        /// <param name="fromFolder">The folder to copy from</param>
        /// <param name="toFolder">The folder to copy into</param>
        /// <param name="fileName">The name of the file to be copied</param>
        /// <returns>The copied file</returns>
        public static async Task <Google.Apis.Drive.v3.Data.File> CopyFileToNewLocation(string fromFolder, string toFolder, string fileName)
        {
            DriveService service = CreateService();

            string fileId = null, fromFolderId = null, toFolderId = null;

            Google.Apis.Drive.v3.Data.File filedata;

            // Get all the files in the Google Drive
            FilesResource.ListRequest fileListRequest = service.Files.List();
            fileListRequest.Fields = "nextPageToken, files(*)";

            //get file in the root folder.
            IList <Google.Apis.Drive.v3.Data.File> rootFolderFiles = fileListRequest.Execute().Files;

            //Get the Id of the fromfolder
            if (rootFolderFiles != null && rootFolderFiles.Count > 0)
            {
                foreach (var file in rootFolderFiles)
                {
                    if (file.Name.ToLower() == fromFolder.ToLower())
                    {
                        fromFolderId = file.Id;
                        break;
                    }
                }
            }

            //Throw an exception if the fromFolderId is still null indicating that the from folder doesn't exist
            if (fromFolderId == null)
            {
                throw new GoogleApiException("Source folder doesn't exist");
            }

            //Get te Id of the toFolder
            if (rootFolderFiles != null && rootFolderFiles.Count > 0)
            {
                foreach (var file in rootFolderFiles)
                {
                    if (file.Name.ToLower() == toFolder.ToLower())
                    {
                        toFolderId = file.Id;
                        break;
                    }
                }
            }

            //Throw an exception if the toFolderId is still null indicating that the to folder doesn't exist
            if (toFolderId == null)
            {
                throw new GoogleApiException("Destination folder doesn't exist");
            }

            //Get the Id and meatadata of the file in the fromFolder
            if (rootFolderFiles != null && rootFolderFiles.Count > 0)
            {
                foreach (var file in rootFolderFiles)
                {
                    foreach (var parent in file.Parents)
                    {
                        if (parent == fromFolderId && file.Name == fileName)
                        {
                            fileId   = file.Id;
                            filedata = file;
                            break;
                        }
                    }
                }
            }

            //Throw an exception to indicate that the file doesn't exist in the from folder
            if (fileId == null)
            {
                throw new GoogleApiException("File does not exist in the folder");
            }

            //copy the file to a new location
            FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId);
            updateRequest.Fields     = "*";
            updateRequest.AddParents = toFolderId;

            //Execute the request
            filedata = await updateRequest.ExecuteAsync();

            return(filedata);
        }