public async Task <IActionResult> CreatePost(MediaProfileViewModel model, string submit)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageMediaProfiles))
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(model.Name))
                {
                    ModelState.AddModelError(nameof(MediaProfileViewModel.Name), S["The name is mandatory."]);
                }
                else
                {
                    var mediaProfilesDocument = await _mediaProfilesManager.GetMediaProfilesDocumentAsync();

                    if (mediaProfilesDocument.MediaProfiles.ContainsKey(model.Name))
                    {
                        ModelState.AddModelError(nameof(MediaProfileViewModel.Name), S["A profile with the same name already exists."]);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var isCustomWidth  = model.SelectedWidth != 0 && Array.BinarySearch <int>(_mediaOptions.SupportedSizes, model.SelectedWidth) < 0;
                var isCustomHeight = model.SelectedHeight != 0 && Array.BinarySearch <int>(_mediaOptions.SupportedSizes, model.SelectedHeight) < 0;

                var mediaProfile = new MediaProfile
                {
                    Hint            = model.Hint,
                    Width           = isCustomWidth ? model.CustomWidth : model.SelectedWidth,
                    Height          = isCustomHeight ? model.CustomHeight : model.SelectedHeight,
                    Mode            = model.SelectedMode,
                    Format          = model.SelectedFormat,
                    Quality         = model.Quality,
                    BackgroundColor = model.BackgroundColor
                };

                await _mediaProfilesManager.UpdateMediaProfileAsync(model.Name, mediaProfile);

                if (submit == "SaveAndContinue")
                {
                    return(RedirectToAction(nameof(Edit), new { name = model.Name }));
                }
                else
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }

            // If we got this far, something failed, redisplay form
            BuildViewModel(model);

            return(View(model));
        }
        public async Task <IActionResult> Create()
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageMediaProfiles))
            {
                return(Forbid());
            }

            var model = new MediaProfileViewModel();

            BuildViewModel(model);

            return(View(model));
        }
        public async Task <IActionResult> Edit(string name)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageMediaProfiles))
            {
                return(Forbid());
            }

            var mediaProfilesDocument = await _mediaProfilesManager.GetMediaProfilesDocumentAsync();

            if (!mediaProfilesDocument.MediaProfiles.ContainsKey(name))
            {
                return(RedirectToAction("Create", new { name }));
            }

            var mediaProfile = mediaProfilesDocument.MediaProfiles[name];
            // Is a custom width if the width is not 0 and it is not in the array of supported sizes.
            var isCustomWidth  = mediaProfile.Width != 0 && Array.BinarySearch <int>(_mediaOptions.SupportedSizes, mediaProfile.Width) < 0;
            var isCustomHeight = mediaProfile.Height != 0 && Array.BinarySearch <int>(_mediaOptions.SupportedSizes, mediaProfile.Height) < 0;

            var model = new MediaProfileViewModel
            {
                Name            = name,
                Hint            = mediaProfile.Hint,
                SelectedWidth   = isCustomWidth ? -1 : mediaProfile.Width,
                CustomWidth     = isCustomWidth ? mediaProfile.Width : 0,
                SelectedHeight  = isCustomHeight ? -1 : mediaProfile.Height,
                CustomHeight    = isCustomHeight ? mediaProfile.Height : 0,
                SelectedMode    = mediaProfile.Mode,
                SelectedFormat  = mediaProfile.Format,
                Quality         = mediaProfile.Quality,
                BackgroundColor = mediaProfile.BackgroundColor
            };

            BuildViewModel(model);

            return(View(model));
        }
        private void BuildViewModel(MediaProfileViewModel model)
        {
            model.AvailableWidths.Add(new SelectListItem()
            {
                Text = S["Default"], Value = "0"
            });
            model.AvailableHeights.Add(new SelectListItem()
            {
                Text = S["Default"], Value = "0"
            });
            model.AvailableWidths.AddRange(_mediaOptions.SupportedSizes.Select(x => new SelectListItem()
            {
                Text = x.ToString(), Value = x.ToString()
            }));
            model.AvailableHeights.AddRange(_mediaOptions.SupportedSizes.Select(x => new SelectListItem()
            {
                Text = x.ToString(), Value = x.ToString()
            }));
            if (_mediaOptions.UseTokenizedQueryString)
            {
                model.AvailableWidths.Add(new SelectListItem()
                {
                    Text = S["Custom Size"], Value = "-1"
                });
                model.AvailableHeights.Add(new SelectListItem()
                {
                    Text = S["Custom Size"], Value = "-1"
                });
            }

            model.AvailableResizeModes.Add(new SelectListItem()
            {
                Text = S["Default (Max)"], Value = ((int)ResizeMode.Undefined).ToString()
            });
            model.AvailableResizeModes.Add(new SelectListItem()
            {
                Text = S["Max"], Value = ((int)ResizeMode.Max).ToString()
            });
            model.AvailableResizeModes.Add(new SelectListItem()
            {
                Text = S["Crop"], Value = ((int)ResizeMode.Crop).ToString()
            });
            model.AvailableResizeModes.Add(new SelectListItem()
            {
                Text = S["Pad"], Value = ((int)ResizeMode.Pad).ToString()
            });
            model.AvailableResizeModes.Add(new SelectListItem()
            {
                Text = S["BoxPad"], Value = ((int)ResizeMode.BoxPad).ToString()
            });
            model.AvailableResizeModes.Add(new SelectListItem()
            {
                Text = S["Min"], Value = ((int)ResizeMode.Min).ToString()
            });
            model.AvailableResizeModes.Add(new SelectListItem()
            {
                Text = S["Stretch"], Value = ((int)ResizeMode.Stretch).ToString()
            });


            model.AvailableFormats.Add(new SelectListItem()
            {
                Text = S["Default"], Value = ((int)Format.Undefined).ToString()
            });
            model.AvailableFormats.Add(new SelectListItem()
            {
                Text = S["Bmp"], Value = ((int)Format.Bmp).ToString()
            });
            model.AvailableFormats.Add(new SelectListItem()
            {
                Text = S["Gif"], Value = ((int)Format.Gif).ToString()
            });
            model.AvailableFormats.Add(new SelectListItem()
            {
                Text = S["Jpg"], Value = ((int)Format.Jpg).ToString()
            });
            model.AvailableFormats.Add(new SelectListItem()
            {
                Text = S["Png"], Value = ((int)Format.Png).ToString()
            });
            model.AvailableFormats.Add(new SelectListItem()
            {
                Text = S["Tga"], Value = ((int)Format.Tga).ToString()
            });
        }