Exemple #1
0
        public ActionResult Edit(VendorModel _model)
        {
            try
            {
                // create a bucket if not exists
                string createBucket = AwsS3Bucket.CreateABucket();


                if (ModelState.ContainsKey("ImageType"))
                {
                    ModelState["ImageType"].Errors.Clear();
                }
                var imageTypes = new string[] {
                    "image/gif",
                    "image/jpeg",
                    "image/pjpeg",
                    "image/png"
                };

                int _maxLength = MaxLength * 1024 * 1024;

                if (_model.ImageType != null)
                {
                    if (!imageTypes.Contains(_model.ImageType.ContentType))
                    {
                        ModelState.AddModelError("ImageType", "Please choose either a GIF, JPG or PNG image.");
                    }
                    else if (_model.ImageType.ContentLength > _maxLength)
                    {
                        ModelState.AddModelError("ImageType", "Maximum allowed file size is " + MaxLength + " MB.");
                    }
                    //else if (imageTypes.Contains(_model.ImageType.ContentType) && _model.ImageType.ContentLength <= _maxLength)
                    //{
                    //    System.Drawing.Image img = System.Drawing.Image.FromStream(_model.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.ContainsKey("TradeAndBusinessFileType"))
                {
                    ModelState["TradeAndBusinessFileType"].Errors.Clear();
                }
                int _maxFileLength = MaxFileLength * 1024 * 1024;
                if (_model.TradeAndBusinessFileType != null)
                {
                    string        AlllowedExtension = ".pdf";
                    List <string> extList           = AlllowedExtension.Split(',').ToList <string>();
                    if (!extList.Contains(System.IO.Path.GetExtension(_model.TradeAndBusinessFileType.FileName)))
                    {
                        ModelState.AddModelError("TradeAndBusinessFileType", "Please choose only " + AlllowedExtension + " file.");
                    }
                    else if (_model.TradeAndBusinessFileType.ContentLength > _maxFileLength)
                    {
                        ModelState.AddModelError("TradeAndBusinessFileType", "Maximum allowed file size is " + MaxFileLength + " MB.");
                    }
                }


                if (ModelState.IsValid)
                {
                    VendorHelper _Helper        = new VendorHelper();
                    long         _vendorId      = 0;
                    int?         _adminApproval = 0;
                    string       uniqueId       = Guid.NewGuid().ToString();
                    string       ext            = string.Empty;
                    string       extFile        = string.Empty;

                    if (_model.TradeAndBusinessFileType != null)
                    {
                        extFile = System.IO.Path.GetExtension(_model.TradeAndBusinessFileType.FileName);
                        _model.TradeAndBusinessFile       = uniqueId + extFile;
                        _model.ActualTradeAndBusinessFile = _model.TradeAndBusinessFileType.FileName;
                    }
                    if (_model.ImageType != null)
                    {
                        ext = System.IO.Path.GetExtension(_model.ImageType.FileName);
                        _model.ImageFile       = uniqueId + "_img" + ext;
                        _model.ActualImageFile = _model.ImageType.FileName;
                    }

                    _adminApproval = _model.AdminApprovalPrev;
                    _vendorId      = _Helper.AddUpdate(_model);

                    if (_vendorId > 0)
                    {
                        string initialPath = "resources/vendor/" + _vendorId;

                        // Add/Delete the new trade and business file and image details
                        string path        = string.Empty;
                        string oldfilePath = string.Empty;

                        int fileMapped = -1;
                        if (_model.ImageType != null)
                        {
                            // save the file locally
                            if (!Directory.Exists(Server.MapPath("~/Content/" + initialPath + "/ProfilePicture/")))
                            {
                                Directory.CreateDirectory(Server.MapPath("~/Content/" + initialPath + "/ProfilePicture/"));
                            }
                            // save the file locally
                            path = Server.MapPath(Path.Combine("~/Content/" + initialPath + "/ProfilePicture/" + _model.ImageFile));
                            _model.ImageType.SaveAs(path);

                            // save the file on s3
                            fileMapped = AwsS3Bucket.CreateFile(initialPath + "/ProfilePicture/" + _model.ImageFile, path);

                            // delete the file locally
                            if (System.IO.File.Exists(path))
                            {
                                System.IO.File.Delete(path);
                            }

                            /*******************************************************************************/

                            // delete the old file locally
                            oldfilePath = Server.MapPath("~/Content/Resources/Vendor/" + _vendorId + "/ProfilePicture/" + _model.OldImageFile);
                            if (System.IO.File.Exists(oldfilePath))
                            {
                                System.IO.File.Delete(oldfilePath);
                            }
                            // delete the old file from s3
                            AwsS3Bucket.DeleteObject(initialPath + "/ProfilePicture/" + _model.OldImageFile);

                            /*******************************************************************************/
                        }
                        if (_model.TradeAndBusinessFileType != null)
                        {
                            // save the file locally
                            if (!Directory.Exists(Server.MapPath("~/Content/" + initialPath + "/TradeFile/")))
                            {
                                Directory.CreateDirectory(Server.MapPath("~/Content/" + initialPath + "/TradeFile/"));
                            }
                            path = Server.MapPath("~/Content/Resources/Vendor/" + _vendorId + "/TradeFile/" + _model.TradeAndBusinessFile);
                            _model.TradeAndBusinessFileType.SaveAs(path);

                            // save the file on s3
                            fileMapped = AwsS3Bucket.CreateFile(initialPath + "/TradeFile/" + _model.TradeAndBusinessFile, path);

                            // delete the file locally
                            if (System.IO.File.Exists(path))
                            {
                                System.IO.File.Delete(path);
                            }



                            // delete the old file locally
                            oldfilePath = Server.MapPath("~/Content/Resources/Vendor/" + _vendorId + "/TradeFile/" + _model.OldTradeAndBusinessFile);
                            if (System.IO.File.Exists(oldfilePath))
                            {
                                System.IO.File.Delete(oldfilePath);
                            }
                            // delete the old file from s3
                            AwsS3Bucket.DeleteObject(initialPath + "/TradeFile/" + _model.OldTradeAndBusinessFile);
                        }

                        if (_model.AdminApproval == 2 && _adminApproval != 2)
                        {
                            SendVendorRejectedMail(_vendorId);
                        }

                        if (_model.AdminApproval == 1 && _adminApproval != 1)
                        {
                            SendVendorApprovedMail(_vendorId);
                        }
                        TempData["CommonMessage"] = AppLogic.setMessage(0, "Record updated successfully.");
                        return(RedirectToAction("Index"));
                    }
                    else if (_vendorId == -1)
                    {
                        TempData["CommonMessage"] = AppLogic.setMessage(1, "Record already exists.");
                    }
                    else
                    {
                        TempData["CommonMessage"] = AppLogic.setMessage(2, "Error, Please try again.");
                    }
                }
            }
            catch (Exception ex)
            {
            }
            ViewBag.DisciplineList = CommonData.GetDisciplineWithoutOtherList();
            ViewBag.StatusList     = AppLogic.BindDDStatus(Convert.ToInt32(_model.Status));
            return(View(_model));
        }
        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));
        }