Skip to content

Commit 7a4f614

Browse files
authored
Merge pull request #17 from SharpGrip/15-add-more-unit-tests
15 add more unit tests
2 parents 68fde6b + c328543 commit 7a4f614

12 files changed

Lines changed: 722 additions & 49 deletions

File tree

FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Amazon.S3;
88
using Amazon.S3.Model;
99
using SharpGrip.FileSystem.Exceptions;
10+
using SharpGrip.FileSystem.Extensions;
1011
using SharpGrip.FileSystem.Models;
1112
using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException;
1213
using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException;
@@ -41,7 +42,7 @@ public override async Task<IFile> GetFileAsync(string virtualPath, CancellationT
4142
{
4243
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);
4344

44-
return ModelFactory.CreateFile(response, virtualPath);
45+
return ModelFactory.CreateFile(response, path, virtualPath);
4546
}
4647
catch (AmazonS3Exception exception)
4748
{
@@ -69,15 +70,12 @@ public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, Can
6970

7071
try
7172
{
72-
var prefix = "";
73-
var pathParts = GetPathParts(path);
74-
75-
if (pathParts.Length > 1)
73+
if (path == "/")
7674
{
77-
prefix = string.Join("/", pathParts.Take(pathParts.Length - 1)) + "/";
75+
return ModelFactory.CreateDirectory(new S3Object {Key = "/"}, virtualPath);
7876
}
7977

80-
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = prefix};
78+
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
8179
var response = await client.ListObjectsV2Async(request, cancellationToken);
8280

8381
if (response.KeyCount == 0)
@@ -106,6 +104,11 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath
106104
await GetDirectoryAsync(virtualPath, cancellationToken);
107105
var path = GetPath(virtualPath);
108106

107+
if (!path.EndsWith("/"))
108+
{
109+
path += "/";
110+
}
111+
109112
try
110113
{
111114
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
@@ -145,7 +148,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string v
145148

146149
foreach (var item in response.S3Objects)
147150
{
148-
var itemName = item.Key.Substring(0, item.Key.Length - path.Length);
151+
var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash();
149152

150153
if (item.Key.EndsWith("/") && itemName.Count(c => c.Equals('/')) == 1)
151154
{
@@ -192,9 +195,9 @@ public override async Task DeleteDirectoryAsync(string virtualPath, Cancellation
192195
try
193196
{
194197
var deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName};
195-
var listObjectsRequest = new ListObjectsRequest {BucketName = bucketName, Prefix = path};
198+
var listObjectsRequest = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
196199

197-
var response = await client.ListObjectsAsync(listObjectsRequest, cancellationToken);
200+
var response = await client.ListObjectsV2Async(listObjectsRequest, cancellationToken);
198201

199202
foreach (S3Object entry in response.S3Objects)
200203
{
@@ -252,8 +255,6 @@ public override async Task WriteFileAsync(string virtualPath, Stream contents, b
252255

253256
try
254257
{
255-
contents.Seek(0, SeekOrigin.Begin);
256-
257258
var request = new PutObjectRequest
258259
{
259260
InputStream = contents,

FileSystem.Adapters.AmazonS3/src/ModelFactory.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
using System;
22
using System.Linq;
33
using Amazon.S3.Model;
4+
using SharpGrip.FileSystem.Extensions;
45
using SharpGrip.FileSystem.Models;
56

67
namespace SharpGrip.FileSystem.Adapters.AmazonS3
78
{
89
public static class ModelFactory
910
{
10-
public static FileModel CreateFile(GetObjectResponse file, string virtualPath)
11+
public static FileModel CreateFile(GetObjectResponse file, string path, string virtualPath)
1112
{
1213
return new FileModel
1314
{
1415
Name = file.Key.Split('/').Last(),
15-
Path = file.Key,
16+
Path = path,
1617
VirtualPath = virtualPath,
1718
Length = file.ContentLength,
1819
LastModifiedDateTime = file.LastModified
@@ -34,7 +35,12 @@ public static FileModel CreateFile(S3Object file, string virtualPath)
3435
public static DirectoryModel CreateDirectory(S3Object directory, string virtualPath)
3536
{
3637
var pathParts = directory.Key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
37-
var name = pathParts.Last();
38+
var name = pathParts.LastOrDefault();
39+
40+
if (name == null)
41+
{
42+
name = "/";
43+
}
3844

3945
if (pathParts.Length == 1)
4046
{
@@ -43,8 +49,8 @@ public static DirectoryModel CreateDirectory(S3Object directory, string virtualP
4349

4450
return new DirectoryModel
4551
{
46-
Name = name.Substring(0, name.Length - 1),
47-
Path = directory.Key.Substring(0, name.Length - 1),
52+
Name = name,
53+
Path = directory.Key.RemoveTrailingForwardSlash(),
4854
VirtualPath = virtualPath,
4955
LastModifiedDateTime = directory.LastModified
5056
};

FileSystem/src/Adapters/Adapter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken ca
109109

110110
try
111111
{
112-
using var stream = await ReadFileStreamAsync(virtualPath, cancellationToken);
112+
var stream = await ReadFileStreamAsync(virtualPath, cancellationToken);
113113
using var memoryStream = await StreamUtilities.CopyContentsToMemoryStreamAsync(stream, cancellationToken);
114114

115115
await stream.CopyToAsync(memoryStream, AdapterConstants.DefaultMemoryStreamBufferSize, cancellationToken);
@@ -130,7 +130,6 @@ public async Task<string> ReadTextFileAsync(string virtualPath, CancellationToke
130130
{
131131
using var stream = await ReadFileStreamAsync(virtualPath, cancellationToken);
132132
using var streamReader = new StreamReader(stream);
133-
stream.Position = 0;
134133

135134
return await streamReader.ReadToEndAsync();
136135
}

FileSystem/src/Utilities/PathUtilities.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static string GetPrefix(string virtualPath)
2727
/// <returns>The path.</returns>
2828
public static string GetPath(string virtualPath, string rootPath)
2929
{
30+
if (string.IsNullOrWhiteSpace(rootPath))
31+
{
32+
return string.Join(PathSeparator, ResolvePrefixAndPath(virtualPath)[1]);
33+
}
34+
3035
return string.Join(PathSeparator, rootPath, ResolvePrefixAndPath(virtualPath)[1]);
3136
}
3237

FileSystem/src/Utilities/StreamUtilities.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public static class StreamUtilities
1010
public static async Task<MemoryStream> CopyContentsToMemoryStreamAsync(Stream contents, CancellationToken cancellationToken = default)
1111
{
1212
var memoryStream = new MemoryStream();
13-
contents.Seek(0, SeekOrigin.Begin);
1413
await contents.CopyToAsync(memoryStream, AdapterConstants.DefaultMemoryStreamBufferSize, cancellationToken);
1514

1615
return memoryStream;

0 commit comments

Comments
 (0)