CloudStorageClient.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using FastReport.Export;
  5. using FastReport.Utils;
  6. namespace FastReport.Cloud.StorageClient
  7. {
  8. /// <summary>
  9. /// The base class for all cloud storage clients.
  10. /// </summary>
  11. public abstract class CloudStorageClient
  12. {
  13. #region Fields
  14. private string filename;
  15. private bool isUserAuthorized;
  16. private CloudProxySettings proxySettings;
  17. #endregion // Fields
  18. #region Properties
  19. /// <summary>
  20. /// Gets or sets the filename.
  21. /// </summary>
  22. protected string Filename
  23. {
  24. get { return filename; }
  25. set { filename = value; }
  26. }
  27. /// <summary>
  28. /// Gets or set the information is user authorized or not.
  29. /// </summary>
  30. public bool IsUserAuthorized
  31. {
  32. get { return isUserAuthorized; }
  33. set { isUserAuthorized = value; }
  34. }
  35. /// <summary>
  36. /// Gets or sets the proxy settings of a client.
  37. /// </summary>
  38. public CloudProxySettings ProxySettings
  39. {
  40. get { return proxySettings; }
  41. set { proxySettings = value; }
  42. }
  43. #endregion // Properties
  44. #region Constructors
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="CloudStorageClient"/> class.
  47. /// </summary>
  48. public CloudStorageClient()
  49. {
  50. filename = "";
  51. isUserAuthorized = false;
  52. proxySettings = null;
  53. }
  54. #endregion // Constructors
  55. #region Protected Methods
  56. /// <summary>
  57. /// Prepares report before it will be saved to cloud storage.
  58. /// </summary>
  59. /// <param name="report">The report template.</param>
  60. /// <param name="export">The export filter.</param>
  61. /// <returns>Memory stream that contains prepared report.</returns>
  62. protected MemoryStream PrepareToSave(Report report, ExportBase export)
  63. {
  64. MemoryStream stream = new MemoryStream();
  65. if (export != null)
  66. {
  67. export.OpenAfterExport = false;
  68. if (!export.HasMultipleFiles)
  69. {
  70. export.Export(report, stream);
  71. }
  72. else
  73. {
  74. export.ExportAndZip(report, stream);
  75. }
  76. }
  77. else
  78. {
  79. report.PreparedPages.Save(stream);
  80. }
  81. filename = "Report";
  82. if (!String.IsNullOrEmpty(report.FileName))
  83. {
  84. filename = Path.GetFileNameWithoutExtension(report.FileName);
  85. }
  86. string ext = ".fpx";
  87. if (export != null)
  88. {
  89. if (!export.HasMultipleFiles)
  90. {
  91. ext = export.FileFilter.Substring(export.FileFilter.LastIndexOf('.'));
  92. }
  93. else
  94. {
  95. ext = ".zip";
  96. }
  97. }
  98. filename += ext;
  99. stream.Position = 0;
  100. return stream;
  101. }
  102. /// <summary>
  103. /// Creates a MemoryStream instance using a Stream instance.
  104. /// </summary>
  105. /// <param name="stream">The Stream instance that should be converted.</param>
  106. /// <returns>The MemoryStream instance.</returns>
  107. protected MemoryStream CreateMemoryStream(Stream stream)
  108. {
  109. long originalStreamPosition = stream.Position;
  110. stream.Position = 0;
  111. byte[] oneByte = new byte[1];
  112. List<byte> allBytes = new List<byte>();
  113. int bytesRead = 0;
  114. do
  115. {
  116. bytesRead = stream.Read(oneByte, 0, 1);
  117. if (bytesRead != 0)
  118. {
  119. allBytes.Add(oneByte[0]);
  120. }
  121. }
  122. while (bytesRead != 0);
  123. stream.Position = originalStreamPosition;
  124. MemoryStream ms = new MemoryStream(allBytes.ToArray());
  125. ms.Position = 0;
  126. return ms;
  127. }
  128. /// <summary>
  129. /// Saves a memory stream to cloud.
  130. /// </summary>
  131. /// <param name="ms">The memory stream that should be saved.</param>
  132. protected abstract void SaveMemoryStream(MemoryStream ms);
  133. #endregion // Protected Methods
  134. #region Public Methods
  135. /// <summary>
  136. /// Saves the report to cloud storage.
  137. /// </summary>
  138. /// <param name="report">The report template that should be saved.</param>
  139. /// <param name="export">The export filter that should export template before.</param>
  140. /// <exception cref="CloudStorageException"/>
  141. public void SaveReport(Report report, ExportBase export)
  142. {
  143. using (MemoryStream ms = PrepareToSave(report, export))
  144. {
  145. SaveMemoryStream(ms);
  146. }
  147. }
  148. /// <summary>
  149. /// Saves the stream to cloud storage.
  150. /// </summary>
  151. /// <param name="stream">The stream that contains report.</param>
  152. /// <param name="filename">The filename in which stream will be saved in cloud.</param>
  153. public void SaveStream(Stream stream, string filename)
  154. {
  155. Filename = filename;
  156. using (MemoryStream ms = CreateMemoryStream(stream))
  157. {
  158. SaveMemoryStream(ms);
  159. }
  160. }
  161. #endregion // Public Methods
  162. }
  163. }