Exemplo n.º 1
0
        public virtual async Task <IActionResult> UploadImage(IFormFile file)
        {
            if (file.Length == 0)
            {
                return(BadRequest());
            }

            if (!CheckAllowedMaxImageSize(file.Length))
            {
                return(MaxImageSizeFailResult());
            }


            ResizeOptions ro = new ResizeOptions
            {
                Mode = ResizeMode.Max,
                Size = new Size(imagesOptions.MaxWidthPixels, imagesOptions.MaxHeightPixels),
            };

            FileAndDir fileAndDir = await imagesService.SaveImageAsync(file, ro);

            if (fileAndDir == null)
            {
                return(BadRequest());
            }

            return(Ok(new { FileName = fileAndDir.Path }));
        }
Exemplo n.º 2
0
        public virtual async Task <IActionResult> UploadUserPhoto(IFormFile file)
        {
            if (file.Length == 0)
            {
                return(BadRequest());
            }

            if (!CheckAllowedMaxImageSize(file.Length))
            {
                return(MaxImageSizeFailResult());
            }

            ResizeOptions resizeOptionsPhoto = new ResizeOptions
            {
                Position = AnchorPositionMode.Center,
                Mode     = ResizeMode.Crop,
                Size     = new Size(imagesOptions.CurrentValue.PhotoMaxWidthPixels,
                                    imagesOptions.CurrentValue.PhotoMaxWidthPixels)
            };

            FileAndDir fileAndDirPhoto = await imagesService.SaveImageAsync(file, resizeOptionsPhoto);

            if (fileAndDirPhoto == null)
            {
                return(BadRequest());
            }


            ResizeOptions resizeOptionsAvatar = new ResizeOptions
            {
                Position = AnchorPositionMode.Center,
                Mode     = ResizeMode.Crop,
                Size     = new Size(imagesOptions.CurrentValue.AvatarSizePixels,
                                    imagesOptions.CurrentValue.AvatarSizePixels)
            };
            FileAndDir fileAndDirAvatar = await imagesService.SaveImageAsync(file, resizeOptionsAvatar);

            if (fileAndDirAvatar == null)
            {
                return(BadRequest());
            }

            await personalManager.SetPhotoAndAvatarAsync(User.UserId, fileAndDirPhoto.Path, fileAndDirAvatar.Path);

            return(Ok());
        }