From e946c2e546efae29112628c0e6d42dc145605f09 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Mon, 19 Aug 2019 00:21:38 +0800 Subject: Improve avatar validation. --- Timeline/Controllers/UserAvatarController.cs | 36 +++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'Timeline/Controllers/UserAvatarController.cs') diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index 6dc767df..710ca764 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -19,9 +19,28 @@ namespace Timeline.Controllers public const int Put_UserNotExist = -2001; public const int Put_Forbid = -2002; + public const int Put_BadFormat_CantDecode = -2011; + public const int Put_BadFormat_UnmatchedFormat = -2012; + public const int Put_BadFormat_BadSize = -2013; public const int Delete_UserNotExist = -3001; public const int Delete_Forbid = -3002; + + + public static int From(AvatarDataException.ErrorReason error) + { + switch (error) + { + case AvatarDataException.ErrorReason.CantDecode: + return Put_BadFormat_CantDecode; + case AvatarDataException.ErrorReason.UnmatchedFormat: + return Put_BadFormat_UnmatchedFormat; + case AvatarDataException.ErrorReason.BadSize: + return Put_BadFormat_BadSize; + default: + throw new Exception("Unknown AvatarDataException.ErrorReason value."); + } + } } private readonly ILogger _logger; @@ -43,9 +62,9 @@ namespace Timeline.Controllers var avatar = await _service.GetAvatar(username); return File(avatar.Data, avatar.Type); } - catch (UserNotExistException) + catch (UserNotExistException e) { - _logger.LogInformation($"Attempt to get a avatar of a non-existent user failed. Username: {username} ."); + _logger.LogInformation(e, $"Attempt to get a avatar of a non-existent user failed. Username: {username} ."); return NotFound(new CommonResponse(ErrorCodes.Get_UserNotExist, "User does not exist.")); } } @@ -76,11 +95,16 @@ namespace Timeline.Controllers _logger.LogInformation($"Succeed to put a avatar of a user. Username: {username} ; Mime Type: {Request.ContentType} ."); return Ok(); } - catch (UserNotExistException) + catch (UserNotExistException e) { - _logger.LogInformation($"Attempt to put a avatar of a non-existent user failed. Username: {username} ."); + _logger.LogInformation(e, $"Attempt to put a avatar of a non-existent user failed. Username: {username} ."); return BadRequest(new CommonResponse(ErrorCodes.Put_UserNotExist, "User does not exist.")); } + catch (AvatarDataException e) + { + _logger.LogInformation(e, $"Attempt to put a avatar of a bad format failed. Username: {username} ."); + return BadRequest(new CommonResponse(ErrorCodes.From(e.Error), "Bad format.")); + } } [HttpDelete("users/{username}/avatar")] @@ -101,9 +125,9 @@ namespace Timeline.Controllers _logger.LogInformation($"Succeed to delete a avatar of a user. Username: {username} ."); return Ok(); } - catch (UserNotExistException) + catch (UserNotExistException e) { - _logger.LogInformation($"Attempt to delete a avatar of a non-existent user failed. Username: {username} ."); + _logger.LogInformation(e, $"Attempt to delete a avatar of a non-existent user failed. Username: {username} ."); return BadRequest(new CommonResponse(ErrorCodes.Delete_UserNotExist, "User does not exist.")); } } -- cgit v1.2.3