Пример #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");
        }
Пример #2
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");
        }