|
@@ -18,9 +18,8 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
//对单用户账号处理核心功能
|
|
|
public class GoogleDriveUserItem
|
|
|
{
|
|
|
- public GoogleDriveUser User { get; private set; }
|
|
|
- public static string[] Scopes = { DriveService.Scope.Drive };
|
|
|
- public DriveService Service { get; private set; }
|
|
|
+ public GoogleDriveUser User { get; set; }
|
|
|
+ public DriveService Service { get; set; }
|
|
|
private List<GoogleDriveFiles> GoogleDriveFilesList = new List<GoogleDriveFiles>();
|
|
|
|
|
|
public GoogleDriveUserItem()
|
|
@@ -29,93 +28,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
|
|
|
}
|
|
|
|
|
|
- #region 请求身份验证
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 获取登录过的账号
|
|
|
- /// </summary>
|
|
|
- /// <returns>历史账号</returns>
|
|
|
- public async Task<List<DriveService>> GetHistoryService()
|
|
|
- {
|
|
|
- DirectoryInfo TheFolder = new DirectoryInfo(GoogleDriveViewModel.FilesPathTemp);
|
|
|
- List<DriveService> DriveServices = new List<DriveService>();
|
|
|
-
|
|
|
- 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<DriveService> GetServiceAsync(string userInfoFile = "")
|
|
|
- {
|
|
|
- DriveService service = null;
|
|
|
-
|
|
|
- await Task.Run(() =>
|
|
|
- {
|
|
|
-
|
|
|
- service = GetService(userInfoFile);
|
|
|
- });
|
|
|
-
|
|
|
- if (userInfoFile == "")
|
|
|
- {
|
|
|
- if (service != null)
|
|
|
- Service = service;
|
|
|
- }
|
|
|
-
|
|
|
- return service;
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 获取Google服务的包信息(包含访问令牌,App Key密钥等)
|
|
|
- /// </summary>
|
|
|
- /// <param name="FilePath">登录过的用户文件;若为空,则为新用户登录</param>
|
|
|
- /// <returns></returns>
|
|
|
- [Obsolete]
|
|
|
- private DriveService GetService(string FilePath = "")
|
|
|
- {
|
|
|
- UserCredential credential;
|
|
|
-
|
|
|
- if (FilePath == "")
|
|
|
- {
|
|
|
- var time = DateTime.Now.ToString("yyyyMMddHHmmss");
|
|
|
- FilePath = System.IO.Path.Combine(GoogleDriveViewModel.FilesPathTemp, time + ".json");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- using (var stream = new FileStream(GoogleDriveViewModel.CredentialsPath, FileMode.Open, FileAccess.Read))
|
|
|
- {
|
|
|
- credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
|
|
|
- GoogleClientSecrets.Load(stream).Secrets,
|
|
|
- Scopes,
|
|
|
- "user",
|
|
|
- CancellationToken.None,
|
|
|
- new FileDataStore(FilePath, true)).Result;
|
|
|
- User.CurrentCredential = credential;
|
|
|
- }
|
|
|
-
|
|
|
- //create Drive API service.
|
|
|
- DriveService service = new DriveService(new BaseClientService.Initializer()
|
|
|
- {
|
|
|
- HttpClientInitializer = credential,
|
|
|
- ApplicationName = "PDF Office",
|
|
|
- });
|
|
|
-
|
|
|
- return service;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- #endregion
|
|
|
+
|
|
|
|
|
|
#region 对用户账号处理
|
|
|
|
|
@@ -136,7 +49,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
/// 获取帐号
|
|
|
/// </summary>
|
|
|
/// <returns>返回用户帐号地址</returns>
|
|
|
- public string GetUserAcount()
|
|
|
+ private async Task<string> GetUserAcount()
|
|
|
{
|
|
|
string userAcount = "";
|
|
|
if (Service != null)
|
|
@@ -149,6 +62,16 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
return userAcount;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 获取帐号
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>返回用户帐号地址</returns>
|
|
|
+ public async Task<string> GetUserAcountAsync()
|
|
|
+ {
|
|
|
+ return await GetUserAcount();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
|
|
@@ -165,10 +88,10 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
/// <summary>
|
|
|
/// 对单个用户,获取文件列表
|
|
|
/// </summary>
|
|
|
- public async Task<List<GoogleDriveFiles>> GetDriveFiles(DriveService service = null)
|
|
|
+ public async Task<List<GoogleDriveFiles>> GetDriveFiles(DriveService service = null)
|
|
|
{
|
|
|
if (service == null)
|
|
|
- service = await GetServiceAsync();
|
|
|
+ return null;
|
|
|
|
|
|
// define parameters of request.
|
|
|
FilesResource.ListRequest FileListRequest = service.Files.List();
|
|
@@ -206,7 +129,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
public async Task<bool> FileUpload(string filepath)
|
|
|
{
|
|
|
if (Service == null)
|
|
|
- Service = await GetServiceAsync();
|
|
|
+ return false;
|
|
|
|
|
|
var FileMetaData = new Google.Apis.Drive.v3.Data.File();
|
|
|
var str = filepath.LastIndexOf("\\");
|
|
@@ -245,7 +168,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
public async Task<string> DownloadGoogleFile(GoogleDriveFiles googleDriveFiles, string savePath)
|
|
|
{
|
|
|
if (Service == null)
|
|
|
- Service = await GetServiceAsync();
|
|
|
+ return "";
|
|
|
|
|
|
string fileId = googleDriveFiles.Id;
|
|
|
if (string.IsNullOrEmpty(fileId))
|