public ActionResult Add(TestimonialModel testimonialModel) { // create a bucket if not exists string createBucket = AwsS3Bucket.CreateABucket(); if (ModelState.ContainsKey("ImageType")) { ModelState["ImageType"].Errors.Clear(); } var imageTypes = new string[] { "image/png", "image/jpeg", "image/pjpeg" }; int _maxLength = MaxLength * 1024 * 1024; if (testimonialModel.ImageType == null || testimonialModel.ImageType.ContentLength == 0) { ModelState.AddModelError("ImageType", "Image is required"); } else if (!imageTypes.Contains(testimonialModel.ImageType.ContentType)) { ModelState.AddModelError("ImageType", "Please choose either a JPG or PNG image."); } else if (testimonialModel.ImageType.ContentLength > _maxLength) { ModelState.AddModelError("ImageType", "Maximum allowed file size is " + MaxLength + " MB."); } //else if (testimonialModel.ImageType != null && imageTypes.Contains(testimonialModel.ImageType.ContentType) && testimonialModel.ImageType.ContentLength <= _maxLength) //{ // System.Drawing.Image img = System.Drawing.Image.FromStream(testimonialModel.ImageType.InputStream); // int height = img.Height; // int width = img.Width; // if (width > MaxImageWidth || height > MaxImageHeight) // { // ModelState.AddModelError("ImageType", "Maximum allowed file dimension is " + MaxImageWidth + "*" + MaxImageHeight); // } //} if (ModelState.IsValid) { TestimonialHelper _Helper = new TestimonialHelper(); if (testimonialModel.ImageType != null) { string ext = System.IO.Path.GetExtension(testimonialModel.ImageType.FileName); testimonialModel.ActualImageFile = testimonialModel.ImageType.FileName.ToString(); testimonialModel.ImageFile = Guid.NewGuid() + ext; int count = _Helper.AddUpdate(testimonialModel); if (count == 0) { string initialPath = "resources/testimonial"; // save the file locally string path = Server.MapPath("~/Content/Resources/Testimonial/" + testimonialModel.ImageFile); testimonialModel.ImageType.SaveAs(path); // save the file on s3 int fileMapped = AwsS3Bucket.CreateFile(initialPath + "/" + testimonialModel.ImageFile, path); // delete the file locally if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } TempData["CommonMessage"] = AppLogic.setMessage(count, "Record added successfully."); return(RedirectToAction("Index")); } else if (count == 1) { TempData["CommonMessage"] = AppLogic.setMessage(count, "Record already exists."); ViewBag.StatusList = AppLogic.BindDDStatus(1); return(View(testimonialModel)); } else { ViewBag.StatusList = AppLogic.BindDDStatus(1); TempData["CommonMessage"] = AppLogic.setMessage(count, "Error, Please try again."); return(View(testimonialModel)); } } } ViewBag.StatusList = AppLogic.BindDDStatus(1); return(View(testimonialModel)); }
public ActionResult Edit(TestimonialModel testimonialModel) { var imageTypes = new string[] { "image/png", "image/jpeg", "image/pjpeg" }; int _maxLength = MaxLength * 1024 * 1024; ModelState.Remove("ImageType"); if (testimonialModel.ImageType != null) { if (!imageTypes.Contains(testimonialModel.ImageType.ContentType)) { ModelState.AddModelError("ImageType", "Please choose either a JPG or PNG image."); } else if (testimonialModel.ImageType.ContentLength > _maxLength) { ModelState.AddModelError("ImageType", "Maximum allowed file size is " + MaxLength + " MB."); } //else if (imageTypes.Contains(testimonialModel.ImageType.ContentType) && testimonialModel.ImageType.ContentLength <= _maxLength) //{ // System.Drawing.Image img = System.Drawing.Image.FromStream(testimonialModel.ImageType.InputStream); // int height = img.Height; // int width = img.Width; // if (width > MaxImageWidth || height > MaxImageHeight) // { // ModelState.AddModelError("ImageType", "Maximum allowed file dimension is " + MaxImageWidth + "*" + MaxImageHeight); // } //} } if (ModelState.IsValid) { TestimonialHelper _helper = new TestimonialHelper(); int count = -1; if (testimonialModel.ImageType != null) { testimonialModel.ActualImageFile = testimonialModel.ImageType.FileName.ToString(); string ext = System.IO.Path.GetExtension(testimonialModel.ImageType.FileName); testimonialModel.ImageFile = Guid.NewGuid() + ext; string path = Server.MapPath("~/Content/Resources/Testimonial/" + testimonialModel.ImageFile); count = _helper.AddUpdate(testimonialModel); if (count == 0) { // save the file locally testimonialModel.ImageType.SaveAs(path); // save the file on s3 int fileMapped = AwsS3Bucket.CreateFile("resources/testimonial/" + testimonialModel.ImageFile, path); // delete the file locally if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } string filePath = Server.MapPath("~/Content/Resources/Testimonial") + "/" + testimonialModel.OldImageFile; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } // delete the old file from s3 AwsS3Bucket.DeleteObject("resources/testimonial/" + testimonialModel.OldImageFile); TempData["CommonMessage"] = AppLogic.setMessage(count, "Record updated successfully."); return(RedirectToAction("Index")); } } else { count = _helper.AddUpdate(testimonialModel); } if (count == 0) { TempData["CommonMessage"] = AppLogic.setMessage(count, "Record updated successfully."); return(RedirectToAction("Index")); } else if (count == 1) { TempData["CommonMessage"] = AppLogic.setMessage(count, "Record already exists."); ViewBag.StatusList = AppLogic.BindDDStatus(Convert.ToInt32(testimonialModel.Status)); return(View(testimonialModel)); } else { TempData["CommonMessage"] = AppLogic.setMessage(count, "Error: Please try again."); ViewBag.StatusList = AppLogic.BindDDStatus(Convert.ToInt32(testimonialModel.Status)); return(View(testimonialModel)); } } ViewBag.StatusList = AppLogic.BindDDStatus(Convert.ToInt32(testimonialModel.Status)); return(View(testimonialModel)); }