diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-18 19:15:44 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-08-18 19:15:44 +0800 |
commit | 47b26e8b2884a2e4c23dfeffa0ff8b0620aaaaa1 (patch) | |
tree | 0e7d208f1c0b51c25e185b0a0f852830c10c3bd1 /Timeline | |
parent | 36a5a7c81569bbc4fa76b77e9823767d951944b4 (diff) | |
download | timeline-47b26e8b2884a2e4c23dfeffa0ff8b0620aaaaa1.tar.gz timeline-47b26e8b2884a2e4c23dfeffa0ff8b0620aaaaa1.tar.bz2 timeline-47b26e8b2884a2e4c23dfeffa0ff8b0620aaaaa1.zip |
Add avatar controller.
Diffstat (limited to 'Timeline')
-rw-r--r-- | Timeline/Controllers/UserAvatarController.cs | 70 | ||||
-rw-r--r-- | Timeline/Services/UserAvatarService.cs | 10 | ||||
-rw-r--r-- | Timeline/Startup.cs | 2 |
3 files changed, 82 insertions, 0 deletions
diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs new file mode 100644 index 00000000..f61fd54a --- /dev/null +++ b/Timeline/Controllers/UserAvatarController.cs @@ -0,0 +1,70 @@ +using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Logging;
+using System;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+using Timeline.Services;
+
+namespace Timeline.Controllers
+{
+ [ApiController]
+ public class UserAvatarController : Controller
+ {
+ public static class ErrorCodes
+ {
+ public const int Get_UserNotExist = -1001;
+
+ public const int Put_UserNotExist = -2001;
+ }
+
+ private readonly ILogger<UserAvatarController> _logger;
+
+ private readonly IUserAvatarService _service;
+
+ public UserAvatarController(ILogger<UserAvatarController> logger, IUserAvatarService service)
+ {
+ _logger = logger;
+ _service = service;
+ }
+
+ [HttpGet("users/{username}/avatar")]
+ public async Task<IActionResult> Get(string username)
+ {
+ try
+ {
+ var avatar = await _service.GetAvatar(username);
+ return File(avatar.Data, avatar.Type);
+ }
+ catch (UserNotExistException)
+ {
+ _logger.LogInformation($"Attempt to get a avatar of a non-existent user failed. Username: {username} .");
+ return NotFound(new CommonResponse(ErrorCodes.Get_UserNotExist, "User does not exist."));
+ }
+ }
+
+ [HttpPut("users/{username}/avatar")]
+ [Consumes("image/png", "image/jpeg", "image/gif", "image/webp")]
+ public async Task<IActionResult> Put(string username)
+ {
+ try
+ {
+ var data = new byte[Convert.ToInt32(Request.ContentLength)];
+ await Request.Body.ReadAsync(data, 0, data.Length);
+
+ await _service.SetAvatar(username, new Avatar
+ {
+ Data = data,
+ Type = Request.ContentType
+ });
+
+ _logger.LogInformation($"Succeed to put a avatar of a user. Username: {username} . Mime Type: {Request.ContentType} .");
+ return Ok();
+ }
+ catch (UserNotExistException)
+ {
+ _logger.LogInformation($"Attempt to put a avatar of a non-existent user failed. Username: {username} .");
+ return BadRequest(new CommonResponse(ErrorCodes.Put_UserNotExist, "User does not exist."));
+ }
+ }
+ }
+}
diff --git a/Timeline/Services/UserAvatarService.cs b/Timeline/Services/UserAvatarService.cs index 21153575..4f11978c 100644 --- a/Timeline/Services/UserAvatarService.cs +++ b/Timeline/Services/UserAvatarService.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
@@ -178,4 +179,13 @@ namespace Timeline.Services }
}
}
+
+ public static class UserAvatarServiceCollectionExtensions
+ {
+ public static void AddUserAvatarService(this IServiceCollection services)
+ {
+ services.AddScoped<IUserAvatarService, UserAvatarService>();
+ services.AddSingleton<IDefaultUserAvatarProvider, DefaultUserAvatarProvider>();
+ }
+ }
}
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 414bc705..afc06d9b 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -58,6 +58,8 @@ namespace Timeline services.AddTransient<IPasswordService, PasswordService>();
services.AddTransient<IClock, Clock>();
+ services.AddUserAvatarService();
+
var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get<DatabaseConfig>();
services.AddDbContext<DatabaseContext>(options =>
|