123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using System;
- using System.Collections.Concurrent;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Reflection;
- using System.Text.RegularExpressions;
- using Vinno.FIS.Sonopost.Helpers;
- using Vinno.FIS.Sonopost.Managers;
- using Vinno.FIS.Sonopost.Managers.Interfaces;
- namespace Vinno.FIS.Sonopost.Assets
- {
- internal class ViewEngine
- {
- private readonly ConcurrentDictionary<string, byte[]> _fileSourceDic;
- private readonly string _namespace;
- public DateTime LastModifiedTime { get; set; }
- public ViewEngine()
- {
- _namespace = MethodBase.GetCurrentMethod().DeclaringType.Namespace;
- _fileSourceDic = new ConcurrentDictionary<string, byte[]>();
- LoadResourceFiles();
- }
- public byte[] FindSource(string fileName)
- {
- if (string.IsNullOrWhiteSpace(fileName) || fileName.IndexOf(".") < 0)
- {
- fileName = "/index.html";
- }
- var match = Regex.Match(fileName, "\\S*[\\.](css|gif|html|json|js|eot|svg|ttf|woff|woff2|jpg|png|ico|gz)");
- if (match.Success && match.Groups.Count > 1)
- {
- return GetSource(match.Value);
- }
- else
- {
- return null;
- }
- }
- private byte[] GetSource(string filePath)
- {
- string cacheKey = filePath.Replace("/", ".").Replace("-", "_").ToLower();
- if (_fileSourceDic.ContainsKey(cacheKey))
- {
- return _fileSourceDic[cacheKey];
- }
- return null;
- }
- private void LoadResourceFiles()
- {
- string resourcePath = $"{_namespace}.wwwroot";
- string[] sourceFileNames = GetType().Assembly.GetManifestResourceNames().Where(x => x.StartsWith(resourcePath)).ToArray();
- foreach (string fileName in sourceFileNames)
- {
- if (string.IsNullOrWhiteSpace(fileName)) continue;
- string key = fileName.Substring(resourcePath.Length).Replace("-", "_").ToLower();
- byte[] fileBytes = GetResourceFile(fileName);
- _fileSourceDic.TryAdd(key, fileBytes);
- }
- }
- /// <summary>
- /// 读取文件
- /// </summary>
- /// <param name="extType"></param>
- /// <param name="model"></param>
- /// <param name="fileStream"></param>
- /// <returns></returns>
- private byte[] GetFileSource(Stream fileStream)
- {
- byte[] bytes;
- if (fileStream != null)
- {
- using (fileStream)
- {
- bytes = new byte[fileStream.Length];
- fileStream.Read(bytes, 0, (int)fileStream.Length);
- }
- }
- else bytes = new byte[0];
- return bytes;
- }
- /// <summary>
- /// 获取资源文件
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private byte[] GetResourceFile(string filePath)
- {
- if (string.IsNullOrWhiteSpace(filePath)) return new byte[0];
- byte[] fileBytes;
- using (var fileStream = GetType().Assembly.GetManifestResourceStream(filePath))
- {
- fileBytes = GetFileSource(fileStream);
- }
- return fileBytes;
- }
- /// <summary>
- /// 读取某个路径下的文件
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- private byte[] GetPathFile(string filePath)
- {
- if (string.IsNullOrWhiteSpace(filePath)) return new byte[0];
- var currentDir = AppDomain.CurrentDomain.BaseDirectory;
- filePath = Path.Combine(currentDir, "Assets", "wwwroot", filePath.Replace("/", "\\").TrimStart('\\'));
- if (File.Exists(filePath))
- {
- using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- return GetFileSource(fileStream);
- }
- }
- return new byte[0];
- }
- public void HandleDownload(HttpListenerContext context)
- {
- string[] pathSplit = context.Request.Url.AbsolutePath.Trim('/').Split('/');
- if (pathSplit.Length < 2)
- {
- context.Response.StatusCode = HttpStatusCode.NotFound.GetHashCode();
- return;
- }
- string key = pathSplit[1].ToLowerInvariant();
- string path = null;
- switch (key)
- {
- case "dicom":
- {
- string id = context.Request.QueryString["id"];
- path = AppManager.Instance.GetManager<IRemedicalManager>().GetDicomFilePath(id);
- }
- break;
- }
- if (!string.IsNullOrWhiteSpace(path) && FileHelper.Exist(path))
- {
- string fileName = Path.GetFileName(path);
- context.Response.AddHeader("Content-Type", "application/octet-stream");
- context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
- FileHelper.CopyStream(path, context.Response.OutputStream);
- context.Response.StatusCode = HttpStatusCode.OK.GetHashCode();
- }
- else
- {
- context.Response.StatusCode = HttpStatusCode.NotFound.GetHashCode();
- }
- }
- }
- }
|