PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/API/BlogML/BlogImporter.cs

#
C# | 112 lines | 73 code | 15 blank | 24 comment | 18 complexity | 60c8985c62b6a67d9a181921729308c7 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.API.BlogML
  2. {
  3. using System;
  4. using System.Globalization;
  5. using System.Text.RegularExpressions;
  6. /// <summary>
  7. /// Blog Importer
  8. /// </summary>
  9. public class BlogImporter
  10. {
  11. #region Public Methods
  12. /// <summary>
  13. /// Add new blog post to system
  14. /// </summary>
  15. /// <returns>
  16. /// string containing unique post identifier
  17. /// </returns>
  18. public string AddPost(BlogMlExtendedPost extPost)
  19. {
  20. if (!Security.IsAdministrator)
  21. {
  22. throw new InvalidOperationException("BlogImporter.AddPost: Wrong credentials");
  23. }
  24. using (var p = new Post())
  25. {
  26. p.Title = extPost.BlogPost.Title;
  27. p.DateCreated = extPost.BlogPost.DateCreated;
  28. p.DateModified = extPost.BlogPost.DateModified;
  29. p.Content = extPost.BlogPost.Content.UncodedText;
  30. p.Description = extPost.BlogPost.Excerpt.UncodedText;
  31. p.IsPublished = extPost.BlogPost.Approved;
  32. if (!string.IsNullOrEmpty(extPost.PostUrl))
  33. {
  34. // looking for a Slug with patterns such as:
  35. // /some-slug.aspx
  36. // /some-slug.html
  37. // /some-slug
  38. //
  39. Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase);
  40. if (slugMatch.Success)
  41. p.Slug = slugMatch.Groups[1].Value.Trim();
  42. }
  43. if(extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0)
  44. p.Author = extPost.BlogPost.Authors[0].Ref;
  45. if (extPost.Categories != null && extPost.Categories.Count > 0)
  46. p.Categories.AddRange(extPost.Categories);
  47. if(extPost.Tags != null && extPost.Tags.Count > 0)
  48. p.Tags.AddRange(extPost.Tags);
  49. // skip if post with this url already exists
  50. var s = PostUrl(p.Slug, p.DateCreated);
  51. var list = Post.Posts.FindAll(ps => ps.RelativeLink == s);
  52. if (list.Count > 0)
  53. {
  54. return string.Empty;
  55. }
  56. if(extPost.Comments != null && extPost.Comments.Count > 0)
  57. {
  58. foreach (var comment in extPost.Comments)
  59. {
  60. p.ImportComment(comment);
  61. }
  62. }
  63. p.Import();
  64. return p.Id.ToString();
  65. }
  66. }
  67. /// <summary>
  68. /// Force Reload of all posts
  69. /// </summary>
  70. public void ForceReload()
  71. {
  72. if (!Security.IsAdministrator)
  73. {
  74. throw new InvalidOperationException("BlogImporter.ForeceReload: Wrong credentials");
  75. }
  76. Post.Reload();
  77. }
  78. /// <summary>
  79. /// post url
  80. /// </summary>
  81. /// <param name="slug">post slug</param>
  82. /// <param name="dateCreated">date created</param>
  83. /// <returns></returns>
  84. static string PostUrl(string slug, DateTime dateCreated)
  85. {
  86. var theslug = Utils.RemoveIllegalCharacters(slug) + BlogConfig.FileExtension;
  87. return BlogSettings.Instance.TimeStampPostLinks
  88. ? string.Format(
  89. "{0}post/{1}{2}",
  90. Utils.RelativeWebRoot,
  91. dateCreated.ToString("yyyy/MM/dd/", CultureInfo.InvariantCulture),
  92. theslug)
  93. : string.Format("{0}post/{1}", Utils.RelativeWebRoot, theslug);
  94. }
  95. #endregion
  96. }
  97. }