-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathChangeTrackingResource.cs
More file actions
61 lines (40 loc) · 1.54 KB
/
ChangeTrackingResource.cs
File metadata and controls
61 lines (40 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using GenHTTP.Api.Content.IO;
using GenHTTP.Api.Protocol;
namespace GenHTTP.Modules.IO.Tracking;
public sealed class ChangeTrackingResource : IResource
{
private ulong? _lastChecksum;
#region Initialization
public ChangeTrackingResource(IResource source)
{
Source = source;
}
#endregion
#region Get-/Setters
private IResource Source { get; }
public string? Name => Source.Name;
public DateTime? Modified => Source.Modified;
public FlexibleContentType? ContentType => Source.ContentType;
public ulong? Length => Source.Length;
#endregion
#region Functionality
public async ValueTask<Stream> GetContentAsync()
{
_lastChecksum = await CalculateChecksumAsync();
return await Source.GetContentAsync();
}
public async ValueTask WriteAsync(Stream target, uint bufferSize)
{
_lastChecksum = await CalculateChecksumAsync();
await Source.WriteAsync(target, bufferSize);
}
public ValueTask<ulong> CalculateChecksumAsync() => Source.CalculateChecksumAsync();
/// <summary>
/// Checks whether the content of the resource has changed
/// since <see cref="GetContentAsync()" /> or <see cref="WriteAsync(Stream, uint)"/>
/// has been called the last time.
/// </summary>
/// <returns>True if the content has changed, false otherwise</returns>
public async ValueTask<bool> CheckChangedAsync() => await CalculateChecksumAsync() != _lastChecksum;
#endregion
}