diff options
Diffstat (limited to 'Timeline')
-rw-r--r-- | Timeline/Entities/UserAvatar.cs | 12 | ||||
-rw-r--r-- | Timeline/Resources/Services/UserAvatarService.Designer.cs | 6 | ||||
-rw-r--r-- | Timeline/Resources/Services/UserAvatarService.resx | 4 | ||||
-rw-r--r-- | Timeline/Services/ETagGenerator.cs | 7 | ||||
-rw-r--r-- | Timeline/Services/UserAvatarService.cs | 19 |
5 files changed, 21 insertions, 27 deletions
diff --git a/Timeline/Entities/UserAvatar.cs b/Timeline/Entities/UserAvatar.cs index 3b5388aa..a5b18b94 100644 --- a/Timeline/Entities/UserAvatar.cs +++ b/Timeline/Entities/UserAvatar.cs @@ -24,17 +24,5 @@ namespace Timeline.Entities public DateTime LastModified { get; set; }
public long UserId { get; set; }
-
- public static UserAvatar Create(DateTime lastModified)
- {
- return new UserAvatar
- {
- Id = 0,
- Data = null,
- Type = null,
- ETag = null,
- LastModified = lastModified
- };
- }
}
}
diff --git a/Timeline/Resources/Services/UserAvatarService.Designer.cs b/Timeline/Resources/Services/UserAvatarService.Designer.cs index cabc9ede..6ee6fef9 100644 --- a/Timeline/Resources/Services/UserAvatarService.Designer.cs +++ b/Timeline/Resources/Services/UserAvatarService.Designer.cs @@ -70,11 +70,11 @@ namespace Timeline.Resources.Services { }
/// <summary>
- /// Looks up a localized string similar to Type of avatar is null..
+ /// Looks up a localized string similar to Type of avatar is null or empty..
/// </summary>
- internal static string ArgumentAvatarTypeNull {
+ internal static string ArgumentAvatarTypeNullOrEmpty {
get {
- return ResourceManager.GetString("ArgumentAvatarTypeNull", resourceCulture);
+ return ResourceManager.GetString("ArgumentAvatarTypeNullOrEmpty", resourceCulture);
}
}
diff --git a/Timeline/Resources/Services/UserAvatarService.resx b/Timeline/Resources/Services/UserAvatarService.resx index ab6389ff..3269bf13 100644 --- a/Timeline/Resources/Services/UserAvatarService.resx +++ b/Timeline/Resources/Services/UserAvatarService.resx @@ -120,8 +120,8 @@ <data name="ArgumentAvatarDataNull" xml:space="preserve">
<value>Data of avatar is null.</value>
</data>
- <data name="ArgumentAvatarTypeNull" xml:space="preserve">
- <value>Type of avatar is null.</value>
+ <data name="ArgumentAvatarTypeNullOrEmpty" xml:space="preserve">
+ <value>Type of avatar is null or empty.</value>
</data>
<data name="DatabaseCorruptedDataAndTypeNotSame" xml:space="preserve">
<value>Database corupted! One of type and data of a avatar is null but the other is not.</value>
diff --git a/Timeline/Services/ETagGenerator.cs b/Timeline/Services/ETagGenerator.cs index e518f01f..d328ea20 100644 --- a/Timeline/Services/ETagGenerator.cs +++ b/Timeline/Services/ETagGenerator.cs @@ -1,5 +1,6 @@ using System;
using System.Security.Cryptography;
+using System.Threading.Tasks;
namespace Timeline.Services
{
@@ -11,7 +12,7 @@ namespace Timeline.Services /// <param name="source">The source data.</param>
/// <returns>The generated etag.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="source"/> is null.</exception>
- string Generate(byte[] source);
+ Task<string> Generate(byte[] source);
}
public sealed class ETagGenerator : IETagGenerator, IDisposable
@@ -24,12 +25,12 @@ namespace Timeline.Services _sha1 = SHA1.Create();
}
- public string Generate(byte[] source)
+ public Task<string> Generate(byte[] source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
- return Convert.ToBase64String(_sha1.ComputeHash(source));
+ return Task.Run(() => Convert.ToBase64String(_sha1.ComputeHash(source)));
}
private bool _disposed = false; // To detect redundant calls
diff --git a/Timeline/Services/UserAvatarService.cs b/Timeline/Services/UserAvatarService.cs index 4c65a0fa..ff80003c 100644 --- a/Timeline/Services/UserAvatarService.cs +++ b/Timeline/Services/UserAvatarService.cs @@ -118,7 +118,7 @@ namespace Timeline.Services {
_cacheData = await File.ReadAllBytesAsync(path);
_cacheLastModified = File.GetLastWriteTime(path);
- _cacheETag = _eTagGenerator.Generate(_cacheData);
+ _cacheETag = await _eTagGenerator.Generate(_cacheData);
}
}
@@ -179,12 +179,15 @@ namespace Timeline.Services private readonly UsernameValidator _usernameValidator;
+ private readonly IClock _clock;
+
public UserAvatarService(
ILogger<UserAvatarService> logger,
DatabaseContext database,
IDefaultUserAvatarProvider defaultUserAvatarProvider,
IUserAvatarValidator avatarValidator,
- IETagGenerator eTagGenerator)
+ IETagGenerator eTagGenerator,
+ IClock clock)
{
_logger = logger;
_database = database;
@@ -192,6 +195,7 @@ namespace Timeline.Services _avatarValidator = avatarValidator;
_eTagGenerator = eTagGenerator;
_usernameValidator = new UsernameValidator();
+ _clock = clock;
}
public async Task<string> GetAvatarETag(string username)
@@ -245,8 +249,8 @@ namespace Timeline.Services {
if (avatar.Data == null)
throw new ArgumentException(Resources.Services.UserAvatarService.ArgumentAvatarDataNull, nameof(avatar));
- if (avatar.Type == null)
- throw new ArgumentException(Resources.Services.UserAvatarService.ArgumentAvatarTypeNull, nameof(avatar));
+ if (string.IsNullOrEmpty(avatar.Type))
+ throw new ArgumentException(Resources.Services.UserAvatarService.ArgumentAvatarTypeNullOrEmpty, nameof(avatar));
}
var userId = await DatabaseExtensions.CheckAndGetUser(_database.Users, _usernameValidator, username);
@@ -263,7 +267,7 @@ namespace Timeline.Services avatarEntity.Data = null;
avatarEntity.Type = null;
avatarEntity.ETag = null;
- avatarEntity.LastModified = DateTime.Now;
+ avatarEntity.LastModified = _clock.GetCurrentTime();
await _database.SaveChangesAsync();
_logger.LogInformation(Resources.Services.UserAvatarService.LogUpdateEntity);
}
@@ -278,8 +282,9 @@ namespace Timeline.Services }
avatarEntity!.Type = avatar.Type;
avatarEntity.Data = avatar.Data;
- avatarEntity.ETag = _eTagGenerator.Generate(avatar.Data);
- avatarEntity.LastModified = DateTime.Now;
+ avatarEntity.ETag = await _eTagGenerator.Generate(avatar.Data);
+ avatarEntity.LastModified = _clock.GetCurrentTime();
+ avatarEntity.UserId = userId;
if (create)
{
_database.UserAvatars.Add(avatarEntity);
|