aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers/UserController.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-30 19:49:27 +0800
committercrupest <crupest@outlook.com>2019-04-30 19:49:27 +0800
commit484f59f9c954fdced635c24c5ab49840c3022d93 (patch)
treef695f1c3d7eb33554784c77dca4afde7f4227503 /Timeline/Controllers/UserController.cs
parente48e5f8d6e0b675493bceb8e26a957da050d282d (diff)
downloadtimeline-484f59f9c954fdced635c24c5ab49840c3022d93.tar.gz
timeline-484f59f9c954fdced635c24c5ab49840c3022d93.tar.bz2
timeline-484f59f9c954fdced635c24c5ab49840c3022d93.zip
Add avatar upload function.
Diffstat (limited to 'Timeline/Controllers/UserController.cs')
-rw-r--r--Timeline/Controllers/UserController.cs34
1 files changed, 30 insertions, 4 deletions
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index eaa205de..a18e36e9 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
+using System.IO;
using System.Threading.Tasks;
using Timeline.Entities;
using Timeline.Entities.Http;
@@ -82,14 +84,38 @@ namespace Timeline.Controllers
[HttpGet("user/{username}/avatar"), Authorize]
public async Task<IActionResult> GetAvatar([FromRoute] string username)
{
- var existence = (await _userService.GetUser(username)) != null;
- if (!existence)
- return NotFound();
-
var url = await _userService.GetAvatarUrl(username);
+ if (url == null)
+ return NotFound();
return Redirect(url);
}
+ [HttpPut("user/{username}/avatar"), Authorize]
+ [Consumes("image/png", "image/gif", "image/jpeg", "image/svg+xml")]
+ public async Task<IActionResult> PutAvatar([FromRoute] string username, [FromHeader(Name="Content-Type")] string contentType)
+ {
+ bool isAdmin = User.IsInRole("admin");
+ if (!isAdmin)
+ {
+ if (username != User.Identity.Name)
+ return StatusCode(StatusCodes.Status403Forbidden, PutAvatarResponse.Forbidden);
+ }
+
+ var stream = new MemoryStream();
+ await Request.Body.CopyToAsync(stream);
+ var result = await _userService.PutAvatar(username, stream.ToArray(), contentType);
+ switch (result)
+ {
+ case PutAvatarResult.Success:
+ return Ok(PutAvatarResponse.Success);
+ case PutAvatarResult.UserNotExists:
+ return BadRequest(PutAvatarResponse.NotExists);
+ default:
+ throw new Exception("Unknown put avatar result.");
+ }
+ }
+
+
[HttpPost("userop/changepassword"), Authorize]
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequest request)
{