Academatica.Api/Academatica.Api.Users/Controllers/UsersController.cs
/// <summary>
/// Endpoint used to set a user profile image.
/// </summary>
/// <param name="id">User ID.</param>
/// <param name="picture">New user profile picture.</param>
Endpoint used to set a user profile image.
Endpoint used to set a user profile image .
[HttpPatch]
        [Route("{id}/image")]
        public async Task<IActionResult> SetUserProfileImage(Guid id, [FromForm] IFormFile picture)
        {
            var userId = User.FindFirst("sub")?.Value;
            if (userId != id.ToString())
            {
                return Forbid("Access denied - token subject invalid.");
            }
            var user = _academaticaDbContext.Users.Where(x => x.Id == id).FirstOrDefault();
            if (user == null)
            {
                return BadRequest("Invalid user ID.");
            }
            long length = picture.Length;
            if (length < 0)
            {
                return BadRequest("Invalid file format.");
            }
            using var fileStream = picture.OpenReadStream();
            byte[] bytes = new byte[length];
            fileStream.Read(bytes, 0, (int)picture.Length);
            S3PutResponse response = await _yandexStorageService.PutObjectAsync(bytes, $"Users/{id}/pic.jpeg");
            if (response.IsSuccessStatusCode)
            {
                user.ProfilePicUrl = response.Result;
                await _academaticaDbContext.SaveChangesAsync();
                return Ok();
            } else
            {
                return BadRequest("Could not upload the file.");
            }
        }
[ HttpPatch ] [ Route ( " " ) ] public async Task < IActionResult > SetUserProfileImage ( Guid id , [ FromForm ] IFormFile picture ) { var userId = User . FindFirst ( " " ) ? . Value ; if ( userId != id . ToString ( ) ) { return Forbid ( " " ) ; } var user = _academaticaDbContext . Users . Where ( x => x . Id == id ) . FirstOrDefault ( ) ; if ( user == null ) { return BadRequest ( " " ) ; } long length = picture . Length ; if ( length < 0 ) { return BadRequest ( " " ) ; } using var fileStream = picture . OpenReadStream ( ) ; byte [ ] bytes = new byte [ length ] ; fileStream . Read ( bytes , 0 , ( int ) picture . Length ) ; S3PutResponse response = await _yandexStorageService . PutObjectAsync ( bytes , $" Users/ { id } /pic.jpeg " ) ; if ( response . IsSuccessStatusCode ) { user . ProfilePicUrl = response . Result ; await _academaticaDbContext . SaveChangesAsync ( ) ; return Ok ( ) ; } else { return BadRequest ( " " ) ; } }
Endpoint used to set a user profile image.
Endpoint used to set a user profile image .
id
Guid
None
IFormFile
[FromForm]
picture
New user profile picture.
New user profile picture .