GoogleDriveViewModel.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Google.Apis.Auth.OAuth2;
  2. using Google.Apis.Download;
  3. using Google.Apis.Drive.v3;
  4. using Google.Apis.Services;
  5. using Google.Apis.Util.Store;
  6. using PDF_Office.Model.CloudDrive;
  7. using Prism.Commands;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace PDF_Office.ViewModels.HomePanel.CloudDrive
  17. {
  18. //多用户UX交互
  19. public class GoogleDriveViewModel
  20. {
  21. public static string[] Scopes = { DriveService.Scope.Drive };
  22. private static readonly string GoogleDriveAppName = "PDF Office";
  23. public static string FilesPathTemp { get; private set; }
  24. public static string CredentialsPath { get; private set; }
  25. public List<GoogleDriveUserItem> GoogleDriveUsers = new List<GoogleDriveUserItem>();
  26. public GoogleDriveViewModel()
  27. {
  28. GetFilesPathTemp();
  29. GetCredentialsPath();
  30. }
  31. #region 请求身份验证
  32. public async Task<bool> GetHistoryUsers()
  33. {
  34. var tuples = await GetHistoryService();
  35. GoogleDriveUsers.Clear();
  36. foreach (var tuple in tuples)
  37. {
  38. if (tuple != null && tuple.Item1 != null && tuple.Item2 != null)
  39. {
  40. AddGoogleDriveUser(tuple);
  41. }
  42. }
  43. if (GoogleDriveUsers.Count > 0)
  44. {
  45. return true;
  46. }
  47. return false;
  48. }
  49. public async void AddGoogleDriveUser(Tuple<DriveService, UserCredential> tuple)
  50. {
  51. GoogleDriveUserItem user = new GoogleDriveUserItem();
  52. user.Service = tuple.Item1;
  53. user.User.CurrentCredential = tuple.Item2;
  54. user.User.UserAccount = await user.GetUserAcountAsync();
  55. GoogleDriveUsers.Add(user);
  56. }
  57. /// <summary>
  58. /// 获取登录过的账号
  59. /// </summary>
  60. /// <returns>历史账号</returns>
  61. private async Task<List<Tuple<DriveService, UserCredential>>> GetHistoryService()
  62. {
  63. DirectoryInfo TheFolder = new DirectoryInfo(GoogleDriveViewModel.FilesPathTemp);
  64. List<Tuple<DriveService, UserCredential>> DriveServices = new List<Tuple<DriveService, UserCredential>>();
  65. foreach (var directorieItem in TheFolder.GetDirectories())
  66. {
  67. var driveServiceItem = await GetServiceAsync(directorieItem.Name);
  68. if (driveServiceItem != null)
  69. {
  70. DriveServices.Add(driveServiceItem);
  71. }
  72. }
  73. return DriveServices;
  74. }
  75. /// <summary>
  76. /// 异步获取Google服务的包信息,避免UI线程卡死
  77. /// </summary>
  78. /// <param name="userInfoFile"></param>
  79. [Obsolete]
  80. public async Task<Tuple<DriveService, UserCredential>> GetServiceAsync(string userInfoFile = "")
  81. {
  82. Tuple<DriveService, UserCredential> tuple = null;
  83. await Task.Run(() =>
  84. {
  85. tuple = GetService(userInfoFile);
  86. });
  87. return tuple;
  88. }
  89. /// <summary>
  90. /// 获取Google服务的包信息(包含访问令牌,App Key密钥等)
  91. /// </summary>
  92. /// <param name="FilePath">登录过的用户文件;若为空,则为新用户登录</param>
  93. /// <returns></returns>
  94. [Obsolete]
  95. private Tuple<DriveService, UserCredential> GetService(string FilePath = "")
  96. {
  97. Tuple<DriveService, UserCredential> tuple = null;
  98. UserCredential credential;
  99. if (FilePath == "")
  100. {
  101. var time = DateTime.Now.ToString("yyyyMMddHHmmss");
  102. FilePath = System.IO.Path.Combine(GoogleDriveViewModel.FilesPathTemp, time + ".json");
  103. }
  104. using (var stream = new FileStream(GoogleDriveViewModel.CredentialsPath, FileMode.Open, FileAccess.Read))
  105. {
  106. credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
  107. GoogleClientSecrets.Load(stream).Secrets,
  108. Scopes,
  109. "user",
  110. CancellationToken.None,
  111. new FileDataStore(FilePath, true)).Result;
  112. }
  113. //create Drive API service.
  114. DriveService service = new DriveService(new BaseClientService.Initializer()
  115. {
  116. HttpClientInitializer = credential,
  117. ApplicationName = GoogleDriveAppName
  118. });
  119. return tuple = new Tuple<DriveService, UserCredential>(service, credential);
  120. }
  121. #endregion
  122. #region 云文档的用户帐户缓存路径和身份验证文件
  123. /// <summary>
  124. /// 获取或创建缓存登录帐户信息
  125. /// </summary>
  126. public void GetFilesPathTemp()
  127. {
  128. string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
  129. String FolderPath = str_1 + "GoogleDriveUsers";
  130. if (Directory.Exists(FolderPath) == false)
  131. Directory.CreateDirectory(FolderPath);
  132. FilesPathTemp = FolderPath;
  133. }
  134. /// <summary>
  135. /// 获取本地身份验证文件
  136. /// </summary>
  137. public void GetCredentialsPath()
  138. {
  139. string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
  140. String filePath = str_1 + @"\credentials.json";
  141. CredentialsPath = filePath;
  142. }
  143. #endregion
  144. }
  145. }