-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathDirectoryProxy.cs
More file actions
224 lines (191 loc) · 8.4 KB
/
DirectoryProxy.cs
File metadata and controls
224 lines (191 loc) · 8.4 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright © 2022 Dmitry Sikorsky. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using ExtCore.FileStorage.Abstractions;
namespace ExtCore.FileStorage.Azure;
/// <summary>
/// Implements the <see cref="IDirectoryProxy">IDirectoryProxy</see> interface and represents a directory in a Azure Storage account.
/// </summary>
public class DirectoryProxy : IDirectoryProxy
{
private readonly string connectionString;
private readonly string rootPath;
private readonly string path;
private readonly string containerName;
private readonly string prefix;
/// <summary>
/// The path of the underlying directory relatively to the root one.
/// </summary>
public string RelativePath { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="DirectoryProxy">DirectoryProxy</see> class.
/// </summary>
/// <param name="connectionString">The Azure Storage account connection string.</param>
/// <param name="rootPath">The root path of the underlying directory's relative one.</param>
/// <param name="relativePath">The path of the underlying directory relatively to the root one.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public DirectoryProxy(string connectionString, string rootPath, string relativePath)
{
if (connectionString == string.Empty)
throw new ArgumentException($"Value can't be empty. Parameter name: connectionString.");
if (connectionString == null)
throw new ArgumentNullException($"Value can't be null. Parameter name: connectionString.", default(Exception));
this.connectionString = connectionString;
this.rootPath = RelativeUrl.Combine(rootPath);
this.RelativePath = RelativeUrl.Combine(relativePath);
this.path = RelativeUrl.Combine(this.rootPath, this.RelativePath);
string[] urlSegments = path.Split('/');
this.containerName = urlSegments.First();
this.prefix = string.Join("/", urlSegments.Skip(1));
}
/// <summary>
/// Checks if the underlying directory exists.
/// </summary>
/// <returns>Returns a flag indicating if the underlying directory exists.</returns>
public async Task<bool> ExistsAsync()
{
try
{
BlobContainerClient blobContainerClient = this.GetBlobContainerClient();
IAsyncEnumerable<Page<BlobHierarchyItem>> pages = blobContainerClient.GetBlobsByHierarchyAsync(prefix: this.prefix).AsPages();
await foreach (Page<BlobHierarchyItem> page in pages)
return page.Values.Count != 0;
}
catch { }
return false;
}
/// <summary>
/// Creates the underlying directory.
/// </summary>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task CreateAsync()
{
throw new NotSupportedException();
}
/// <summary>
/// Moves the underlying directory.
/// </summary>
/// <param name="destinationRelativePath"></param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task MoveAsync(string destinationRelativePath)
{
throw new NotSupportedException();
}
/// <summary>
/// Deletes the underlying directory.
/// </summary>
/// <param name="recursive">Pass true to remove all the underlying directory content recursively; otherwise false.</param>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task DeleteAsync(bool recursive)
{
try
{
BlobContainerClient blobContainerClient = this.GetBlobContainerClient();
if (string.IsNullOrEmpty(this.prefix))
{
await blobContainerClient.DeleteAsync();
return;
}
string prefix = this.prefix + "/";
IAsyncEnumerable<Page<BlobHierarchyItem>> pages = blobContainerClient.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: recursive ? null : "/").AsPages();
await foreach (Page<BlobHierarchyItem> page in pages)
{
foreach (BlobHierarchyItem blobItem in page.Values)
{
if (blobItem.IsBlob)
{
BlobClient blobClient = await this.GetBlobClient(blobItem.Blob);
await blobClient.DeleteAsync();
}
}
}
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
}
/// <summary>
/// Gets the directory proxies for the directories inside the underlying one.
/// </summary>
/// <returns>The directory proxies for the directories inside the underlying one</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<IEnumerable<IDirectoryProxy>> GetDirectoryProxiesAsync()
{
try
{
IList<IDirectoryProxy> directoryProxies = new List<IDirectoryProxy>();
BlobContainerClient blobContainerClient = this.GetBlobContainerClient();
string prefix = string.IsNullOrEmpty(this.prefix) ? null : this.prefix + "/";
IAsyncEnumerable<Page<BlobHierarchyItem>> pages = blobContainerClient.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/").AsPages();
await foreach (Page<BlobHierarchyItem> page in pages)
foreach (BlobHierarchyItem blobItem in page.Values)
if (blobItem.IsPrefix)
directoryProxies.Add(new DirectoryProxy(this.connectionString, this.rootPath, RelativeUrl.Combine(this.RelativePath, RelativeUrl.Combine(blobItem.Prefix).Split('/').Last())));
return directoryProxies;
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
}
/// <summary>
/// Gets the file proxies for the files inside the underlying one.
/// </summary>
/// <returns>The file proxies for the files inside the underlying directory.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<IEnumerable<IFileProxy>> GetFileProxiesAsync()
{
try
{
IList<IFileProxy> fileProxies = new List<IFileProxy>();
BlobContainerClient blobContainerClient = this.GetBlobContainerClient();
string prefix = string.IsNullOrEmpty(this.prefix) ? null : this.prefix + "/";
IAsyncEnumerable<Page<BlobHierarchyItem>> pages = blobContainerClient.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/").AsPages();
await foreach (Page<BlobHierarchyItem> page in pages)
foreach (BlobHierarchyItem blobItem in page.Values)
if (blobItem.IsBlob)
fileProxies.Add(new FileProxy(this.connectionString, this.rootPath, this.RelativePath, blobItem.Blob.Name.Split('/').Last()));
return fileProxies;
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.path}\". See inner exception for details.", e);
}
}
private BlobContainerClient GetBlobContainerClient()
{
BlobServiceClient blobServiceClient = new BlobServiceClient(this.connectionString);
return blobServiceClient.GetBlobContainerClient(this.containerName);
}
private async Task<BlobClient> GetBlobClient(BlobItem blobItem)
{
BlobContainerClient blobContainerClient = this.GetBlobContainerClient();
await blobContainerClient.CreateIfNotExistsAsync();
return blobContainerClient.GetBlobClient(blobItem.Name);
}
}