|
@@ -0,0 +1,307 @@
|
|
|
|
+using Google.Apis.Auth.OAuth2;
|
|
|
|
+using Google.Apis.Download;
|
|
|
|
+using Google.Apis.Drive.v3;
|
|
|
|
+using Google.Apis.Services;
|
|
|
|
+using Google.Apis.Util.Store;
|
|
|
|
+using PDF_Office.Model.CloudDrive;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.IO;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+
|
|
|
|
+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; }
|
|
|
|
+ private List<GoogleDriveFiles> GoogleDriveFilesList = new List<GoogleDriveFiles>();
|
|
|
|
+
|
|
|
|
+ public GoogleDriveUserItem()
|
|
|
|
+ {
|
|
|
|
+ User = new GoogleDriveUser();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #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 对用户账号处理
|
|
|
|
+
|
|
|
|
+ public async Task<bool> RemoveUser()
|
|
|
|
+ {
|
|
|
|
+ bool result = false;
|
|
|
|
+ if (User.CurrentCredential != null)
|
|
|
|
+ {
|
|
|
|
+ result = await User.CurrentCredential.RevokeTokenAsync(CancellationToken.None);
|
|
|
|
+ }
|
|
|
|
+ if (result == true)
|
|
|
|
+ User.CurrentCredential = null;
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取帐号
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns>返回用户帐号地址</returns>
|
|
|
|
+ public string GetUserAcount()
|
|
|
|
+ {
|
|
|
|
+ string userAcount = "";
|
|
|
|
+ if (Service != null)
|
|
|
|
+ {
|
|
|
|
+ var about = Service.About.Get();
|
|
|
|
+ about.Fields = "user";
|
|
|
|
+ userAcount = about.Execute().User.EmailAddress;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return userAcount;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ #region 文件夹
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ #region 文件
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 对单个用户,获取文件列表
|
|
|
|
+ /// </summary>
|
|
|
|
+ public async Task<List<GoogleDriveFiles>> GetDriveFiles(DriveService service = null)
|
|
|
|
+ {
|
|
|
|
+ if (service == null)
|
|
|
|
+ service = await GetServiceAsync();
|
|
|
|
+
|
|
|
|
+ // define parameters of request.
|
|
|
|
+ FilesResource.ListRequest FileListRequest = service.Files.List();
|
|
|
|
+
|
|
|
|
+ //listRequest.PageSize = 10;
|
|
|
|
+ //listRequest.PageToken = 10;
|
|
|
|
+ FileListRequest.Fields = "nextPageToken, files(id, name, size, version, createdTime)";
|
|
|
|
+
|
|
|
|
+ //get file list.
|
|
|
|
+ IList<Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;
|
|
|
|
+ List<GoogleDriveFiles> FileList = new List<GoogleDriveFiles>();
|
|
|
|
+
|
|
|
|
+ if (files != null && files.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ foreach (var file in files)
|
|
|
|
+ {
|
|
|
|
+ GoogleDriveFiles File = new GoogleDriveFiles
|
|
|
|
+ {
|
|
|
|
+ Id = file.Id,
|
|
|
|
+ Name = file.Name,
|
|
|
|
+ Size = file.Size,
|
|
|
|
+ Version = file.Version,
|
|
|
|
+ CreatedTime = file.CreatedTime
|
|
|
|
+ };
|
|
|
|
+ FileList.Add(File);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return FileList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 上传文件
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="filepath">本地文件路径</param>
|
|
|
|
+ public async Task<bool> FileUpload(string filepath)
|
|
|
|
+ {
|
|
|
|
+ if (Service == null)
|
|
|
|
+ Service = await GetServiceAsync();
|
|
|
|
+
|
|
|
|
+ var FileMetaData = new Google.Apis.Drive.v3.Data.File();
|
|
|
|
+ var str = filepath.LastIndexOf("\\");
|
|
|
|
+ var str2 = filepath.Substring(str + 1, filepath.Length - str - 1);
|
|
|
|
+
|
|
|
|
+ FileMetaData.Name = str2;
|
|
|
|
+ FileMetaData.MimeType = "";
|
|
|
|
+
|
|
|
|
+ FilesResource.CreateMediaUpload request;
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ using (var stream = new System.IO.FileStream(filepath, System.IO.FileMode.Open))
|
|
|
|
+ {
|
|
|
|
+ request = Service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
|
|
|
|
+ request.Fields = "id";
|
|
|
|
+ request.Upload();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ catch
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Download file from Google Drive by fileId.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="googleDriveFiles"></param>
|
|
|
|
+ /// <param name="savePath"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ //
|
|
|
|
+ public async Task<string> DownloadGoogleFile(GoogleDriveFiles googleDriveFiles, string savePath)
|
|
|
|
+ {
|
|
|
|
+ if (Service == null)
|
|
|
|
+ Service = await GetServiceAsync();
|
|
|
|
+
|
|
|
|
+ string fileId = googleDriveFiles.Id;
|
|
|
|
+ if (string.IsNullOrEmpty(fileId))
|
|
|
|
+ return "";
|
|
|
|
+
|
|
|
|
+ //fileId = DriveFiles[0].Id;
|
|
|
|
+
|
|
|
|
+ FilesResource.GetRequest request = Service.Files.Get(fileId);
|
|
|
|
+
|
|
|
|
+ string FileName = request.Execute().Name;
|
|
|
|
+ string FilePath = System.IO.Path.Combine(savePath, FileName);
|
|
|
|
+
|
|
|
|
+ MemoryStream stream = new MemoryStream();
|
|
|
|
+
|
|
|
|
+ // Add a handler which will be notified on progress changes.
|
|
|
|
+ // It will notify on each chunk download and when the
|
|
|
|
+ // download is completed or failed.
|
|
|
|
+ request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
|
|
|
|
+ {
|
|
|
|
+ switch (progress.Status)
|
|
|
|
+ {
|
|
|
|
+ case DownloadStatus.Downloading:
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine(progress.BytesDownloaded);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ case DownloadStatus.Completed:
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine("Download complete.");
|
|
|
|
+ SaveStream(stream, FilePath);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ case DownloadStatus.Failed:
|
|
|
|
+ {
|
|
|
|
+ Console.WriteLine("Download failed.");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ request.Download(stream);
|
|
|
|
+
|
|
|
|
+ if (string.IsNullOrEmpty(savePath) == false)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ return FilePath;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void SaveStream(MemoryStream stream, string FilePath)
|
|
|
|
+ {
|
|
|
|
+ using (System.IO.FileStream file = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite))
|
|
|
|
+ {
|
|
|
|
+ stream.WriteTo(file);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+ }
|
|
|
|
+}
|