using CP.Common; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Xml; namespace CP.Web { /// /// 资源文件管理 by JNswins /// public class ResourcesConfigs { private ResourcesConfigs() { } private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\Resources.config"; private static ResourcesConfigInfo _config = null; private static object lockObject = new object(); static ResourcesConfigs() { LoadConfig(); } /// /// 加载配置 /// public static void LoadConfig() { var config = new ResourcesConfigInfo(); if (File.Exists(configPath)) { var doc = new XmlDocument(); doc.Load(configPath); var node = doc.SelectSingleNode("Resources/ResourcesPath"); if (null != node) config.ResourcesPath = node.InnerText.Trim(); var list = doc.SelectNodes("Resources/FileList/File"); if (null != list && 0 < list.Count) { config.FileList = new List(); foreach (XmlNode item in list) { var entity = new ResourcesFile(); node = item.SelectSingleNode("FileName"); if (null != node) entity.FileName = node.InnerText.Trim(); node = item.SelectSingleNode("FileUrl"); if (null != node) entity.FileUrl = node.InnerText.Trim(); node = item.SelectSingleNode("Version"); if (null != node) entity.Version = TypeConverter.StrToInt(node.InnerText.Trim()); node = item.SelectSingleNode("IsLocal"); if (null != node) entity.IsLocal = TypeConverter.StrToBool(node.InnerText.Trim(), false); node = item.SelectSingleNode("FileType"); if (null != node) entity.FileType = (ResourcesFileType)Enum.Parse(typeof(ResourcesFileType), node.InnerText.Trim(), true); config.FileList.Add(entity); } } } _config = config; } /// /// 重新加载资源配置 /// public static void ResetLoadConfig(string configPath = "") { lock (lockObject) { var config = GetConfig(); var newConfig = new ResourcesConfigInfo(); if (null != config) newConfig.ResourcesPath = config.ResourcesPath; if (!string.IsNullOrEmpty(configPath) && File.Exists(configPath)) newConfig.ResourcesPath = configPath; if (!string.IsNullOrEmpty(newConfig.ResourcesPath)) { var list = new List(); string[] files = null; string path = ""; #region 原有远端资源文件 if (null != config && null != config.FileList && 0 < config.FileList.Count) { foreach (var item in config.FileList) { if (!item.IsLocal) list.Add(item); } } #endregion #region 本地JS资源文件 path = AppDomain.CurrentDomain.BaseDirectory + newConfig.ResourcesPath.Replace("/", "\\") + "js\\"; if (Directory.Exists(path)) { files = Directory.GetFiles(path); foreach (var item in files) { list.Add(new ResourcesFile() { FileName = System.IO.Path.GetFileName(item), FileUrl = "", Version = TypeConverter.StrToInt(File.GetLastWriteTime(item).ToString("yyyyMMddHH")), IsLocal = true, FileType = ResourcesFileType.JS }); } } #endregion #region 本地CSS资源 path = AppDomain.CurrentDomain.BaseDirectory + newConfig.ResourcesPath.Replace("/", "\\") + "css\\"; if (Directory.Exists(path)) { files = Directory.GetFiles(path); foreach (var item in files) { list.Add(new ResourcesFile() { FileName = System.IO.Path.GetFileName(item), FileUrl = "", Version = TypeConverter.StrToInt(File.GetLastWriteTime(item).ToString("yyyyMMddHH")), IsLocal = true, FileType = ResourcesFileType.CSS }); } } #endregion newConfig.FileList = list; _config = newConfig; SaveConfig(newConfig); } } } /// /// 保存配置文件 /// /// /// public static bool SaveConfig(ResourcesConfigInfo config) { if (null == config) throw new ArgumentNullException("ResourcesConfigInfo 对象为空!"); var sp = new StringBuilder(2000); sp.Append("\r\n"); sp.Append("\r\n"); sp.Append(" " + config.ResourcesPath + "\r\n"); sp.Append(" \r\n"); if (null != config.FileList && 0 < config.FileList.Count) { foreach (var item in config.FileList) { sp.AppendFormat(" \r\n {0}\r\n {1}\r\n {2}\r\n {3}\r\n {4}\r\n \r\n", item.FileName, item.FileUrl, item.Version, item.IsLocal, item.FileType); } } sp.Append(" \r\n"); sp.Append(""); using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, sp.Length)) { writer.Write(sp.ToString()); } return true; } /// /// 返回配置 /// /// public static ResourcesConfigInfo GetConfig() { if (null == _config) LoadConfig(); return _config; } /// /// 批量获取资源文件HTML代码 /// /// 文件名数组 /// public static string GetMultiResourcesFilesHtml(string[] files) { if (null == files || 0 >= files.Length) return ""; var config = GetConfig(); if (null == config || null == config.FileList || 0 >= config.FileList.Count) return ""; var sp = new StringBuilder(1000); for (int i = 0; i < files.Length; i++) { if (-1 == files[i].IndexOf('.')) continue; ResourcesFileType fileType; foreach (var item in config.FileList) { if (Enum.TryParse(System.IO.Path.GetExtension(files[i]).TrimStart('.'), true, out fileType)) { if (item.FileName.ToLower() == files[i].ToLower() && item.FileType == fileType) { sp.AppendLine(item.GetFileHtml(config.ResourcesPath)); } } } } return sp.ToString(); } /// /// 批量获取资源文件HTML代码,多个文件请以","分隔 /// /// 资源文件以","分隔 /// public static string GetMultiResourcesFilesHtml(string files) { if (string.IsNullOrWhiteSpace(files)) return ""; return GetMultiResourcesFilesHtml(files.Split(',')); } /// /// 翻页列表 /// /// /// /// /// public static IList ToPaging(int pageSize, int pageIndex, out int recordCount) { recordCount = 0; var config = GetConfig(); if (null == config || null == config.FileList || 0 >= config.FileList.Count) return null; recordCount = config.FileList.Count; int end = (pageIndex - 1) * pageSize + pageSize; int start = pageIndex == 1 ? 0 : (pageIndex - 1) * pageSize; if (start > recordCount) return null; end = end > recordCount ? recordCount : end; IList list = new List(); for (int i = start; i < end; i++) { list.Add(config.FileList[i]); } return list; } /// /// 添加资源文件 /// /// /// public static bool AddResourcesFiles(ResourcesFile file) { lock (lockObject) { var config = GetConfig(); if (null == config || null == config.FileList || 0 >= config.FileList.Count) return true; bool isContains = false; foreach (var item in config.FileList) { if (item.FileUrl == file.FileUrl) isContains = true; } if (!isContains) { config.FileList.Add(file); SaveConfig(config); return true; } return false; } } /// /// 移除资源文件 /// /// /// /// public static bool RemoveResourcesFile(string fileName, ResourcesFileType fileType) { lock (lockObject) { var config = GetConfig(); if (null == config || null == config.FileList || 0 >= config.FileList.Count) return true; foreach (var item in config.FileList) { if (fileName == item.FileName && fileType == item.FileType) { config.FileList.Remove(item); SaveConfig(config); return true; } } return false; } } /// /// 更新文件版本号 /// /// 文件名 /// 文件类型 /// 是否保存文件 /// public static bool UpdateFileVersion(string fileName, ResourcesFileType fileType, bool saveFile = true) { var config = GetConfig(); if (null == config || null == config.FileList || 0 >= config.FileList.Count) return true; foreach (var item in config.FileList) { if (item.IsLocal && fileName == item.FileName && fileType == item.FileType) { string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}{1}\\{2}", config.ResourcesPath.Replace("/", "\\"), item.FileType.ToString(), item.FileName)); if (File.Exists(filePath)) item.Version = TypeConverter.StrToInt(File.GetLastWriteTime(filePath).ToString("yyyyMMddHH")); if (saveFile) SaveConfig(config); return true; } } return false; } /// /// 更新所有文件版本号 /// /// /// /// public static bool UpdateAllFileVersion() { var config = GetConfig(); if (null == config || null == config.FileList || 0 >= config.FileList.Count) return true; foreach (var item in config.FileList) { if (item.IsLocal) { string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}{1}\\{2}", config.ResourcesPath.Replace("/", "\\"), item.FileType.ToString(), item.FileName)); if (File.Exists(filePath)) item.Version = TypeConverter.StrToInt(File.GetLastWriteTime(filePath).ToString("yyyyMMddHH")); } } return SaveConfig(config); } } /// /// 资源配置文件 By:JNswins /// public class ResourcesConfigInfo { /// /// 资源文件路劲 /// public string ResourcesPath { get; set; } /// /// 资源文件 /// public IList FileList { get; set; } } /// /// 资源文件 /// public class ResourcesFile { /// /// 文件名称 /// public string FileName { get; set; } /// /// 文件地址 /// public string FileUrl { get; set; } /// /// 版本 /// public int Version { get; set; } /// /// 是否本地资源文件 /// public bool IsLocal { get; set; } /// /// 资源文件类型 /// public ResourcesFileType FileType { get; set; } /// /// 返回文件地址 /// /// 资源文件配置路径 /// public string GetFileUrl(string path) { if (!IsLocal) return FileUrl; return string.Format("{0}{1}/{2}?v={3}", path, this.FileType.ToString().ToLower(), this.FileName.ToLower(), this.Version); } /// /// 返回HTML代码 /// /// /// public string GetFileHtml(string path) { switch (this.FileType) { case ResourcesFileType.CSS: return string.Format("", GetFileUrl(path)); case ResourcesFileType.JS: return string.Format("", GetFileUrl(path)); } return ""; } } /// /// 资源文件类型 /// public enum ResourcesFileType { CSS = 1, JS = 2, IMG = 3 } }