public Task<SaveResult> SaveDocument(BlogSetting blog, WebDocument document)
        {
            if (blog.WebSourceType == WebSourceType.MetaWebLog)
            {
                return TaskEx.Run(() =>
                {
                    var categories = document.Categories.ToArray();
                    return CreateOrUpdateMetaWebLogPost(document, categories, blog);
                });
            }
            if (blog.WebSourceType == WebSourceType.GitHub)
            {
                return CreateOrUpdateGithubPost(document.Title, document.MarkdownContent, document.AssociatedFiles, blog);   
            }

            return TaskEx.Run(new Func<SaveResult>(() =>
            {
                throw BadWebSourceTypeException(blog);
            }));
        }
Exemplo n.º 2
0
        public Task<string> SaveDocument(BlogSetting blog, WebDocument document)
        {
            if (blog.WebSourceType == WebSourceType.MetaWebLog)
            {
                return TaskEx.Run(() =>
                {
                    var categories = document.Categories.ToArray();
                    return CreateOrUpdateMetaWebLogPost(document.Id, document.Title, categories, document.MarkdownContent,
                                                 document.ImagesToSaveOnPublish, blog);
                });
            }
            if (blog.WebSourceType == WebSourceType.GitHub)
            {
                return CreateOrUpdateGithubPost(document.Title, document.MarkdownContent, document.ImagesToSaveOnPublish, blog);   
            }

            return TaskEx.Run(new Func<string>(() =>
            {
                throw BadWebSourceTypeException(blog);
            }));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Publishes any document
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="document"></param>
        /// <returns></returns>
        public Task<IMarkpadDocument> PublishDocument(string postId, IMarkpadDocument document)
        {
            var blogs = blogService.GetBlogs();
            if (blogs == null || blogs.Count == 0)
            {
                if (!blogService.ConfigureNewBlog("Publish document"))
                    return TaskEx.FromResult<IMarkpadDocument>(null);
                blogs = blogService.GetBlogs();
                if (blogs == null || blogs.Count == 0)
                    return TaskEx.FromResult<IMarkpadDocument>(null);
            }

            var categories = new List<string>();
            var webDocument = document as WebDocument;
            if (webDocument != null)
                categories = webDocument.Categories;
            var pd = new Details { Title = document.Title, Categories = categories.ToArray()};
            var detailsResult = windowManager.ShowDialog(new PublishDetailsViewModel(pd, blogs));
            if (detailsResult != true)
                return TaskEx.FromResult<IMarkpadDocument>(null);

            var newDocument = new WebDocument(pd.Blog, null, pd.Title, document.MarkdownContent, new FileReference[0], this,
                                              webDocumentService.Value, siteContextGenerator.GetWebContext(pd.Blog), fileSystem);

            foreach (var associatedFile in document.AssociatedFiles)
            {
                newDocument.AddFile(new FileReference(associatedFile.FullPath, associatedFile.RelativePath, false));
            }

            return newDocument.Save();
        }
        SaveResult CreateOrUpdateMetaWebLogPost(WebDocument document, string[] categories, BlogSetting blog)
        {
            var newContent = document.MarkdownContent;
            var proxy = getMetaWeblog(blog.WebAPI);

            if (document.AssociatedFiles.Count(f=>!f.Saved) > 0)
            {
                foreach (var imageToUpload in document.AssociatedFiles.Where(f=>!f.Saved))
                {
                    var response = proxy.NewMediaObject(blog, new MediaObject
                    {
                        name = imageToUpload.FullPath,
                        type = "image/png",
                        bits = File.ReadAllBytes(imageToUpload.FullPath)
                    });

                    newContent = newContent.Replace(imageToUpload.RelativePath, response.url);
                    imageToUpload.Saved = true;
                }
            }

            var newpost = new Post();
            try
            {
                if (string.IsNullOrWhiteSpace(document.Id))
                {
                    var permalink = document.Title;

                    newpost = new Post
                    {
                        permalink = permalink,
                        title = document.Title,
                        dateCreated = DateTime.Now,
                        description = blog.Language == "HTML" ? DocumentParser.GetBodyContents(newContent) : newContent,
                        categories = categories,
                        format = blog.Language
                    };
                    newpost.postid = proxy.NewPost(blog, newpost, true);
                }
                else
                {
                    newpost = proxy.GetPost(document.Id, blog);
                    newpost.title = document.Title;
                    newpost.description = blog.Language == "HTML" ? DocumentParser.GetBodyContents(newContent) : newContent;
                    newpost.categories = categories;
                    newpost.format = blog.Language;

                    proxy.EditPost(document.Id, blog, newpost, true);
                }
            }
            catch (WebException ex)
            {
                dialogService.ShowError("Error Publishing", ex.Message, "");
            }
            catch (XmlRpcException ex)
            {
                dialogService.ShowError("Error Publishing", ex.Message, "");
            }
            catch (XmlRpcFaultException ex)
            {
                dialogService.ShowError("Error Publishing", ex.Message, "");
            }

            return new SaveResult
                   {
                       Id = newpost.postid.ToString(),
                       NewDocumentContent = newContent
                   };
        }