public ActionResult AddPDF(string webpage, ImageFile pdfThumb, DocumentFile pdf, int pdfOverlay) { string thumbPath = ""; if (pdfThumb != null) { pdfThumb.ValidateForUpload(true); if (ModelState.IsValid) { pdfThumb.Save("sidebar_pdf_thumb", new System.Drawing.Size(Config.Sidebar.Thumbnail.Width, Config.Sidebar.Thumbnail.Height), false); thumbPath = "/" + pdfThumb.SavePath; pdfThumb.Cleanup(); } } else { ModelState.AddModelError("", "PDF File is not recognized"); } string dbPath = ""; if (pdf != null) { pdf.ValidateForUpload(true); if (ModelState.IsValid) { if (System.IO.Path.GetExtension(pdf.Name).Equals(".pdf")) { ModelState.AddModelError("", "Invalid file type. Expected: .pdf"); } if (ModelState.IsValid) { pdf.Save("sidebar_pdf_file" + System.IO.Path.GetExtension(pdf.Name)); dbPath = "/" + pdf.SavePath; pdf.Cleanup(); } } } else { ModelState.AddModelError("", "PDF File is not recognized"); } if (!string.IsNullOrEmpty(dbPath) && !string.IsNullOrEmpty(thumbPath)) { DBDataContext db = Utils.DB.GetContext(); WebPage pg = db.WebPages.SingleOrDefault(x => x.ID == Convert.ToInt32(webpage)); if (pg != null) { pg.Sidebars.Add(new Sidebar() { TypeID = db.Types.Single(x => x.Name.Equals("PDF")).ID, Source = dbPath, Thumb = thumbPath, IsOverlay = Convert.ToBoolean(pdfOverlay) }); try { db.SubmitChanges(); return(RedirectToAction("Index", "Sidebar", new { controller = "Sidebar", action = "Index", webpage = webpage })); } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred. Please try again in few minutes."); ErrorHandler.Report.Exception(ex, "Sidebar/AddPDF[POST]"); } } else { ModelState.AddModelError("", "Web Page is doesn't exist"); } } else { ModelState.AddModelError("", "There has been an issue with uploading your PDF file. Please try again in few minutes."); } if (System.IO.File.Exists(HttpContext.Server.MapPath(dbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(dbPath)); } if (System.IO.File.Exists(HttpContext.Server.MapPath(thumbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(thumbPath)); } ViewData["Type"] = "PDF"; ViewData["WebPageID"] = webpage; return(View("Add")); }
public ActionResult PDFPrev(string webPageId, DocumentFile doc) { string code = ""; if (doc != null) { doc.ValidateForUpload(true); if (ModelState.IsValid) { if (doc.Save(doc.Name.Replace(" ", "").ToLower())) { code = doc.SavedName; doc.Cleanup(); } } } else { ModelState.AddModelError("", "Document File format is not recognized"); } if (!string.IsNullOrEmpty(code)) { DBDataContext db = Utils.DB.GetContext(); WebPage pg = db.WebPages.SingleOrDefault(x => x.ID == Convert.ToInt32(webPageId)); if (pg != null) { Body b = pg.Bodies.FirstOrDefault(); if (b != null) { //delete existing PDF file if (System.IO.File.Exists(HttpContext.Server.MapPath(Url.Data(b.HTML)))) { System.IO.File.Delete(HttpContext.Server.MapPath(Url.Data(b.HTML))); } b.HTML = HttpUtility.HtmlEncode(code); } else { pg.Bodies.Add(new Body() { HTML = HttpUtility.HtmlEncode(code) }); } try { db.SubmitChanges(); return RedirectToAction("Index", "WebPage", new { controller = "WebPage", action = "Index", webpage = webPageId }); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); ErrorHandler.Report.Exception(ex, "Body/PDFPrev [POST]"); } } else { ModelState.AddModelError("", "Web Page does not exist"); } } else { ModelState.AddModelError("", "There has been an issue with uploading your Document file. Please try again in few minutes."); } return View(); }
public ActionResult ManageDocument(Document doc, DocumentFile file, ImageFile thumb, string category, string name, string webPageId, string tmpl) { string dbPath = "", dbThumbPath = ""; DBDataContext db = Utils.DB.GetContext(); //check for category <> 0 int categoryId = 0; if (!string.IsNullOrEmpty(name)) { //add category to database Category c = new Category() { Name = name }; try { db.Categories.InsertOnSubmit(c); db.SubmitChanges(); categoryId = c.ID; } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred while adding category to the database."); ErrorHandler.Report.Exception(ex, "Body/ManageDocument[HTTPPOST]"); } } //no new category, try parsing existing one from dropdown list if (categoryId == 0) { Int32.TryParse(category, out categoryId); } //validate category if (categoryId > 0) { WebPage page = db.WebPages.SingleOrDefault(x => x.ID == Convert.ToInt32(webPageId)); if (page != null) { if (file != null) { file.ValidateForUpload(true); if (ModelState.IsValid) { string documentName = "doc-lib_" + DateTime.Now.Ticks.ToString() + "_" + new Random().Next(1, 100).ToString() + System.IO.Path.GetExtension(file.Name); if (file.Save(documentName)) { dbPath = "/" + file.SavePath; file.Cleanup(); } } } else { ModelState.AddModelError("", "Document file is not recognized"); } if (thumb != null) { thumb.ValidateForUpload(true); if (ModelState.IsValid) { string thumbName = "doc-lib_thumb_" + DateTime.Now.Ticks.ToString() + "_" + new Random().Next(1, 100).ToString(); if (thumb.Save(thumbName, new System.Drawing.Size(Config.DocLib.Thumbnail.Width, Config.DocLib.Thumbnail.Height), false)) { dbThumbPath = "/" + thumb.SavePath; thumb.Cleanup(); } } } else { ModelState.AddModelError("", "Image file is not recognized"); } if (!string.IsNullOrEmpty(dbPath) && !string.IsNullOrEmpty(dbThumbPath)) { Category c = db.Categories.SingleOrDefault(x => x.ID == categoryId); Document d = new Document() { Title = doc.Title, Source = dbPath, Thumb = dbThumbPath }; d.Category = c; page.Documents.Add(d); try { db.SubmitChanges(); return(RedirectToAction("DocLib", "Body", new { controller = "Body", action = "DocLib", webpage = webPageId.ToString(), tmpl = tmpl })); } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred. Please try again in a few minutes."); ErrorHandler.Report.Exception(ex, "Body/ManageDocument[HTTPPOST]"); } } else { ModelState.AddModelError("", "There has been an issue with uploading your Image file. Please try again in few minutes."); } } else { ModelState.AddModelError("", "Page cannot be found. Please try again in few minutes."); } } else { ModelState.AddModelError("category", "You must either add new or select valid category for this document."); } if (System.IO.File.Exists(HttpContext.Server.MapPath(dbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(dbPath)); } if (System.IO.File.Exists(HttpContext.Server.MapPath(dbThumbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(dbThumbPath)); } ViewData["WebPageID"] = webPageId; ViewData["TemplateID"] = tmpl; return(View("ManageDocument", new Document())); }
public ActionResult ManageDocument(Document doc, DocumentFile file, ImageFile thumb, string category, string name, string webPageId, string tmpl) { string dbPath = "", dbThumbPath = ""; DBDataContext db = Utils.DB.GetContext(); //check for category <> 0 int categoryId = 0; if (!string.IsNullOrEmpty(name)) { //add category to database Category c = new Category() { Name = name }; try { db.Categories.InsertOnSubmit(c); db.SubmitChanges(); categoryId = c.ID; } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred while adding category to the database."); ErrorHandler.Report.Exception(ex, "Body/ManageDocument[HTTPPOST]"); } } //no new category, try parsing existing one from dropdown list if (categoryId == 0) { Int32.TryParse(category, out categoryId); } //validate category if (categoryId > 0) { WebPage page = db.WebPages.SingleOrDefault(x => x.ID == Convert.ToInt32(webPageId)); if (page != null) { if (file != null) { file.ValidateForUpload(true); if (ModelState.IsValid) { string documentName = "doc-lib_" + DateTime.Now.Ticks.ToString() + "_" + new Random().Next(1, 100).ToString() + System.IO.Path.GetExtension(file.Name); if (file.Save(documentName)) { dbPath = "/" + file.SavePath; file.Cleanup(); } } } else { ModelState.AddModelError("", "Document file is not recognized"); } if (thumb != null) { thumb.ValidateForUpload(true); if (ModelState.IsValid) { string thumbName = "doc-lib_thumb_" + DateTime.Now.Ticks.ToString() + "_" + new Random().Next(1, 100).ToString(); if (thumb.Save(thumbName, new System.Drawing.Size(Config.DocLib.Thumbnail.Width, Config.DocLib.Thumbnail.Height), false)) { dbThumbPath = "/" + thumb.SavePath; thumb.Cleanup(); } } } else { ModelState.AddModelError("", "Image file is not recognized"); } if (!string.IsNullOrEmpty(dbPath) && !string.IsNullOrEmpty(dbThumbPath)) { Category c = db.Categories.SingleOrDefault(x => x.ID == categoryId); Document d = new Document() { Title = doc.Title, Source = dbPath, Thumb = dbThumbPath }; d.Category = c; page.Documents.Add(d); try { db.SubmitChanges(); return RedirectToAction("DocLib", "Body", new { controller = "Body", action = "DocLib", webpage = webPageId.ToString(), tmpl = tmpl }); } catch (Exception ex) { ModelState.AddModelError("", "An unknown error occurred. Please try again in a few minutes."); ErrorHandler.Report.Exception(ex, "Body/ManageDocument[HTTPPOST]"); } } else { ModelState.AddModelError("", "There has been an issue with uploading your Image file. Please try again in few minutes."); } } else { ModelState.AddModelError("", "Page cannot be found. Please try again in few minutes."); } } else { ModelState.AddModelError("category", "You must either add new or select valid category for this document."); } if (System.IO.File.Exists(HttpContext.Server.MapPath(dbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(dbPath)); } if (System.IO.File.Exists(HttpContext.Server.MapPath(dbThumbPath))) { System.IO.File.Delete(HttpContext.Server.MapPath(dbThumbPath)); } ViewData["WebPageID"] = webPageId; ViewData["TemplateID"] = tmpl; return View("ManageDocument", new Document()); }
public ActionResult PDFPrev(string webPageId, DocumentFile doc) { string code = ""; if (doc != null) { doc.ValidateForUpload(true); if (ModelState.IsValid) { if (doc.Save(doc.Name.Replace(" ", "").ToLower())) { code = doc.SavedName; doc.Cleanup(); } } } else { ModelState.AddModelError("", "Document File format is not recognized"); } if (!string.IsNullOrEmpty(code)) { DBDataContext db = Utils.DB.GetContext(); WebPage pg = db.WebPages.SingleOrDefault(x => x.ID == Convert.ToInt32(webPageId)); if (pg != null) { Body b = pg.Bodies.FirstOrDefault(); if (b != null) { //delete existing PDF file if (System.IO.File.Exists(HttpContext.Server.MapPath(Url.Data(b.HTML)))) { System.IO.File.Delete(HttpContext.Server.MapPath(Url.Data(b.HTML))); } b.HTML = HttpUtility.HtmlEncode(code); } else { pg.Bodies.Add(new Body() { HTML = HttpUtility.HtmlEncode(code) }); } try { db.SubmitChanges(); return(RedirectToAction("Index", "WebPage", new { controller = "WebPage", action = "Index", webpage = webPageId })); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); ErrorHandler.Report.Exception(ex, "Body/PDFPrev [POST]"); } } else { ModelState.AddModelError("", "Web Page does not exist"); } } else { ModelState.AddModelError("", "There has been an issue with uploading your Document file. Please try again in few minutes."); } return(View()); }