From 5a20a9ce5671d12ac82688dd6e14d14052c87842 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 5 Oct 2020 17:40:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=9D=99=E6=80=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=99=9A=E6=8B=9F=E7=9B=AE=E5=BD=95=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mozi.HttpEmbedded.Test/Program.cs | 1 - Mozi.HttpEmbedded.Test/admin/Readme.md | 1 + Mozi.HttpEmbedded/HttpServer.cs | 14 +++ Mozi.HttpEmbedded/Source/StaticFiles.cs | 144 ++++++++++++++++++++---- 4 files changed, 138 insertions(+), 22 deletions(-) diff --git a/Mozi.HttpEmbedded.Test/Program.cs b/Mozi.HttpEmbedded.Test/Program.cs index f51b1e3..e3421da 100644 --- a/Mozi.HttpEmbedded.Test/Program.cs +++ b/Mozi.HttpEmbedded.Test/Program.cs @@ -17,7 +17,6 @@ namespace Mozi.HttpEmbedded.Test //开启静态文件支持 hs.UseStaticFiles(""); - //路由映射 Router router = Router.Default; router.Map("services/{controller}/{id}"); diff --git a/Mozi.HttpEmbedded.Test/admin/Readme.md b/Mozi.HttpEmbedded.Test/admin/Readme.md index 1508317..482ea30 100644 --- a/Mozi.HttpEmbedded.Test/admin/Readme.md +++ b/Mozi.HttpEmbedded.Test/admin/Readme.md @@ -4,5 +4,6 @@ ## 三方库引用说明 + [Clearmin][1] + [Codemirror][2] + [1]:https://github.com/paomedia/clearmin [2]:https://github.com/codemirror/codemirror \ No newline at end of file diff --git a/Mozi.HttpEmbedded/HttpServer.cs b/Mozi.HttpEmbedded/HttpServer.cs index 5899958..cb997ea 100644 --- a/Mozi.HttpEmbedded/HttpServer.cs +++ b/Mozi.HttpEmbedded/HttpServer.cs @@ -339,6 +339,20 @@ namespace Mozi.HttpEmbedded return this; } /// + /// 配置虚拟目录 + /// + /// + /// + /// + public HttpServer SetVirtualDirectory(string name,string path) + { + if (StaticFiles.Default.Enabled) + { + StaticFiles.Default.SetVirtualDirectory(name, path); + } + return this; + } + /// /// 启用WebDav /// /// diff --git a/Mozi.HttpEmbedded/Source/StaticFiles.cs b/Mozi.HttpEmbedded/Source/StaticFiles.cs index 20afa9a..d01d7b6 100644 --- a/Mozi.HttpEmbedded/Source/StaticFiles.cs +++ b/Mozi.HttpEmbedded/Source/StaticFiles.cs @@ -1,11 +1,13 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; namespace Mozi.HttpEmbedded.Source { //TODO 加入扩展名黑名单 - //TODO 加入虚拟目录 + //DONE 加入虚拟目录 /// /// 静态资源管理 /// @@ -14,7 +16,6 @@ namespace Mozi.HttpEmbedded.Source public bool Enabled { get; set; } private string _root; - private static StaticFiles _staticfiles; public static StaticFiles Default @@ -25,9 +26,14 @@ namespace Mozi.HttpEmbedded.Source /// 静态文件根路径 本地磁盘路径 /// public string RootDirectory { get { return _root; } } + /// + /// 虚拟路径 + /// + public List VirtualDirs = new List(); private StaticFiles() { + //初始化根路径为AppDomain基目录 _root = AppDomain.CurrentDomain.BaseDirectory; init(); } @@ -59,11 +65,35 @@ namespace Mozi.HttpEmbedded.Source /// /// 设置虚拟目录 /// - /// + /// 虚拟路径名 + /// 磁盘路径 /// - public StaticFiles SetVirtualRoot(string vroot) + /// 虚拟路径名不能与ROOT路径的子路径名重复,否则设置会被忽略 + public StaticFiles SetVirtualDirectory(string name, string realpath) { - throw new NotImplementedException(); + if (!realpath.EndsWith("\\")) + { + realpath = realpath + "\\"; + } + + var dir = VirtualDirs.Find(x => x.Name.Equals(name)); + if (dir != null) + { + dir.Path = realpath; + } + else + { + if (!string.IsNullOrEmpty(name)) + { + DirectoryInfo rootdir = new DirectoryInfo(_root); + var dirs = rootdir.GetDirectories(); + if (!dirs.Any(a => a.Name.Equals(name))) + { + VirtualDirs.Add(new DirPublished() { Name = name, Path = realpath }); + } + } + } + return this; } /// /// 初始化 @@ -72,7 +102,6 @@ namespace Mozi.HttpEmbedded.Source { //载入MIME类型 } - /// /// 判断是否静态文件 /// @@ -94,7 +123,52 @@ namespace Mozi.HttpEmbedded.Source /// public bool Exists(string path, string ext) { - return System.IO.File.Exists(_root + path); + var filepath = _root + path; + if (IsVirtualFile(path)) + { + filepath = GetVirtualFilePhysicalDirectory(path); + } + return System.IO.File.Exists(filepath); + } + + private bool IsVirtualFile(string path) + { + foreach (var d in VirtualDirs) + { + var prefix = "/" + d.Name + "/"; + //Config/files1.xml; + if (path.StartsWith(prefix)) + { + return true; + } + else + { + continue; + } + } + return false; + } + /// + /// 取虚拟目录中的文件物理路径 + /// + /// + /// + private string GetVirtualFilePhysicalDirectory(string path) + { + foreach (var d in VirtualDirs) + { + var prefix = "/" + d.Name + "/"; + //Config/files1.xml; + if (path.StartsWith(prefix) && System.IO.File.Exists(d.Path + path.Substring(prefix.Length))) + { + return d.Path + path.Substring(prefix.Length); + } + else + { + continue; + } + } + return null; } /// /// 检查最后修改日期 @@ -104,7 +178,12 @@ namespace Mozi.HttpEmbedded.Source /// Modified public bool CheckIfModified(string path, string ifModifiedSince) { - DateTime dtModified=System.IO.File.GetLastWriteTime(_root + path); + var filepath = _root + path; + if (IsVirtualFile(path)) + { + filepath = GetVirtualFilePhysicalDirectory(path); + } + DateTime dtModified=System.IO.File.GetLastWriteTime(filepath); try { if (!string.IsNullOrEmpty(ifModifiedSince)) @@ -131,7 +210,12 @@ namespace Mozi.HttpEmbedded.Source /// public DateTime GetLastModified(string path) { - return System.IO.File.GetLastWriteTime(_root + path); + var filepath = _root + path; + if (IsVirtualFile(path)) + { + filepath = GetVirtualFilePhysicalDirectory(path); + } + return System.IO.File.GetLastWriteTime(filepath); } /// /// 提取文件流 @@ -141,7 +225,12 @@ namespace Mozi.HttpEmbedded.Source /// public byte[] Load(string path,string ext) { - using (FileStream fs = new FileStream(_root + path, FileMode.Open)) + var filepath = _root + path; + if (IsVirtualFile(path)) + { + filepath = GetVirtualFilePhysicalDirectory(path); + } + using (FileStream fs = new FileStream(filepath, FileMode.Open)) { byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); @@ -160,7 +249,12 @@ namespace Mozi.HttpEmbedded.Source { if (end > offset&&offset>=0) { - using (FileStream fs = new FileStream(_root+ path, FileMode.Open)) + var filepath = _root + path; + if (IsVirtualFile(path)) + { + filepath = GetVirtualFilePhysicalDirectory(path); + } + using (FileStream fs = new FileStream(filepath, FileMode.Open)) { byte[] data = new byte[fs.Length]; fs.Read(data, offset, end - offset + 1); @@ -172,15 +266,23 @@ namespace Mozi.HttpEmbedded.Source return new byte[] { }; } } - /// - /// 取文件大小 - /// - /// - /// - public long GetFileSize(string path) - { - FileInfo fi = new FileInfo(path); - return fi.Length; - } + ///// + ///// 取文件大小 + ///// + ///// + ///// + //public long GetFileSize(string path) + //{ + // FileInfo fi = new FileInfo(path); + // return fi.Length; + //} + } + /// + /// 发布的目录 + /// + internal sealed class DirPublished + { + public string Name { get; set; } + public string Path { get; set; } } } -- GitLab