|
@@ -12,34 +12,41 @@ using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
-namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
+namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
|
|
|
{
|
|
|
|
|
|
- //对单用户账号处理核心功能
|
|
|
- public class GoogleDriveUserItem
|
|
|
+ /// <summary>
|
|
|
+ /// 对单账号处理的核心功能类
|
|
|
+ /// </summary>
|
|
|
+ public class GoogleDriveUserItem: UserBaseItem
|
|
|
{
|
|
|
- public GoogleDriveUser User { get; set; }
|
|
|
- public DriveService Service { get; set; }
|
|
|
- private List<GoogleDriveFiles> GoogleDriveFilesList = new List<GoogleDriveFiles>();
|
|
|
+
|
|
|
+ public DriveService Service { get; set; }//Google提供服务
|
|
|
+ private List<GoogleDriveFiles> FilesList = new List<GoogleDriveFiles>();//文件
|
|
|
+
|
|
|
+ public UserCredential CurrentCredential { get; set; }//当前用户访问令牌
|
|
|
|
|
|
public GoogleDriveUserItem()
|
|
|
{
|
|
|
- User = new GoogleDriveUser();
|
|
|
+ // User = new User();
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
#region 对用户账号处理
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 移除用户帐号
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>移除后的状态</returns>
|
|
|
public async Task<bool> RemoveUser()
|
|
|
{
|
|
|
bool result = false;
|
|
|
- if (User.CurrentCredential != null)
|
|
|
+ if (CurrentCredential != null)
|
|
|
{
|
|
|
- result = await User.CurrentCredential.RevokeTokenAsync(CancellationToken.None);
|
|
|
+ result = await CurrentCredential.RevokeTokenAsync(CancellationToken.None);
|
|
|
}
|
|
|
if (result == true)
|
|
|
- User.CurrentCredential = null;
|
|
|
+ CurrentCredential = null;
|
|
|
|
|
|
return result;
|
|
|
}
|
|
@@ -92,7 +99,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
return null;
|
|
|
|
|
|
// define parameters of request.
|
|
|
- FilesResource.ListRequest FileListRequest = service.Files.List();
|
|
|
+ FilesResource.ListRequest FileListRequest = service.Files.List();
|
|
|
|
|
|
//listRequest.PageSize = 10;
|
|
|
//listRequest.PageToken = 10;
|
|
@@ -106,7 +113,7 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
{
|
|
|
foreach (var file in files)
|
|
|
{
|
|
|
- GoogleDriveFiles File = new GoogleDriveFiles
|
|
|
+ GoogleDriveFiles File = new GoogleDriveFiles
|
|
|
{
|
|
|
Id = file.Id,
|
|
|
Name = file.Name,
|
|
@@ -225,4 +232,123 @@ namespace PDF_Office.ViewModels.HomePanel.CloudDrive
|
|
|
|
|
|
#endregion
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public static class GoogleDriveStatic
|
|
|
+ {
|
|
|
+ public static string[] Scopes = { DriveService.Scope.Drive };
|
|
|
+ //Google Drive应用名称
|
|
|
+ private static readonly string GoogleDriveAppName = "PDF Office848";
|
|
|
+ //请求应用进行身份验证的信息
|
|
|
+ public static string CredentialsPath { get; private set; }
|
|
|
+
|
|
|
+ //存放已通过身份验证的用户信息,以便下次不用登录便可使用云文档
|
|
|
+ public static string FilesPathTemp { get; private set; }
|
|
|
+ /// <summary>
|
|
|
+ /// 异步获取Google服务的包信息,避免UI线程卡死
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="userInfoFile"></param>
|
|
|
+ [Obsolete]
|
|
|
+ public static 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]
|
|
|
+ public static 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(FilesPathTemp, time + ".json");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ using (var stream = new FileStream(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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取登录过的账号
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>历史账号</returns>
|
|
|
+ public static async Task<List<Tuple<DriveService, UserCredential>>> GetHistoryService()
|
|
|
+ {
|
|
|
+ DirectoryInfo TheFolder = new DirectoryInfo(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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #region 云文档的用户帐户缓存路径和身份验证文件
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取或创建缓存登录帐户信息
|
|
|
+ /// </summary>
|
|
|
+ public static string GetFilesPathTemp()
|
|
|
+ {
|
|
|
+ string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
|
|
|
+ String FolderPath = str_1 + "GoogleDriveUsers";
|
|
|
+
|
|
|
+ if (Directory.Exists(FolderPath) == false)
|
|
|
+ Directory.CreateDirectory(FolderPath);
|
|
|
+
|
|
|
+ FilesPathTemp = FolderPath;
|
|
|
+ return FolderPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取本地身份验证文件
|
|
|
+ /// </summary>
|
|
|
+ public static string GetCredentialsPath()
|
|
|
+ {
|
|
|
+ string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
|
|
|
+ String filePath = str_1 + @"\credentials.json";
|
|
|
+ CredentialsPath = filePath;
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
}
|