WebUtils.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #if !WASM
  2. using FastReport.Web.Infrastructure;
  3. #endif
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Net;
  8. using System.Text;
  9. namespace FastReport.Web
  10. {
  11. static class WebUtils
  12. {
  13. #region Extensions
  14. internal static bool IsNullOrEmpty(this string value)
  15. {
  16. return string.IsNullOrEmpty(value);
  17. }
  18. internal static bool IsNullOrWhiteSpace(this string value)
  19. {
  20. return string.IsNullOrWhiteSpace(value);
  21. }
  22. #endregion
  23. #region Static Methods
  24. internal static string MapPath(string path)
  25. {
  26. if (path.IsNullOrWhiteSpace())
  27. return path;
  28. if (Path.IsPathRooted(path))
  29. return path;
  30. #if !WASM
  31. return Path.Combine(FastReportGlobal.HostingEnvironment.ContentRootPath, path);
  32. #endif
  33. return string.Empty;
  34. }
  35. internal static string ToUrl(params string[] segments)
  36. {
  37. var sb = new StringBuilder();
  38. foreach (var segment in segments)
  39. {
  40. var trimmedSegment = segment.Trim('/', '\\');
  41. if (trimmedSegment.IsNullOrWhiteSpace())
  42. continue;
  43. sb.Append('/');
  44. sb.Append(trimmedSegment);
  45. }
  46. return sb.ToString();
  47. }
  48. //internal static string GetAppRoot(string path)
  49. //{
  50. // if (path.IndexOf("://") != -1)
  51. // return path;
  52. // return String.Concat(
  53. // FastReportGlobal.HostingEnvironment.ContentRootPath == "/" ? "" : FastReportGlobal.HostingEnvironment.ContentRootPath,
  54. // path.IndexOf("/") == 0 ? "" : "/",
  55. // path.Replace("~/", ""));
  56. //}
  57. internal static void Write(Stream stream, string value)
  58. {
  59. byte[] buf = Encoding.UTF8.GetBytes(value);
  60. stream.Write(buf, 0, buf.Length);
  61. }
  62. internal static bool IsPng(byte[] image)
  63. {
  64. byte[] pngHeader = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 };
  65. bool isPng = true;
  66. for (int i = 0; i < 8; i++)
  67. if (image[i] != pngHeader[i])
  68. {
  69. isPng = false;
  70. break;
  71. }
  72. return isPng;
  73. }
  74. #endregion
  75. }
  76. #if DESIGNER
  77. /// <summary>
  78. /// Event arguments for Save report from Designer
  79. /// </summary>
  80. public class SaveDesignedReportEventArgs : EventArgs
  81. {
  82. /// <summary>
  83. /// Contain the stream with designed report
  84. /// </summary>
  85. public Stream Stream;
  86. }
  87. #endif
  88. }