Example #1
0
        public static File getfolderid(string foldername)
        {
            ChildrenResource.ListRequest request = service.Children.List("root");
            do
            {
                try
                {
                    ChildList children = request.Fetch();

                    foreach (ChildReference child in children.Items)
                    {
                        //Console.WriteLine("File Id: " + child.Kind+child.SelfLink);
                        var file = service.Files.Get(child.Id).Fetch();

                        if (file.MimeType == mimetype.folder && file.Title == foldername && file.ExplicitlyTrashed != true)
                        {
                            return(file);
                        }
                    }
                    request.PageToken = children.NextPageToken;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    request.PageToken = null;
                    return(null);
                }
            } while (!String.IsNullOrEmpty(request.PageToken));

            return(null);
        }
        /// <summary>
        /// thread to dowload temp files
        /// </summary>
        /// <param name="service"></param>
        /// <param name="parentId"></param>
        private static void DownloadTempFiles(DriveService service, string parentId)
        {
            while (true)
            {
                ChildrenResource.ListRequest request = service.Children.List(parentId);
                File file = null;
                do
                {
                    try
                    {
                        ChildList children = request.Fetch();

                        foreach (ChildReference child in children.Items)
                        {
                            //Console.WriteLine("File Id: " + child.Id);
                            file = service.Files.Get(child.Id).Fetch();
                            string title = file.Title.Replace(":", "_");
                            string id    = file.Description.Split(' ')[1];
                            if (XmlDataLayer.CheckIfTempExists(title) == true || XmlDataLayer.GetDownloadEntry(title) == true || id == XmlDataLayer.GetConfigEntry("pc_id"))
                            {
                                continue;
                            }
                            try
                            {
                                System.IO.Stream tfile = DownloadFile(createAuthenticator(), file);
                                using (tfile)
                                {
                                    string filePath = XmlDataLayer.GetUserApplicationDirectory() + "\\Temp\\" + title;
                                    tfile.CopyTo(new System.IO.FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write));
                                    XmlDataLayer.SetDownloadEntry(title);
                                    XmlDataLayer x = new XmlDataLayer();
                                    x.CombineRecentResults();
                                }
                                Console.WriteLine(title);
                            }
                            catch (Exception)
                            {
                                UploadDownloadNewFiles(false);
                            }
                            //Console.WriteLine(title);
                            //File file = service.files().get(child.Id).execute();
                        }
                        request.PageToken = children.NextPageToken;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("An error occurred: " + e.Message);
                        request.PageToken = null;
                    }
                } while (!String.IsNullOrEmpty(request.PageToken));
                Thread.Sleep(5 * 60 * 1000);
            }
        }
Example #3
0
        public static List <File> listfiles(string foldername)
        {
            File        myfolder      = getfolderid(foldername);
            List <File> filesinfodler = new List <File>();

            if (myfolder != null)
            {
                ChildrenResource.ListRequest request = service.Children.List(myfolder.Id);

                do
                {
                    try
                    {
                        ChildList children = request.Fetch();

                        foreach (ChildReference child in children.Items)
                        {
                            //Console.WriteLine("File Id: " + child.Kind+child.SelfLink);
                            File file = service.Files.Get(child.Id).Fetch();
                            Console.WriteLine("File : " + file.OriginalFilename);
                            filesinfodler.Add(file);
                        }
                        request.PageToken = children.NextPageToken;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("An error occurred: " + e.Message);
                        request.PageToken = null;
                    }
                } while (!String.IsNullOrEmpty(request.PageToken));

                return(filesinfodler);
            }
            else
            {
                MessageBox.Show("Folder not found");
                return(null);
            }
        }
Example #4
0
        public void Children(int folderRequestId,
                             string folder,
                             Dispatcher dispatch,
                             Func <int, FileEntry, int, bool> addEntry)
        {
            numFilesBeingFetched = 0;
            ChildrenResource.ListRequest request = _service.Children.List(folder);
            do
            {
                try
                {
                    ChildList children = request.Fetch();

                    var expectedCount = children.Items.Count();

                    foreach (ChildReference child in children.Items)
                    {
                        do
                        {
                            lock (_thumbDownloaders)
                            {
                                if (_thumbDownloaders.Count < 12)
                                {
                                    break;
                                }
                            }

                            System.Threading.Thread.Sleep(100);
                        } while (true);

                        var doContinue = true;
                        dispatch.Invoke(
                            new Action(() => { doContinue = addEntry(expectedCount, null, folderRequestId); }));
                        if (!doContinue)
                        {
                            break;
                        }

                        do
                        {
                            lock (this)
                            {
                                if (numFilesBeingFetched < 20)
                                {
                                    break;
                                }
                            }
                            System.Threading.Thread.Sleep(100);
                        } while (true);

                        FilesResource.GetRequest getReq = _service.Files.Get(child.Id);
                        ++numFilesBeingFetched;
                        getReq.FetchAsync((LazyResult <Google.Apis.Drive.v2.Data.File> response) =>
                        {
                            --numFilesBeingFetched;
                            Google.Apis.Drive.v2.Data.File file = response.GetResult();

                            doContinue = true;
                            dispatch.Invoke(
                                new Action(() => { doContinue = addEntry(expectedCount, null, folderRequestId); }));
                            if (!doContinue)
                            {
                                return;
                            }

                            //download thumbnail
                            if (file.ThumbnailLink != null)
                            {
                                if (_thumbCache.ContainsKey(file.Id))
                                {
                                    dispatch.BeginInvoke(new Action(() =>
                                    {
                                        addEntry(expectedCount,
                                                 new FileEntry(file, _thumbCache[file.Id], file.AlternateLink),
                                                 folderRequestId);
                                    }));
                                }
                                else
                                {
                                    TaskCompletionSource <byte[]> thumbTaskSrc = null;
                                    lock (_thumbDownloaders)
                                    {
                                        thumbTaskSrc = DownloadThumb(file.ThumbnailLink);
                                        _thumbDownloaders.Add(thumbTaskSrc);
                                    }

                                    thumbTaskSrc.Task.ContinueWith(
                                        (Task <byte[]> thumbTask) =>
                                    {
                                        if (thumbTask.Result != null)
                                        {
                                            dispatch.BeginInvoke(new Action(() =>
                                            {
                                                var thumb = new BitmapImage();
                                                thumb.BeginInit();
                                                thumb.StreamSource = new MemoryStream(thumbTask.Result);
                                                thumb.EndInit();

                                                if (!_thumbCache.ContainsKey(file.Id))
                                                {
                                                    _thumbCache.Add(file.Id, thumb);
                                                }

                                                addEntry(expectedCount,
                                                         new FileEntry(file, thumb, file.AlternateLink),
                                                         folderRequestId);
                                            }));
                                        }
                                        else
                                        {
                                            //thumbnails may be absent
                                            dispatch.BeginInvoke(new Action(() =>
                                            {
                                                addEntry(expectedCount,
                                                         new FileEntry(file, GetFileIcon(file),
                                                                       file.AlternateLink),
                                                         folderRequestId);
                                            }));
                                        }

                                        lock (_thumbDownloaders)
                                            _thumbDownloaders.Remove(thumbTaskSrc);
                                    });
                                }
                            }
                            else
                            {
                                dispatch.BeginInvoke(new Action(() =>
                                {
                                    addEntry(expectedCount,
                                             new FileEntry(file, GetFileIcon(file), file.AlternateLink),
                                             folderRequestId);
                                }));
                            }
                        });
                    }
                    request.PageToken = children.NextPageToken;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    request.PageToken = null;
                }

                var doContinue2 = true;
                dispatch.Invoke(new Action(() => { doContinue2 = addEntry(1, null, folderRequestId); //1 anything != 0
                                           }));
                if (!doContinue2)
                {
                    break;
                }
            } while (!String.IsNullOrEmpty(request.PageToken));
        }
        /// <summary>
        /// makes directory on google drive if it does not exist
        /// </summary>
        /// <param name="service"></param>
        /// <param name="folder"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private static File makeDirectoryIfNotExists(DriveService service, string folder, string parentId)
        {
            ChildrenResource.ListRequest request = service.Children.List(parentId);
            bool folderExists = false;
            File file         = null;

            do
            {
                try
                {
                    ChildList children = request.Fetch();

                    foreach (ChildReference child in children.Items)
                    {
                        //Console.WriteLine("File Id: " + child.Id);
                        file = service.Files.Get(child.Id).Fetch();
                        string title = file.Title;
                        Console.WriteLine(title);
                        if (title == folder)
                        {
                            folderExists = true;
                            break;
                        }
                        //Console.WriteLine(title);
                        //File file = service.files().get(child.Id).execute();
                    }
                    request.PageToken = children.NextPageToken;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    request.PageToken = null;
                }
            } while (!String.IsNullOrEmpty(request.PageToken));
            if (folderExists)
            {
                return(file);
            }

            File body = new File();

            body.Title       = folder;
            body.Description = "PCTrack_id " + XmlDataLayer.GetConfigEntry("id");
            body.MimeType    = "application/vnd.google-apps.folder";

            // Set the parent folder.
            if (!String.IsNullOrEmpty(parentId))
            {
                body.Parents = new List <ParentReference>()
                {
                    new ParentReference()
                    {
                        Id = parentId
                    }
                };
            }


            try
            {
                file = service.Files.Insert(body).Fetch();
                return(file);
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                return(null);
            }
        }