コード例 #1
0
        private async Task <DriveFile> GetParentFolderDo(string folderId)
        {
            FilesResource.GetRequest request = _service.Files.Get(folderId);
            request.Fields = "id, name, parents";
            DriveFile folder = await request.ExecuteAsync();

            request = _service.Files.Get(folder.Parents[0]);
            return(await request.ExecuteAsync());
        }
        public async Task <FileInfo> GetFileInfoAsync(string id)
        {
            FilesResource.GetRequest request = _driveService.Files.Get(id);
            request.Fields = GetFields;
            File file = await request.ExecuteAsync();

            return(GetInfo(file));
        }
コード例 #3
0
        private async Task <string> GetFileFullNameAsync(File file, DriveService service)
        {
            if (file.Parents == null || !file.Parents.Any())
            {
                // stop when we get to the root ('My Drive' folder)
                return(string.Empty);
            }

            string parentId = file.Parents[0];

            File parent = await _cache.GetOrCreateAsync <File>(key : parentId, async entry =>
            {
                FilesResource.GetRequest getRequest = service.Files.Get(parentId);
                getRequest.Fields = "id, name, parents";
                File item         = await getRequest.ExecuteAsync();
                entry.SetSize(1);
                return(item);
            });

            return(Path.Combine(await GetFileFullNameAsync(parent, service), file.Name));
        }
コード例 #4
0
ファイル: GD.cs プロジェクト: gellerda/HelloMemo
 //--------------------------------------------------------------------------------------------------
 public static async Task <Google.Apis.Drive.v3.Data.File> GetRootFolderAsync()
 {
     FilesResource.GetRequest rootFolderReq = Service.Files.Get("root");
     rootFolderReq.Fields = "id, name, parents";
     return(await rootFolderReq.ExecuteAsync());
 }
コード例 #5
0
        public async Task <IActionResult> GetDocuments(string documentId)
        {
            string webFolder         = "1IjWXjO_2rthBbp77-FsPXp1im7yxTjkw";
            var    documentViewModel = new DocumentViewModel();
            var    breadcrumb        = -1;
            var    breadcrumbList    = new List <Tuple <string, string> >();
            var    folderList        = new List <Document>();
            var    documentList      = new List <Document>();

            var     json = System.IO.File.ReadAllText("client_secrets.json");
            JObject cr   = (JObject)JsonConvert.DeserializeObject(json);

            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.GetValue("client_email").ToString())
            {
                Scopes = new[] {
                    DriveService.Scope.Drive
                }
            }.FromPrivateKey(cr.GetValue("private_key").ToString()));

            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });

            try
            {
                if (String.IsNullOrEmpty(documentId))
                {
                    documentId = webFolder;
                }

                FilesResource.ListRequest listRequest = service.Files.List();
                listRequest.Q       = "'" + documentId + "' in parents";
                listRequest.Fields  = "nextPageToken, files(fileExtension, iconLink, id, mimeType, name, parents, thumbnailLink, webContentLink, webViewLink)";
                listRequest.OrderBy = "name";

                var fileList = await listRequest.ExecuteAsync();

                var parentFile = fileList.Files[0];

                breadcrumbList.Add(new Tuple <string, string>(parentFile.Parents[0], ""));

                while ((parentFile.Parents != null) && (parentFile.Parents.Count > 0) && !parentFile.Parents[0].Equals(webFolder))
                {
                    FilesResource.GetRequest parentRequest = service.Files.Get(parentFile.Parents[0]);

                    parentRequest.Fields = "id, name, parents";

                    parentFile = await parentRequest.ExecuteAsync();

                    if (parentFile.Parents != null)
                    {
                        breadcrumb = breadcrumbList.IndexOf(new Tuple <string, string>(parentFile.Id, ""));

                        if (breadcrumb >= 0)
                        {
                            breadcrumbList[breadcrumb] = new Tuple <string, string>(parentFile.Id, parentFile.Name);
                        }

                        breadcrumbList.Add(new Tuple <string, string>(parentFile.Parents[0], ""));
                    }
                }

                breadcrumb = breadcrumbList.IndexOf(new Tuple <string, string>(webFolder, ""));

                if (breadcrumb >= 0)
                {
                    breadcrumbList[breadcrumb] = new Tuple <string, string>(webFolder, "Home");
                }

                breadcrumbList.Reverse();

                foreach (var file in fileList.Files)
                {
                    Document currentDoc = new Document();

                    currentDoc.FileExtension  = file.FileExtension;
                    currentDoc.IconLink       = file.IconLink;
                    currentDoc.Id             = file.Id;
                    currentDoc.MimeType       = file.MimeType;
                    currentDoc.Name           = file.Name;
                    currentDoc.Parents        = file.Parents;
                    currentDoc.ThumbnailLink  = "https://drive.google.com/thumbnail?authuser=0&sz=s200&id=" + currentDoc.Id;
                    currentDoc.WebContentLink = file.WebContentLink;
                    currentDoc.WebViewLink    = file.WebViewLink;

                    if (currentDoc.MimeType.Equals("application/vnd.google-apps.folder", StringComparison.OrdinalIgnoreCase))
                    {
                        folderList.Add(currentDoc);
                    }
                    else
                    {
                        documentList.Add(currentDoc);
                    }
                }
            }
            catch
            {
                // Ignore this exception...cause it's a Google issue
            }

            documentViewModel.breadcrumbs = breadcrumbList;
            documentViewModel.folders     = folderList;
            documentViewModel.documents   = documentList;

            return(PartialView("Components/Resources/Default", documentViewModel));
        }
コード例 #6
0
        public async Task <long> ContentLength(CancellationToken cancellationToken = default)
        {
            var response = await _getRequest.ExecuteAsync(cancellationToken);

            return(response.Size.GetValueOrDefault());
        }