aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers/UserAvatarController.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-19 00:21:38 +0800
committer杨宇千 <crupest@outlook.com>2019-08-19 00:21:38 +0800
commite946c2e546efae29112628c0e6d42dc145605f09 (patch)
tree70ef2814622b2b3e3578b4fe1d3afbf26cacb768 /Timeline/Controllers/UserAvatarController.cs
parente5f5be69f854565d4f58d996cbf4347fa0eae0ff (diff)
downloadtimeline-e946c2e546efae29112628c0e6d42dc145605f09.tar.gz
timeline-e946c2e546efae29112628c0e6d42dc145605f09.tar.bz2
timeline-e946c2e546efae29112628c0e6d42dc145605f09.zip
Improve avatar validation.
Diffstat (limited to 'Timeline/Controllers/UserAvatarController.cs')
-rw-r--r--Timeline/Controllers/UserAvatarController.cs36
1 files changed, 30 insertions, 6 deletions
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<UserAvatarController> _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."));
}
}