Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Amazon.S3;
using Amazon.S3.Model;
using SharpGrip.FileSystem.Exceptions;
using SharpGrip.FileSystem.Extensions;
using SharpGrip.FileSystem.Models;
using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException;
using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException;
Expand Down Expand Up @@ -41,7 +42,7 @@ public override async Task<IFile> GetFileAsync(string virtualPath, CancellationT
{
using var response = await client.GetObjectAsync(bucketName, path, cancellationToken);

return ModelFactory.CreateFile(response, virtualPath);
return ModelFactory.CreateFile(response, path, virtualPath);
}
catch (AmazonS3Exception exception)
{
Expand Down Expand Up @@ -69,15 +70,12 @@ public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, Can

try
{
var prefix = "";
var pathParts = GetPathParts(path);

if (pathParts.Length > 1)
if (path == "/")
{
prefix = string.Join("/", pathParts.Take(pathParts.Length - 1)) + "/";
return ModelFactory.CreateDirectory(new S3Object {Key = "/"}, virtualPath);
}

var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = prefix};
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
var response = await client.ListObjectsV2Async(request, cancellationToken);

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

if (!path.EndsWith("/"))
{
path += "/";
}

try
{
var request = new ListObjectsV2Request {BucketName = bucketName, Prefix = path};
Expand Down Expand Up @@ -145,7 +148,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string v

foreach (var item in response.S3Objects)
{
var itemName = item.Key.Substring(0, item.Key.Length - path.Length);
var itemName = item.Key.Substring(path.Length).RemoveLeadingForwardSlash();

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

var response = await client.ListObjectsAsync(listObjectsRequest, cancellationToken);
var response = await client.ListObjectsV2Async(listObjectsRequest, cancellationToken);

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

try
{
contents.Seek(0, SeekOrigin.Begin);

var request = new PutObjectRequest
{
InputStream = contents,
Expand Down
16 changes: 11 additions & 5 deletions FileSystem.Adapters.AmazonS3/src/ModelFactory.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System;
using System.Linq;
using Amazon.S3.Model;
using SharpGrip.FileSystem.Extensions;
using SharpGrip.FileSystem.Models;

namespace SharpGrip.FileSystem.Adapters.AmazonS3
{
public static class ModelFactory
{
public static FileModel CreateFile(GetObjectResponse file, string virtualPath)
public static FileModel CreateFile(GetObjectResponse file, string path, string virtualPath)
{
return new FileModel
{
Name = file.Key.Split('/').Last(),
Path = file.Key,
Path = path,
VirtualPath = virtualPath,
Length = file.ContentLength,
LastModifiedDateTime = file.LastModified
Expand All @@ -34,7 +35,12 @@ public static FileModel CreateFile(S3Object file, string virtualPath)
public static DirectoryModel CreateDirectory(S3Object directory, string virtualPath)
{
var pathParts = directory.Key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
var name = pathParts.Last();
var name = pathParts.LastOrDefault();

if (name == null)
{
name = "/";
}

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

return new DirectoryModel
{
Name = name.Substring(0, name.Length - 1),
Path = directory.Key.Substring(0, name.Length - 1),
Name = name,
Path = directory.Key.RemoveTrailingForwardSlash(),
VirtualPath = virtualPath,
LastModifiedDateTime = directory.LastModified
};
Expand Down
3 changes: 1 addition & 2 deletions FileSystem/src/Adapters/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async Task<byte[]> ReadFileAsync(string virtualPath, CancellationToken ca

try
{
using var stream = await ReadFileStreamAsync(virtualPath, cancellationToken);
var stream = await ReadFileStreamAsync(virtualPath, cancellationToken);
using var memoryStream = await StreamUtilities.CopyContentsToMemoryStreamAsync(stream, cancellationToken);

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

return await streamReader.ReadToEndAsync();
}
Expand Down
5 changes: 5 additions & 0 deletions FileSystem/src/Utilities/PathUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static string GetPrefix(string virtualPath)
/// <returns>The path.</returns>
public static string GetPath(string virtualPath, string rootPath)
{
if (string.IsNullOrWhiteSpace(rootPath))
{
return string.Join(PathSeparator, ResolvePrefixAndPath(virtualPath)[1]);
}

return string.Join(PathSeparator, rootPath, ResolvePrefixAndPath(virtualPath)[1]);
}

Expand Down
1 change: 0 additions & 1 deletion FileSystem/src/Utilities/StreamUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public static class StreamUtilities
public static async Task<MemoryStream> CopyContentsToMemoryStreamAsync(Stream contents, CancellationToken cancellationToken = default)
{
var memoryStream = new MemoryStream();
contents.Seek(0, SeekOrigin.Begin);
await contents.CopyToAsync(memoryStream, AdapterConstants.DefaultMemoryStreamBufferSize, cancellationToken);

return memoryStream;
Expand Down
Loading