Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sunxiaotianmg
Mozi.HttpEmbedded
提交
5a20a9ce
M
Mozi.HttpEmbedded
项目概览
sunxiaotianmg
/
Mozi.HttpEmbedded
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Mozi.HttpEmbedded
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5a20a9ce
编写于
10月 05, 2020
作者:
JasonWcx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加静态文件虚拟目录支持
上级
f42b38bf
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
138 addition
and
22 deletion
+138
-22
Mozi.HttpEmbedded.Test/Program.cs
Mozi.HttpEmbedded.Test/Program.cs
+0
-1
Mozi.HttpEmbedded.Test/admin/Readme.md
Mozi.HttpEmbedded.Test/admin/Readme.md
+1
-0
Mozi.HttpEmbedded/HttpServer.cs
Mozi.HttpEmbedded/HttpServer.cs
+14
-0
Mozi.HttpEmbedded/Source/StaticFiles.cs
Mozi.HttpEmbedded/Source/StaticFiles.cs
+123
-21
未找到文件。
Mozi.HttpEmbedded.Test/Program.cs
浏览文件 @
5a20a9ce
...
...
@@ -17,7 +17,6 @@ namespace Mozi.HttpEmbedded.Test
//开启静态文件支持
hs
.
UseStaticFiles
(
""
);
//路由映射
Router
router
=
Router
.
Default
;
router
.
Map
(
"services/{controller}/{id}"
);
...
...
Mozi.HttpEmbedded.Test/admin/Readme.md
浏览文件 @
5a20a9ce
...
...
@@ -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
Mozi.HttpEmbedded/HttpServer.cs
浏览文件 @
5a20a9ce
...
...
@@ -339,6 +339,20 @@ namespace Mozi.HttpEmbedded
return
this
;
}
/// <summary>
/// 配置虚拟目录
/// </summary>
/// <param name="name"></param>
/// <param name="path"></param>
/// <returns></returns>
public
HttpServer
SetVirtualDirectory
(
string
name
,
string
path
)
{
if
(
StaticFiles
.
Default
.
Enabled
)
{
StaticFiles
.
Default
.
SetVirtualDirectory
(
name
,
path
);
}
return
this
;
}
/// <summary>
/// 启用WebDav
/// </summary>
/// <param name="root"></param>
...
...
Mozi.HttpEmbedded/Source/StaticFiles.cs
浏览文件 @
5a20a9ce
using
System
;
using
System.Collections.Generic
;
using
System.Globalization
;
using
System.IO
;
using
System.Linq
;
namespace
Mozi.HttpEmbedded.Source
{
//TODO 加入扩展名黑名单
//
TODO
加入虚拟目录
//
DONE
加入虚拟目录
/// <summary>
/// 静态资源管理
/// </summary>
...
...
@@ -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
/// 静态文件根路径 本地磁盘路径
/// </summary>
public
string
RootDirectory
{
get
{
return
_root
;
}
}
/// <summary>
/// 虚拟路径
/// </summary>
public
List
<
DirPublished
>
VirtualDirs
=
new
List
<
DirPublished
>();
private
StaticFiles
()
{
//初始化根路径为AppDomain基目录
_root
=
AppDomain
.
CurrentDomain
.
BaseDirectory
;
init
();
}
...
...
@@ -59,11 +65,35 @@ namespace Mozi.HttpEmbedded.Source
/// <summary>
/// 设置虚拟目录
/// </summary>
/// <param name="vroot"></param>
/// <param name="name">虚拟路径名</param>
/// <param name="realpath">磁盘路径</param>
/// <returns></returns>
public
StaticFiles
SetVirtualRoot
(
string
vroot
)
/// <remarks>虚拟路径名不能与ROOT路径的子路径名重复,否则设置会被忽略</remarks>
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
;
}
/// <summary>
/// 初始化
...
...
@@ -72,7 +102,6 @@ namespace Mozi.HttpEmbedded.Source
{
//载入MIME类型
}
/// <summary>
/// 判断是否静态文件
/// </summary>
...
...
@@ -94,7 +123,52 @@ namespace Mozi.HttpEmbedded.Source
/// <returns></returns>
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
;
}
/// <summary>
/// 取虚拟目录中的文件物理路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
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
;
}
/// <summary>
/// 检查最后修改日期
...
...
@@ -104,7 +178,12 @@ namespace Mozi.HttpEmbedded.Source
/// <returns><see cref="bool:true">Modified</see></returns>
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
/// <returns></returns>
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
);
}
/// <summary>
/// 提取文件流
...
...
@@ -141,7 +225,12 @@ namespace Mozi.HttpEmbedded.Source
/// <returns></returns>
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
[]
{
};
}
}
/// <summary>
/// 取文件大小
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public
long
GetFileSize
(
string
path
)
{
FileInfo
fi
=
new
FileInfo
(
path
);
return
fi
.
Length
;
}
///// <summary>
///// 取文件大小
///// </summary>
///// <param name="path"></param>
///// <returns></returns>
//public long GetFileSize(string path)
//{
// FileInfo fi = new FileInfo(path);
// return fi.Length;
//}
}
/// <summary>
/// 发布的目录
/// </summary>
internal
sealed
class
DirPublished
{
public
string
Name
{
get
;
set
;
}
public
string
Path
{
get
;
set
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录