Skip to content

Commit fa40f54

Browse files
GetEtagAsync (#6577)
Add GetEtagAsync.
1 parent cdbfae0 commit fa40f54

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

src/NuGetGallery.Core/Services/CloudBlobCoreFileStorageService.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,27 @@ public async Task SetMetadataAsync(
426426
}
427427
}
428428

429+
public async Task<string> GetETagOrNullAsync(
430+
string folderName,
431+
string fileName)
432+
{
433+
folderName = folderName ?? throw new ArgumentNullException(nameof(folderName));
434+
fileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
435+
436+
var container = await GetContainerAsync(folderName);
437+
var blob = container.GetBlobReference(fileName);
438+
try
439+
{
440+
await blob.FetchAttributesAsync();
441+
return blob.ETag;
442+
}
443+
// In case that the blob does not exist return null.
444+
catch (StorageException)
445+
{
446+
return null;
447+
}
448+
}
449+
429450
private static SharedAccessBlobPermissions MapFileUriPermissions(FileUriPermissions permissions)
430451
{
431452
return (SharedAccessBlobPermissions)permissions;

src/NuGetGallery.Core/Services/ICoreFileStorageService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,15 @@ Task SetMetadataAsync(
120120
string folderName,
121121
string fileName,
122122
Func<Lazy<Task<Stream>>, IDictionary<string, string>, Task<bool>> updateMetadataAsync);
123+
124+
/// <summary>
125+
/// Returns the etag value for the specified blob. If the blob does not exists it will return null.
126+
/// </summary>
127+
/// <param name="folderName">The folder name.</param>
128+
/// <param name="fileName">The file name.</param>
129+
/// <returns>The etag of the specified file.</returns>
130+
Task<string> GetETagOrNullAsync(
131+
string folderName,
132+
string fileName);
123133
}
124134
}

src/NuGetGallery/Services/FileSystemFileStorageService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ public static string ResolvePath(string fileStorageDirectory)
273273
return fileStorageDirectory;
274274
}
275275

276+
public Task<string> GetETagOrNullAsync(
277+
string folderName,
278+
string fileName)
279+
{
280+
throw new NotImplementedException(nameof(GetETagOrNullAsync));
281+
}
282+
276283
private static string GetContentType(string folderName)
277284
{
278285
switch (folderName)

tests/NuGetGallery.Core.Facts/Services/CloudBlobCoreFileStorageServiceFacts.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,5 +1410,61 @@ await _service.SetMetadataAsync(
14101410
_blobClient.VerifyAll();
14111411
}
14121412
}
1413+
1414+
public class TheGetETagMethod
1415+
{
1416+
private const string _etag = "dummy_etag";
1417+
1418+
private readonly Mock<ICloudBlobClient> _blobClient;
1419+
private readonly Mock<ICloudBlobContainer> _blobContainer;
1420+
private readonly Mock<ISimpleCloudBlob> _blob;
1421+
private readonly CloudBlobCoreFileStorageService _service;
1422+
1423+
public TheGetETagMethod()
1424+
{
1425+
_blobClient = new Mock<ICloudBlobClient>();
1426+
_blobContainer = new Mock<ICloudBlobContainer>();
1427+
_blob = new Mock<ISimpleCloudBlob>();
1428+
1429+
_blobClient.Setup(x => x.GetContainerReference(It.IsAny<string>()))
1430+
.Returns(_blobContainer.Object);
1431+
_blobContainer.Setup(x => x.CreateIfNotExistAsync())
1432+
.Returns(Task.FromResult(0));
1433+
_blobContainer.Setup(x => x.SetPermissionsAsync(It.IsAny<BlobContainerPermissions>()))
1434+
.Returns(Task.FromResult(0));
1435+
_blobContainer.Setup(x => x.GetBlobReference(It.IsAny<string>()))
1436+
.Returns(_blob.Object);
1437+
1438+
_service = CreateService(fakeBlobClient: _blobClient);
1439+
}
1440+
1441+
[Fact]
1442+
public async Task VerifyTheETagValue()
1443+
{
1444+
// Arrange
1445+
_blob.SetupGet(x => x.ETag).Returns(_etag);
1446+
1447+
// Act
1448+
var etagValue = await _service.GetETagOrNullAsync(folderName: CoreConstants.PackagesFolderName, fileName: "a");
1449+
1450+
// Assert
1451+
Assert.Equal(_etag, etagValue);
1452+
}
1453+
1454+
1455+
[Fact]
1456+
public async Task VerifyETagIsNullWhenBlobDoesNotExist()
1457+
{
1458+
// Arrange
1459+
_blob.Setup(x => x.FetchAttributesAsync()).ThrowsAsync(new StorageException("Boo"));
1460+
1461+
// Act
1462+
var etagValue = await _service.GetETagOrNullAsync(folderName: CoreConstants.PackagesFolderName, fileName: "a");
1463+
1464+
// Assert
1465+
Assert.Null(etagValue);
1466+
}
1467+
}
1468+
14131469
}
14141470
}

0 commit comments

Comments
 (0)