Exemplo n.º 1
0
        public async Task <List <GoogleDriveFolder> > GetFoldersAsync(string folderId)
        {
            var drive = await GetDriveAsync();

            var listRequest = drive.Files.List();

            listRequest.Q      = $"'{folderId}' in parents and mimeType='application/vnd.google-apps.folder'";
            listRequest.Fields = "files(*)";

            var folders = await listRequest.ExecuteAsync();

            var result = new List <GoogleDriveFolder>();

            foreach (var file in folders.Files)
            {
                var folder = new GoogleDriveFolder {
                    Id = file.Id, Name = file.Name
                };

                folder.ParentId = file.Parents.FirstOrDefault();

                result.Add(folder);

                listRequest.Q      = $"'{folderId}' in parents and mimeType='image/jpeg'";
                listRequest.Fields = "files(*)";

                var images = await listRequest.ExecuteAsync();

                if (!images.Files.Any())
                {
                    listRequest.Q      = $"'{file.Id}' in parents and mimeType='image/jpeg'";
                    listRequest.Fields = "files(*)";

                    images = await listRequest.ExecuteAsync();

                    if (!images.Files.Any())
                    {
                        continue;
                    }
                }

                var image = images.Files.FirstOrDefault(e => Regex.IsMatch(e.Name, @"^[a-zA-Z]+$")) ?? images.Files.First();

                folder.Url = image.WebContentLink;

                using (var memoryStream = new MemoryStream())
                {
                    await drive.Files.Get(image.Id).DownloadAsync(memoryStream);

                    memoryStream.Position = 0;

                    folder.Base64Img = Convert.ToBase64String(memoryStream.ToArray());
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public async Task <List <GoogleDriveFolder> > GetFoldersByCategoryAsync(string folderId)
        {
            var drive = await GetDriveAsync();

            var listRequest = drive.Files.List();

            listRequest.Q      = $"'{folderId}' in parents and mimeType='application/vnd.google-apps.folder'";
            listRequest.Fields = "files(*)";

            var folders = await listRequest.ExecuteAsync();

            var result = new List <GoogleDriveFolder>();

            foreach (var file in folders.Files)
            {
                var folder = new GoogleDriveFolder {
                    Id = file.Id, Name = file.Name
                };

                folder.ParentId = file.Parents.FirstOrDefault();

                result.Add(folder);

                listRequest.Q      = $"'{file.Id}' in parents and mimeType='image/jpeg'";
                listRequest.Fields = "files(*)";

                var images = await listRequest.ExecuteAsync();

                if (!images.Files.Any())
                {
                    continue;
                }

                var image = images.Files.FirstOrDefault(e => Regex.IsMatch(e.Name, @"^[a-zA-Z]+$")) ?? images.Files.First();
                folder.Url = image.WebContentLink;
                //HttpWebRequest httpWebRequest = WebRequest.Create(image.WebContentLink) as HttpWebRequest;
                //httpWebRequest.Method = "HEAD";
                //httpWebRequest.AllowAutoRedirect = false;
                //HttpWebResponse httpWebResponse = await httpWebRequest.GetResponseAsync() as HttpWebResponse;
                // httpWebResponse.StatusCode == HttpStatusCode.Redirect ? httpWebResponse.GetResponseHeader("Location") : image.WebContentLink;
            }

            return(result);
        }