public void Execute(SetUserProfilePhotoDto request)
        {
            var user = _context.Users.FirstOrDefault(u => !u.IsDeleted && u.Id == request.Id);

            if (user == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(User));
            }

            if (user.Id != _actor.Id && _actor.RoleType != RoleType.Administrator && _actor.RoleType != RoleType.Moderator)
            {
                throw new NotAllowedException(UseCase.getUseCase(this.Id), _actor, "You can only edit your own profile photo.");
            }

            _validator.ValidateAndThrow(request);

            var newFileName = Guid.NewGuid() + Path.GetExtension(request.ProfilePhoto.FileName);

            using (Image image = Image.Load(request.ProfilePhoto.OpenReadStream()))
            {
                image.Mutate(x => x.Resize(150, 150));

                image.Save(Path.Combine("wwwroot", User.PofileImageFolderPath, newFileName));
            }

            user.ProfilePhotoFileName = newFileName;

            _context.SaveChanges(_actor.Id);

            // if up to here all went without errors: delete old photo forms erver if one is set
            if (user.ProfilePhotoFileName != null && File.Exists(user.getProfilePhotoPath()))
            {
                File.Delete(user.getProfilePhotoPath());
            }
        }
Ejemplo n.º 2
0
 public IActionResult SetProfilePhoto(int id, [FromBody] SetUserProfilePhotoDto request, [FromServices] ISetUserProfilePhotoCommand command)
 {
     request.Id = id;
     _executor.ExecuteCommand(command, request);
     return(NoContent());
 }