Ejemplo n.º 1
0
        public ActionResult Add(string webpage, ImageFile thumb, string link, int linkOverlay)
        {
            string dbPath = "";

            if (thumb != null)
            {
                thumb.ValidateForUpload(true);
                if (ModelState.IsValid)
                {
                    thumb.Save("sidebar_link_thumb", new System.Drawing.Size(Config.Sidebar.Thumbnail.Width, Config.Sidebar.Thumbnail.Height), false);
                    dbPath = "/" + thumb.SavePath;
                    thumb.Cleanup();
                }
            }
            else
            {
                ModelState.AddModelError("", "Thumbnail is not recognized");
            }

            if (!string.IsNullOrEmpty(dbPath))
            {
                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("Link")).ID, Source = link, Thumb = dbPath, IsOverlay = Convert.ToBoolean(linkOverlay)
                    });

                    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/Add[POST]");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Web Page is doesn't exist");
                }
            }
            else
            {
                ModelState.AddModelError("", "There has been an issue with uploading your Thumbnail file. Please try again in few minutes.");
            }

            if (System.IO.File.Exists(HttpContext.Server.MapPath(dbPath)))
            {
                System.IO.File.Delete(HttpContext.Server.MapPath(dbPath));
            }

            ViewData["Type"]      = "Link";
            ViewData["WebPageID"] = webpage;
            return(View("Add"));
        }
Ejemplo n.º 2
0
        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());
        }
Ejemplo n.º 3
0
        public ActionResult Add(ImageFile image, VideoFile video, string webPageId)
        {
            string dbImagePath   = "";
            string dbVideoPath   = "";
            string videoFileName = "";
            string imageFileName = "";

            if (image != null)
            {
                image.ValidateForUpload(true);
                if (ModelState.IsValid)
                {
                    if (image.Save("header_image", new System.Drawing.Size(Config.Header.Image.Width, Config.Header.Image.Height), false))
                    {
                        dbImagePath   = "/" + image.SavePath;
                        imageFileName = image.SavedName;
                        image.Cleanup();
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "Image File is not recognized");
            }

            if (video != null)
            {
                video.ValidateForUpload(true);
                if (ModelState.IsValid)
                {
                    if (!System.IO.Path.GetExtension(video.Name).Equals(".swf"))
                    {
                        ModelState.AddModelError("", "Invalid file type. Expected: .swf");
                    }

                    if (ModelState.IsValid)
                    {
                        if (video.Save("header_flash" + System.IO.Path.GetExtension(video.Name)))
                        {
                            dbVideoPath   = "/" + video.SavePath;
                            videoFileName = video.SavedName;
                            video.Cleanup();
                        }
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "Video File is not recognized");
            }

            bool removeFiles = false;

            if (!string.IsNullOrEmpty(dbImagePath) && !string.IsNullOrEmpty(dbVideoPath))
            {
                DBDataContext db = Utils.DB.GetContext();
                WebPage       pg = db.WebPages.SingleOrDefault(x => x.ID == Convert.ToInt32(webPageId));
                if (pg != null)
                {
                    Header h = new Header()
                    {
                        ImagePath = dbImagePath,
                        MoviePath = dbVideoPath
                    };
                    pg.Headers.Add(h);

                    try
                    {
                        db.SubmitChanges();
                        return(RedirectToAction("Index", "Header", new { controller = "Header", action = "Index", webpage = webPageId }));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", ex.Message);
                        ErrorHandler.Report.Exception(ex, "Header/Add");
                        removeFiles = true;
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Web Page does not exist");
                    removeFiles = true;
                }
            }
            else
            {
                ModelState.AddModelError("", "There has been an issue with uploading your Image/Video file. Please try again in few minutes.");
                removeFiles = true;
            }

            if (removeFiles)
            {
                if (System.IO.File.Exists(Url.UploadPath(imageFileName)))
                {
                    System.IO.File.Delete(Url.UploadPath(imageFileName));
                }

                if (System.IO.File.Exists(Url.UploadPath(videoFileName)))
                {
                    System.IO.File.Delete(Url.UploadPath(videoFileName));
                }
            }

            ViewData["Title"]  = "Add Header";
            ViewData["Action"] = "Add";
            return(View("Manage"));
        }
Ejemplo n.º 4
0
        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()));
        }