private string getFilterPath(Google.Apis.Drive.v2.Data.File dir, bool isSearhPath)
        {
            DriveService service = GetService();

            if (isSearhPath && dir.Parents.Count > 0 && !(bool)dir.Parents[0].IsRoot)
            {
                path.Add(service.Files.Get(dir.Parents[0].Id).Execute().Title);
                getFilterPath(service.Files.Get(dir.Parents[0].Id).Execute(), isSearhPath);
            }
            else
            {
                if (!isSearhPath)
                {
                    if (dir.Parents.Count > 0)
                    {
                        path.Add(service.Files.Get(dir.Parents[0].Id).Execute().Title);
                        if (!(bool)dir.Parents[0].IsRoot)
                        {
                            getFilterPath(service.Files.Get(dir.Parents[0].Id).Execute(), isSearhPath);
                        }
                    }
                    else
                    {
                        path.Add(dir.Title);
                    }
                }
            }
            return((isSearhPath ? @"\" : "") + string.Join(@"\", path.ToArray().Reverse()));
        }
        private string obtainFilterId(Google.Apis.Drive.v2.Data.File file)
        {
            DriveService service = GetService();
            string       value   = file.MimeType == "application/vnd.google-apps.folder" ? getFilterId(file) : (getFilterId(service.Files.Get(file.Parents[0].Id).Execute()) + service.Files.Get(file.Parents[0].Id).Execute().Id + @"\");

            idValues = new List <string>();
            return(value);
        }
        private string getFilterId(Google.Apis.Drive.v2.Data.File dir)
        {
            DriveService service = GetService();

            if (dir.Parents.Count > 0)
            {
                idValues.Add(service.Files.Get(dir.Parents[0].Id).Execute().Id);
                getFilterId(service.Files.Get(dir.Parents[0].Id).Execute());
            }
            return(string.Join(@"\", idValues.ToArray().Reverse()) + @"\");
        }
コード例 #4
0
ファイル: OldProgram.cs プロジェクト: sukanya0009/pocprojects
        //private static string UploadFileToDrive(DriveService service, string fileName, string filePath, string conentType)
        //{
        //    var fileMatadata = new Google.Apis.Drive.v3.Data.File();
        //    fileMatadata.Name = fileName;
        //    //fileMatadata.Parents = new List<string> { _folderId };

        //    FilesResource.CreateMediaUpload request;

        //    Google.Apis.Upload.IUploadProgress progress;
        //    using (var stream = new FileStream(filePath, FileMode.Open))
        //    {
        //        request = service.Files.Create(fileMatadata, stream, conentType);
        //        progress = request.Upload();
        //    }

        //    Console.WriteLine(progress.Status);
        //    if (progress.Exception != null)
        //    {
        //        Console.WriteLine(progress.Exception);
        //    }
        //    var file = request.ResponseBody;

        //    return file.Id;
        //}

        private static Google.Apis.Drive.v2.Data.File copyTemplate(DriveService service, string documentId, Google.Apis.Drive.v2.Data.File doc)
        {
            String copyTitle = "CopyTitle_" + DateTime.Now.ToLongDateString();

            Google.Apis.Drive.v2.Data.File copyMetadata = new Google.Apis.Drive.v2.Data.File();
            //copyMetadata.Name = copyTitle;

            //// Retrieve the existing parents to remove
            //Google.Apis.Drive.v3.FilesResource.GetRequest getRequest = service.Files.Get(documentId);
            //getRequest.Fields = "parents";
            //Google.Apis.Drive.v3.Data.File file = getRequest.Execute();

            Google.Apis.Drive.v2.Data.File documentCopyFile =
                service.Files.Copy(doc, copyMetadata.Id).Execute();
            String documentCopyId = documentCopyFile.Id;

            return(documentCopyFile);
        }
        // Reads the file(s) and folder(s)
        public FileManagerResponse GetFiles(string path, bool showHiddenItems, params FileManagerDirectoryContent[] data)
        {
            string id = (path == "/") ? null : data[0].Id;
            // Create Drive API service.
            DriveService service = GetService();

            // Define parameters of request.
            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.Fields = "nextPageToken, files(*)";
            List <Google.Apis.Drive.v2.Data.File> result = new List <Google.Apis.Drive.v2.Data.File>();

            FilesResource.ListRequest req = service.Files.List();
            IList <Google.Apis.Drive.v2.Data.File> files = req.Execute().Items;
            FileManagerResponse readResponse             = new FileManagerResponse();

            if (files != null && files.Count > 0)
            {
                FileManagerDirectoryContent    cwd       = new FileManagerDirectoryContent();
                Google.Apis.Drive.v2.Data.File directory = (id == null) ? service.Files.Get(files.Where(a => a.Parents.Any(c => (bool)c.IsRoot == true)).ToList()[0].Parents[0].Id).Execute() :
                                                           service.Files.Get(id).Execute();
                cwd.Name = directory.Title;
                cwd.Size = directory.FileSize != null?long.Parse(directory.FileSize.ToString()) : 0;

                cwd.IsFile       = directory.MimeType == "application/vnd.google-apps.folder" ? false : true;
                cwd.DateModified = Convert.ToDateTime(directory.ModifiedDate);
                cwd.DateCreated  = Convert.ToDateTime(directory.CreatedDate);
                cwd.Id           = directory.Id;
                cwd.HasChild     = true;
                cwd.Type         = "Folder";
                this.path        = new List <string>();
                cwd.FilterPath   = directory.Parents.Count == 0 ? "" : data[0].FilterPath;
                List <FileManagerDirectoryContent> rootFileList = files.Where(x => x.Parents.Any(c => (bool)c.IsRoot == true)).Select(x => new FileManagerDirectoryContent()
                {
                    Id           = x.Id,
                    Name         = x.Title,
                    Size         = x.FileSize != null ? long.Parse(x.FileSize.ToString()) : 0,
                    DateCreated  = Convert.ToDateTime(x.CreatedDate),
                    DateModified = Convert.ToDateTime(x.ModifiedDate),
                    Type         = x.FileExtension == null ? "folder" : x.FileExtension,
                    HasChild     = getChildrenById(x.Id),
                    FilterPath   = @"\",
                    FilterId     = obtainFilterId(x),
                    IsFile       = x.MimeType == "application/vnd.google-apps.folder" ? false : true
                }).ToList();
                if (id == null)
                {
                    readResponse.Files = rootFileList;
                }
                else
                {
                    ChildrenResource.ListRequest request = service.Children.List(id);
                    ChildList children = request.Execute();
                    List <FileManagerDirectoryContent> childFileList = new List <FileManagerDirectoryContent>();
                    string[] childId = children.Items.Select(x => x.Id).ToList().ToArray();
                    foreach (string idValue in childId)
                    {
                        File details = service.Files.Get(idValue).Execute();
                        FileManagerDirectoryContent content = new FileManagerDirectoryContent()
                        {
                            Id   = idValue,
                            Name = details.Title,
                            Size = details.FileSize != null?long.Parse(details.FileSize.ToString()) : 0,
                                       DateCreated  = Convert.ToDateTime(details.CreatedDate),
                                       DateModified = Convert.ToDateTime(details.ModifiedDate),
                                       Type         = details.FileExtension,
                                       FilterPath   = data.Length != 0 ? obtainFilterPath(details, true) + @"\" : @"\",
                                       FilterId     = obtainFilterId(details),
                                       HasChild     = getChildrenById(idValue),
                                       IsFile       = details.MimeType == "application/vnd.google-apps.folder" ? false : true
                        };
                        childFileList.Add(content);
                    }
                    readResponse.Files = childFileList;
                }
                readResponse.CWD = cwd;
                return(readResponse);
            }
            return(readResponse);
        }
        private string obtainFilterPath(Google.Apis.Drive.v2.Data.File details, bool isSearch)
        {
            DriveService service = GetService();

            return(details.MimeType == "application/vnd.google-apps.folder" ? getFilterPath(details, isSearch) : (getFilterPath(service.Files.Get(details.Parents[0].Id).Execute(), isSearch) + @"\" + service.Files.Get(details.Parents[0].Id).Execute().Title));
        }
コード例 #7
0
ファイル: OldProgram.cs プロジェクト: sukanya0009/pocprojects
        public void CallDocs()
        {
            UserCredential credential;

            using (var stream =
                       new FileStream("credentials.json", FileMode.Open, FileAccess.ReadWrite))
            {
                string credPath = "token.json";

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)
                    ).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Google Docs API service.
            var service = new DocsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            // Define request parameters.
            //String documentId = "195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE";
            //DocumentsResource.GetRequest request = service.Documents.Get(documentId);

            // Prints the title of the requested doc:
            // https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit
            //Document doc = request.Execute();

            Document newDoc = createDoc(service);



            DriveService driveService = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            // Retrieve the existing parents to remove
            Google.Apis.Drive.v2.FilesResource.GetRequest getRequest = driveService.Files.Get(newDoc.DocumentId);
            //getRequest.Fields = "parents";
            Google.Apis.Drive.v2.Data.File fileNew = getRequest.Execute();

            Google.Apis.Drive.v2.Data.File file = driveService.Files.Get(templateDocumentId).Execute();
            // Fetch file from drive
            //var request = driveService.Files.Get(templateDocumentId);
            //request.Fields = "name,parents";
            //var parent = request.Execute();

            //var newCopiedFile = copyTemplate(driveService, fileNew.Id, file);
            //var item = driveService.Files.Create(file);
            //var newCopiedFile = copyTemplate(driveService, item.F, file);
            //Console.WriteLine("The title of the doc is: {0}", doc.Title);
            //Console.WriteLine("The title of the doc is: {0}", doc22.Title);

            // working
            // copy google doc
            //var newFile = CopyFile(driveService, templateDocumentId, "CopyTitle_" + DateTime.Now.ToString("ddMMyyyy_hhmmss"));

            string replaceText = "1elDcQNW9sZHRsNDz7Ds_TxT8xXgak5JITAPxaSMsVow";

            mergeText(service, replaceText);
        }