Skip to content

Commit 8e9c987

Browse files
committed
Merge branch '1.1' into 15-add-more-unit-tests
2 parents bc12886 + 68fde6b commit 8e9c987

14 files changed

Lines changed: 337 additions & 387 deletions

File tree

FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -227,48 +227,24 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
227227
}
228228
}
229229

230-
public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
230+
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
231231
{
232232
await GetFileAsync(virtualPath, cancellationToken);
233233
var path = GetPath(virtualPath);
234234

235235
try
236236
{
237-
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
238-
using var memoryStream = new MemoryStream();
239-
await response.ResponseStream.CopyToAsync(memoryStream, 81920, cancellationToken);
237+
var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
240238

241-
return memoryStream.ToArray();
239+
return response.ResponseStream;
242240
}
243241
catch (Exception exception)
244242
{
245243
throw Exception(exception);
246244
}
247245
}
248246

249-
public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
250-
{
251-
await GetFileAsync(virtualPath, cancellationToken);
252-
var path = GetPath(virtualPath);
253-
254-
try
255-
{
256-
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
257-
using var memoryStream = new MemoryStream();
258-
await response.ResponseStream.CopyToAsync(memoryStream, 81920, cancellationToken);
259-
260-
using var streamReader = new StreamReader(memoryStream);
261-
memoryStream.Position = 0;
262-
263-
return await streamReader.ReadToEndAsync();
264-
}
265-
catch (Exception exception)
266-
{
267-
throw Exception(exception);
268-
}
269-
}
270-
271-
public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
247+
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
272248
{
273249
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
274250
{
@@ -279,37 +255,11 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b
279255

280256
try
281257
{
282-
using var memoryStream = new MemoryStream(contents);
283-
var request = new PutObjectRequest
284-
{
285-
InputStream = memoryStream,
286-
BucketName = bucketName,
287-
Key = path
288-
};
289-
290-
await client.PutObjectAsync(request, cancellationToken);
291-
}
292-
catch (Exception exception)
293-
{
294-
throw Exception(exception);
295-
}
296-
}
297-
298-
public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
299-
{
300-
await GetFileAsync(virtualPath, cancellationToken);
301-
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
302-
contents = existingContents.Concat(contents).ToArray();
303-
await DeleteFileAsync(virtualPath, cancellationToken);
258+
contents.Seek(0, SeekOrigin.Begin);
304259

305-
var path = GetPath(virtualPath);
306-
307-
try
308-
{
309-
using var memoryStream = new MemoryStream(contents);
310260
var request = new PutObjectRequest
311261
{
312-
InputStream = memoryStream,
262+
InputStream = contents,
313263
BucketName = bucketName,
314264
Key = path
315265
};
@@ -322,7 +272,7 @@ public override async Task AppendFileAsync(string virtualPath, byte[] contents,
322272
}
323273
}
324274

325-
private static Exception Exception(Exception exception)
275+
protected override Exception Exception(Exception exception)
326276
{
327277
if (exception is FileSystemException)
328278
{

FileSystem.Adapters.AzureBlobStorage/src/AzureBlobStorageAdapter.cs

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Azure;
@@ -203,46 +202,22 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
203202
}
204203
}
205204

206-
public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
205+
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
207206
{
208207
await GetFileAsync(virtualPath, cancellationToken);
209208
var path = GetPath(virtualPath);
210209

211210
try
212211
{
213-
using var memoryStream = new MemoryStream();
214-
await client.GetBlobClient(path).DownloadToAsync(memoryStream, cancellationToken);
215-
216-
return memoryStream.ToArray();
217-
}
218-
catch (Exception exception)
219-
{
220-
throw Exception(exception);
221-
}
222-
}
223-
224-
public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
225-
{
226-
await GetFileAsync(virtualPath, cancellationToken);
227-
var path = GetPath(virtualPath);
228-
229-
try
230-
{
231-
using var memoryStream = new MemoryStream();
232-
await client.GetBlobClient(path).DownloadToAsync(memoryStream, cancellationToken);
233-
234-
using var streamReader = new StreamReader(memoryStream);
235-
memoryStream.Position = 0;
236-
237-
return await streamReader.ReadToEndAsync();
212+
return await client.GetBlobClient(path).OpenReadAsync(cancellationToken: cancellationToken);
238213
}
239214
catch (Exception exception)
240215
{
241216
throw Exception(exception);
242217
}
243218
}
244219

245-
public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
220+
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
246221
{
247222
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
248223
{
@@ -253,38 +228,17 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b
253228

254229
try
255230
{
256-
using var memoryStream = new MemoryStream(contents);
257-
258-
await client.UploadBlobAsync(path, memoryStream, cancellationToken);
259-
}
260-
catch (Exception exception)
261-
{
262-
throw Exception(exception);
263-
}
264-
}
265-
266-
public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
267-
{
268-
await GetFileAsync(virtualPath, cancellationToken);
269-
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
270-
contents = existingContents.Concat(contents).ToArray();
271-
await DeleteFileAsync(virtualPath, cancellationToken);
272-
273-
var path = GetPath(virtualPath);
274-
275-
try
276-
{
277-
using var memoryStream = new MemoryStream(contents);
231+
contents.Seek(0, SeekOrigin.Begin);
278232

279-
await client.UploadBlobAsync(path, memoryStream, cancellationToken);
233+
await client.UploadBlobAsync(path, contents, cancellationToken);
280234
}
281235
catch (Exception exception)
282236
{
283237
throw Exception(exception);
284238
}
285239
}
286240

287-
private static Exception Exception(Exception exception)
241+
protected override Exception Exception(Exception exception)
288242
{
289243
if (exception is FileSystemException)
290244
{

FileSystem.Adapters.AzureFileStorage/src/AzureFileStorageAdapter.cs

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using System.Linq;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using Azure;
@@ -185,7 +184,7 @@ public override async Task DeleteFileAsync(string virtualPath, CancellationToken
185184
}
186185
}
187186

188-
public override async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken cancellationToken = default)
187+
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default)
189188
{
190189
await GetFileAsync(virtualPath, cancellationToken);
191190

@@ -198,45 +197,15 @@ public override async Task<byte[]> ReadFileAsync(string virtualPath, Cancellatio
198197
var directory = client.GetDirectoryClient(directoryPath);
199198
var download = await directory.GetFileClient(filePath).DownloadAsync(cancellationToken: cancellationToken);
200199

201-
using var memoryStream = new MemoryStream();
202-
await download.Value.Content.CopyToAsync(memoryStream, 81920, cancellationToken);
203-
204-
return memoryStream.ToArray();
205-
}
206-
catch (Exception exception)
207-
{
208-
throw Exception(exception);
209-
}
210-
}
211-
212-
public override async Task<string> ReadTextFileAsync(string virtualPath, CancellationToken cancellationToken = default)
213-
{
214-
await GetFileAsync(virtualPath, cancellationToken);
215-
216-
var path = GetPath(virtualPath);
217-
var filePath = GetLastPathPart(path);
218-
var directoryPath = GetParentPathPart(path);
219-
220-
try
221-
{
222-
var directory = client.GetDirectoryClient(directoryPath);
223-
var file = directory.GetFileClient(filePath);
224-
var download = await file.DownloadAsync(cancellationToken: cancellationToken);
225-
226-
using var memoryStream = new MemoryStream();
227-
await download.Value.Content.CopyToAsync(memoryStream, 81920, cancellationToken);
228-
using var streamReader = new StreamReader(memoryStream);
229-
memoryStream.Position = 0;
230-
231-
return await streamReader.ReadToEndAsync();
200+
return download.Value.Content;
232201
}
233202
catch (Exception exception)
234203
{
235204
throw Exception(exception);
236205
}
237206
}
238207

239-
public override async Task WriteFileAsync(string virtualPath, byte[] contents, bool overwrite = false, CancellationToken cancellationToken = default)
208+
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default)
240209
{
241210
if (!overwrite && await FileExistsAsync(virtualPath, cancellationToken))
242211
{
@@ -253,47 +222,18 @@ public override async Task WriteFileAsync(string virtualPath, byte[] contents, b
253222
await directory.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
254223
var file = directory.GetFileClient(filePath);
255224

256-
using var memoryStream = new MemoryStream(contents);
257-
await file.CreateAsync(memoryStream.Length, cancellationToken: cancellationToken);
258-
259-
await file.UploadRangeAsync(new HttpRange(0, memoryStream.Length), memoryStream, cancellationToken: cancellationToken);
260-
}
261-
catch (Exception exception)
262-
{
263-
throw Exception(exception);
264-
}
265-
}
266-
267-
public override async Task AppendFileAsync(string virtualPath, byte[] contents, CancellationToken cancellationToken = default)
268-
{
269-
await GetFileAsync(virtualPath, cancellationToken);
270-
var existingContents = await ReadFileAsync(virtualPath, cancellationToken);
271-
272-
var path = GetPath(virtualPath);
273-
var filePath = GetLastPathPart(path);
274-
var directoryPath = GetParentPathPart(path);
275-
276-
try
277-
{
278-
var directory = client.GetDirectoryClient(directoryPath);
279-
var file = directory.GetFileClient(filePath);
280-
281-
contents = existingContents.Concat(contents).ToArray();
282-
283-
using var memoryStream = new MemoryStream(contents);
284-
285-
await file.DeleteAsync(cancellationToken);
286-
await file.CreateAsync(memoryStream.Length, cancellationToken: cancellationToken);
225+
contents.Seek(0, SeekOrigin.Begin);
287226

288-
await file.UploadRangeAsync(new HttpRange(0, memoryStream.Length), memoryStream, cancellationToken: cancellationToken);
227+
await file.CreateAsync(contents.Length, cancellationToken: cancellationToken);
228+
await file.UploadRangeAsync(new HttpRange(0, contents.Length), contents, cancellationToken: cancellationToken);
289229
}
290230
catch (Exception exception)
291231
{
292232
throw Exception(exception);
293233
}
294234
}
295235

296-
private static Exception Exception(Exception exception)
236+
protected override Exception Exception(Exception exception)
297237
{
298238
if (exception is FileSystemException)
299239
{

0 commit comments

Comments
 (0)