示例#1
0
        /// <summary>
        /// 获取指定页的文章
        /// </summary>
        /// <param name="page"></param>
        /// <param name="maxPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List <Post> GetPostInPage(int page, ref int maxPage, bool includeDraft, int pageSize = 5)
        {
            List <Post> postsList = new List <Post>();

            maxPage = (int)Math.Ceiling(AllPosts.Count() / (double)pageSize);
            List <Post> postsQuery = new List <Post>();

            if (!includeDraft)
            {
                postsQuery = AllPosts.Where(s => s.Visible == true).Skip((page - 1) * pageSize).Take(pageSize).ToList();
            }
            else
            {
                postsQuery = AllPosts.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            }

            if (TinyfxCore.Configuration.Encryption)
            {
                foreach (var item in postsQuery)
                {
                    var citem = new Post();
                    citem.Id      = item.Id;
                    citem.Visible = item.Visible;
                    citem.Content = faes.Decrypt(item.Content);
                    citem.Title   = faes.Decrypt(item.Title);
                    postsList.Add(citem);
                }

                return(postsList);
            }
            else
            {
                return(postsQuery);
            }
        }
示例#2
0
        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
            }));
        }
示例#3
0
        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());
                        }
                    }
                }
            });
        }