DriveAccountService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Masuit.MyBlogs.Core.Extensions.DriveHelpers;
  2. using Masuit.MyBlogs.Core.Models.Drive;
  3. using Microsoft.Identity.Client;
  4. namespace Masuit.MyBlogs.Core.Infrastructure.Drive
  5. {
  6. public class DriveAccountService : IDriveAccountService
  7. {
  8. private readonly IConfidentialClientApplication _app;
  9. public DriveContext SiteContext { get; set; }
  10. /// <summary>
  11. /// Graph实例
  12. /// </summary>
  13. /// <value></value>
  14. public Microsoft.Graph.GraphServiceClient Graph { get; set; }
  15. public DriveAccountService(DriveContext siteContext, TokenService tokenService)
  16. {
  17. SiteContext = siteContext;
  18. _app = tokenService.app;
  19. Graph = tokenService.Graph;
  20. }
  21. /// <summary>
  22. /// 返回 Oauth 验证url
  23. /// </summary>
  24. /// <returns></returns>
  25. public async Task<string> GetAuthorizationRequestUrl()
  26. {
  27. var redirectUrl = await _app.GetAuthorizationRequestUrl(OneDriveConfiguration.Scopes).ExecuteAsync();
  28. return redirectUrl.AbsoluteUri;
  29. }
  30. /// <summary>
  31. /// 添加 SharePoint Site-ID 到数据库
  32. /// </summary>
  33. /// <param name="siteName"></param>
  34. /// <param name="nickName"></param>
  35. /// <returns></returns>
  36. public async Task AddSiteId(string siteName, string nickName)
  37. {
  38. Site site = new();
  39. //使用 Onedrive
  40. if (siteName == "onedrive")
  41. {
  42. site.Name = siteName;
  43. site.NickName = nickName;
  44. }
  45. else
  46. {
  47. using var httpClient = new HttpClient
  48. {
  49. Timeout = TimeSpan.FromSeconds(20)
  50. };
  51. var apiCaller = new ProtectedApiCallHelper(httpClient);
  52. await apiCaller.CallWebApiAndProcessResultASync($"{OneDriveConfiguration.GraphApi}/v1.0/sites/{OneDriveConfiguration.DominName}:/sites/{siteName}", GetToken(), result =>
  53. {
  54. site.SiteId = result.Properties().Single((prop) => prop.Name == "id").Value.ToString();
  55. site.Name = result.Properties().Single((prop) => prop.Name == "name").Value.ToString();
  56. site.NickName = nickName;
  57. });
  58. }
  59. if (!SiteContext.Sites.Any(s => s.SiteId == site.SiteId))
  60. {
  61. //若是首次添加则设置为默认的驱动器
  62. using (var setting = new SettingService(new DriveContext()))
  63. {
  64. if (!SiteContext.Sites.Any())
  65. {
  66. await setting.Set("DefaultDrive", site.Name);
  67. }
  68. }
  69. await SiteContext.Sites.AddAsync(site);
  70. await SiteContext.SaveChangesAsync();
  71. }
  72. else
  73. {
  74. throw new Exception("站点已被创建");
  75. }
  76. }
  77. public List<Site> GetSites()
  78. {
  79. List<Site> result = SiteContext.Sites.ToList();
  80. return result;
  81. }
  82. /// <summary>
  83. /// 获取 Drive Info
  84. /// </summary>
  85. /// <returns></returns>
  86. public async Task<List<DriveInfo>> GetDriveInfo()
  87. {
  88. var drivesInfo = new List<DriveInfo>();
  89. foreach (var item in SiteContext.Sites.ToArray())
  90. {
  91. Microsoft.Graph.Drive drive;
  92. //Onedrive
  93. if (string.IsNullOrEmpty(item.SiteId))
  94. {
  95. drive = await Graph.Me.Drive.Request().GetAsync();
  96. }
  97. else
  98. {
  99. drive = await Graph.Sites[item.SiteId].Drive.Request().GetAsync();
  100. }
  101. drivesInfo.Add(new DriveInfo()
  102. {
  103. Quota = drive.Quota,
  104. NickName = item.NickName,
  105. Name = item.Name,
  106. HiddenFolders = item.HiddenFolders
  107. });
  108. }
  109. return drivesInfo;
  110. }
  111. public async Task Unbind(string nickName)
  112. {
  113. SiteContext.Sites.Remove(SiteContext.Sites.Single(site => site.NickName == nickName));
  114. await SiteContext.SaveChangesAsync();
  115. }
  116. /// <summary>
  117. /// 获取 Token
  118. /// </summary>
  119. /// <returns></returns>
  120. public string GetToken()
  121. {
  122. return _app.AcquireTokenSilent(OneDriveConfiguration.Scopes, OneDriveConfiguration.AccountName).ExecuteAsync().Result.AccessToken;
  123. }
  124. public class DriveInfo
  125. {
  126. public Microsoft.Graph.Quota Quota { get; set; }
  127. public string NickName { get; set; }
  128. public string Name { get; set; }
  129. public string[] HiddenFolders { get; set; }
  130. }
  131. }
  132. }