From 4249a52c856dcb4a33e8a381076542f563e89ac2 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 22 Apr 2019 17:15:06 +0800 Subject: Fix a bug in cos service. Add avatar api. --- Timeline/Controllers/UserController.cs | 8 ++++++++ Timeline/Services/TencentCloudCosService.cs | 5 +++++ Timeline/Services/UserService.cs | 15 ++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'Timeline') diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 552bfb2f..1231befb 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -79,6 +79,14 @@ namespace Timeline.Controllers } } + [HttpGet("user/{username}/avatar"), Authorize] + public async Task GetAvatar([FromRoute] string username) + { + // TODO: test user existence. + var url = await _userService.GetAvatarUrl(username); + return Redirect(url); + } + [HttpPost("userop/changepassword"), Authorize] public async Task ChangePassword([FromBody] ChangePasswordRequest request) { diff --git a/Timeline/Services/TencentCloudCosService.cs b/Timeline/Services/TencentCloudCosService.cs index f1f52ec5..bc812e57 100644 --- a/Timeline/Services/TencentCloudCosService.cs +++ b/Timeline/Services/TencentCloudCosService.cs @@ -66,6 +66,11 @@ namespace Timeline.Services } if (serverException != null) { + if (serverException.statusCode == 404) + { + t.SetResult(false); + return; + } _logger.LogError(serverException, "An server error occured when test cos object existence. Bucket : {} . Key : {} .", bucket, key); t.SetException(serverException); return; diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 75ad3331..a444d434 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -140,6 +140,8 @@ namespace Timeline.Services /// if user does not exist. /// if old password is wrong. Task ChangePassword(string username, string oldPassword, string newPassword); + + Task GetAvatarUrl(string username); } public class UserService : IUserService @@ -148,13 +150,15 @@ namespace Timeline.Services private readonly DatabaseContext _databaseContext; private readonly IJwtService _jwtService; private readonly IPasswordService _passwordService; + private readonly ITencentCloudCosService _cosService; - public UserService(ILogger logger, DatabaseContext databaseContext, IJwtService jwtService, IPasswordService passwordService) + public UserService(ILogger logger, DatabaseContext databaseContext, IJwtService jwtService, IPasswordService passwordService, ITencentCloudCosService cosService) { _logger = logger; _databaseContext = databaseContext; _jwtService = jwtService; _passwordService = passwordService; + _cosService = cosService; } public async Task CreateToken(string username, string password) @@ -294,5 +298,14 @@ namespace Timeline.Services await _databaseContext.SaveChangesAsync(); return ChangePasswordResult.Success; } + + public async Task GetAvatarUrl(string username) + { + var exists = await _cosService.Exists("avatar", username); + if (exists) + return _cosService.GetObjectUrl("avatar", username); + else + return _cosService.GetObjectUrl("avatar", "__default"); + } } } -- cgit v1.2.3