-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathFileProxy.cs
More file actions
267 lines (229 loc) · 9.12 KB
/
FileProxy.cs
File metadata and controls
267 lines (229 loc) · 9.12 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// 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.IO;
using System.Linq;
using System.Threading.Tasks;
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 file in a Azure Storage account.
/// </summary>
public class FileProxy : IFileProxy
{
private readonly string connectionString;
private readonly string filepath;
private readonly string containerName;
private readonly string blobName;
/// <summary>
/// The path of the underlying file relatively to the root one.
/// </summary>
public string RelativePath { get; private set; }
/// <summary>
/// The filename of the underlying file.
/// </summary>
public string Filename { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="FileProxy">FileProxy</see> class.
/// </summary>
/// <param name="connectionString">The Azure Storage account connection string.</param>
/// <param name="rootPath">The root path of the underlying file's relative one.</param>
/// <param name="relativePath">The path of the underlying file relatively to the root one.</param>
/// <param name="filename">The filename of the underlying file.</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public FileProxy(string connectionString, string rootPath, string relativePath, string filename)
{
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));
if (filename == string.Empty)
throw new ArgumentException($"Value can't be empty. Parameter name: filename.");
if (filename == null)
throw new ArgumentNullException($"Value can't be null. Parameter name: filename.", default(Exception));
this.connectionString = connectionString;
this.RelativePath = RelativeUrl.Combine(relativePath);
this.Filename = filename;
this.filepath = RelativeUrl.Combine(rootPath, this.RelativePath, this.Filename);
string[] urlSegments = filepath.Split('/');
this.containerName = urlSegments.First();
this.blobName = string.Join("/", urlSegments.Skip(1));
}
/// <summary>
/// Checks if the underlying file exists.
/// </summary>
/// <returns>Returns a flag indicating if the underlying file exists.</returns>
public async Task<bool> ExistsAsync()
{
try
{
BlobClient blobClient = await this.GetBlobClient();
return await blobClient.ExistsAsync();
}
catch
{
return false;
}
}
/// <summary>
/// Reads content of the underlying file as a byte array.
/// </summary>
/// <returns>Content of the underlying file as a byte array.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<Stream> ReadStreamAsync()
{
try
{
BlobClient blobClient = await this.GetBlobClient();
MemoryStream stream = new MemoryStream();
await blobClient.DownloadToAsync(stream);
stream.Position = 0;
return stream;
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Reads content of the underlying file as a byte array.
/// </summary>
/// <returns>Content of the underlying file as a byte array.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<byte[]> ReadBytesAsync()
{
try
{
BlobClient blobClient = await this.GetBlobClient();
BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync();
return downloadResult.Content.ToArray();
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Reads content of the underlying file as a text string.
/// </summary>
/// <returns>Content of the underlying file as a text string.</returns>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task<string> ReadTextAsync()
{
try
{
BlobClient blobClient = await this.GetBlobClient();
BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync();
return downloadResult.Content.ToString();
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Writes content to the underlying file as a stream.
/// </summary>
/// <param name="inputStream">Content to write to the underlying file as a stream.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task WriteStreamAsync(Stream inputStream)
{
try
{
BlobClient blobClient = await this.GetBlobClient();
await blobClient.UploadAsync(inputStream);
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Writes content to the underlying file as a byte array.
/// </summary>
/// <param name="bytes">Content to write to the underlying file as a byte array.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task WriteBytesAsync(byte[] bytes)
{
try
{
BlobClient blobClient = await this.GetBlobClient();
await blobClient.UploadAsync(BinaryData.FromBytes(bytes));
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Writes content to the underlying file as a text string.
/// </summary>
/// <param name="text">Content to write to the underlying file as a text string.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task WriteTextAsync(string text)
{
try
{
BlobClient blobClient = await this.GetBlobClient();
await blobClient.UploadAsync(BinaryData.FromString(text));
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
/// <summary>
/// Deletes the underlying file.
/// </summary>
/// <exception cref="AccessDeniedException"></exception>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="PathTooLongException"></exception>
/// <exception cref="FileStorageException"></exception>
public async Task DeleteAsync()
{
try
{
BlobClient blobClient = await this.GetBlobClient();
await blobClient.DeleteAsync();
}
catch (Exception e)
{
throw new FileStorageException($"Generic file storage exception: \"{this.filepath}\". See inner exception for details.", e);
}
}
private async Task<BlobClient> GetBlobClient()
{
BlobServiceClient blobServiceClient = new BlobServiceClient(this.connectionString);
BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(this.containerName);
await blobContainerClient.CreateIfNotExistsAsync();
return blobContainerClient.GetBlobClient(this.blobName);
}
}