GoogleDriveUserItem.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using Google.Apis.Auth.OAuth2;
  2. using Google.Apis.Download;
  3. using Google.Apis.Drive.v3;
  4. using Google.Apis.Services;
  5. using Google.Apis.Util.Store;
  6. using PDF_Office.Model.CloudDrive;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace PDF_Office.ViewModels.HomePanel.CloudDrive
  15. {
  16. //对单用户账号处理核心功能
  17. public class GoogleDriveUserItem
  18. {
  19. public GoogleDriveUser User { get; set; }
  20. public DriveService Service { get; set; }
  21. private List<GoogleDriveFiles> GoogleDriveFilesList = new List<GoogleDriveFiles>();
  22. public GoogleDriveUserItem()
  23. {
  24. User = new GoogleDriveUser();
  25. }
  26. #region 对用户账号处理
  27. public async Task<bool> RemoveUser()
  28. {
  29. bool result = false;
  30. if (User.CurrentCredential != null)
  31. {
  32. result = await User.CurrentCredential.RevokeTokenAsync(CancellationToken.None);
  33. }
  34. if (result == true)
  35. User.CurrentCredential = null;
  36. return result;
  37. }
  38. /// <summary>
  39. /// 获取帐号
  40. /// </summary>
  41. /// <returns>返回用户帐号地址</returns>
  42. private async Task<string> GetUserAcount()
  43. {
  44. string userAcount = "";
  45. if (Service != null)
  46. {
  47. var about = Service.About.Get();
  48. about.Fields = "user";
  49. userAcount = about.Execute().User.EmailAddress;
  50. }
  51. return userAcount;
  52. }
  53. /// <summary>
  54. /// 获取帐号
  55. /// </summary>
  56. /// <returns>返回用户帐号地址</returns>
  57. public async Task<string> GetUserAcountAsync()
  58. {
  59. return await GetUserAcount();
  60. }
  61. #endregion
  62. #region 文件夹
  63. #endregion
  64. #region 文件
  65. /// <summary>
  66. /// 对单个用户,获取文件列表
  67. /// </summary>
  68. public async Task<List<GoogleDriveFiles>> GetDriveFiles(DriveService service = null)
  69. {
  70. if (service == null)
  71. return null;
  72. // define parameters of request.
  73. FilesResource.ListRequest FileListRequest = service.Files.List();
  74. //listRequest.PageSize = 10;
  75. //listRequest.PageToken = 10;
  76. FileListRequest.Fields = "nextPageToken, files(id, name, size, version, createdTime)";
  77. //get file list.
  78. IList<Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;
  79. List<GoogleDriveFiles> FileList = new List<GoogleDriveFiles>();
  80. if (files != null && files.Count > 0)
  81. {
  82. foreach (var file in files)
  83. {
  84. GoogleDriveFiles File = new GoogleDriveFiles
  85. {
  86. Id = file.Id,
  87. Name = file.Name,
  88. Size = file.Size,
  89. Version = file.Version,
  90. CreatedTime = file.CreatedTime
  91. };
  92. FileList.Add(File);
  93. }
  94. }
  95. return FileList;
  96. }
  97. /// <summary>
  98. /// 上传文件
  99. /// </summary>
  100. /// <param name="filepath">本地文件路径</param>
  101. public async Task<bool> FileUpload(string filepath)
  102. {
  103. if (Service == null)
  104. return false;
  105. var FileMetaData = new Google.Apis.Drive.v3.Data.File();
  106. var str = filepath.LastIndexOf("\\");
  107. var str2 = filepath.Substring(str + 1, filepath.Length - str - 1);
  108. FileMetaData.Name = str2;
  109. FileMetaData.MimeType = "";
  110. FilesResource.CreateMediaUpload request;
  111. try
  112. {
  113. using (var stream = new System.IO.FileStream(filepath, System.IO.FileMode.Open))
  114. {
  115. request = Service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
  116. request.Fields = "id";
  117. request.Upload();
  118. }
  119. return true;
  120. }
  121. catch
  122. {
  123. return false;
  124. }
  125. }
  126. /// <summary>
  127. /// Download file from Google Drive by fileId.
  128. /// </summary>
  129. /// <param name="googleDriveFiles"></param>
  130. /// <param name="savePath"></param>
  131. /// <returns></returns>
  132. //
  133. public async Task<string> DownloadGoogleFile(GoogleDriveFiles googleDriveFiles, string savePath)
  134. {
  135. if (Service == null)
  136. return "";
  137. string fileId = googleDriveFiles.Id;
  138. if (string.IsNullOrEmpty(fileId))
  139. return "";
  140. //fileId = DriveFiles[0].Id;
  141. FilesResource.GetRequest request = Service.Files.Get(fileId);
  142. string FileName = request.Execute().Name;
  143. string FilePath = System.IO.Path.Combine(savePath, FileName);
  144. MemoryStream stream = new MemoryStream();
  145. // Add a handler which will be notified on progress changes.
  146. // It will notify on each chunk download and when the
  147. // download is completed or failed.
  148. request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
  149. {
  150. switch (progress.Status)
  151. {
  152. case DownloadStatus.Downloading:
  153. {
  154. Console.WriteLine(progress.BytesDownloaded);
  155. break;
  156. }
  157. case DownloadStatus.Completed:
  158. {
  159. Console.WriteLine("Download complete.");
  160. SaveStream(stream, FilePath);
  161. break;
  162. }
  163. case DownloadStatus.Failed:
  164. {
  165. Console.WriteLine("Download failed.");
  166. break;
  167. }
  168. }
  169. };
  170. request.Download(stream);
  171. if (string.IsNullOrEmpty(savePath) == false)
  172. {
  173. }
  174. return FilePath;
  175. }
  176. private static void SaveStream(MemoryStream stream, string FilePath)
  177. {
  178. using (System.IO.FileStream file = new FileStream(FilePath, FileMode.Create, FileAccess.ReadWrite))
  179. {
  180. stream.WriteTo(file);
  181. }
  182. }
  183. #endregion
  184. }
  185. }