Skip to content

Commit

Permalink
主页显示当前图书馆的html
Browse files Browse the repository at this point in the history
  • Loading branch information
renyh1013 committed May 20, 2016
1 parent e809e97 commit 2af1e16
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 15 deletions.
177 changes: 168 additions & 9 deletions DigitalPlatform.IO/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,177 @@ public static void WriteText(string strFileName,
}
}

// 功能:文件到字符串,使用直接读到尾的方法
// strFileName: 文件名
public static string File2StringE(string strFileName)
// 能自动识别文件内容的编码方式的读入文本文件内容模块
// parameters:
// lMaxLength 装入的最大长度。如果超过,则超过的部分不装入。如果为-1,表示不限制装入长度
// return:
// -1 出错 strError中有返回值
// 0 文件不存在 strError中有返回值
// 1 文件存在
// 2 读入的内容不是全部
public static int ReadTextFileContent(string strFilePath,
long lMaxLength,
out string strContent,
out Encoding encoding,
out string strError)
{
if (strFileName == null
|| strFileName == "")
return "";
using (StreamReader sr = new StreamReader(strFileName, true))
strError = "";
strContent = "";
encoding = null;

if (File.Exists(strFilePath) == false)
{
string strText = sr.ReadToEnd();
return strText;
strError = "文件 '" + strFilePath + "' 不存在";
return 0;
}

encoding = FileUtil.DetectTextFileEncoding(strFilePath);

try
{
bool bExceed = false;

using (FileStream file = File.Open(
strFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
// TODO: 这里的自动探索文件编码方式功能不正确,
// 需要专门编写一个函数来探测文本文件的编码方式
// 目前只能用UTF-8编码方式
using (StreamReader sr = new StreamReader(file, encoding))
{
if (lMaxLength == -1)
{
strContent = sr.ReadToEnd();
}
else
{
long lLoadedLength = 0;
StringBuilder temp = new StringBuilder(4096);
for (; ; )
{
string strLine = sr.ReadLine();
if (strLine == null)
break;
if (lLoadedLength + strLine.Length > lMaxLength)
{
strLine = strLine.Substring(0, (int)(lMaxLength - lLoadedLength));
temp.Append(strLine + " ...");
bExceed = true;
break;
}

temp.Append(strLine + "\r\n");
lLoadedLength += strLine.Length + 2;
if (lLoadedLength > lMaxLength)
{
temp.Append(strLine + " ...");
bExceed = true;
break;
}
}
strContent = temp.ToString();
}
/*
sr.Close();
sr = null;
* */
}

if (bExceed == true)
return 2;
}
catch (Exception ex)
{
strError = "打开或读入文件 '" + strFilePath + "' 时出错: " + ex.Message;
return -1;
}

return 1;
}


// 如果未能探测出来,则当作 936
public static Encoding DetectTextFileEncoding(string strFilename)
{
Encoding encoding = DetectTextFileEncoding(strFilename, null);
if (encoding == null)
return Encoding.GetEncoding(936); // default

return encoding;
}

// 检测文本文件的encoding
/*
UTF-8: EF BB BF
UTF-16 big-endian byte order: FE FF
UTF-16 little-endian byte order: FF FE
UTF-32 big-endian byte order: 00 00 FE FF
UTF-32 little-endian byte order: FF FE 00 00
* */
public static Encoding DetectTextFileEncoding(string strFilename,
Encoding default_encoding)
{
byte[] buffer = new byte[4];

try
{
using (FileStream file = File.Open(
strFilename,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
if (file.Length >= 2)
{
file.Read(buffer, 0, 2); // 1, 2 BUG

if (buffer[0] == 0xff && buffer[1] == 0xfe)
{
return Encoding.Unicode; // little-endian
}

if (buffer[0] == 0xfe && buffer[1] == 0xff)
{
return Encoding.BigEndianUnicode;
}
}

if (file.Length >= 3)
{
file.Read(buffer, 2, 1);
if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
{
return Encoding.UTF8;
}

}

if (file.Length >= 4)
{
file.Read(buffer, 3, 1);

// UTF-32 big-endian byte order: 00 00 FE FF
// UTF-32 little-endian byte order: FF FE 00 00

if (buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xfe && buffer[3] == 0xff)
{
return Encoding.UTF32; // little-endian
}

if (buffer[0] == 0xff && buffer[1] == 0xfe && buffer[2] == 0x00 && buffer[3] == 0x00)
{
return Encoding.GetEncoding(65006); // UTF-32 big-endian
}
}
}
}
catch
{
}

return default_encoding; // default
}
}
}
5 changes: 4 additions & 1 deletion dp2Command.Server/dp2CmdService2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@ private void DoMessage(IList<MessageRecord> records)
XmlNode root = bodyDom.DocumentElement;
XmlNode typeNode = root.SelectSingleNode("type");
if (typeNode == null)
throw new Exception("消息data的body中未定义type节点");
{
//throw new Exception("消息data的body中未定义type节点");
continue;//todo
}
string strType = DomUtil.GetNodeText(typeNode);

// 目前只处理这两种消息
Expand Down
2 changes: 1 addition & 1 deletion dp2weixinP2P/App_Data/weixin_data/weixin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<version>0.01</version>
<dp2mserver url="http://localhost:8083/dp2MServer" username="weixinclient" password="uOrIkBeqdQM=" />
<dp2mserver url="http://dp2003.com:8083/dp2MServer" username="weixinclient" password="uOrIkBeqdQM=" />
<mongoDB connectionString="mongodb://localhost:27017" instancePrefix="wx" />
<dp2weixin url="http://dp2003.com/dp2weixin" AppId="wx57aa3682c59d16c2" Secret="61ac93be56e3f7f42d0861bf073427e6" EncodingAESKey="ReQ72EHh7KkROvs1AEE5IK76py9oHmhRtVs30ur2DlD" Token="dp3weixin"/>
<externalMessageInterface>
Expand Down
49 changes: 45 additions & 4 deletions dp2weixinP2P/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DigitalPlatform.LibraryRestClient;
using DigitalPlatform.IO;
using DigitalPlatform.LibraryRestClient;
using DigitalPlatform.Text;
using dp2Command.Service;
using dp2weixin;
Expand All @@ -7,6 +8,7 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -53,9 +55,48 @@ public ActionResult Index(string code, string state, string admin, string weiXin
string libName = userItem.libName;
libInfo = new LibInfoModel();
libInfo.Title = libName+" 主页";
libInfo.Content = @"<div class='mui-content-padded'>"
+"欢迎访问”"+libName+"“图书馆"
+"</div>";

string htmlFile = dp2CmdService2.Instance.weiXinDataDir + "/lib/" + userItem.libCode+"/index.html";
if (System.IO.File.Exists(htmlFile) == false)
{
// 先缺省html文件
htmlFile = dp2CmdService2.Instance.weiXinDataDir + "/lib/index.html";
}

string strHtml = "";
// 文件存在,取出文件 的内容
if (System.IO.File.Exists(htmlFile) == true)
{
Encoding encoding;
// 能自动识别文件内容的编码方式的读入文本文件内容模块
// parameters:
// lMaxLength 装入的最大长度。如果超过,则超过的部分不装入。如果为-1,表示不限制装入长度
// return:
// -1 出错 strError中有返回值
// 0 文件不存在 strError中有返回值
// 1 文件存在
// 2 读入的内容不是全部
nRet = FileUtil.ReadTextFileContent(htmlFile,
-1,
out strHtml,
out encoding,
out strError);
if (nRet == -1 || nRet == 0)
throw new Exception(strError);
if (nRet == 2)
throw new Exception("FileUtil.ReadTextFileContent() error");

// 替换关键词
strHtml = strHtml.Replace("%libName%", userItem.libName);
}
else
{
strHtml=@"<div class='mui-content-padded'>"
+"欢迎访问 "+libName+" 图书馆"
+"</div>";
}

libInfo.Content = strHtml;
}

return View(libInfo);
Expand Down

0 comments on commit 2af1e16

Please sign in to comment.