ViewEngine.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using Vinno.FIS.Sonopost.Helpers;
  9. using Vinno.FIS.Sonopost.Managers;
  10. using Vinno.FIS.Sonopost.Managers.Interfaces;
  11. namespace Vinno.FIS.Sonopost.Assets
  12. {
  13. internal class ViewEngine
  14. {
  15. private readonly ConcurrentDictionary<string, byte[]> _fileSourceDic;
  16. private readonly string _namespace;
  17. public DateTime LastModifiedTime { get; set; }
  18. public ViewEngine()
  19. {
  20. _namespace = MethodBase.GetCurrentMethod().DeclaringType.Namespace;
  21. _fileSourceDic = new ConcurrentDictionary<string, byte[]>();
  22. LoadResourceFiles();
  23. }
  24. public byte[] FindSource(string fileName)
  25. {
  26. if (string.IsNullOrWhiteSpace(fileName) || fileName.IndexOf(".") < 0)
  27. {
  28. fileName = "/index.html";
  29. }
  30. var match = Regex.Match(fileName, "\\S*[\\.](css|gif|html|json|js|eot|svg|ttf|woff|woff2|jpg|png|ico|gz)");
  31. if (match.Success && match.Groups.Count > 1)
  32. {
  33. return GetSource(match.Value);
  34. }
  35. else
  36. {
  37. return null;
  38. }
  39. }
  40. private byte[] GetSource(string filePath)
  41. {
  42. string cacheKey = filePath.Replace("/", ".").Replace("-", "_").ToLower();
  43. if (_fileSourceDic.ContainsKey(cacheKey))
  44. {
  45. return _fileSourceDic[cacheKey];
  46. }
  47. return null;
  48. }
  49. private void LoadResourceFiles()
  50. {
  51. string resourcePath = $"{_namespace}.wwwroot";
  52. string[] sourceFileNames = GetType().Assembly.GetManifestResourceNames().Where(x => x.StartsWith(resourcePath)).ToArray();
  53. foreach (string fileName in sourceFileNames)
  54. {
  55. if (string.IsNullOrWhiteSpace(fileName)) continue;
  56. string key = fileName.Substring(resourcePath.Length).Replace("-", "_").ToLower();
  57. byte[] fileBytes = GetResourceFile(fileName);
  58. _fileSourceDic.TryAdd(key, fileBytes);
  59. }
  60. }
  61. /// <summary>
  62. /// 读取文件
  63. /// </summary>
  64. /// <param name="extType"></param>
  65. /// <param name="model"></param>
  66. /// <param name="fileStream"></param>
  67. /// <returns></returns>
  68. private byte[] GetFileSource(Stream fileStream)
  69. {
  70. byte[] bytes;
  71. if (fileStream != null)
  72. {
  73. using (fileStream)
  74. {
  75. bytes = new byte[fileStream.Length];
  76. fileStream.Read(bytes, 0, (int)fileStream.Length);
  77. }
  78. }
  79. else bytes = new byte[0];
  80. return bytes;
  81. }
  82. /// <summary>
  83. /// 获取资源文件
  84. /// </summary>
  85. /// <param name="filePath"></param>
  86. /// <returns></returns>
  87. private byte[] GetResourceFile(string filePath)
  88. {
  89. if (string.IsNullOrWhiteSpace(filePath)) return new byte[0];
  90. byte[] fileBytes;
  91. using (var fileStream = GetType().Assembly.GetManifestResourceStream(filePath))
  92. {
  93. fileBytes = GetFileSource(fileStream);
  94. }
  95. return fileBytes;
  96. }
  97. /// <summary>
  98. /// 读取某个路径下的文件
  99. /// </summary>
  100. /// <param name="filePath"></param>
  101. /// <returns></returns>
  102. private byte[] GetPathFile(string filePath)
  103. {
  104. if (string.IsNullOrWhiteSpace(filePath)) return new byte[0];
  105. var currentDir = AppDomain.CurrentDomain.BaseDirectory;
  106. filePath = Path.Combine(currentDir, "Assets", "wwwroot", filePath.Replace("/", "\\").TrimStart('\\'));
  107. if (File.Exists(filePath))
  108. {
  109. using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  110. {
  111. return GetFileSource(fileStream);
  112. }
  113. }
  114. return new byte[0];
  115. }
  116. public void HandleDownload(HttpListenerContext context)
  117. {
  118. string[] pathSplit = context.Request.Url.AbsolutePath.Trim('/').Split('/');
  119. if (pathSplit.Length < 2)
  120. {
  121. context.Response.StatusCode = HttpStatusCode.NotFound.GetHashCode();
  122. return;
  123. }
  124. string key = pathSplit[1].ToLowerInvariant();
  125. string path = null;
  126. switch (key)
  127. {
  128. case "dicom":
  129. {
  130. string id = context.Request.QueryString["id"];
  131. path = AppManager.Instance.GetManager<IRemedicalManager>().GetDicomFilePath(id);
  132. }
  133. break;
  134. }
  135. if (!string.IsNullOrWhiteSpace(path) && FileHelper.Exist(path))
  136. {
  137. string fileName = Path.GetFileName(path);
  138. context.Response.AddHeader("Content-Type", "application/octet-stream");
  139. context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
  140. FileHelper.CopyStream(path, context.Response.OutputStream);
  141. context.Response.StatusCode = HttpStatusCode.OK.GetHashCode();
  142. }
  143. else
  144. {
  145. context.Response.StatusCode = HttpStatusCode.NotFound.GetHashCode();
  146. }
  147. }
  148. }
  149. }