예제 #1
0
        public EmailAPIResultModel Post([FromBody] EmailAPIModel emailAPIModel)
        {
            int    mediaId     = -1;
            string errorString = "";

            try
            {
                if (emailAPIModel == null)
                {
                    errorString = T("The provided data does not correspond to the required format.").ToString();
                }
                else
                {
                    ContactFormRecord contactForm = _contactFormService.GetContactForm(emailAPIModel.ContentId);

                    if (contactForm == null)
                    {
                        errorString = T("The content Id has not been provided or does not correspond to a content part of the correct type.").ToString();
                    }

                    if (errorString == "")
                    {
                        errorString = _contactFormService.ValidateAPIRequest(emailAPIModel.SenderName, emailAPIModel.SendFrom, emailAPIModel.MessageText, contactForm.RequireNameField, (emailAPIModel.Attachment.Length > 0), contactForm.RequireAttachment);
                    }

                    if (errorString == "" && !string.IsNullOrWhiteSpace(emailAPIModel.Attachment) && !string.IsNullOrWhiteSpace(emailAPIModel.AttachmentName))
                    {
                        if (_contactFormService.FileAllowed(emailAPIModel.AttachmentName))
                        {
                            mediaId = _contactFormService.UploadFromBase64(emailAPIModel.Attachment, contactForm.PathUpload, emailAPIModel.AttachmentName);
                        }
                        else
                        {
                            errorString = T("The file extension in the filename is not allowed or has not been provided.").ToString();
                        }
                    }

                    if (errorString == "")
                    {
                        _contactFormService.SendContactEmail(emailAPIModel.SenderName, emailAPIModel.SendFrom, emailAPIModel.SendFrom, emailAPIModel.MessageSubject, emailAPIModel.MessageText, mediaId, contactForm, emailAPIModel.AdditionalData);
                    }
                }
            }
            catch (Exception e)
            {
                errorString = e.Message;
            }

            return(new EmailAPIResultModel {
                Error = errorString, Information = ""
            });
        }
        public ActionResult Add(int ContactFormID)
        {
            var model = new FileUploadModel
            {
                ParentID  = ContactFormID,
                MediaData = null
            };

            if (Request != null)
            {
                if (Request.Files.Count > 0)
                {
                    if (string.IsNullOrWhiteSpace(Path.GetFileName(Request.Files[0].FileName)))
                    {
                        _notifier.Error(T("Please select a file."));
                    }
                    else
                    {
                        ContactFormRecord contactForm = _contactFormService.GetContactForm(ContactFormID);

                        var folderPath = contactForm.PathUpload;
                        for (int i = 0; i < Request.Files.Count; i++)
                        {
                            var file     = Request.Files[i];
                            var filename = Path.GetFileName(file.FileName);

                            if (_contactFormService.FileAllowed(filename))
                            {
                                var mediaPart = _mediaLibraryService.ImportMedia(file.InputStream, folderPath, filename);
                                _contentManager.Create(mediaPart);
                                var fullPart = _contentManager.Get(mediaPart.Id).As <MediaPart>();
                                model.MediaData = fullPart;
                            }
                            else
                            {
                                _notifier.Error(T("The file extension in the filename is not allowed or has not been provided."));
                            }
                        }
                    }
                }
            }

            return(View(model));
        }