ResourcesConfigs.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Text;
  6. using CB.Common;
  7. namespace CB.Config
  8. {
  9. /// <summary>
  10. /// 资源文件管理 by JNswins
  11. /// </summary>
  12. public class ResourcesConfigs
  13. {
  14. private ResourcesConfigs() { }
  15. private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\Resources.config";
  16. private static ResourcesConfigInfo _config = null;
  17. private static object lockObject = new object();
  18. static ResourcesConfigs()
  19. {
  20. LoadConfig();
  21. }
  22. /// <summary>
  23. /// 加载配置
  24. /// </summary>
  25. public static void LoadConfig()
  26. {
  27. var config = new ResourcesConfigInfo();
  28. if (File.Exists(configPath))
  29. {
  30. var doc = new XmlDocument();
  31. doc.Load(configPath);
  32. var node = doc.SelectSingleNode("Resources/ResourcesPath");
  33. if (null != node)
  34. config.ResourcesPath = node.InnerText.Trim();
  35. var list = doc.SelectNodes("Resources/FileList/File");
  36. if (null != list && 0 < list.Count)
  37. {
  38. config.FileList = new List<ResourcesFile>();
  39. foreach (XmlNode item in list)
  40. {
  41. var entity = new ResourcesFile();
  42. node = item.SelectSingleNode("FileName");
  43. if (null != node)
  44. entity.FileName = node.InnerText.Trim();
  45. node = item.SelectSingleNode("FileUrl");
  46. if (null != node)
  47. entity.FileUrl = node.InnerText.Trim();
  48. node = item.SelectSingleNode("Version");
  49. if (null != node)
  50. entity.Version = TypeConverter.StrToInt(node.InnerText.Trim());
  51. node = item.SelectSingleNode("IsLocal");
  52. if (null != node)
  53. entity.IsLocal = TypeConverter.StrToBool(node.InnerText.Trim(), false);
  54. node = item.SelectSingleNode("FileType");
  55. if (null != node)
  56. entity.FileType = (ResourcesFileType)Enum.Parse(typeof(ResourcesFileType), node.InnerText.Trim(), true);
  57. config.FileList.Add(entity);
  58. }
  59. }
  60. }
  61. _config = config;
  62. }
  63. /// <summary>
  64. /// 重新加载资源配置
  65. /// </summary>
  66. public static void ResetLoadConfig(string configPath = "")
  67. {
  68. lock (lockObject)
  69. {
  70. var config = GetConfig();
  71. var newConfig = new ResourcesConfigInfo();
  72. if (null != config)
  73. newConfig.ResourcesPath = config.ResourcesPath;
  74. if (!string.IsNullOrEmpty(configPath) && File.Exists(configPath))
  75. newConfig.ResourcesPath = configPath;
  76. if (!string.IsNullOrEmpty(newConfig.ResourcesPath))
  77. {
  78. var list = new List<ResourcesFile>();
  79. string[] files = null;
  80. string path = "";
  81. #region 原有远端资源文件
  82. if (null != config && null != config.FileList && 0 < config.FileList.Count)
  83. {
  84. foreach (var item in config.FileList)
  85. {
  86. if (!item.IsLocal)
  87. list.Add(item);
  88. }
  89. }
  90. #endregion
  91. #region 本地JS资源文件
  92. path = AppDomain.CurrentDomain.BaseDirectory + newConfig.ResourcesPath.Replace("/", "\\") + "js\\";
  93. if (Directory.Exists(path))
  94. {
  95. files = Directory.GetFiles(path);
  96. foreach (var item in files)
  97. {
  98. list.Add(new ResourcesFile()
  99. {
  100. FileName = System.IO.Path.GetFileName(item),
  101. FileUrl = "",
  102. Version = TypeConverter.StrToInt(File.GetLastWriteTime(item).ToString("yyyyMMddHH")),
  103. IsLocal = true,
  104. FileType = ResourcesFileType.JS
  105. });
  106. }
  107. }
  108. #endregion
  109. #region 本地CSS资源
  110. path = AppDomain.CurrentDomain.BaseDirectory + newConfig.ResourcesPath.Replace("/", "\\") + "css\\";
  111. if (Directory.Exists(path))
  112. {
  113. files = Directory.GetFiles(path);
  114. foreach (var item in files)
  115. {
  116. list.Add(new ResourcesFile()
  117. {
  118. FileName = System.IO.Path.GetFileName(item),
  119. FileUrl = "",
  120. Version = TypeConverter.StrToInt(File.GetLastWriteTime(item).ToString("yyyyMMddHH")),
  121. IsLocal = true,
  122. FileType = ResourcesFileType.CSS
  123. });
  124. }
  125. }
  126. #endregion
  127. newConfig.FileList = list;
  128. _config = newConfig;
  129. SaveConfig(newConfig);
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// 保存配置文件
  135. /// </summary>
  136. /// <param name="config"></param>
  137. /// <returns></returns>
  138. public static bool SaveConfig(ResourcesConfigInfo config)
  139. {
  140. if (null == config)
  141. throw new ArgumentNullException("ResourcesConfigInfo 对象为空!");
  142. var sp = new StringBuilder(2000);
  143. sp.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
  144. sp.Append("<Resources>\r\n");
  145. sp.Append(" <ResourcesPath>" + config.ResourcesPath + "</ResourcesPath>\r\n");
  146. sp.Append(" <FileList>\r\n");
  147. if (null != config.FileList && 0 < config.FileList.Count)
  148. {
  149. foreach (var item in config.FileList)
  150. {
  151. sp.AppendFormat(" <File>\r\n <FileName>{0}</FileName>\r\n <FileUrl>{1}</FileUrl>\r\n <Version>{2}</Version>\r\n <IsLocal>{3}</IsLocal>\r\n <FileType>{4}</FileType>\r\n </File>\r\n",
  152. item.FileName, item.FileUrl, item.Version, item.IsLocal, item.FileType);
  153. }
  154. }
  155. sp.Append(" </FileList>\r\n");
  156. sp.Append("</Resources>");
  157. using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, sp.Length))
  158. {
  159. writer.Write(sp.ToString());
  160. }
  161. return true;
  162. }
  163. /// <summary>
  164. /// 返回配置
  165. /// </summary>
  166. /// <returns></returns>
  167. public static ResourcesConfigInfo GetConfig()
  168. {
  169. if (null == _config)
  170. LoadConfig();
  171. return _config;
  172. }
  173. /// <summary>
  174. /// 批量获取资源文件HTML代码
  175. /// </summary>
  176. /// <param name="files">文件名数组</param>
  177. /// <returns></returns>
  178. public static string GetMultiResourcesFilesHtml(string[] files)
  179. {
  180. if (null == files || 0 >= files.Length)
  181. return "";
  182. var config = GetConfig();
  183. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  184. return "";
  185. var sp = new StringBuilder(1000);
  186. for (int i = 0; i < files.Length; i++)
  187. {
  188. if (-1 == files[i].IndexOf('.'))
  189. continue;
  190. ResourcesFileType fileType;
  191. foreach (var item in config.FileList)
  192. {
  193. if (Enum.TryParse(System.IO.Path.GetExtension(files[i]).TrimStart('.'), true, out fileType))
  194. {
  195. if (item.FileName.ToLower() == files[i].ToLower() && item.FileType == fileType)
  196. {
  197. sp.AppendLine(item.GetFileHtml(config.ResourcesPath));
  198. }
  199. }
  200. }
  201. }
  202. return sp.ToString();
  203. }
  204. /// <summary>
  205. /// 批量获取资源文件HTML代码,多个文件请以","分隔
  206. /// </summary>
  207. /// <param name="files">资源文件以","分隔</param>
  208. /// <returns></returns>
  209. public static string GetMultiResourcesFilesHtml(string files)
  210. {
  211. if (string.IsNullOrWhiteSpace(files))
  212. return "";
  213. return GetMultiResourcesFilesHtml(files.Split(','));
  214. }
  215. /// <summary>
  216. /// 翻页列表
  217. /// </summary>
  218. /// <param name="pageSize"></param>
  219. /// <param name="pageIndex"></param>
  220. /// <param name="recordCount"></param>
  221. /// <returns></returns>
  222. public static IList<ResourcesFile> ToPaging(int pageSize, int pageIndex, out int recordCount)
  223. {
  224. recordCount = 0;
  225. var config = GetConfig();
  226. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  227. return null;
  228. recordCount = config.FileList.Count;
  229. int end = (pageIndex - 1) * pageSize + pageSize;
  230. int start = pageIndex == 1 ? 0 : (pageIndex - 1) * pageSize;
  231. if (start > recordCount)
  232. return null;
  233. end = end > recordCount ? recordCount : end;
  234. IList<ResourcesFile> list = new List<ResourcesFile>();
  235. for (int i = start; i < end; i++)
  236. {
  237. list.Add(config.FileList[i]);
  238. }
  239. return list;
  240. }
  241. /// <summary>
  242. /// 添加资源文件
  243. /// </summary>
  244. /// <param name="file"></param>
  245. /// <returns></returns>
  246. public static bool AddResourcesFiles(ResourcesFile file)
  247. {
  248. lock (lockObject)
  249. {
  250. var config = GetConfig();
  251. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  252. return true;
  253. bool isContains = false;
  254. foreach (var item in config.FileList)
  255. {
  256. if (item.FileUrl == file.FileUrl)
  257. isContains = true;
  258. }
  259. if (!isContains)
  260. {
  261. config.FileList.Add(file);
  262. SaveConfig(config);
  263. return true;
  264. }
  265. return false;
  266. }
  267. }
  268. /// <summary>
  269. /// 移除资源文件
  270. /// </summary>
  271. /// <param name="fileName"></param>
  272. /// <param name="fileType"></param>
  273. /// <returns></returns>
  274. public static bool RemoveResourcesFile(string fileName, ResourcesFileType fileType)
  275. {
  276. lock (lockObject)
  277. {
  278. var config = GetConfig();
  279. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  280. return true;
  281. foreach (var item in config.FileList)
  282. {
  283. if (fileName == item.FileName && fileType == item.FileType)
  284. {
  285. config.FileList.Remove(item);
  286. SaveConfig(config);
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. }
  293. /// <summary>
  294. /// 更新文件版本号
  295. /// </summary>
  296. /// <param name="fileName">文件名</param>
  297. /// <param name="fileType">文件类型</param>
  298. /// <param name="saveFile">是否保存文件</param>
  299. /// <returns></returns>
  300. public static bool UpdateFileVersion(string fileName, ResourcesFileType fileType, bool saveFile = true)
  301. {
  302. var config = GetConfig();
  303. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  304. return true;
  305. foreach (var item in config.FileList)
  306. {
  307. if (item.IsLocal && fileName == item.FileName && fileType == item.FileType)
  308. {
  309. string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
  310. string.Format("{0}{1}\\{2}", config.ResourcesPath.Replace("/", "\\"), item.FileType.ToString(), item.FileName));
  311. if (File.Exists(filePath))
  312. item.Version = TypeConverter.StrToInt(File.GetLastWriteTime(filePath).ToString("yyyyMMddHH"));
  313. if (saveFile)
  314. SaveConfig(config);
  315. return true;
  316. }
  317. }
  318. return false;
  319. }
  320. /// <summary>
  321. /// 更新所有文件版本号
  322. /// </summary>
  323. /// <param name="fileName"></param>
  324. /// <param name="fileType"></param>
  325. /// <returns></returns>
  326. public static bool UpdateAllFileVersion()
  327. {
  328. var config = GetConfig();
  329. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  330. return true;
  331. foreach (var item in config.FileList)
  332. {
  333. if (item.IsLocal)
  334. {
  335. string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
  336. string.Format("{0}{1}\\{2}", config.ResourcesPath.Replace("/", "\\"), item.FileType.ToString(), item.FileName));
  337. if (File.Exists(filePath))
  338. item.Version = TypeConverter.StrToInt(File.GetLastWriteTime(filePath).ToString("yyyyMMddHH"));
  339. }
  340. }
  341. return SaveConfig(config);
  342. }
  343. }
  344. }