Skip to content

Commit da586c6

Browse files
Align returned values with PhysicalFileSystem when blob does not exist
1 parent 7db0211 commit da586c6

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/Umbraco.StorageProviders.AzureBlob/IO/AzureBlobFileSystem.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net;
12
using Azure;
23
using Azure.Storage.Blobs;
34
using Azure.Storage.Blobs.Models;
@@ -12,6 +13,10 @@ namespace Umbraco.StorageProviders.AzureBlob.IO;
1213
/// <inheritdoc />
1314
public sealed class AzureBlobFileSystem : IAzureBlobFileSystem, IFileProviderFactory
1415
{
16+
// When not found, default to 1-1-1601 00:00:00 +00:00 for created/last modified and -1 for size to align with PhysicalFileSystem
17+
private static readonly DateTimeOffset _notFoundDateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(-11644473600);
18+
private const long NotFoundSize = -1;
19+
1520
private readonly string _requestRootPath;
1621
private readonly string _containerRootPath;
1722
private readonly BlobContainerClient _container;
@@ -91,6 +96,9 @@ public IEnumerable<string> GetDirectories(string path)
9196

9297
/// <inheritdoc />
9398
/// <exception cref="System.ArgumentNullException"><paramref name="path" /> is <c>null</c>.</exception>
99+
/// <remarks>
100+
/// Azure Blob Storage can only delete blobs, so deleting directories is always recursive.
101+
/// </remarks>
94102
public void DeleteDirectory(string path)
95103
{
96104
ArgumentNullException.ThrowIfNull(path);
@@ -100,6 +108,9 @@ public void DeleteDirectory(string path)
100108

101109
/// <inheritdoc />
102110
/// <exception cref="System.ArgumentNullException"><paramref name="path" /> is <c>null</c>.</exception>
111+
/// <remarks>
112+
/// Azure Blob Storage can only delete blobs, so deleting directories is always recursive.
113+
/// </remarks>
103114
public void DeleteDirectory(string path, bool recursive)
104115
{
105116
ArgumentNullException.ThrowIfNull(path);
@@ -303,7 +314,14 @@ public DateTimeOffset GetLastModified(string path)
303314
{
304315
ArgumentNullException.ThrowIfNull(path);
305316

306-
return GetBlobClient(path).GetProperties().Value.LastModified;
317+
try
318+
{
319+
return GetBlobClient(path).GetProperties().Value.LastModified;
320+
}
321+
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotFound)
322+
{
323+
return _notFoundDateTimeOffset;
324+
}
307325
}
308326

309327
/// <inheritdoc />
@@ -312,7 +330,14 @@ public DateTimeOffset GetCreated(string path)
312330
{
313331
ArgumentNullException.ThrowIfNull(path);
314332

315-
return GetBlobClient(path).GetProperties().Value.CreatedOn;
333+
try
334+
{
335+
return GetBlobClient(path).GetProperties().Value.CreatedOn;
336+
}
337+
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotFound)
338+
{
339+
return _notFoundDateTimeOffset;
340+
}
316341
}
317342

318343
/// <inheritdoc />
@@ -321,7 +346,14 @@ public long GetSize(string path)
321346
{
322347
ArgumentNullException.ThrowIfNull(path);
323348

324-
return GetBlobClient(path).GetProperties().Value.ContentLength;
349+
try
350+
{
351+
return GetBlobClient(path).GetProperties().Value.ContentLength;
352+
}
353+
catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.NotFound)
354+
{
355+
return NotFoundSize;
356+
}
325357
}
326358

327359
/// <inheritdoc />

0 commit comments

Comments
 (0)