Exemple #1
0
        public async Task <IActionResult> DeleteRecipeImage(int recipeId, int imageId)
        {
            var updateImageCommand = _recipeCommandFactory.CreateImageCommand();
            var imageDto           = new ImageFileDto(null, null)
            {
                Id       = imageId,
                RecipeId = recipeId
            };
            await updateImageCommand.DeleteAsync(imageDto).ConfigureAwait(false);

            return(await EditRecipe(recipeId).ConfigureAwait(false));
        }
Exemple #2
0
        public async Task <IActionResult> AddRecipeImage(int recipeId, IFormFile image)
        {
            if (image != null)
            {
                var           updateImageCommand = _recipeCommandFactory.CreateImageCommand();
                long          maxFileSizeBytes   = 512000;
                List <string> allowedExtensions  = new List <string> {
                    ".jpg", ".jpeg", ".bmp", ".png", ".gif"
                };

                // Max File Size per Image: 500 KB
                if (image.Length > maxFileSizeBytes)
                {
                    Danger(_localizer["ErrorFileTooBig"], true);
                    return(await EditRecipe(recipeId).ConfigureAwait(false));
                }

                // Allowed Image Extensions: .jpg | .gif | .bmp | .jpeg | .png ONLY
                var ext = Path.GetExtension(image.FileName);
                if (!allowedExtensions.Any(e => e.Equals(ext, StringComparison.OrdinalIgnoreCase)))
                {
                    Danger(_localizer["ErrorFileExtensionWrong"], true);
                    return(await EditRecipe(recipeId).ConfigureAwait(false));
                }

                using MemoryStream ms = new MemoryStream();
                image.OpenReadStream().CopyTo(ms);
                var imageData = await ResizeImage(ms.ToArray(), 360, 480).ConfigureAwait(false);

                var thumbData = await ResizeImage(ms.ToArray(), 100, 150).ConfigureAwait(false);

                var imageDto = new ImageFileDto(thumbData, imageData)
                {
                    RecipeId    = recipeId,
                    FileName    = image.FileName,
                    Name        = image.Name,
                    Length      = image.Length,
                    ContentType = image.ContentType
                };

                await updateImageCommand.AddAsync(imageDto).ConfigureAwait(false);
            }

            return(await EditRecipe(recipeId).ConfigureAwait(false));
        }
Exemple #3
0
        public ImageViewModel CreateImageViewModel(ImageFileDto imageDto)
        {
            if (imageDto == null)
            {
                throw new ArgumentNullException(nameof(imageDto));
            }

            var model = new ImageViewModel(imageDto.Data())
            {
                Id          = imageDto.Id,
                ContentType = imageDto.ContentType,
                FileName    = imageDto.FileName,
                Length      = imageDto.Length,
                Name        = imageDto.Name,
                RecipeId    = imageDto.RecipeId
            };

            return(model);
        }