FastCloudStorageClient.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Utils;
  9. namespace FastReport.Cloud.StorageClient.FastCloud
  10. {
  11. /// <summary>
  12. /// FastCloud storage client.
  13. /// </summary>
  14. public class FastCloudStorageClient : CloudStorageClient
  15. {
  16. #region Constants
  17. private const string HOST = "https://cloud.fast-report.com";
  18. #endregion // Constants
  19. #region Fields
  20. private string accessToken;
  21. private string reportUrl;
  22. #endregion // Fields
  23. #region Properties
  24. /// <summary>
  25. /// Gets or sets the access token.
  26. /// </summary>
  27. public string AccessToken
  28. {
  29. get { return accessToken; }
  30. set { accessToken = value; }
  31. }
  32. /// <summary>
  33. /// Gets the report URL that can be used to download report from cloud.
  34. /// </summary>
  35. public string ReportUrl
  36. {
  37. get { return reportUrl; }
  38. }
  39. #endregion // Properties
  40. #region Constructors
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="FastCloudStorageClient"/> class.
  43. /// </summary>
  44. public FastCloudStorageClient() : base()
  45. {
  46. accessToken = "";
  47. reportUrl = "";
  48. }
  49. #endregion // Constructors
  50. #region Private Methods
  51. private byte[] BuildGetAccessTokenRequestContent(string email, string password)
  52. {
  53. Dictionary<string, string> data = new Dictionary<string, string>();
  54. data.Add("email", email);
  55. data.Add("password", password);
  56. return Encoding.UTF8.GetBytes(HttpUtils.UrlDataEncode(data));
  57. }
  58. #endregion // Private Methods
  59. #region Protected Methods
  60. /// <inheritdoc/>
  61. protected override void SaveMemoryStream(MemoryStream ms)
  62. {
  63. try
  64. {
  65. System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  66. string uri = String.Format(HOST + @"/api/v1/reports.json?user_token={0}", accessToken);
  67. WebRequest request = WebRequest.Create(uri);
  68. request.Method = HttpMethod.Post;
  69. RequestUtils.SetProxySettings(request, ProxySettings);
  70. request.ContentType = "multipart/form-data; boundary=foo_bar_baz";
  71. List<byte> content = new List<byte>();
  72. StringBuilder sb = new StringBuilder("--foo_bar_baz\r\n");
  73. sb.Append(String.Format("Content-Disposition: form-data; name=\"report[frx_file]\"; filename=\"{0}\"\r\n", Filename));
  74. sb.Append("Content-Type: application/octet-stream\r\n");
  75. sb.Append("\r\n");
  76. content.AddRange(Encoding.UTF8.GetBytes(sb.ToString()));
  77. int msLength = Convert.ToInt32(ms.Length);
  78. byte[] msBuffer = new byte[msLength];
  79. ms.Read(msBuffer, 0, msLength);
  80. content.AddRange(msBuffer);
  81. sb = new StringBuilder("--foo_bar_baz--");
  82. content.AddRange(Encoding.UTF8.GetBytes(sb.ToString()));
  83. int length = content.Count;
  84. byte[] buffer = new byte[length];
  85. buffer = content.ToArray();
  86. request.ContentLength = buffer.Length;
  87. using (Stream rs = request.GetRequestStream())
  88. {
  89. rs.Write(buffer, 0, buffer.Length);
  90. }
  91. string response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
  92. if (!String.IsNullOrEmpty(response))
  93. {
  94. int startIndex = response.IndexOf("id\":") + 5;
  95. int endIndex = response.IndexOf(",", startIndex);
  96. string id = response.Substring(startIndex, endIndex - startIndex - 1);
  97. reportUrl = HOST + "/reports/" + id;
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. throw new CloudStorageException(ex.Message, ex);
  103. }
  104. }
  105. #endregion // Protected Methods
  106. #region Public Methods
  107. /// <summary>
  108. /// Gets the access token.
  109. /// </summary>
  110. /// <returns>The access token string.</returns>
  111. public string GetAccessToken(string email, string password)
  112. {
  113. System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  114. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HOST + @"/api/v1/tokens.json");
  115. request.Method = HttpMethod.Post;
  116. RequestUtils.SetProxySettings(request, ProxySettings);
  117. request.ContentType = "application/x-www-form-urlencoded";
  118. byte[] content = BuildGetAccessTokenRequestContent(email, password);
  119. request.ContentLength = content.Length;
  120. using (Stream rs = request.GetRequestStream())
  121. {
  122. rs.Write(content, 0, content.Length);
  123. }
  124. try
  125. {
  126. WebResponse response = request.GetResponse();
  127. accessToken = Parser.ParseFastCloudToken(response.GetResponseStream());
  128. }
  129. catch (Exception ex)
  130. {
  131. if (!Config.WebMode)
  132. {
  133. FRMessageBox.Error(ex.Message);
  134. }
  135. }
  136. return accessToken;
  137. }
  138. #endregion // Public Methods
  139. }
  140. }