温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:NETCMSv1.5(Build0509)完整源码版
当前文件路径:NetCMSv15/NetCMS.Content/Collect/PageRes.cs

1//====================================================== 2
//== (c)2008 aspxcms inc by NeTCMS v1.0 == 3
//== Forum:bbs.aspxcms.com == 4
//== Website:www.aspxcms.com == 5
//====================================================== 6
using System; 7
using System.Collections.Generic; 8
using System.Text; 9
using System.Text.RegularExpressions; 10
using System.IO; 11
using System.Net; 12
13
namespace NetCMS.Content.Collect 14
{ 15
struct resinfo 16
{ 17
/// <summary> 18
/// 完整的原始路径 19
/// </summary> 20
public string orgurl; 21
/// <summary> 22
/// 原始文件名 23
/// </summary> 24
public string orgname; 25
/// <summary> 26
/// 原始的扩展文件名 27
/// </summary> 28
public string extname; 29
/// <summary> 30
/// 新文件名 31
/// </summary> 32
public string newname; 33
} 34
/// <summary> 35
/// 获取(网页)内容中的远程资源 36
/// </summary> 37
public class RemoteResource 38
{ 39
private int SeriesNum; 40
private string FileNum; 41
private string restype = ".gif|.jpg|.bmp|.png|.jpeg"; 42
private string _remoteurl; 43
private string _localurl; 44
private string _localpath; 45
private string _content = ""; 46
private bool _rename; 47
private bool bcomp = false; 48
/// <summary> 49
/// 构造函数 50
/// </summary> 51
/// <param name="Content">包含要获取远程资源的内容</param> 52
/// <param name="LocalURLDirectory">要将文件保存到本地服务器的虚拟目录,用于替换原来的远程链接地址,如:http://www.aspxcms.com/remoteres,可以为空,也可以为../一个或多个。</param> 53
/// <param name="LocalPhysicalDirectory">要将文件保存到本地服务器的磁盘路径,如:C:\Inetpub\wwwroot\remoteres,如果不存在可以创建</param> 54
/// <param name="RemoteUrl">用于处理相对路径(如src="../images/netcms.gif")的资源,如果为空,则只取完整路径的资源,以http(或https,ftp,rtsp,mms)://开头</param> 55
/// <param name="RenameFile">是否要重命名资源文件,如为false则自动覆盖重名文件</param> 56
public RemoteResource(string Content,string LocalURLDirectory,string LocalPhysicalDirectory,string RemoteUrl,bool RenameFile) 57
{ 58
_content = Content; 59
_localurl= LocalURLDirectory.Trim(); 60
_localpath = LocalPhysicalDirectory.Trim(); 61
if (RemoteUrl == null) 62
_remoteurl = ""; 63
else 64
_remoteurl = RemoteUrl.Trim(); 65
if (_remoteurl.Equals("")) 66
bcomp = true; 67
if (_localpath.Equals("")) 68
throw new NullReferenceException ("本地的物理路径不能为空!"); 69
_rename = RenameFile; 70
SeriesNum = 1; 71
FileNum = NetCMS.Common.Rand.Number(6); 72
_localpath = _localpath.Replace("/", "\\"); 73
_localurl = _localurl.Replace("\\", "/"); 74
_remoteurl = _remoteurl.Replace("\\", "/"); 75
_localpath = _localpath.TrimEnd('\\'); 76
_localurl = _localurl.TrimEnd('/'); 77
if (!Directory.Exists(_localpath)) 78
Directory.CreateDirectory(_localpath); 79
} 80
/// <summary> 81
/// 要获取的资源文件扩展名,扩展名不要加点(.),如{"gif","jpg","png"},默认的下载文件有gif,jpg,bmp,png 82
/// </summary> 83
public string[] FileExtends 84
{ 85
set 86
{ 87
restype = ""; 88
string[] flexs = value; 89
for(int i=0;i<flexs.Length;i++) 90
{ 91
if (i > 0) 92
restype += "|"; 93
restype += "." + flexs[i].TrimStart('.'); 94
} 95
} 96
} 97
/// <summary> 98
/// 获取远程资源的路径 99
/// </summary> 100
private IList<resinfo> ObtainResURL() 101
{ 102
IList<resinfo> list = new List<resinfo>(); 103
string pattern = "src\\s?=\\s?['\"]?(?<resurl>.+?(" + restype.Replace(".", "\\.") + "))"; 104
//string pattern = "[=\\(]['\"\\ ]??(?<resurl>[^<>\"]+?(" + restype.Replace(".","\\.") + "))"; 105
if (bcomp) 106
pattern = @"(http|https|ftp|rtsp|mms)://\S+(" + restype.Replace(".", "\\.") + ")"; 107
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); 108
Match m = reg.Match(_content); 109
while (m.Success) 110
{ 111
string url = ""; 112
if (bcomp) 113
{ 114
url = m.Value; 115
} 116
else 117
{ 118
url = m.Groups["resurl"].Value; 119
} 120
bool bsame = false; 121
foreach (resinfo res in list) 122
{ 123
if (res.orgurl.Equals(url)) 124
{ 125
bsame = true; 126
break; 127
} 128
} 129
if (!bsame) 130
{ 131
加入资源列表 加入资源列表 150
} 151
m = m.NextMatch(); 152
} 153
return list; 154
} 155
/// <summary> 156
/// 保存远程图片并替换原文内容 157
/// </summary> 158
public void FetchResource() 159
{ 160
WebClient wb = new WebClient(); 161
IList<resinfo> list = ObtainResURL(); 162
if(!_localurl.Equals("")) 163
_localurl += "/"; 164
foreach (resinfo r in list) 165
{ 166
try 167
{ 168
string url = Utility.StickUrl(_remoteurl, r.orgurl); 169
string newurl = "",newpath=""; 170
if (_rename) 171
{ 172
生成新文件名 183
} 184
else 185
{ 186
newurl = _localurl + r.orgname + "." + r.extname; 187
wb.DownloadFile(url, _localpath + "\\" + r.orgname + "." + r.extname); 188
} 189
替换文件名 替换文件名 192
SeriesNum++; 193
} 194
catch 195
{ } 196
} 197
if (wb != null) 198
wb.Dispose(); 199
} 200
/// <summary> 201
/// 获取内容 202
/// </summary> 203
public string Content 204
{ 205
get { return _content; } 206
} 207
} 208
} 209





}