Skip to content

Commit 9c96062

Browse files
committed
Use ReadExactlyAsync in ThumbnailCacheModel
1 parent 9600573 commit 9c96062

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

src/Sdk/SecureFolderFS.Sdk/AppModels/ThumbnailCacheModel.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public async Task CacheThumbnailAsync(string cacheKey, IImageStream thumbnailStr
8080
var data = new byte[thumbnailStream.Inner.Length];
8181
var savedPosition = thumbnailStream.Inner.Position;
8282
thumbnailStream.Inner.Position = 0L;
83-
var read = await thumbnailStream.Inner.ReadAsync(data, cancellationToken);
83+
84+
// A single call to ReadAsync is not guaranteed to fill the buffer
85+
await thumbnailStream.Inner.ReadExactlyAsync(data, cancellationToken);
8486
thumbnailStream.Inner.Position = savedPosition;
85-
if (read != data.Length)
86-
return;
8787

8888
await _database.SetValueAsync(cacheKey, data, cancellationToken);
8989
}
@@ -113,8 +113,19 @@ public Task ClearCacheAsync(CancellationToken cancellationToken = default)
113113
/// <returns>A unique cache key string.</returns>
114114
public static async Task<string> GetCacheKeyAsync(IFile file, CancellationToken cancellationToken)
115115
{
116-
var pathHash = GetPathHash(file.Id);
117116
var dateModified = await file.GetDateModifiedAsync(cancellationToken);
117+
return GetCacheKey(file.Id, dateModified);
118+
}
119+
120+
/// <summary>
121+
/// Generates a cache key from an already-known file ID and modification date.
122+
/// </summary>
123+
/// <param name="id">The unique ID of the file.</param>
124+
/// <param name="dateModified">The file's last modification date, if known.</param>
125+
/// <returns>A unique cache key string.</returns>
126+
public static string GetCacheKey(string id, DateTime? dateModified)
127+
{
128+
var pathHash = GetPathHash(id);
118129
if (dateModified is null)
119130
return pathHash;
120131

@@ -126,11 +137,11 @@ public static async Task<string> GetCacheKeyAsync(IFile file, CancellationToken
126137
/// <summary>
127138
/// Generates a hash of the file path using SHA256.
128139
/// </summary>
129-
/// <param name="filePath">The file path to hash.</param>
140+
/// <param name="id">The file path to hash.</param>
130141
/// <returns>A hexadecimal hash string.</returns>
131-
private static string GetPathHash(string filePath)
142+
private static string GetPathHash(string id)
132143
{
133-
var bytes = Encoding.UTF8.GetBytes(filePath);
144+
var bytes = Encoding.UTF8.GetBytes(id);
134145
var hash = SHA256.HashData(bytes);
135146

136147
return Convert.ToHexString(hash).ToLowerInvariant();

0 commit comments

Comments
 (0)