From 1bce70b36d4666797ae157167be4a84217a71374 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 5 May 2021 16:36:18 +0800 Subject: refactor: ... --- .../Timeline/Controllers/UserAvatarController.cs | 32 ++++++---------------- 1 file changed, 9 insertions(+), 23 deletions(-) (limited to 'BackEnd/Timeline/Controllers/UserAvatarController.cs') diff --git a/BackEnd/Timeline/Controllers/UserAvatarController.cs b/BackEnd/Timeline/Controllers/UserAvatarController.cs index 376e1f11..5b8c5cdf 100644 --- a/BackEnd/Timeline/Controllers/UserAvatarController.cs +++ b/BackEnd/Timeline/Controllers/UserAvatarController.cs @@ -7,7 +7,6 @@ using Timeline.Helpers.Cache; using Timeline.Models; using Timeline.Models.Http; using Timeline.Models.Validation; -using Timeline.Services.Imaging; using Timeline.Services.User; using Timeline.Services.User.Avatar; @@ -36,7 +35,7 @@ namespace Timeline.Controllers /// If-None-Match header. /// Avatar data. [HttpGet("users/{username}/avatar")] - [Produces("image/png", "image/jpeg", "image/gif", "image/webp", "application/json", "text/json")] + [ProducesImages] [ProducesResponseType(typeof(byte[]), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -54,7 +53,7 @@ namespace Timeline.Controllers /// The avatar data. [HttpPut("users/{username}/avatar")] [Authorize] - [Consumes("image/png", "image/jpeg", "image/gif", "image/webp")] + [ConsumesImages] [MaxContentLength(1000 * 1000 * 10)] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] @@ -62,31 +61,17 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task Put([FromRoute][Username] string username, [FromBody] ByteData body) { - if (!UserHasPermission(UserPermission.UserManagement) && User.Identity!.Name != username) + if (!UserHasPermission(UserPermission.UserManagement) && GetUsername() != username) { return ForbidWithCommonResponse(Resource.MessageForbidNotAdministratorOrOwner); } long id = await _userService.GetUserIdByUsernameAsync(username); - try - { - var digest = await _service.SetAvatarAsync(id, body); - - Response.Headers.Append("ETag", $"\"{digest.ETag}\""); + var digest = await _service.SetAvatarAsync(id, body); - return Ok(); - } - catch (ImageException e) - { - return BadRequest(e.Error switch - { - ImageException.ErrorReason.CantDecode => new CommonResponse(ErrorCodes.Image.CantDecode, Resource.MessageImageDecodeFailed), - ImageException.ErrorReason.UnmatchedFormat => new CommonResponse(ErrorCodes.Image.UnmatchedFormat, Resource.MessageImageFormatUnmatch), - ImageException.ErrorReason.BadSize => new CommonResponse(ErrorCodes.Image.BadSize, Resource.MessageImageBadSize), - _ => new CommonResponse(ErrorCodes.Image.Unknown, Resource.MessageImageUnknownError) - }); - } + Response.Headers.Append("ETag", $"\"{digest.ETag}\""); + return Ok(); } /// @@ -98,11 +83,12 @@ namespace Timeline.Controllers /// You have not logged in. /// You are not administrator. [HttpDelete("users/{username}/avatar")] + [Authorize] + [NotEntityDelete] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] - [Authorize] public async Task Delete([FromRoute][Username] string username) { if (!UserHasPermission(UserPermission.UserManagement) && User.Identity!.Name != username) @@ -113,7 +99,7 @@ namespace Timeline.Controllers long id = await _userService.GetUserIdByUsernameAsync(username); await _service.DeleteAvatarAsync(id); - return Ok(); + return OkWithCommonResponse(); } } } -- cgit v1.2.3