GoogleDriveStorageClient.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Net;
  6. using FastReport.Export;
  7. using FastReport.Cloud.OAuth;
  8. using FastReport.Cloud.StorageClient.SkyDrive;
  9. using FastReport.Utils;
  10. namespace FastReport.Cloud.StorageClient.GoogleDrive
  11. {
  12. /// <summary>
  13. /// Google Drive cloud storage client.
  14. /// </summary>
  15. public class GoogleDriveStorageClient : CloudStorageClient
  16. {
  17. #region Fields
  18. private ClientInfo clientInfo;
  19. private string authCode;
  20. private string accessToken;
  21. #endregion // Fields
  22. #region Properties
  23. /// <summary>
  24. /// Gets or sets the client info.
  25. /// </summary>
  26. public ClientInfo ClientInfo
  27. {
  28. get { return clientInfo; }
  29. set { clientInfo = value; }
  30. }
  31. /// <summary>
  32. /// Gets or sets the authorization code.
  33. /// </summary>
  34. public string AuthCode
  35. {
  36. get { return authCode; }
  37. set { authCode = value; }
  38. }
  39. /// <summary>
  40. /// Gets or sets the access token.
  41. /// </summary>
  42. public string AccessToken
  43. {
  44. get { return accessToken; }
  45. set { accessToken = value; }
  46. }
  47. #endregion // Properties
  48. #region Constructors
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="GoogleDriveStorageClient"/> class.
  51. /// </summary>
  52. public GoogleDriveStorageClient() : base()
  53. {
  54. this.clientInfo = new ClientInfo("", "", "");
  55. authCode = "";
  56. accessToken = "";
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="GoogleDriveStorageClient"/> class.
  60. /// </summary>
  61. /// <param name="clientInfo">The storage client info.</param>
  62. public GoogleDriveStorageClient(ClientInfo clientInfo) : base()
  63. {
  64. this.clientInfo = clientInfo;
  65. authCode = "";
  66. accessToken = "";
  67. }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="GoogleDriveStorageClient"/> class.
  70. /// </summary>
  71. /// <param name="clientId">Client ID.</param>
  72. /// <param name="clientSecret">Client Secret.</param>
  73. public GoogleDriveStorageClient(string clientId, string clientSecret) : base()
  74. {
  75. this.clientInfo = new ClientInfo("", clientId, clientSecret);
  76. authCode = "";
  77. accessToken = "";
  78. }
  79. #endregion // Constructors
  80. #region Private Methods
  81. private byte[] BuildGetAccessTokenRequestContent()
  82. {
  83. Dictionary<string, string> data = new Dictionary<string, string>();
  84. data.Add("code", authCode);
  85. data.Add("client_id", clientInfo.Id);
  86. data.Add("client_secret", clientInfo.Secret);
  87. data.Add("redirect_uri", @"urn:ietf:wg:oauth:2.0:oob");
  88. data.Add("grant_type", "authorization_code");
  89. return Encoding.UTF8.GetBytes(HttpUtils.UrlDataEncode(data));
  90. }
  91. #endregion // Private Methods
  92. #region Protected Methods
  93. /// <inheritdoc/>
  94. protected override void SaveMemoryStream(MemoryStream ms)
  95. {
  96. try
  97. {
  98. System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  99. string uri = String.Format(@"https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token={0}", accessToken);
  100. WebRequest request = WebRequest.Create(uri);
  101. request.Method = HttpMethod.Post;
  102. RequestUtils.SetProxySettings(request, ProxySettings);
  103. request.ContentType = "multipart/related; boundary=foo_bar_baz";
  104. List<byte> content = new List<byte>();
  105. StringBuilder sb = new StringBuilder("--foo_bar_baz\r\n");
  106. sb.Append("Content-Type: application/json; charset=UTF-8\r\n");
  107. sb.Append("\r\n");
  108. sb.Append("{\r\n");
  109. sb.AppendFormat("\"title\": \"{0}\"\r\n", Filename);
  110. sb.Append("}\r\n");
  111. sb.Append("\r\n");
  112. sb.Append("--foo_bar_baz\r\n");
  113. sb.Append("Content-Type: application/octet-stream\r\n");
  114. sb.Append("\r\n");
  115. content.AddRange(Encoding.UTF8.GetBytes(sb.ToString()));
  116. int msLength = Convert.ToInt32(ms.Length);
  117. byte[] msBuffer = new byte[msLength];
  118. ms.Read(msBuffer, 0, msLength);
  119. content.AddRange(msBuffer);
  120. sb = new StringBuilder("\r\n");
  121. sb.Append("--foo_bar_baz--");
  122. content.AddRange(Encoding.UTF8.GetBytes(sb.ToString()));
  123. int length = content.Count;
  124. byte[] buffer = new byte[length];
  125. buffer = content.ToArray();
  126. request.ContentLength = buffer.Length;
  127. using (Stream rs = request.GetRequestStream())
  128. {
  129. rs.Write(buffer, 0, buffer.Length);
  130. }
  131. WebResponse response = request.GetResponse();
  132. Stream stream = response.GetResponseStream();
  133. }
  134. catch (Exception ex)
  135. {
  136. throw new CloudStorageException(ex.Message, ex);
  137. }
  138. }
  139. #endregion // Proteced Methods
  140. #region Public Methods
  141. /// <summary>
  142. /// Gets the authorization URL.
  143. /// </summary>
  144. /// <returns>The authorization URL stirng.</returns>
  145. public string GetAuthorizationUrl()
  146. {
  147. return String.Format(@"https://accounts.google.com/o/oauth2/auth?scope={0}&redirect_uri={1}&response_type=code&client_id={2}",
  148. "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file",
  149. "urn:ietf:wg:oauth:2.0:oob", clientInfo.Id);
  150. }
  151. /// <summary>
  152. /// Gets the access token.
  153. /// </summary>
  154. /// <returns>The access token string.</returns>
  155. public string GetAccessToken()
  156. {
  157. System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  158. string uri = String.Format(@"?client_id={0}&client_secret={1}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code&code={2}", clientInfo.Id, clientInfo.Secret, authCode);
  159. WebRequest request = WebRequest.Create(@"https://accounts.google.com/o/oauth2/token");
  160. request.Method = HttpMethod.Post;
  161. RequestUtils.SetProxySettings(request, ProxySettings);
  162. request.ContentType = "application/x-www-form-urlencoded";
  163. byte[] content = BuildGetAccessTokenRequestContent();
  164. request.ContentLength = content.Length;
  165. //!!! need exception
  166. using (Stream rs = request.GetRequestStream())
  167. {
  168. rs.Write(content, 0, content.Length);
  169. }
  170. WebResponse response = request.GetResponse();
  171. accessToken = Parser.ParseGoogleDriveToken(response.GetResponseStream());
  172. return accessToken;
  173. }
  174. #endregion // Public Methods
  175. }
  176. }