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; set; } public DriveService Service { get; set; } private List GoogleDriveFilesList = new List(); public GoogleDriveUserItem() { User = new GoogleDriveUser(); } #region 对用户账号处理 public async Task RemoveUser() { bool result = false; if (User.CurrentCredential != null) { result = await User.CurrentCredential.RevokeTokenAsync(CancellationToken.None); } if (result == true) User.CurrentCredential = null; return result; } /// /// 获取帐号 /// /// 返回用户帐号地址 private async Task GetUserAcount() { string userAcount = ""; if (Service != null) { var about = Service.About.Get(); about.Fields = "user"; var uss = about.Execute().User; userAcount = uss.EmailAddress; } return userAcount; } /// /// 获取帐号 /// /// 返回用户帐号地址 public async Task GetUserAcountAsync() { return await GetUserAcount(); } #endregion #region 文件夹 #endregion #region 文件 /// /// 对单个用户,获取文件列表 /// public async Task> GetDriveFiles(DriveService service = null) { if (service == null) return null; // 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 files = FileListRequest.Execute().Files; List FileList = new List(); 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; } /// /// 上传文件 /// /// 本地文件路径 public async Task FileUpload(string filepath) { if (Service == null) return false; 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; } } /// /// Download file from Google Drive by fileId. /// /// /// /// // public async Task DownloadGoogleFile(GoogleDriveFiles googleDriveFiles, string savePath) { if (Service == null) return ""; 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 } }