Ejemplo n.º 1
0
        public void Test_UploadedDataViewModel_FormValidation_Email_Regex_Should_Not_Pass()
        {
            CheckFieldsValidation cfv = new CheckFieldsValidation();
            UploadedDataViewModel fvm = new UploadedDataViewModel()
            {
                Email = "R0lGODlhAQABAAD/[email protected]"
            };

            var errorcount = cfv.Validate(fvm).Count();

            Assert.Equal(0, errorcount);
        }
Ejemplo n.º 2
0
        public void Test_UploadedDataViewModel_FormValidation_Name_MaxLength_Should_Not_Pass()
        {
            CheckFieldsValidation cfv = new CheckFieldsValidation();
            UploadedDataViewModel fvm = new UploadedDataViewModel()
            {
                Name = "R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=Handel Batista"
            };

            var errorcount = cfv.Validate(fvm).Count();

            Assert.Equal(0, errorcount);
        }
Ejemplo n.º 3
0
        public void Test_UploadedDataViewModel_FormValidation_required_fields_Empty_Should_Not_Pass()
        {
            CheckFieldsValidation cfv = new CheckFieldsValidation();
            UploadedDataViewModel fvm = new UploadedDataViewModel()
            {
                Name   = "",
                Email  = "",
                Base64 = ""
            };

            var errorcount = cfv.Validate(fvm).Count();

            Assert.Equal(0, errorcount);
        }
Ejemplo n.º 4
0
        public void Test_UploadedDataViewModel_FormValidation_required_fields_Should_Pass()
        {
            CheckFieldsValidation cfv = new CheckFieldsValidation();
            UploadedDataViewModel fvm = new UploadedDataViewModel()
            {
                Name   = "Handel Batista",
                Email  = "*****@*****.**",
                Base64 = "R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
            };

            var errorcount = cfv.Validate(fvm).Count();

            Assert.Equal(0, errorcount);
        }
Ejemplo n.º 5
0
        public IActionResult Index(UploadedDataViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = null;
                string filePath       = null;

                // If the Photo property on the incoming model object is not null, then the user
                // has selected an image to upload.
                if (model.Base64 != null)
                {
                    try
                    {
                        // The image must be uploaded to the images folder in wwwroot
                        // To get the path of the wwwroot folder we are using the inject
                        // HostingEnvironment service provided by ASP.NET Core
                        string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images\\" + model.Name);

                        //if the the directory doesn't exist
                        Directory.CreateDirectory(uploadsFolder);

                        // To make sure the file name is unique we are appending a new
                        // GUID value and and an underscore to the file name
                        uniqueFileName = Guid.NewGuid().ToString() + "_PostCardImage.png";
                        filePath       = Path.Combine(uploadsFolder, uniqueFileName);

                        byte[] imgBytes = Convert.FromBase64String(model.Base64);
                        using (var imageFile = new FileStream(filePath, FileMode.Create))
                        {
                            imageFile.Write(imgBytes, 0, imgBytes.Length);
                            imageFile.Flush();
                        }

                        //Saves the provided information on the database.
                        //We store the filepath to delete the image from the server
                        //at the user's request.
                        UploadedData newDataEntry = new UploadedData
                        {
                            Name      = model.Name,
                            Email     = model.Email,
                            SentDate  = DateTime.Now,
                            ImageName = uniqueFileName,
                            ImagePath = filePath
                        };
                        _uploadedDataRespository.UploadData(newDataEntry);


                        //Sends the email with the select image as an attachement if no errors occur above
                        var          fromAddress  = new MailAddress("*****@*****.**");
                        var          toAddress    = new MailAddress(model.Email);
                        const string fromPassword = "******";
                        string       subject      = "";
                        if (model.Subject != "")
                        {
                            subject = model.Subject;
                        }
                        else
                        {
                            subject = "Post Card - Image(s)";
                        }
                        string body = model.EmailBody;

                        var smtp = new SmtpClient
                        {
                            Host                  = "smtp.gmail.com",
                            Port                  = 587,
                            EnableSsl             = true,
                            DeliveryMethod        = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials           = new NetworkCredential(fromAddress.Address, fromPassword)
                        };
                        Attachment attachment = new Attachment(filePath);
                        using (MailMessage message = new MailMessage(fromAddress, toAddress)
                        {
                            Subject = subject,
                            Body = body
                        })
                        {
                            message.Attachments.Add(attachment);
                            smtp.Send(message);
                        }

                        ViewBag.CssClassName  = "text-success";
                        ViewBag.ServerMessage = "Image successfully sent.";
                        return(View("Index"));
                    }
                    catch (Exception ex)
                    {
                        //deletes the uploaded file if an error occurs
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        ViewBag.TextClass = "text-danger";
                        ViewBag.Message   = ex.Message;
                    }
                }
                else
                {
                    ViewBag.TextClass = "text-danger";
                    ViewBag.Message   = "Please select an image.";
                }
                return(View());
            }
            else
            {
                ViewBag.TextClass = "text-danger";
                ViewBag.Message   = "The form validation failed.";
            }

            return(View());
        }