|
@@ -15,13 +15,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
//多用户UX交互
|
|
|
public class GoogleDriveManager
|
|
|
{
|
|
|
- public static string[] Scopes = { DriveService.Scope.Drive };
|
|
|
- //Google Drive应用名称
|
|
|
- private static readonly string GoogleDriveAppName = "PDF Office";
|
|
|
- //请求应用进行身份验证的信息
|
|
|
- public static string CredentialsPath { get; private set; }
|
|
|
- //存放已通过身份验证的用户信息,以便下次不用登录便可使用云文档
|
|
|
- public static string FilesPathTemp { get; private set; }
|
|
|
|
|
|
//已登录的用户们
|
|
|
public List<GoogleDriveUserItem> GoogleDriveUsers = new List<GoogleDriveUserItem>();
|
|
@@ -29,40 +22,10 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
|
|
|
public GoogleDriveManager()
|
|
|
{
|
|
|
- GetFilesPathTemp();
|
|
|
- GetCredentialsPath();
|
|
|
+ GoogleDriveStatic.GetFilesPathTemp();
|
|
|
+ GoogleDriveStatic.GetCredentialsPath();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- #region 云文档的用户帐户缓存路径和身份验证文件
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 获取或创建缓存登录帐户信息
|
|
|
- /// </summary>
|
|
|
- public void GetFilesPathTemp()
|
|
|
- {
|
|
|
- string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
|
|
|
- String FolderPath = str_1 + "GoogleDriveUsers";
|
|
|
-
|
|
|
- if (Directory.Exists(FolderPath) == false)
|
|
|
- Directory.CreateDirectory(FolderPath);
|
|
|
-
|
|
|
- FilesPathTemp = FolderPath;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 获取本地身份验证文件
|
|
|
- /// </summary>
|
|
|
- public void GetCredentialsPath()
|
|
|
- {
|
|
|
- string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
|
|
|
- String filePath = str_1 + @"\credentials.json";
|
|
|
- CredentialsPath = filePath;
|
|
|
- }
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
-
|
|
|
#region 请求身份验证
|
|
|
|
|
|
/// <summary>
|
|
@@ -70,7 +33,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
/// </summary>
|
|
|
public async Task<bool> GetHistoryUsers()
|
|
|
{
|
|
|
- var tuples = await GetHistoryService();
|
|
|
+ var tuples = await GoogleDriveStatic.GetHistoryService();
|
|
|
GoogleDriveUsers.Clear();
|
|
|
foreach (var tuple in tuples)
|
|
|
{
|
|
@@ -98,82 +61,6 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 获取登录过的账号
|
|
|
- /// </summary>
|
|
|
- /// <returns>历史账号</returns>
|
|
|
- private async Task<List<Tuple<DriveService, UserCredential>>> GetHistoryService()
|
|
|
- {
|
|
|
- DirectoryInfo TheFolder = new DirectoryInfo(GoogleDriveManager.FilesPathTemp);
|
|
|
- List<Tuple<DriveService, UserCredential>> DriveServices = new List<Tuple<DriveService, UserCredential>>();
|
|
|
-
|
|
|
- foreach (var directorieItem in TheFolder.GetDirectories())
|
|
|
- {
|
|
|
- var driveServiceItem = await GetServiceAsync(directorieItem.Name);
|
|
|
-
|
|
|
- if (driveServiceItem != null)
|
|
|
- {
|
|
|
- DriveServices.Add(driveServiceItem);
|
|
|
- }
|
|
|
- }
|
|
|
- return DriveServices;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 异步获取Google服务的包信息,避免UI线程卡死
|
|
|
- /// </summary>
|
|
|
- /// <param name="userInfoFile"></param>
|
|
|
- [Obsolete]
|
|
|
- public async Task<Tuple<DriveService, UserCredential>> GetServiceAsync(string userInfoFile = "")
|
|
|
- {
|
|
|
- Tuple<DriveService, UserCredential> tuple = null;
|
|
|
- await Task.Run(() =>
|
|
|
- {
|
|
|
- tuple = GetService(userInfoFile);
|
|
|
- });
|
|
|
- return tuple;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 获取Google服务的包信息(包含访问令牌,App Key密钥等)
|
|
|
- /// </summary>
|
|
|
- /// <param name="FilePath">登录过的用户文件;若为空,则为新用户登录</param>
|
|
|
- /// <returns></returns>
|
|
|
- [Obsolete]
|
|
|
- private Tuple<DriveService, UserCredential> GetService(string FilePath = "")
|
|
|
- {
|
|
|
- Tuple<DriveService, UserCredential> tuple = null;
|
|
|
- UserCredential credential;
|
|
|
-
|
|
|
- if (FilePath == "")
|
|
|
- {
|
|
|
- var time = DateTime.Now.ToString("yyyyMMddHHmmss");
|
|
|
- FilePath = System.IO.Path.Combine(GoogleDriveManager.FilesPathTemp, time + ".json");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- using (var stream = new FileStream(GoogleDriveManager.CredentialsPath, FileMode.Open, FileAccess.Read))
|
|
|
- {
|
|
|
- credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
|
|
|
- GoogleClientSecrets.Load(stream).Secrets,
|
|
|
- Scopes,
|
|
|
- "user",
|
|
|
- CancellationToken.None,
|
|
|
- new FileDataStore(FilePath, true)).Result;
|
|
|
- }
|
|
|
-
|
|
|
- //create Drive API service.
|
|
|
- DriveService service = new DriveService(new BaseClientService.Initializer()
|
|
|
- {
|
|
|
- HttpClientInitializer = credential,
|
|
|
- ApplicationName = GoogleDriveAppName
|
|
|
- });
|
|
|
-
|
|
|
- return tuple = new Tuple<DriveService, UserCredential>(service, credential);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
#endregion
|
|
|
}
|
|
|
}
|