예제 #1
0
        public async Task <ActionResult> Create(create_blog_post_viewModel model, FormCollection f, HttpPostedFileBase Feature_img)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    if (!string.IsNullOrEmpty(f["short_description"]) && !string.IsNullOrEmpty(f["post_content"]))
                    {
                        blog_Post bp = new blog_Post();
                        bp.post_Title        = model.post_Title;
                        bp.short_description = Server.HtmlEncode(sanitizer.Sanitize(f["short_description"]));
                        bp.post_content      = Server.HtmlEncode(sanitizer.Sanitize(f["post_content"], "", null));
                        bp.post_tags         = model.post_tags;
                        bp.post_url          = !string.IsNullOrEmpty(model.post_url) ? seo.generate_title(model.post_url) : seo.generate_title(model.post_Title);
                        bp.meta_description  = Server.HtmlEncode(sanitizer.Sanitize(f["meta_description"]));
                        bp.meta_keyword      = model.meta_keyword;
                        bp.meta_title        = !string.IsNullOrEmpty(model.meta_title) ? model.meta_title : model.post_Title;
                        bp.published_by_Id   = Session["Id"].ToString();
                        if (f["chkVideoUrlAsFeatureImg"] != null)
                        {
                            if (f["chkVideoUrlAsFeatureImg"].ToString() == "on")
                            {
                                if (!string.IsNullOrEmpty(model.video_url))
                                {
                                    bp.video_url = model.video_url;
                                    bp.use_video_as_cover_img = true;
                                }
                                else
                                {
                                    TempData["error"]  = "Supply URL to your video";
                                    ViewBag.videoerror = "Supply Url to your video";
                                    return(View());
                                }
                            }
                            else
                            {
                                bp.use_video_as_cover_img = false;
                            }
                        }
                        else
                        {
                            bp.use_video_as_cover_img = false;
                        }

                        if (Feature_img != null)
                        {
                            var    ImageName    = Path.GetFileName(Feature_img.FileName);
                            string physicalPath = Server.MapPath("~/Content/Blog_post_image/" + ImageName);
                            byte[] img;
                            System.Drawing.Image image2validate = System.Drawing.Image.FromStream(Feature_img.InputStream, true, true);
                            decimal size = Math.Round(Feature_img.ContentLength / (decimal)1024, 2);
                            if (size <= 2000)
                            {
                                img = ImageRz.imageToByteArray(image2validate, image2validate.RawFormat, 1280, 800);
                                bp.Feature_Image = img;
                                System.IO.File.WriteAllBytes(physicalPath, img);
                                Feature_img.SaveAs(physicalPath);
                                bp.Image_url = ImageName;
                            }
                            else
                            {
                                TempData["error"] = "Uploaded Image size exceeded 2000kb(i.e.2MB)!";
                                return(View());
                            }
                        }
                        if (f["chkAccept"] != null)
                        {
                            if (f["chkAccept"].ToString() == "on")
                            {
                                bp.allow_comment = true;
                            }
                            else
                            {
                                bp.allow_comment = false;
                            }
                        }
                        else
                        {
                            bp.allow_comment = false;
                        }
                        bp.publish_date = !string.IsNullOrEmpty(f["publish_date"]) ? Convert.ToDateTime(f["publish_date"]) : DateTime.UtcNow.Date;
                        db.blog_posts.Add(bp);
                        await db.SaveChangesAsync();

                        TempData["success"] = "Article has been published successfully!";
                        return(Redirect(Request.Url.ToString()));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Field Marked with '*' must be filled");
                        return(View());
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Field Marked with '*' must be filled");
                    return(View());
                }
                // TempData["success"] = "Article has been published successfully!";
                // return Redirect(Request.Url.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
                //return View();
            }
        }
예제 #2
0
        public async Task <ActionResult> Edit_Blog_Post(Edit_blog_post_viewModel model, FormCollection f, HttpPostedFileBase Feature_img)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (!string.IsNullOrEmpty(f["short_description"]) && !string.IsNullOrEmpty(f["post_content"]))
                    {
                        blog_Post bp = await db.blog_posts.FindAsync(model.Id);

                        bp.post_Title        = model.post_Title;
                        bp.short_description = Server.HtmlEncode(sanitizer.Sanitize(f["short_description"]));
                        // var content = sanitizer.Sanitize(f["post_content"], "", null);
                        bp.post_content     = Server.HtmlEncode(sanitizer.Sanitize(f["post_content"], "", null));
                        bp.post_tags        = model.post_tags;
                        bp.video_url        = model.video_url;
                        bp.post_url         = !string.IsNullOrEmpty(model.post_url) ? model.post_url : model.post_Title;
                        bp.meta_description = Server.HtmlEncode(sanitizer.Sanitize(f["meta_description"]));
                        bp.meta_keyword     = model.meta_keyword;
                        bp.meta_title       = !string.IsNullOrEmpty(model.meta_title) ? model.meta_title : model.post_Title;
                        // var che = f["chkVideoUrlAsFeatureImg"];
                        if (f["chkVideoUrlAsFeatureImg"] != null)
                        {
                            if (f["chkVideoUrlAsFeatureImg"].ToString() == "on")
                            {
                                if (!string.IsNullOrEmpty(model.video_url))
                                {
                                    bp.video_url = model.video_url;
                                    bp.use_video_as_cover_img = true;
                                }
                                else
                                {
                                    ViewBag.videoerror = "Supply Url to your video";
                                    return(View());
                                }
                            }
                            else
                            {
                                bp.use_video_as_cover_img = false;
                            }
                        }
                        else
                        {
                            bp.use_video_as_cover_img = false;
                        }


                        // (f["chkVideoUrlAsFeatureImg"].ToString() == "false") ? false : true
                        if (Feature_img != null)
                        {
                            var    ImageName    = Path.GetFileName(Feature_img.FileName);
                            string physicalPath = Server.MapPath("~/content/Blog_post_image/" + ImageName);
                            byte[] img;
                            System.Drawing.Image image2validate = System.Drawing.Image.FromStream(Feature_img.InputStream, true, true);
                            decimal size = Math.Round(Feature_img.ContentLength / (decimal)1024, 2);
                            if (size <= 2000)
                            {
                                img = ImageRz.imageToByteArray(image2validate, image2validate.RawFormat, 1280, 800);
                                bp.Feature_Image = img;
                                System.IO.File.WriteAllBytes(physicalPath, img);
                                Feature_img.SaveAs(physicalPath);
                                bp.Image_url = ImageName;
                            }
                            else
                            {
                                TempData["error"] = "Uploaded Image size exceeded 2000kb(i.e.2MB)!";
                                return(View());
                            }
                        }
                        if (f["chkAccept"] != null)
                        {
                            if (f["chkAccept"].ToString() == "on")
                            {
                                bp.allow_comment = true;
                            }
                            else
                            {
                                bp.allow_comment = false;
                            }
                        }
                        else
                        {
                            bp.allow_comment = false;
                        }
                        bp.publish_date = !string.IsNullOrEmpty(f["publish_date"]) ? Convert.ToDateTime(f["publish_date"]) : DateTime.UtcNow.Date;
                        await db.SaveChangesAsync();

                        //Transfer logic back to the blog owner's blog list begins
                        string s = System.Configuration.ConfigurationManager.AppSettings["MaxLevel"];

                        String UserID = iHealth2.CustomClasses.Cryptoclass.EncryptStringAES(bp.published_by_Id, s);
                        UserID = UserID.Replace("/", "$25");
                        UserID = UserID.Replace("+", "$24");
                        //ends
                        TempData["success"] = "Article has been updated successfully!";
                        return(RedirectToAction("my_blog_posts", new { post_by_id = UserID }));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Field Marked with '*' must be filled");
                        return(View());
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Field Marked with '*' must be filled");
                    return(View());
                }
            }
            catch
            {
                return(View());
            }
        }