123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- 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
- {
- /// <summary>
- /// 资源文件管理 by JNswins
- /// </summary>
- 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();
- }
- /// <summary>
- /// 加载配置
- /// </summary>
- 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<ResourcesFile>();
- 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;
- }
- /// <summary>
- /// 重新加载资源配置
- /// </summary>
- 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<ResourcesFile>();
- 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);
- }
- }
- }
- /// <summary>
- /// 保存配置文件
- /// </summary>
- /// <param name="config"></param>
- /// <returns></returns>
- public static bool SaveConfig(ResourcesConfigInfo config)
- {
- if (null == config)
- throw new ArgumentNullException("ResourcesConfigInfo 对象为空!");
- var sp = new StringBuilder(2000);
- sp.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
- sp.Append("<Resources>\r\n");
- sp.Append(" <ResourcesPath>" + config.ResourcesPath + "</ResourcesPath>\r\n");
- sp.Append(" <FileList>\r\n");
- if (null != config.FileList && 0 < config.FileList.Count)
- {
- foreach (var item in config.FileList)
- {
- 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",
- item.FileName, item.FileUrl, item.Version, item.IsLocal, item.FileType);
- }
- }
- sp.Append(" </FileList>\r\n");
- sp.Append("</Resources>");
- using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, sp.Length))
- {
- writer.Write(sp.ToString());
- }
- return true;
- }
- /// <summary>
- /// 返回配置
- /// </summary>
- /// <returns></returns>
- public static ResourcesConfigInfo GetConfig()
- {
- if (null == _config)
- LoadConfig();
- return _config;
- }
- /// <summary>
- /// 批量获取资源文件HTML代码
- /// </summary>
- /// <param name="files">文件名数组</param>
- /// <returns></returns>
- 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();
- }
- /// <summary>
- /// 批量获取资源文件HTML代码,多个文件请以","分隔
- /// </summary>
- /// <param name="files">资源文件以","分隔</param>
- /// <returns></returns>
- public static string GetMultiResourcesFilesHtml(string files)
- {
- if (string.IsNullOrWhiteSpace(files))
- return "";
- return GetMultiResourcesFilesHtml(files.Split(','));
- }
- /// <summary>
- /// 翻页列表
- /// </summary>
- /// <param name="pageSize"></param>
- /// <param name="pageIndex"></param>
- /// <param name="recordCount"></param>
- /// <returns></returns>
- public static IList<ResourcesFile> 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<ResourcesFile> list = new List<ResourcesFile>();
- for (int i = start; i < end; i++)
- {
- list.Add(config.FileList[i]);
- }
- return list;
- }
- /// <summary>
- /// 添加资源文件
- /// </summary>
- /// <param name="file"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 移除资源文件
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="fileType"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 更新文件版本号
- /// </summary>
- /// <param name="fileName">文件名</param>
- /// <param name="fileType">文件类型</param>
- /// <param name="saveFile">是否保存文件</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 更新所有文件版本号
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="fileType"></param>
- /// <returns></returns>
- 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);
- }
- }
- /// <summary>
- /// 资源配置文件 By:JNswins
- /// </summary>
- public class ResourcesConfigInfo
- {
- /// <summary>
- /// 资源文件路劲
- /// </summary>
- public string ResourcesPath { get; set; }
- /// <summary>
- /// 资源文件
- /// </summary>
- public IList<ResourcesFile> FileList { get; set; }
- }
- /// <summary>
- /// 资源文件
- /// </summary>
- public class ResourcesFile
- {
- /// <summary>
- /// 文件名称
- /// </summary>
- public string FileName { get; set; }
- /// <summary>
- /// 文件地址
- /// </summary>
- public string FileUrl { get; set; }
- /// <summary>
- /// 版本
- /// </summary>
- public int Version { get; set; }
- /// <summary>
- /// 是否本地资源文件
- /// </summary>
- public bool IsLocal { get; set; }
- /// <summary>
- /// 资源文件类型
- /// </summary>
- public ResourcesFileType FileType { get; set; }
- /// <summary>
- /// 返回文件地址
- /// </summary>
- /// <param name="path">资源文件配置路径</param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 返回HTML代码
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public string GetFileHtml(string path)
- {
- switch (this.FileType)
- {
- case ResourcesFileType.CSS:
- return string.Format("<link type=\"text/css\" href=\"{0}\" rel=\"stylesheet\"/>", GetFileUrl(path));
- case ResourcesFileType.JS:
- return string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", GetFileUrl(path));
- }
- return "";
- }
- }
- /// <summary>
- /// 资源文件类型
- /// </summary>
- public enum ResourcesFileType
- {
- CSS = 1,
- JS = 2,
- IMG = 3
- }
- }
|