public ActionResult CreateArticle()
        {
            var model = new PreviewContentModel();

            return View(model);
        }
        public ActionResult EditArticle(
            FormCollection fc,
            int? contentId,
            HttpPostedFileBase imageFile,
            HttpPostedFileBase videoFile)
        {
            var isExisting = contentId != null && contentId > 0;
            var model = new PreviewContentModel();
            TryUpdateModel(model);

            if (!HasImage(imageFile, model, contentId) ||
                !ModelState.IsValid ||
                !HasValidTitle(model, contentId) ||
                !IsContentCorrectLength(model) ||
                !IsMetaDescriptionLengthCorrect(model))
            {
                return View("CreateArticle", model);
            }

            Content content;

            if (isExisting)
            {
                content = new Content(Convert.ToInt32(contentId))
                {
                    UpdatedByUserID = Convert.ToInt32(_mu.ProviderUserKey)
                };
            }
            else
            {
                content = new Content
                {
                    CurrentStatus = Convert.ToChar(SiteEnums.ContentStatus.N.ToString()),
                    CreatedByUserID = Convert.ToInt32(this._mu.ProviderUserKey),
                    ReleaseDate = DateTime.UtcNow,
                    ContentKey = FromString.UrlKey(model.PreviewTitle),
                };
            }

            content.PreviewTitle = model.PreviewTitle.Trim();
            content.DetailPreview = model.DetailPreview.Trim();
            content.PreviewLanguage = model.PreviewLanguage.Trim();
            content.PreviewMetaDescription = model.PreviewMetaDescription.Trim();
            content.PreviewMetaKeywords = model.PreviewMetaKeywords.Trim();

            if (imageFile != null && imageFile.InputStream != null)
            {
                // set images
                const CannedAcl acl = CannedAcl.PublicRead;

                var s3 = new S3Service
                {
                    AccessKeyID = AmazonCloudConfigs.AmazonAccessKey,
                    SecretAccessKey = AmazonCloudConfigs.AmazonSecretKey
                };

                var bitmap = new Bitmap(imageFile.InputStream);
                var fileNameFull = Utilities.CreateUniqueContentFilename(imageFile);
                var imgPhoto = ImageResize.FixedSize(bitmap, 600, 400, Color.Black);
                var maker = imgPhoto.ToAStream(ImageFormat.Jpeg);

                s3.AddObject(
                    maker,
                    maker.Length,
                    AmazonCloudConfigs.AmazonBucketName,
                    fileNameFull,
                    imageFile.ContentType,
                    acl);

                if (!string.IsNullOrWhiteSpace(model.PreviewContentPhotoUrl))
                {
                    if (s3.ObjectExists(AmazonCloudConfigs.AmazonBucketName, model.PreviewContentPhotoUrl))
                    {
                        s3.DeleteObject(AmazonCloudConfigs.AmazonBucketName, model.PreviewContentPhotoUrl);
                    }
                }

                content.PreviewContentPhotoURL = fileNameFull;

                var fileNameThumb = Utilities.CreateUniqueContentFilename(imageFile);
                var imgPhotoThumb = ImageResize.FixedSize(bitmap, 350, 250, Color.Black);

                maker = imgPhotoThumb.ToAStream(ImageFormat.Jpeg);

                s3.AddObject(
                    maker,
                    maker.Length,
                    AmazonCloudConfigs.AmazonBucketName,
                    fileNameThumb,
                    imageFile.ContentType,
                    acl);

                if (!string.IsNullOrWhiteSpace(model.PreviewContentPhotoThumbUrl))
                {
                    if (s3.ObjectExists(AmazonCloudConfigs.AmazonBucketName, model.PreviewContentPhotoThumbUrl))
                    {
                        s3.DeleteObject(AmazonCloudConfigs.AmazonBucketName, model.PreviewContentPhotoThumbUrl);
                    }
                }

                content.PreviewContentPhotoThumbURL = fileNameThumb;
            }

            content.Set();

            return RedirectToAction("Preview", new {key= content.ContentKey });
        }
        private bool IsMetaDescriptionLengthCorrect(PreviewContentModel model)
        {
            var length = model.PreviewMetaDescription.Split(' ').Length;

            if (length > MinimumMetaDescWordCount && length < MaxMetaDescWordCount) return true;

            ModelState.AddModelError(
                "PreviewMetaDescription",
                string.Format("Meta Description must be between {0} and {1} words",
                    MinimumMetaDescWordCount,
                    MaxMetaDescWordCount));

            return false;
        }
        private bool IsContentCorrectLength(PreviewContentModel model)
        {
            var length = model.DetailPreview.Replace("&nbsp;", " ").Split(' ').Length;

            if ((length >= MinimumWordCount)) return true;

            ModelState.AddModelError(
                "DetailPreview",
                string.Format("Article too short, included at least {0} words", MinimumWordCount));

            return false;
        }
        private bool HasValidTitle(PreviewContentModel model, int? contentId)
        {
            var content = new Content();
            content.GetContentByKey(FromString.UrlKey(model.PreviewTitle));

            if (content.ContentID == 0 ||
                (contentId != null && Convert.ToInt32(contentId) == content.ContentID))
                    return true;

            // already exists
            ModelState.AddModelError("PreviewTitle", "An article with this title already exists");
            return false;
        }
        private bool HasImage(HttpPostedFileBase imageFile, PreviewContentModel model, int? contentId)
        {
            if ((contentId != null && contentId > 0) ||
               imageFile != null ||
               !string.IsNullOrWhiteSpace(model.PreviewContentPhotoUrl))
                    return true;

            ModelState.AddModelError("PreviewContentPhotoThumbUrl", Messages.Required + @": " + Messages.Photo);
            return false;
        }
        private PreviewContentModel ContentToPreviewContentModel(Content databaseModel)
        {
            var content = new PreviewContentModel
            {
                PreviewTitle = databaseModel.PreviewTitle,
                DetailPreview = databaseModel.DetailPreview,
                PreviewLanguage = databaseModel.PreviewLanguage,
                PreviewMetaDescription = databaseModel.PreviewMetaDescription,
                PreviewMetaKeywords = databaseModel.PreviewMetaKeywords,
                PreviewContentPhotoUrl = databaseModel.PreviewContentPhotoURL,
                PreviewContentPhotoThumbUrl = databaseModel.PreviewContentPhotoThumbURL,
                ContentID = databaseModel.ContentID
            };

            return content;
        }