aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/UserService.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
commit2c9ee904731d3c931607ba99f1bab21fcb1e1bb4 (patch)
treef695f1c3d7eb33554784c77dca4afde7f4227503 /Timeline/Services/UserService.cs
parenta35090ab44a51f22a5e2fda95310738bdfee698d (diff)
downloadtimeline-2c9ee904731d3c931607ba99f1bab21fcb1e1bb4.tar.gz
timeline-2c9ee904731d3c931607ba99f1bab21fcb1e1bb4.tar.bz2
timeline-2c9ee904731d3c931607ba99f1bab21fcb1e1bb4.zip
Add avatar upload function.
Diffstat (limited to 'Timeline/Services/UserService.cs')
-rw-r--r--Timeline/Services/UserService.cs42
1 files changed, 42 insertions, 0 deletions
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs
index 4a47ca0f..9ebf2668 100644
--- a/Timeline/Services/UserService.cs
+++ b/Timeline/Services/UserService.cs
@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
+using System;
using System.Linq;
using System.Threading.Tasks;
using Timeline.Entities;
@@ -65,6 +66,18 @@ namespace Timeline.Services
BadOldPassword
}
+ public enum PutAvatarResult
+ {
+ /// <summary>
+ /// Success to upload avatar.
+ /// </summary>
+ Success,
+ /// <summary>
+ /// The user does not exists.
+ /// </summary>
+ UserNotExists
+ }
+
public interface IUserService
{
/// <summary>
@@ -141,7 +154,14 @@ namespace Timeline.Services
/// <see cref="ChangePasswordResult.BadOldPassword"/> if old password is wrong.</returns>
Task<ChangePasswordResult> ChangePassword(string username, string oldPassword, string newPassword);
+ /// <summary>
+ /// Get the true avatar url of a user.
+ /// </summary>
+ /// <param name="username">The name of user.</param>
+ /// <returns>The url if user exists. Null if user does not exist.</returns>
Task<string> GetAvatarUrl(string username);
+
+ Task<PutAvatarResult> PutAvatar(string username, byte[] data, string mimeType);
}
public class UserService : IUserService
@@ -301,11 +321,33 @@ namespace Timeline.Services
public async Task<string> GetAvatarUrl(string username)
{
+ if (username == null)
+ throw new ArgumentNullException(nameof(username));
+
+ if ((await GetUser(username)) == null)
+ return null;
+
var exists = await _cosService.IsObjectExists("avatar", username);
if (exists)
return _cosService.GenerateObjectGetUrl("avatar", username);
else
return _cosService.GenerateObjectGetUrl("avatar", "__default");
}
+
+ public async Task<PutAvatarResult> PutAvatar(string username, byte[] data, string mimeType)
+ {
+ if (username == null)
+ throw new ArgumentNullException(nameof(username));
+ if (data == null)
+ throw new ArgumentNullException(nameof(data));
+ if (mimeType == null)
+ throw new ArgumentNullException(nameof(mimeType));
+
+ if ((await GetUser(username)) == null)
+ return PutAvatarResult.UserNotExists;
+
+ await _cosService.PutObject("avatar", username, data, mimeType);
+ return PutAvatarResult.Success;
+ }
}
}