public ResponseItem Build(yy_Page value) { if (String.IsNullOrEmpty(value.PageCode)) { return new ResponseItem(1, "页面源码不能为空。"); } try { MessageQueue_Send("CodeCompile", value.PageCode); return new ResponseItem(0, String.Empty); } catch(Exception ex) { return new ResponseItem(2, ex.Message + ex.StackTrace); } }
public ResponseItem Post(yy_Page value) { try { value.PageCode = HttpUtility.UrlDecode(value.PageCode); DB.yy_Page.Add(value); DB.SaveChanges(); return new ResponseItem(0, "添加模板页成功。"); } catch (Exception ex) { return new ResponseItem(2, ex.Message); } }
public ResponseItem Put(yy_Page value) { var _Entity = DB.yy_Page.Find(value.ID); if (_Entity != null) { _Entity.BuildType = value.BuildType; _Entity.CanBuild = value.CanBuild; _Entity.CreateDate = value.CreateDate; _Entity.ExtensionName = value.ExtensionName; _Entity.LastUpdateTime = value.LastUpdateTime; _Entity.PageCode = HttpUtility.UrlDecode(value.PageCode); _Entity.PageKind = value.PageKind; _Entity.PageName = value.PageName; _Entity.SavePath = value.SavePath; _Entity.Summary = value.Summary; _Entity.TypeID = value.TypeID; DB.SaveChanges(); return new ResponseItem(0, ""); } return new ResponseItem(2, "不存在的模板页。"); }
/// <summary> /// 导入模板 /// </summary> /// <param name="FileData">模板包</param> /// <param name="UserID">当前上传者的ID</param> /// <returns></returns> public String Import(Byte[] FileData,long UserID) { SkinConfig SConfig = null; var SkinDirectoryName = String.Empty; using (var SkinPackage = ZipFile.Read(new MemoryStream(FileData))) { #region 获取Config对象 var ConfigEntry = SkinPackage.Entries.Where(a => a.FileName.ToLower().Equals("config.js")).FirstOrDefault(); if (ConfigEntry == null) { return "未找到config.js文件。"; } #endregion MemoryStream ms = new MemoryStream(); ConfigEntry.Extract(ms); String ConfigContent = Encoding.Default.GetString(ms.ToArray()); SConfig = JsonConvert.DeserializeObject<SkinConfig>(ConfigContent); #region 序列化是否成功 if (SConfig == null) { return "配置文件反序列化错误。"; } #endregion #region 模板中文名称不得为空 if (String.IsNullOrEmpty(SConfig.Name)) { return "当前风格的名称不得为空。"; } #endregion #region 验证模板英文名称 if (Regex.IsMatch(SConfig.Name_En, "^[A-Za-z0-9]{1,50}$") == false) { return "模板Name_EN应为字母或数字,长度在1~50位内。"; } #endregion #region 检测版本号 if (SConfig.Version < 1) { return "版本号不得小于1。"; } #endregion SkinDirectoryName = SkinBaseDirectory + SConfig.Name_En + SConfig.Version.ToString(); #region 是否已经存在同名目录(模板+版本) if (Directory.Exists(SkinDirectoryName)) { return "模板已存在。"; } #endregion #region 检测图片/模板数量,不得小于1 if (SConfig.Images == null || SConfig.Images.Count < 1) { return "至少需要上传一张截图。"; } if (SConfig.Pages == null || SConfig.Pages.Count < 1) { return "至少需要上传1个模板。"; } #endregion SkinPackage.ExtractAll(SkinDirectoryName, ExtractExistingFileAction.OverwriteSilently); #region 检测图片路径 if (SConfig.Images != null && SConfig.Images.Count > 0) { String[] AllowTypes = new String[] { ".jpg", ".png", "jpeg" }; foreach (var v in SConfig.Images) { if (!File.Exists(SkinDirectoryName + "/" + v)) { Directory.Delete(SkinDirectoryName, true); return v + "图片不存在目录中。"; } if (!AllowTypes.Contains(Path.GetExtension(v).ToLower())) { Directory.Delete(SkinDirectoryName, true); return "模板截图只能是jpg/png/jpeg格式文件。"; } Thread.Sleep(1); } } #endregion #region 检测模板源文件路径,如果没问题就读取 if (SConfig.Pages != null && SConfig.Pages.Count > 0) { foreach (var v in SConfig.Pages) { /* * 源码路径读取的方式: * 优先级1:根据Source节点读取源码 * 如果不存在 * 优先级2:根据SaveName姐弟读取源码 */ String SourcePath = SkinDirectoryName + "/" + v.Source; //如果为空,则尝试取保存文件名作为源代码文件的存放路径 if (String.IsNullOrEmpty(v.Source)) { SourcePath = SkinDirectoryName + "/" + v.SaveName; } if (!File.Exists(SourcePath)) { Directory.Delete(SkinDirectoryName, true); return "模板[" + v.Title + "]路径配置不正确。"; } using (StreamReader sr = new StreamReader(SourcePath, Encoding.UTF8)) { v.Source = sr.ReadToEnd(); } File.Delete(SourcePath); Thread.Sleep(1); } } #endregion } #region 当前新的模板 var SkinType = new yy_Page_Type() { Version = SConfig.Version, Name = SConfig.Name.Trim(), Name_En = SConfig.Name_En.Trim(), Images = String.Join(",", SConfig.Images), CreateDate = DateTime.Now, IsMaster = 0, Email = String.IsNullOrEmpty(SConfig.Email) ? "" : SConfig.Email, Mobile = String.IsNullOrEmpty(SConfig.Mobile) ? "" : SConfig.Mobile, QQ = String.IsNullOrEmpty(SConfig.QQ) ? "" : SConfig.QQ, Summary = String.IsNullOrEmpty(SConfig.Summary) ? "" : SConfig.Summary, SupportPlatform = SConfig.SupportPlatform, UserID = UserID, Website = String.IsNullOrEmpty(SConfig.Website) ? "" : SConfig.Website, Author = String.IsNullOrEmpty(SConfig.Author) ? "" : SConfig.Author, }; #endregion #region 如果不存在同英文名,版本号的记录,就添加模板 Boolean HasSkin = false; if (DB.yy_Page_Type.Where(a => a.Name_En == SkinType.Name_En && a.Version == SkinType.Version).FirstOrDefault() == null) { DB.yy_Page_Type.Add(SkinType); DB.SaveChanges(); } else { HasSkin = true; } if (HasSkin) { return "网站已经有这个风格了。"; } #endregion String SkinPath = SkinBaseDirectory + SConfig.Name_En + SConfig.Version.ToString("f0"); String relativeSkinPath = "/" + new DirectoryInfo(SkinBaseDirectory).Name + "/" + SConfig.Name_En + SConfig.Version.ToString("f0"); #region 添加组件 var Compoments = new List<yy_Page>(); var CompomentsMasterPages = SConfig.Pages.Where(a => a.PageType == PageType.Component).ToList(); foreach (var v in CompomentsMasterPages) { var CompomentItem = new yy_Page() { CanBuild = v.CanBuild ? 1 : 0, ExtensionName = v.SaveName, LastUpdateTime = DateTime.Now, PageCode = v.Source, PageKind = (int)v.PageType, Summary = v.Remark, PageName = v.Title, SavePath = v.SaveDirectory, TypeID = SkinType.ID, CreateDate = DateTime.Now }; CompomentItem.PageCode = SourceReference_Replace(CompomentItem.PageCode, relativeSkinPath, v.CompressScript, v.CompressStyle, v.CompressDocument); Compoments.Add(CompomentItem); Thread.Sleep(1); } if (Compoments.Count > 0) { foreach (var v in Compoments) { DB.yy_Page.Add(v); } DB.SaveChanges(); } #endregion #region 添加页面(并替换页面中引用的组件ID) var Pages = new List<yy_Page>(); var MasterPages = SConfig.Pages.Where(a => a.PageType != PageType.Component).ToList(); foreach (var v in MasterPages) { var PageItem = new yy_Page() { CanBuild = v.CanBuild ? 1 : 0, CreateDate = DateTime.Now, ExtensionName = v.SaveName, LastUpdateTime = DateTime.Now, PageCode = v.Source, PageKind = (int)v.PageType, Summary = v.Remark, PageName = v.Title, SavePath = v.SaveDirectory, TypeID = SkinType.ID, BuildType = (int)v.BuildType }; PageItem.PageCode = PageReference_Replace(Compoments, PageItem.PageCode); PageItem.PageCode = SourceReference_Replace(PageItem.PageCode, relativeSkinPath, v.CompressScript, v.CompressStyle, v.CompressDocument); Pages.Add(PageItem); Thread.Sleep(1); } foreach (var v in Pages) { DB.yy_Page.Add(v); } DB.SaveChanges(); #endregion #region 压缩样式文件夹文件 if (!String.IsNullOrEmpty(SConfig.CompressStyle) && Directory.Exists(SkinPath + "/" + SConfig.CompressStyle)) { String[] Files = Directory.GetFiles(SkinPath + "/" + SConfig.CompressStyle); foreach (var v in Files) { String CompressStyleSource = String.Empty; using (var sr = new StreamReader(v)) { CompressStyleSource = sr.ReadToEnd(); } CompressStyleSource = new Minifier().MinifyStyleSheet(CompressStyleSource); using (var sr = new StreamWriter(v, false)) { sr.Write(CompressStyleSource); } Thread.Sleep(1); } } #endregion #region 压缩脚本文件夹文件 if (!String.IsNullOrEmpty(SConfig.CompressScript) && Directory.Exists(SkinPath + "/" + SConfig.CompressScript)) { String[] Files = Directory.GetFiles(SkinPath + "/" + SConfig.CompressScript); foreach (var v in Files) { String CompressScriptSource = String.Empty; using (var sr = new StreamReader(v)) { CompressScriptSource = sr.ReadToEnd(); } CompressScriptSource = new Minifier().MinifyJavaScript(CompressScriptSource); using (var sr = new StreamWriter(v, false)) { sr.Write(CompressScriptSource); } Thread.Sleep(1); } } #endregion File.Delete(SkinPath + "/config.js"); return String.Empty; }