-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/new-adapter-google-cloud-storage
- Loading branch information
Showing
8 changed files
with
320 additions
and
14 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
FileSystem.Adapters.GoogleCloudStorage/FileSystem.Adapters.GoogleCloudStorage.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>SharpGrip.FileSystem.Adapters.GoogleCloudStorage</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>SharpGrip.FileSystem.Adapters.GoogleCloudStorage</AssemblyName> | ||
<PackageId>SharpGrip.FileSystem.Adapters.GoogleCloudStorage</PackageId> | ||
<Title>SharpGrip FileSystem Google Cloud Storage adapter</Title> | ||
<Description>The SharpGrip FileSystem Google Cloud Storage adapter.</Description> | ||
<PackageTags>sharpgrip;file-system;google-cloud-storage</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\README.md" Pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Google.Cloud.Storage.V1" Version="4.7.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FileSystem\FileSystem.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
222 changes: 222 additions & 0 deletions
222
FileSystem.Adapters.GoogleCloudStorage/src/GoogleCloudStorageAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Google.Cloud.Storage.V1; | ||
using SharpGrip.FileSystem.Exceptions; | ||
using SharpGrip.FileSystem.Extensions; | ||
using SharpGrip.FileSystem.Models; | ||
using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException; | ||
using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException; | ||
|
||
namespace SharpGrip.FileSystem.Adapters.GoogleCloudStorage | ||
{ | ||
public class GoogleCloudStorageAdapter : Adapter<GoogleCloudStorageAdapterConfiguration, string, string> | ||
{ | ||
private readonly StorageClient client; | ||
private readonly string bucketName; | ||
|
||
public GoogleCloudStorageAdapter(string prefix, string rootPath, StorageClient client, string bucketName, Action<GoogleCloudStorageAdapterConfiguration>? configuration = null) | ||
: base(prefix, rootPath, configuration) | ||
{ | ||
this.client = client; | ||
this.bucketName = bucketName; | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
client.Dispose(); | ||
} | ||
|
||
public override async Task ConnectAsync(CancellationToken cancellationToken = default) | ||
{ | ||
Logger.LogStartConnectingAdapter(this); | ||
await Task.CompletedTask; | ||
Logger.LogFinishedConnectingAdapter(this); | ||
} | ||
|
||
public override async Task<IFile> GetFileAsync(string virtualPath, CancellationToken cancellationToken = default) | ||
{ | ||
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().RemoveTrailingForwardSlash(); | ||
|
||
try | ||
{ | ||
var file = await client.GetObjectAsync(bucketName, path, new GetObjectOptions(), cancellationToken); | ||
|
||
if (file == null) | ||
{ | ||
throw new FileNotFoundException(path, Prefix); | ||
} | ||
|
||
return ModelFactory.CreateFile(file, path, virtualPath); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw Exception(exception); | ||
} | ||
} | ||
|
||
public override async Task<IDirectory> GetDirectoryAsync(string virtualPath, CancellationToken cancellationToken = default) | ||
{ | ||
var path = GetPath(virtualPath).EnsureLeadingForwardSlash().EnsureTrailingForwardSlash(); | ||
var parentPath = GetParentPathPart(path).EnsureTrailingForwardSlash(); | ||
|
||
try | ||
{ | ||
var request = client.Service.Objects.List(bucketName); | ||
|
||
request.Prefix = parentPath == "/" ? null : parentPath; | ||
request.Delimiter = "/"; | ||
|
||
do | ||
{ | ||
var objects = await request.ExecuteAsync(cancellationToken: cancellationToken); | ||
|
||
foreach (var directoryName in objects.Prefixes) | ||
{ | ||
var directoryPath = parentPath + directoryName; | ||
|
||
if (directoryPath == path) | ||
{ | ||
return ModelFactory.CreateDirectory(directoryName.RemoveTrailingForwardSlash(), directoryPath, GetVirtualPath(directoryPath)); | ||
} | ||
} | ||
|
||
request.PageToken = objects.NextPageToken; | ||
} while (request.PageToken != null); | ||
|
||
throw new DirectoryNotFoundException(path, Prefix); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw Exception(exception); | ||
} | ||
} | ||
|
||
public override async Task<IEnumerable<IFile>> GetFilesAsync(string virtualPath = "", CancellationToken cancellationToken = default) | ||
{ | ||
await GetDirectoryAsync(virtualPath, cancellationToken); | ||
|
||
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash(); | ||
|
||
try | ||
{ | ||
var files = new List<IFile>(); | ||
|
||
var request = client.Service.Objects.List(bucketName); | ||
|
||
request.Prefix = path == "/" ? null : path; | ||
request.Delimiter = "/"; | ||
|
||
do | ||
{ | ||
var objects = await request.ExecuteAsync(cancellationToken: cancellationToken); | ||
|
||
foreach (var file in objects.Items) | ||
{ | ||
var filePath = path + file.Name; | ||
|
||
files.Add(ModelFactory.CreateFile(file, filePath.RemoveTrailingForwardSlash(), GetVirtualPath(filePath))); | ||
} | ||
|
||
request.PageToken = objects.NextPageToken; | ||
} while (request.PageToken != null); | ||
|
||
return files; | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw Exception(exception); | ||
} | ||
} | ||
|
||
public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(string virtualPath = "", CancellationToken cancellationToken = default) | ||
{ | ||
await GetDirectoryAsync(virtualPath, cancellationToken); | ||
|
||
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash(); | ||
|
||
try | ||
{ | ||
var directories = new List<IDirectory>(); | ||
|
||
var request = client.Service.Objects.List(bucketName); | ||
|
||
request.Prefix = path == "/" ? null : path; | ||
request.Delimiter = "/"; | ||
|
||
do | ||
{ | ||
var objects = await request.ExecuteAsync(cancellationToken: cancellationToken); | ||
|
||
foreach (var directoryName in objects.Prefixes) | ||
{ | ||
var directoryPath = path + directoryName; | ||
|
||
directories.Add(ModelFactory.CreateDirectory(directoryName.RemoveTrailingForwardSlash(), directoryPath.EnsureTrailingForwardSlash(), GetVirtualPath(directoryPath))); | ||
} | ||
|
||
request.PageToken = objects.NextPageToken; | ||
} while (request.PageToken != null); | ||
|
||
return directories; | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw Exception(exception); | ||
} | ||
} | ||
|
||
public override async Task CreateDirectoryAsync(string virtualPath, CancellationToken cancellationToken = default) | ||
{ | ||
if (await DirectoryExistsAsync(virtualPath, cancellationToken)) | ||
{ | ||
throw new DirectoryExistsException(GetPath(virtualPath), Prefix); | ||
} | ||
|
||
var path = GetPath(virtualPath).RemoveLeadingForwardSlash().EnsureTrailingForwardSlash(); | ||
|
||
try | ||
{ | ||
// client.Service. | ||
|
||
await client.UploadObjectAsync(bucketName, GetLastPathPart(path).EnsureTrailingForwardSlash(), null, Stream.Null, cancellationToken: cancellationToken); | ||
} | ||
catch (Exception exception) | ||
{ | ||
throw Exception(exception); | ||
} | ||
} | ||
|
||
public override async Task DeleteDirectoryAsync(string virtualPath, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override async Task DeleteFileAsync(string virtualPath, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override async Task<Stream> ReadFileStreamAsync(string virtualPath, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override async Task WriteFileAsync(string virtualPath, Stream contents, bool overwrite = false, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override Exception Exception(Exception exception) | ||
{ | ||
if (exception is FileSystemException) | ||
{ | ||
return exception; | ||
} | ||
|
||
return new AdapterRuntimeException(exception); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
FileSystem.Adapters.GoogleCloudStorage/src/GoogleCloudStorageAdapterConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using SharpGrip.FileSystem.Configuration; | ||
|
||
namespace SharpGrip.FileSystem.Adapters.GoogleCloudStorage | ||
{ | ||
public class GoogleCloudStorageAdapterConfiguration : AdapterConfiguration | ||
{ | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
FileSystem.Adapters.GoogleCloudStorage/src/ModelFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Google.Apis.Storage.v1.Data; | ||
using SharpGrip.FileSystem.Models; | ||
|
||
namespace SharpGrip.FileSystem.Adapters.GoogleCloudStorage | ||
{ | ||
public static class ModelFactory | ||
{ | ||
public static FileModel CreateFile(Object file, string path, string virtualPath) | ||
{ | ||
return new FileModel | ||
{ | ||
Name = file.Name, | ||
Path = path, | ||
VirtualPath = virtualPath, | ||
Length = (long?) file.Size, | ||
LastModifiedDateTime = file.UpdatedDateTimeOffset?.DateTime, | ||
CreatedDateTime = file.TimeCreatedDateTimeOffset?.DateTime | ||
}; | ||
} | ||
|
||
public static DirectoryModel CreateDirectory(string name, string path, string virtualPath) | ||
{ | ||
return new DirectoryModel | ||
{ | ||
Name = name, | ||
Path = path, | ||
VirtualPath = virtualPath | ||
}; | ||
} | ||
|
||
public static DirectoryModel CreateDirectory(Object directory, string path, string virtualPath) | ||
{ | ||
return new DirectoryModel | ||
{ | ||
Name = directory.Name, | ||
Path = path, | ||
VirtualPath = virtualPath, | ||
LastModifiedDateTime = directory.UpdatedDateTimeOffset?.DateTime, | ||
CreatedDateTime = directory.TimeCreatedDateTimeOffset?.DateTime | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.