/// <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); } }
/// <summary> /// thread to upload temp files /// </summary> /// <param name="service"></param> /// <param name="folderId"></param> private static void UploadTempFiles(DriveService service, string folderId) { while (true) { string tempFolder = XmlDataLayer.GetUserApplicationDirectory() + "\\Temp"; foreach (string f in System.IO.Directory.EnumerateFiles(tempFolder)) { try { string fileName = f.Split('\\')[f.Split('\\').Length - 1]; if (fileName == XmlDataLayer.GetCurrentTempFileName() || XmlDataLayer.GetUploadEntry(fileName) == true) { continue; } File body = new File(); body.Title = fileName; body.Description = "PCTrack_id " + XmlDataLayer.GetConfigEntry("pc_id"); body.MimeType = "text/plain"; body.Parents = new List <ParentReference>() { new ParentReference() { Id = folderId } }; byte[] byteArray = System.IO.File.ReadAllBytes(f); System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain"); request.Upload(); File file = request.ResponseBody; if (file != null) { XmlDataLayer.SetUploadEntry(fileName); } } catch (Exception) { UploadDownloadNewFiles(false); } } Thread.Sleep(5 * 60 * 1000); } }
/// <summary> /// gets authorization from google drive /// </summary> /// <param name="arg"></param> /// <returns></returns> private static IAuthorizationState GetAuthorization(NativeApplicationClient arg) { if (XmlDataLayer.GetConfigEntry("authentication") == "done" && System.IO.File.Exists(XmlDataLayer.GetUserApplicationDirectory() + "\\credentials")) { TaskCompletionSource <SelfAuthorizationState> tcs = new TaskCompletionSource <SelfAuthorizationState>(); string filePath = XmlDataLayer.GetUserApplicationDirectory() + "\\credentials"; var obj = System.IO.File.ReadAllText(filePath); tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize <SelfAuthorizationState>(obj)); SelfAuthorizationState k = tcs.Task.Result; return(k); } else { // Get the auth URL: IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() }); state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); Uri authUri = arg.RequestUserAuthorization(state); // Request authorization from the user (by opening a browser window): Process.Start(authUri.ToString()); Thread response = new Thread(GetResponse); response.SetApartmentState(ApartmentState.STA); response.IsBackground = true; response.Start(); while (verificationEntered != true) { Thread.Sleep(200); } string authCode = verificationString; // Retrieve the access token by using the authorization code: IAuthorizationState s = arg.ProcessUserAuthorization(authCode, state); var serialized = NewtonsoftJsonSerializer.Instance.Serialize(s); System.IO.File.WriteAllText(XmlDataLayer.GetUserApplicationDirectory() + "\\credentials", serialized); return(s); } }