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