/// <summary> /// Loads draft /// Called when creating a post /// </summary> /// <param name="draftID"> /// Draft ID /// </param> /// <returns> /// Draft Post /// </returns> public DraftPost Load(string draftID) { string path = this.GetFilePath(draftID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } XElement postElem = root; DraftPost post = new DraftPost(); try { post.FileID = postElem.Attribute("FileID").Value; post.DraftID = draftID; post.Title = postElem.Attribute("Title").Value; post.Author = postElem.Attribute("Author").Value; post.CatID = postElem.Attribute("CatID").Value; XAttribute attr = postElem.Attribute("Type"); if (attr != null) { post.Type = (PostType)Enum.Parse(typeof(PostType), attr.Value, true); } // Load draft data, if we are loading a draft if (post.Type == PostType.Draft) { if (postElem.Attribute("OldCatID") != null) { post.OldCatID = postElem.Attribute("OldCatID").Value; } } try { post.Time = DateTime.ParseExact( postElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); } catch (Exception ex) { string msg = string.Format(DATE_FORMAT_ERROR, draftID); Logger.Log(msg, ex); post.Time = DateTime.MinValue; } XElement docsElem = postElem.Element("Documents"); if (docsElem != null) { foreach (XElement docElem in docsElem.Elements("Document")) { Document document = new Document(); document.Path = docElem.Attribute("Path").Value; post.Documents.Add(document); } } // string contents = HttpContext.Current.Server.HtmlDecode(postElem.Element("Content").Value); string contents = HttpContext.Current.Server.HtmlDecode(GetInnerXml(postElem.Element("Content"))); post.Contents = contents; XElement commentsElem = postElem.Element("Comments"); if (commentsElem != null) { foreach (XElement commentElem in commentsElem.Elements("Comment")) { Comment comment = new Comment(); comment.FileID = post.FileID; comment.ID = commentElem.Attribute("ID").Value; comment.Name = commentElem.Attribute("Name").Value; comment.Text = HttpContext.Current.Server.HtmlDecode(GetInnerXml(commentElem)); comment.Url = commentElem.Attribute("Url").Value; comment.Time = DateTime.ParseExact( commentElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); comment.Ip = commentElem.Attribute("Ip").Value; post.Comments.Add(comment); } } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return post; }
/// <summary> /// Gets the post data /// Called when displaying a post /// </summary> /// <param name="fileID"> /// Post ID /// </param> /// <returns> /// The Post /// </returns> public Post Load(string fileID) { string path = this.GetFilePath(fileID); XElement root = null; try { root = XElement.Load(path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } Post post = new Post(); XElement postElem = root; try { post.FileID = postElem.Attribute("FileID").Value; post.Title = postElem.Attribute("Title").Value; post.Author = postElem.Attribute("Author").Value; post.CatID = postElem.Attribute("CatID").Value; try { post.Time = DateTime.ParseExact( postElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); } catch (Exception ex) { string msg = string.Format(DATE_FORMAT_ERROR, fileID); Logger.Log(msg, ex); post.Time = DateTime.MinValue; } XElement docsElem = postElem.Element("Documents"); if (docsElem != null) { foreach (XElement docElem in docsElem.Elements("Document")) { Document document = new Document(); document.Path = docElem.Attribute("Path").Value; post.Documents.Add(document); } } string contents = HttpContext.Current.Server.HtmlDecode(GetInnerXml(postElem.Element("Content"))); post.Contents = contents; XElement commentsElem = postElem.Element("Comments"); if (commentsElem != null) { foreach (XElement commentElem in commentsElem.Elements("Comment")) { Comment comment = new Comment(); comment.FileID = post.FileID; comment.ID = commentElem.Attribute("ID").Value; comment.Name = commentElem.Attribute("Name").Value; comment.Text = HttpContext.Current.Server.HtmlDecode(GetInnerXml(commentElem)); // comment.Text = HttpContext.Current.Server.HtmlDecode(comment.Text); comment.Url = commentElem.Attribute("Url").Value; comment.Time = DateTime.ParseExact( commentElem.Attribute("Time").Value, DataContext.DateTimeFormat, CultureInfo.InvariantCulture); comment.Ip = commentElem.Attribute("Ip").Value; if (commentElem.Attribute("IsAuthor") != null) { comment.IsAuthor = bool.Parse(commentElem.Attribute("IsAuthor").Value); } post.Comments.Add(comment); } } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return post; }
public ActionResult Upload(ComposePostModel model) { this.UpdatePost(model); for (int i = 0; i < this.Request.Files.Count; i++) { HttpPostedFileBase file = this.Request.Files[i]; if (file.ContentLength > 0) { string docPath = this.Server.MapPath(this.Url.Content("~/Docs/")); string fileName = DraftComp.StoreDocument(file, docPath); Document doc = new Document(); doc.Path = this.Url.Content(string.Format("~/Docs/{0}", fileName)); model.Post.Documents.Add(doc); } } this.Session["Post"] = model.Post; return this.RedirectToAction("Compose", new { id = string.Empty }); }