/// <summary> /// Sends a complete post to a server. Parses the post and sends /// embedded images as media attachments. /// </summary> /// <param name="post"></param> /// <param name="basePath"></param> /// <param name="sendAsDraft"></param> /// <param name="markdown"></param> /// <returns></returns> public bool PublishCompletePost(Post post, string basePath = null, bool sendAsDraft = false, string markdown = null) { WeblogTypes type = WeblogInfo.Type; if (type == WeblogTypes.Unknown) { type = WeblogInfo.Type; } var wrapper = GetWrapper(); string body = post.Body; try { post.MediaObjects = WeblogAddinConfiguration.Current.StoreMediaObjectInfo ? (post.MediaObjects ?? new Dictionary <string, MediaObjectInfo>()) : null; body = SendImages(body, basePath, wrapper, post.MediaObjects); } catch (Exception ex) { ErrorMessage = $"Error sending images to Weblog at {WeblogInfo.ApiUrl}: " + ex.Message; mmApp.Log($"Error sending images to Weblog at {WeblogInfo.ApiUrl}: ", ex); return(false); } if (body == null) { return(false); } post.Body = body; if (!string.IsNullOrEmpty(FeaturedImageUrl) || !string.IsNullOrEmpty(FeatureImageId)) { var featuredImage = FeaturedImageUrl; if (!string.IsNullOrEmpty(FeatureImageId)) // id takes precedence { featuredImage = FeatureImageId; } post.wp_post_thumbnail = featuredImage; var thumbnailCustomField = post.CustomFields.FirstOrDefault(cf => cf.Key == "wp_post_thumbnail"); if (thumbnailCustomField != null) { thumbnailCustomField.Value = featuredImage; } else { var cfl = post.CustomFields.ToList(); cfl.Add( new CustomField() { Key = "wp_post_thumbnail", Value = featuredImage }); post.CustomFields = cfl.ToArray(); } } else { post.wp_post_thumbnail = null; } bool isNewPost = IsNewPost(post.PostId); try { if (!isNewPost) { wrapper.EditPost(post, !sendAsDraft); } else { post.PostId = wrapper.NewPost(post, !sendAsDraft); } } catch (Exception ex) { mmApp.Log($"Error sending post to Weblog at {WeblogInfo.ApiUrl}: ", ex); ErrorMessage = $"Error sending post to Weblog: " + ex.Message; return(false); } return(true); }
public bool PublishCompletePost(Post post, string basePath = null, bool sendAsDraft = false, string markdown = null) { WeblogTypes type = WeblogInfo.Type; if (type == WeblogTypes.Unknown) { type = WeblogInfo.Type; } var wrapper = GetWrapper(); string body = post.Body; try { body = SendImages(body, basePath, wrapper); } catch (Exception ex) { ErrorMessage = $"Error sending images to Weblog at {WeblogInfo.ApiUrl}: " + ex.Message; mmApp.Log($"Error sending images to Weblog at {WeblogInfo.ApiUrl}: ", ex); return(false); } if (body == null) { return(false); } post.Body = body; var customFields = new List <CustomField>(); if (!string.IsNullOrEmpty(markdown)) { customFields.Add( new CustomField() { ID = "mt_markdown", Key = "mt_markdown", Value = markdown }); } if (!string.IsNullOrEmpty(FeaturedImageUrl) || !string.IsNullOrEmpty(FeatureImageId)) { var featuredImage = FeaturedImageUrl; if (!string.IsNullOrEmpty(FeatureImageId)) // id takes precedence { featuredImage = FeatureImageId; } post.wp_post_thumbnail = featuredImage; customFields.Add( new CustomField() { ID = "wp_post_thumbnail", Key = "wp_post_thumbnail", Value = featuredImage }); } post.CustomFields = customFields.ToArray(); bool isNewPost = IsNewPost(post.PostID); try { if (!isNewPost) { wrapper.EditPost(post, !sendAsDraft); } else { post.PostID = wrapper.NewPost(post, !sendAsDraft); } } catch (Exception ex) { mmApp.Log($"Error sending post to Weblog at {WeblogInfo.ApiUrl}: ", ex); ErrorMessage = $"Error sending post to Weblog: " + ex.Message; return(false); } return(true); }
/// <summary> /// High level method that sends posts to the Weblog /// /// </summary> /// <returns></returns> public bool SendPost(WeblogTypes type = WeblogTypes.Unknown, bool sendAsDraft = false) { var editor = Model.ActiveEditor; if (editor == null) { return(false); } var doc = editor.MarkdownDocument; ActivePost = new Post() { DateCreated = DateTime.Now }; // start by retrieving the current Markdown from the editor string markdown = editor.GetMarkdown(); // Retrieve Meta data from post and clean up the raw markdown // so we render without the config data var meta = GetPostConfigFromMarkdown(markdown); string html = doc.RenderHtml(meta.MarkdownBody, WeblogAddinConfiguration.Current.RenderLinksOpenExternal); var config = WeblogAddinConfiguration.Current; var kv = config.Weblogs.FirstOrDefault(kvl => kvl.Value.Name == meta.WeblogName); if (kv.Equals(default(KeyValuePair <string, WeblogInfo>))) { MessageBox.Show("Invalid Weblog configuration selected.", "Weblog Posting Failed", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } WeblogInfo weblogInfo = kv.Value; if (type == WeblogTypes.Unknown) { type = weblogInfo.Type; } MetaWeblogWrapper wrapper; if (type == WeblogTypes.MetaWeblogApi) { wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.DecryptPassword(weblogInfo.Password), weblogInfo.BlogId); } else { wrapper = new WordPressWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.DecryptPassword(weblogInfo.Password)); } string body; try { body = SendImages(html, doc.Filename, wrapper, meta); } catch (Exception ex) { mmApp.Log($"Error sending images to Weblog at {weblogInfo.ApiUrl}: ", ex); return(false); } if (body == null) { return(false); } ActivePost.Body = body; ActivePost.PostID = meta.PostId; var customFields = new List <CustomField>(); customFields.Add( new CustomField() { ID = "mt_markdown", Key = "mt_markdown", Value = meta.MarkdownBody }); if (!string.IsNullOrEmpty(meta.FeaturedImageUrl)) { customFields.Add( new CustomField() { ID = "wp_post_thumbnail", Key = "wp_post_thumbnail", Value = meta.FeaturedImageUrl }); } ActivePost.CustomFields = customFields.ToArray(); bool isNewPost = IsNewPost(ActivePost.PostID); try { if (!isNewPost) { wrapper.EditPost(ActivePost, !sendAsDraft); } else { ActivePost.PostID = wrapper.NewPost(ActivePost, !sendAsDraft); } } catch (Exception ex) { mmApp.Log($"Error sending post to Weblog at {weblogInfo.ApiUrl}: ", ex); MessageBox.Show($"Error sending post to Weblog: " + ex.Message, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); mmApp.Log(ex); return(false); } meta.PostId = ActivePost.PostID.ToString(); // retrieve the raw editor markdown markdown = editor.GetMarkdown(); meta.RawMarkdownBody = markdown; // add the meta configuration to it markdown = SetConfigInMarkdown(meta); // write it back out to editor editor.SetMarkdown(markdown); // preview post if (!string.IsNullOrEmpty(weblogInfo.PreviewUrl)) { var url = weblogInfo.PreviewUrl.Replace("{0}", ActivePost.PostID.ToString()); ShellUtils.GoUrl(url); } else { try { var postRaw = wrapper.GetPostRaw(ActivePost.PostID); var link = postRaw.link; if (!string.IsNullOrEmpty(link) && (link.StartsWith("http://") || link.StartsWith("https://"))) { ShellUtils.GoUrl(link); } else { // just go to the base domain - assume posts are listed there ShellUtils.GoUrl(new Uri(weblogInfo.ApiUrl).GetLeftPart(UriPartial.Authority)); } } catch { } } return(true); }
/// <summary> /// High level method that sends posts to the Weblog /// /// </summary> /// <returns></returns> public bool SendPost(WeblogTypes type = WeblogTypes.MetaWeblogApi, bool sendAsDraft = false) { var editor = Model.ActiveEditor; if (editor == null) { return(false); } var doc = editor.MarkdownDocument; ActivePost = new Post() { DateCreated = DateTime.Now }; // start by retrieving the current Markdown from the editor string markdown = editor.GetMarkdown(); // Retrieve Meta data from post and clean up the raw markdown // so we render without the config data var meta = GetPostConfigFromMarkdown(markdown); string html = doc.RenderHtml(meta.MarkdownBody, WeblogAddinConfiguration.Current.RenderLinksOpenExternal); var config = WeblogAddinConfiguration.Current; var kv = config.Weblogs.FirstOrDefault(kvl => kvl.Value.Name == meta.WeblogName); if (kv.Equals(default(KeyValuePair <string, WeblogInfo>))) { MessageBox.Show("Invalid Weblog configuration selected.", "Weblog Posting Failed", MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } WeblogInfo weblogInfo = kv.Value; MetaWeblogWrapper wrapper; if (type == WeblogTypes.MetaWeblogApi) { wrapper = new MetaWeblogWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password, weblogInfo.BlogId) as MetaWeblogWrapper; } else { wrapper = new WordPressWrapper(weblogInfo.ApiUrl, weblogInfo.Username, weblogInfo.Password) as MetaWeblogWrapper; } string body = SendImages(html, doc.Filename, wrapper); if (body == null) { return(false); } ActivePost.Body = body; ActivePost.PostID = meta.PostId; ActivePost.CustomFields = new CustomField[1] { new CustomField() { ID = "mt_markdown", Key = "mt_markdown", Value = meta.MarkdownBody } }; bool isNewPost = IsNewPost(ActivePost.PostID); try { if (!isNewPost) { wrapper.EditPost(ActivePost, !sendAsDraft); } else { ActivePost.PostID = wrapper.NewPost(ActivePost, !sendAsDraft); } } catch (Exception ex) { MessageBox.Show("Error sending post to Weblog: " + ex.Message, mmApp.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation); mmApp.Log(ex); return(false); } meta.PostId = ActivePost.PostID.ToString(); // retrieve the raw editor markdown markdown = editor.GetMarkdown(); meta.RawMarkdownBody = markdown; // add the meta configuration to it markdown = SetConfigInMarkdown(meta); // write it back out to editor editor.SetMarkdown(markdown); // preview post if (!string.IsNullOrEmpty(weblogInfo.PreviewUrl)) { var url = weblogInfo.PreviewUrl.Replace("{0}", ActivePost.PostID.ToString()); ShellUtils.GoUrl(url); } return(true); }