diff --git a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlobContainer.cs b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlobContainer.cs index 1ccf07e..d7150c2 100644 --- a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlobContainer.cs +++ b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlobContainer.cs @@ -5,6 +5,7 @@ namespace Spotflow.InMemory.Azure.Storage.Blobs.Internals; internal class InMemoryBlobContainer(string name, IDictionary? metadata, InMemoryBlobService service) { + private readonly TimeProvider _timeProvider = service.Account.Provider.TimeProvider; private readonly object _lock = new(); private readonly Dictionary _blobEntries = []; @@ -70,7 +71,7 @@ private BlobEntry GetBlobEntry(string blobName) { if (!_blobEntries.TryGetValue(blobName, out entry)) { - var blob = new InMemoryBlockBlob(blobName, this); + var blob = new InMemoryBlockBlob(blobName, this, _timeProvider); entry = new(blob, new(1, 1)); _blobEntries.Add(blobName, entry); } diff --git a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs index be343d6..e8037c2 100644 --- a/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs +++ b/src/Spotflow.InMemory.Azure.Storage/Blobs/Internals/InMemoryBlockBlob.cs @@ -9,7 +9,7 @@ namespace Spotflow.InMemory.Azure.Storage.Blobs.Internals; -internal class InMemoryBlockBlob(string blobName, InMemoryBlobContainer container) +internal class InMemoryBlockBlob(string blobName, InMemoryBlobContainer container, TimeProvider timeProvider) { private Dictionary? _uncommittedBlocks = null; private List? _committedBlocks = null; @@ -336,19 +336,23 @@ private BinaryData GetContent() [MemberNotNull(nameof(_committedBlocks))] private void SetCommitedState(BlobHttpHeaders? headers, IDictionary? metadata, List committedBlocks) { - var newProperties = BlobsModelFactory.BlobProperties( - contentLength: _committedBlocks is null ? 0 : GetContent().ToMemory().Length, + _cachedContent = null; + _uncommittedBlocks = null; + _committedBlocks = committedBlocks; + + var now = timeProvider.GetUtcNow(); + + var createdOn = _properties?.CreatedOn ?? now; + + _properties = BlobsModelFactory.BlobProperties( + contentLength: GetContent().ToMemory().Length, metadata: metadata ?? _properties?.Metadata, eTag: new ETag(Guid.NewGuid().ToString()), - lastModified: DateTimeOffset.UtcNow, + lastModified: now, contentType: headers?.ContentType ?? _properties?.ContentType, - contentEncoding: headers?.ContentEncoding ?? _properties?.ContentEncoding + contentEncoding: headers?.ContentEncoding ?? _properties?.ContentEncoding, + createdOn: createdOn ); - - _properties = newProperties; - _cachedContent = null; - _uncommittedBlocks = null; - _committedBlocks = committedBlocks; } private void DeleteCore() diff --git a/tests/Tests/Storage/Blobs/BlobClientTests.cs b/tests/Tests/Storage/Blobs/BlobClientTests.cs index 9edf5a7..0454bb8 100644 --- a/tests/Tests/Storage/Blobs/BlobClientTests.cs +++ b/tests/Tests/Storage/Blobs/BlobClientTests.cs @@ -350,6 +350,9 @@ public void GetProperties_For_Existing_Blob_Should_Succeed(BlobClientType client var properties = blobClient.GetProperties().Value; + properties.ContentLength.Should().Be(13); + properties.CreatedOn.Should().BeCloseTo(DateTimeOffset.UtcNow, TimeSpan.FromHours(1)); + properties.LastModified.Should().Be(properties.CreatedOn); properties.BlobType.Should().Be(BlobType.Block); }