You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
6.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Web;
  5. using System.Web.Hosting;
  6. using System.Xml.Linq;
  7. using System.Xml.XPath;
  8. public static class Storage
  9. {
  10. private static string _folder = HostingEnvironment.MapPath("~/posts/");
  11. public static List<Post> GetAllPosts()
  12. {
  13. if (HttpRuntime.Cache["posts"] == null)
  14. LoadPosts();
  15. if (HttpRuntime.Cache["posts"] != null)
  16. {
  17. return (List<Post>)HttpRuntime.Cache["posts"];
  18. }
  19. return new List<Post>();
  20. }
  21. public static void Save(Post post)
  22. {
  23. string file = Path.Combine(_folder, post.ID + ".xml");
  24. post.LastModified = DateTime.UtcNow;
  25. XDocument doc = new XDocument(
  26. new XElement("post",
  27. new XElement("title", post.Title),
  28. new XElement("slug", post.Slug),
  29. new XElement("author", post.Author),
  30. new XElement("pubDate", post.PubDate.ToString("yyyy-MM-dd HH:mm:ss")),
  31. new XElement("lastModified", post.LastModified.ToString("yyyy-MM-dd HH:mm:ss")),
  32. new XElement("content", post.Content),
  33. new XElement("ispublished", post.IsPublished),
  34. new XElement("categories", string.Empty),
  35. new XElement("comments", string.Empty)
  36. ));
  37. XElement categories = doc.XPathSelectElement("post/categories");
  38. foreach (string category in post.Categories)
  39. {
  40. categories.Add(new XElement("category", category));
  41. }
  42. XElement comments = doc.XPathSelectElement("post/comments");
  43. foreach (Comment comment in post.Comments)
  44. {
  45. comments.Add(
  46. new XElement("comment",
  47. new XElement("author", comment.Author),
  48. new XElement("email", comment.Email),
  49. new XElement("website", comment.Website),
  50. new XElement("ip", comment.Ip),
  51. new XElement("userAgent", comment.UserAgent),
  52. new XElement("date", comment.PubDate.ToString("yyyy-MM-dd HH:m:ss")),
  53. new XElement("content", comment.Content),
  54. new XAttribute("isAdmin", comment.IsAdmin),
  55. new XAttribute("isApproved", comment.IsApproved),
  56. new XAttribute("id", comment.ID)
  57. ));
  58. }
  59. if (!File.Exists(file)) // New post
  60. {
  61. var posts = GetAllPosts();
  62. posts.Insert(0, post);
  63. posts.Sort((p1, p2) => p2.PubDate.CompareTo(p1.PubDate));
  64. HttpRuntime.Cache.Insert("posts", posts);
  65. }
  66. doc.Save(file);
  67. }
  68. public static void Delete(Post post)
  69. {
  70. var posts = GetAllPosts();
  71. string file = Path.Combine(_folder, post.ID + ".xml");
  72. File.Delete(file);
  73. posts.Remove(post);
  74. }
  75. private static void LoadPosts()
  76. {
  77. if (!Directory.Exists(_folder))
  78. Directory.CreateDirectory(_folder);
  79. List<Post> list = new List<Post>();
  80. foreach (string file in Directory.GetFiles(_folder, "*.xml", SearchOption.TopDirectoryOnly))
  81. {
  82. XElement doc = XElement.Load(file);
  83. Post post = new Post()
  84. {
  85. ID = Path.GetFileNameWithoutExtension(file),
  86. Title = ReadValue(doc, "title"),
  87. Author = ReadValue(doc, "author"),
  88. Content = ReadValue(doc, "content"),
  89. Slug = ReadValue(doc, "slug").ToLowerInvariant(),
  90. PubDate = DateTime.Parse(ReadValue(doc, "pubDate")),
  91. LastModified = DateTime.Parse(ReadValue(doc, "lastModified", DateTime.Now.ToString())),
  92. IsPublished = bool.Parse(ReadValue(doc, "ispublished", "true")),
  93. };
  94. LoadCategories(post, doc);
  95. LoadComments(post, doc);
  96. list.Add(post);
  97. }
  98. if (list.Count > 0)
  99. {
  100. list.Sort((p1, p2) => p2.PubDate.CompareTo(p1.PubDate));
  101. HttpRuntime.Cache.Insert("posts", list);
  102. }
  103. }
  104. private static void LoadCategories(Post post, XElement doc)
  105. {
  106. XElement categories = doc.Element("categories");
  107. if (categories == null)
  108. return;
  109. List<string> list = new List<string>();
  110. foreach (var node in categories.Elements("category"))
  111. {
  112. list.Add(node.Value);
  113. }
  114. post.Categories = list.ToArray();
  115. }
  116. private static void LoadComments(Post post, XElement doc)
  117. {
  118. var comments = doc.Element("comments");
  119. if (comments == null)
  120. return;
  121. foreach (var node in comments.Elements("comment"))
  122. {
  123. Comment comment = new Comment()
  124. {
  125. ID = ReadAttribute(node, "id"),
  126. Author = ReadValue(node, "author"),
  127. Email = ReadValue(node, "email"),
  128. Website = ReadValue(node, "website"),
  129. Ip = ReadValue(node, "ip"),
  130. UserAgent = ReadValue(node, "userAgent"),
  131. IsAdmin = bool.Parse(ReadAttribute(node, "isAdmin", "false")),
  132. IsApproved = bool.Parse(ReadAttribute(node, "isApproved", "true")),
  133. Content = ReadValue(node, "content").Replace("\n", "<br />"),
  134. PubDate = DateTime.Parse(ReadValue(node, "date", "2000-01-01")),
  135. };
  136. post.Comments.Add(comment);
  137. }
  138. }
  139. private static string ReadValue(XElement doc, XName name, string defaultValue = "")
  140. {
  141. if (doc.Element(name) != null)
  142. return doc.Element(name).Value;
  143. return defaultValue;
  144. }
  145. private static string ReadAttribute(XElement element, XName name, string defaultValue = "")
  146. {
  147. if (element.Attribute(name) != null)
  148. return element.Attribute(name).Value;
  149. return defaultValue;
  150. }
  151. }