public HomeModule() { var config = TinyfxCore.Configuration; if (!config.IsSitePublic) { this.RequiresAuthentication(); } _tinyfxPageRender = new Cores.TinyfxPageRender(config); Get("/", _ => { return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(1, 0), "text/html")); }); Get("/page/{page}", _ => { int page = 0; try { page = _.page; } catch { page = 1; } if (page < 1) { page = 1; } return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(page, 0), "text/html")); }); Get("/post/{post}", _ => { long post = 0; try { post = _.post; } catch { post = 0; } return(Response.AsText(_tinyfxPageRender.RenderPageOrPost(0, post), "text/html")); }); Get("/files/{filename}", _ => { string filename = _.filename; if (filename == null || filename.Length < 1) { return(new NotFoundResponse()); } else { string[] seqs = filename.Split(new char[] { '_' }); if (seqs.Length != 3) { return(new NotFoundResponse()); } else { string realfile = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, seqs[0], seqs[1], seqs[2]); if (!String.IsNullOrEmpty(TinyfxCore.Configuration.DataDirectory)) { realfile = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, seqs[0], seqs[1], seqs[2]); } if (System.IO.File.Exists(realfile)) { string mime = "application/octet-stream"; string ext = System.IO.Path.GetExtension(filename); if (!string.IsNullOrEmpty(ext)) { if (TinyfxCore.Mime.ContainsKey(ext)) { mime = TinyfxCore.Mime[ext]; } } var fs = System.IO.File.OpenRead(realfile); if (TinyfxCore.Configuration.Encryption) { var ms = new System.IO.MemoryStream(); Faes faes = new Faes(); faes.Decrypt(fs, ms); ms.Seek(0, System.IO.SeekOrigin.Begin); Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(ms); }, mime); if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") { return(streamResponse.WithHeader("Cache-Control", "max-age=315360000")); } else { return(streamResponse); } } else { Nancy.Responses.StreamResponse streamResponse = new Nancy.Responses.StreamResponse(() => { return(fs); }, mime); if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif") { return(streamResponse.WithHeader("Cache-Control", "max-age=315360000")); } else { return(streamResponse); } } } else { return(new NotFoundResponse()); } } } }); }
public string RenderPostList(int page, string action, long pid) { if (page < 1) { page = 1; } string error = null; string success = null; if (action == "delete") { bool rs = _pressService.DeletePost(pid); if (!rs) { error = "删除失败"; } else { success = "删除ID为" + pid + "文章成功"; } } else if (action == "visible") { bool rs = _pressService.SwitchVisibleByID(pid); if (!rs) { error = "设置失败"; } else { success = "设置成功"; } } var allPosts = _pressService.ReadPostsFromDatabase(); int pageSize = 8; int maxPage = 0; maxPage = (int)Math.Ceiling((double)allPosts.Count / pageSize); var tmpPageList = allPosts.Skip((page - 1) * pageSize).Take(pageSize).ToList(); List <Post> pageList = new List <Post>(); if (!TinyfxCore.Configuration.Encryption) { pageList = tmpPageList; } else { Faes faes = new Faes(); foreach (var item in tmpPageList) { var citem = new Post(); citem.Id = item.Id; citem.Visible = item.Visible; citem.Content = faes.Decrypt(item.Content); citem.Title = faes.Decrypt(item.Title); pageList.Add(citem); } } List <string[]> pageData = new List <string[]>(); for (int i = 0; i < pageList.Count(); i++) { pageData.Add(new string[] { pageList[i].Id.ToString(), pageList[i].Title, new DateTime(pageList[i].Id).ToString(), pageList[i].Visible.ToString() }); } List <string[]> naviData = new List <string[]>(); if (page > maxPage) { page = maxPage; } if (maxPage >= 1) { PageNumber pageNumber = new PageNumber(); pageNumber.UrlPrefix = "/admin/post-list?page="; naviData = pageNumber.GetPageNumbers(page, maxPage); } string postListHtmlTmpl = ResourceHelper.LoadStringResource("postlist.html"); string adminHtmlTmpl = ResourceHelper.LoadStringResource("admin.html"); return(adminHtmlTmpl.AsHtmlFromTemplate(new { Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), RenderBody = postListHtmlTmpl.AsHtmlFromTemplate(new { PageData = pageData.ToArray(), NaviData = naviData, CurrentPage = page.ToString(), Error = error, Success = success, Configuration = _tinyConfiguration }), Configuration = _tinyConfiguration })); }
public AdminModule() : base("/admin") { _tinyfxPageRender = new Cores.TinyfxPageRender(TinyfxCore.Configuration); this.RequiresAuthentication(); // 全局文本替换 Get("/global-replace", _ => { bool enable = false; if (enable) { // 原始 string srcText = "/images/"; // 替换为 string dstTest = "/files/"; int bb = 0; var ps = new PressService(); var data = ps.AllPosts; var newpp = new List <Models.Post>(); foreach (var item in data) { var pc = item; string ori = new Faes().Decrypt(item.Content); int poc = ori.IndexOf(srcText); if (poc >= 0) { string mr = ori.Replace(srcText, dstTest); pc.Content = new Faes().Encrypt(mr); bb++; } newpp.Add(pc); } string xml = new XmlSerializor().SerializorToString(newpp); return(xml); } else { return(new NotFoundResponse()); } }); Get("/", _ => { return(Response.AsText(_tinyfxPageRender.RenderAdminDashboard(), "text/html")); }); Get("/dashboard", _ => { return(Response.AsText(_tinyfxPageRender.RenderAdminDashboard(), "text/html")); }); Get("/edit-post", _ => { long pid = 0; string pidstr = Request.Query.Pid; pid = pidstr.AsLong(); return(Response.AsText(_tinyfxPageRender.RenderCreatePost("GET", pid, null, null, false), "text/html")); }); Post("/edit-post", _ => { long pid = 0; string pidstr = Request.Query.Pid; pid = pidstr.AsLong(); string title = Request.Form.title; string content = Request.Form.content; bool isPublic = false; if (Request.Form.isPublic != null && Request.Form.isPublic == "on") { isPublic = true; } string html = _tinyfxPageRender.RenderCreatePost("POST", pid, title, content, isPublic); if (html != null) { return(Response.AsText(html, "text/html")); } else { return(Response.AsRedirect(this.ModulePath + "/post-list")); } }); Get("/post-list", _ => { string pageStr = Request.Query.page; string pidStr = Request.Query.pid; string action = Request.Query.action + ""; int page = pageStr.AsInt(); long pid = pidStr.AsLong(); return(Response.AsText(_tinyfxPageRender.RenderPostList(page, action, pid), "text/html")); }); Post("/upload", _ => { var faes = new Faes(); var config = TinyfxCore.Configuration; var file = this.Request.Files.FirstOrDefault(); if (file != null) { try { DateTime now = DateTime.Now; string ext = System.IO.Path.GetExtension(file.Name).ToLower(); if (!TinyfxCore.Mime.ContainsKey(ext)) { return(Response.AsJson(new { error = 3, url = "" })); } string filename = now.Ticks.ToString() + ext; string year = now.Year.ToString(); string month = now.Month.ToString(); string dir = System.IO.Path.Combine(config.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, year, month); if (!string.IsNullOrEmpty(TinyfxCore.Configuration.DataDirectory)) { dir = System.IO.Path.Combine(TinyfxCore.Configuration.DataDirectory, TinyfxCore.IMAGE_UPLOAD_DIR, year, month); } string fullname = System.IO.Path.Combine(dir, filename); if (!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } if (TinyfxCore.Configuration.Encryption) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); faes.Encrypt(file.Value, ms); ms.Seek(0, System.IO.SeekOrigin.Begin); using (var fs = System.IO.File.Open(fullname, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)) { ms.CopyTo(fs); } } else { using (var fs = System.IO.File.Open(fullname, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite)) { file.Value.CopyTo(fs); } } string url = "/files/" + year + "_" + month + "_" + filename; return(Response.AsJson(new { error = 0, url = url })); } catch (Exception) { return(Response.AsJson(new { error = 1, url = "" })); } } else { return(Response.AsJson(new { error = 2, url = "" })); } }); Get("/change-password", _ => { LogHelper.WriteLog(LogHelper.LogType.INFO, "HTTP GET /change-password", null); return(Response.AsText(_tinyfxPageRender.RenderChangePassword(Request.Method, null, null, null), "text/html")); }); Post("/change-password", _ => { LogHelper.WriteLog(LogHelper.LogType.INFO, "HTTP POST /change-password", null); string username = Request.Form.username + ""; string password = Request.Form.password + ""; string repassword = Request.Form.repassword + ""; return(Response.AsText(_tinyfxPageRender.RenderChangePassword(Request.Method, username, password, repassword), "text/html")); }); }