Skip to content

Commit 697c8c4

Browse files
committed
add Name property to IStore
improve getAsync add Delete Test
1 parent cf2f0e0 commit 697c8c4

File tree

10 files changed

+76
-10
lines changed

10 files changed

+76
-10
lines changed

src/GeekLearning.Storage.Azure/AzureStorageProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public string Name
1212
}
1313
}
1414

15-
public IStore BuildStore(StorageOptions.StorageStore storeOptions)
15+
public IStore BuildStore(string storeName, StorageOptions.StorageStore storeOptions)
1616
{
17-
return new AzureStore(storeOptions.Parameters["ConnectionString"], storeOptions.Parameters["Container"]);
17+
return new AzureStore(storeName, storeOptions.Parameters["ConnectionString"], storeOptions.Parameters["Container"]);
1818
}
1919
}
2020
}

src/GeekLearning.Storage.Azure/AzureStore.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public class AzureStore : IStore
1515
private Lazy<CloudBlobClient> client;
1616
private string containerName;
1717

18-
public AzureStore(string connectionString, string containerName)
18+
public AzureStore(string storeName, string connectionString, string containerName)
1919
{
20+
this.Name = storeName;
21+
2022
if (string.IsNullOrWhiteSpace(connectionString))
2123
{
2224
throw new ArgumentNullException("connectionString");
@@ -34,9 +36,23 @@ public AzureStore(string connectionString, string containerName)
3436
container = new Lazy<CloudBlobContainer>(() => this.client.Value.GetContainerReference(this.containerName));
3537
}
3638

39+
public string Name { get; }
40+
3741
private async Task<Internal.AzureFileReference> InternalGetAsync(IPrivateFileReference file)
3842
{
39-
return new Internal.AzureFileReference(file.Path, await this.container.Value.GetBlobReferenceFromServerAsync(file.Path));
43+
try
44+
{
45+
var blob = await this.container.Value.GetBlobReferenceFromServerAsync(file.Path);
46+
return new Internal.AzureFileReference(file.Path, blob);
47+
}
48+
catch (StorageException storageException)
49+
{
50+
if (storageException.RequestInformation.HttpStatusCode == 404)
51+
{
52+
return null;
53+
}
54+
throw;
55+
}
4056
}
4157

4258
public async Task<IFileReference> GetAsync(IPrivateFileReference file)

src/GeekLearning.Storage.FileSystem/FileSystemStorageProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public string Name
2020
}
2121
}
2222

23-
public IStore BuildStore(StorageOptions.StorageStore storeOptions)
23+
public IStore BuildStore(string storeName, StorageOptions.StorageStore storeOptions)
2424
{
25-
return new FileSystemStore(storeOptions.Parameters["Path"], this.options.Value.RootPath);
25+
return new FileSystemStore(storeName, storeOptions.Parameters["Path"], this.options.Value.RootPath);
2626
}
2727
}
2828
}

src/GeekLearning.Storage.FileSystem/FileSystemStore.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ public class FileSystemStore : IStore
99
{
1010
private string absolutePath;
1111

12-
public FileSystemStore(string path, string rootPath)
12+
public FileSystemStore(string storeName, string path, string rootPath)
1313
{
14+
this.Name = storeName;
15+
1416
if (string.IsNullOrEmpty(path))
1517
{
1618
throw new ArgumentNullException("path");
@@ -25,10 +27,16 @@ public FileSystemStore(string path, string rootPath)
2527
this.absolutePath = Path.Combine(rootPath, path);
2628
}
2729
}
30+
public string Name { get; }
2831

2932
private Internal.FileSystemFileReference InternalGetAsync(IPrivateFileReference file)
3033
{
31-
return new Internal.FileSystemFileReference(Path.Combine(this.absolutePath, file.Path), file.Path);
34+
var fullPath = Path.Combine(this.absolutePath, file.Path);
35+
if (File.Exists(fullPath))
36+
{
37+
return new Internal.FileSystemFileReference(fullPath, file.Path);
38+
}
39+
return null;
3240
}
3341

3442
public async Task<IFileReference> GetAsync(IPrivateFileReference file)

src/GeekLearning.Storage/IStorageProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public interface IStorageProvider
44
{
55
string Name { get; }
66

7-
IStore BuildStore(StorageOptions.StorageStore storeOptions);
7+
IStore BuildStore(string storeName, StorageOptions.StorageStore storeOptions);
88
}
99
}

src/GeekLearning.Storage/IStore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
public interface IStore
88
{
9+
string Name { get; }
10+
911
Task<IFileReference[]> ListAsync(string path, bool recursive);
1012

1113
Task<IFileReference[]> ListAsync(string path, string searchPattern, bool recursive);

src/GeekLearning.Storage/StorageFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public StorageFactory(IEnumerable<IStorageProvider> storageProviders, IOptions<S
1818
public IStore GetStore(string store)
1919
{
2020
var conf = this.options.Value.Stores[store];
21-
return this.storageProviders.FirstOrDefault(x => x.Name == conf.Provider).BuildStore(conf);
21+
return this.storageProviders.FirstOrDefault(x => x.Name == conf.Provider).BuildStore(store, conf);
2222
}
2323
}
2424
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace GeekLearning.Integration.Test
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using GeekLearning.Storage;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using System.IO;
11+
12+
[Collection(nameof(IntegrationCollection))]
13+
[Trait("Operation", "Delete"), Trait("Kind", "Integration")]
14+
public class DeleteTests
15+
{
16+
StoresFixture storeFixture;
17+
18+
public DeleteTests(StoresFixture fixture)
19+
{
20+
this.storeFixture = fixture;
21+
}
22+
23+
[Theory(DisplayName = nameof(Delete)), InlineData("azure"), InlineData("filesystem")]
24+
public async Task Delete(string storeName)
25+
{
26+
var storageFactory = this.storeFixture.Services.GetRequiredService<IStorageFactory>();
27+
28+
var store = storageFactory.GetStore(storeName);
29+
30+
var file = await store.GetAsync("Delete/ToDelete.txt");
31+
32+
await file.DeleteAsync();
33+
34+
Assert.Null(await store.GetAsync("Delete/ToDelete.txt"));
35+
Assert.NotNull(await store.GetAsync("Delete/ToSurvive.txt"));
36+
}
37+
}
38+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


0 commit comments

Comments
 (0)