DropboxStorageClient.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.IO;
  5. using FastReport.Export;
  6. using FastReport.Utils;
  7. namespace FastReport.Cloud.StorageClient.Dropbox
  8. {
  9. /// <summary>
  10. /// Dropbox cloud storage client.
  11. /// </summary>
  12. public class DropboxStorageClient : CloudStorageClient
  13. {
  14. #region Constants
  15. /// <summary>
  16. /// The base URL for files_put command.
  17. /// </summary>
  18. public const string FILES_PUT_URL_BASE = "https://content.dropboxapi.com/2/files/upload";
  19. #endregion // Constants
  20. #region Fields
  21. private string accessToken;
  22. #endregion // Fields
  23. #region Properties
  24. /// <summary>
  25. /// Gets or sets the application access token.
  26. /// </summary>
  27. public string AccessToken
  28. {
  29. get { return accessToken; }
  30. set { accessToken = value; }
  31. }
  32. #endregion // Properties
  33. #region Constructors
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="DropboxStorageClient"/> class.
  36. /// </summary>
  37. public DropboxStorageClient() : base()
  38. {
  39. accessToken = "";
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="DropboxStorageClient"/> class.
  43. /// </summary>
  44. /// <param name="accessToken">The Dropbox application access token.</param>
  45. public DropboxStorageClient(string accessToken) : base()
  46. {
  47. this.accessToken = accessToken;
  48. }
  49. #endregion // Constructors
  50. #region Protected Methods
  51. /// <inheritdoc/>
  52. protected override void SaveMemoryStream(MemoryStream ms)
  53. {
  54. try
  55. {
  56. System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  57. HttpWebRequest request = WebRequest.Create(FILES_PUT_URL_BASE) as HttpWebRequest;
  58. request.Method = HttpMethod.Post;
  59. RequestUtils.SetProxySettings(request, ProxySettings);
  60. request.Headers.Add("Authorization", "Bearer " + accessToken);
  61. request.Headers.Add("Dropbox-API-Arg", "{\"path\": \"/" + Filename + "\",\"mode\": \"overwrite\",\"autorename\": false,\"mute\": false}");
  62. request.ContentType = "application/octet-stream";
  63. int msLength = Convert.ToInt32(ms.Length);
  64. byte[] msBuffer = new byte[msLength];
  65. ms.Read(msBuffer, 0, msLength);
  66. List<byte> content = new List<byte>();
  67. content.AddRange(msBuffer);
  68. int length = content.Count;
  69. byte[] buffer = new byte[length];
  70. buffer = content.ToArray();
  71. request.ContentLength = buffer.Length;
  72. using (Stream rs = request.GetRequestStream())
  73. {
  74. rs.Write(buffer, 0, buffer.Length);
  75. }
  76. WebResponse response = request.GetResponse();
  77. Stream stream = response.GetResponseStream();
  78. }
  79. catch (Exception ex)
  80. {
  81. throw new CloudStorageException(ex.Message, ex);
  82. }
  83. }
  84. #endregion // Protected Methods
  85. }
  86. }