Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Code cleanup + fix upload param validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rducom committed Jul 29, 2021
1 parent 37af6b9 commit b0abe6b
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 149 deletions.
134 changes: 0 additions & 134 deletions TCC.Lib/ObjectStorage/IObjectRemoteServer.cs

This file was deleted.

29 changes: 29 additions & 0 deletions TCC.Lib/Storage/AzureRemoteStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Azure.Storage.Blobs;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace TCC.Lib.Storage
{
public class AzureRemoteStorage : IRemoteStorage
{
private readonly BlobContainerClient _container;

public AzureRemoteStorage(BlobContainerClient container)
{
_container = container;
}

public async Task<UploadResponse> UploadAsync(string targetPath, Stream data, CancellationToken token)
{
var result = await _container.UploadBlobAsync(targetPath, data, token);
var response = result.GetRawResponse();
return new UploadResponse
{
IsSuccess = response.Status == 201,
ErrorMessage = response.ReasonPhrase,
RemoteFilePath = targetPath
};
}
}
}
43 changes: 43 additions & 0 deletions TCC.Lib/Storage/GoogleRemoteStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Google.Cloud.Storage.V1;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Object = Google.Apis.Storage.v1.Data.Object;

namespace TCC.Lib.Storage
{
public class GoogleRemoteStorage : IRemoteStorage
{
internal StorageClient Storage { get; }
internal string BucketName { get; }

public GoogleRemoteStorage(StorageClient storage, string bucketName)
{
Storage = storage;
BucketName = bucketName;
}

public async Task<UploadResponse> UploadAsync(string targetPath, Stream data, CancellationToken token)
{
try
{
Object uploaded = await Storage.UploadObjectAsync(BucketName, targetPath, null, data, cancellationToken: token);
}
catch (Exception e)
{
return new UploadResponse
{
IsSuccess = false,
RemoteFilePath = targetPath,
ErrorMessage = e.Message
};
}
return new UploadResponse
{
IsSuccess = true,
RemoteFilePath = targetPath
};
}
}
}
19 changes: 19 additions & 0 deletions TCC.Lib/Storage/IRemoteStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using TCC.Lib.Dependencies;

namespace TCC.Lib.Storage
{
public interface IRemoteStorage
{
Task<UploadResponse> UploadAsync(string targetPath, Stream data, CancellationToken token);

public async Task<UploadResponse> UploadAsync(FileInfo file, DirectoryInfo rootFolder, CancellationToken token)
{
string targetPath = file.GetRelativeTargetPathTo(rootFolder);
await using FileStream uploadFileStream = File.OpenRead(file.FullName);
return await UploadAsync(targetPath, uploadFileStream, token);
}
}
}
14 changes: 14 additions & 0 deletions TCC.Lib/Storage/NoneRemoteStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace TCC.Lib.Storage
{
public class NoneRemoteStorage : IRemoteStorage
{
public Task<UploadResponse> UploadAsync(string targetPath, Stream data, CancellationToken token)
{
return Task.FromResult(new UploadResponse { IsSuccess = true, RemoteFilePath = targetPath });
}
}
}
67 changes: 67 additions & 0 deletions TCC.Lib/Storage/RemoteStorageFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Azure.Storage.Blobs;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Storage.V1;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TCC.Lib.Options;

namespace TCC.Lib.Storage
{
public static class RemoteStorageFactory
{
public static async Task<IRemoteStorage> GetRemoteStorageAsync(this CompressOption option, ILogger logger, CancellationToken token)
{
switch (option.UploadMode)
{
case UploadMode.AzureSdk:
{
if (string.IsNullOrEmpty(option.AzBlobUrl)
|| string.IsNullOrEmpty(option.AzBlobContainer)
|| string.IsNullOrEmpty(option.AzSaS))
{
logger.LogCritical("Configuration error for azure blob upload");
return new NoneRemoteStorage();
}
var client = new BlobServiceClient(new Uri(option.AzBlobUrl + "/" + option.AzBlobContainer + "?" + option.AzSaS));
BlobContainerClient container = client.GetBlobContainerClient(option.AzBlobContainer);
return new AzureRemoteStorage(container);
}
case UploadMode.GoogleCloudStorage:
{
if (string.IsNullOrEmpty(option.GoogleStorageCredentialFile)
|| string.IsNullOrEmpty(option.GoogleStorageBucketName))
{
logger.LogCritical("Configuration error for google storage upload");
return new NoneRemoteStorage();
}
StorageClient storage = await GetGoogleStorageClient(option, token);
return new GoogleRemoteStorage(storage, option.GoogleStorageBucketName);
}
case UploadMode.None:
case null:
return new NoneRemoteStorage();
default:
throw new ArgumentOutOfRangeException();
}
}

private static async Task<StorageClient> GetGoogleStorageClient(CompressOption option, CancellationToken token)
{
GoogleCredential credential;
if (File.Exists(option.GoogleStorageCredentialFile))
{
credential = await GoogleCredential.FromFileAsync(option.GoogleStorageCredentialFile, token);
}
else
{
var decodedJson = Encoding.UTF8.GetString(Convert.FromBase64String(option.GoogleStorageCredentialFile));
credential = GoogleCredential.FromJson(decodedJson);
}
return await StorageClient.CreateAsync(credential);
}
}
}
9 changes: 9 additions & 0 deletions TCC.Lib/Storage/UploadResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TCC.Lib.Storage
{
public class UploadResponse
{
public string RemoteFilePath { get; set; }
public bool IsSuccess { get; set; }
public string ErrorMessage { get; set; }
}
}
11 changes: 4 additions & 7 deletions TCC.Lib/TarCompressCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
using TCC.Lib.Database;
using TCC.Lib.Dependencies;
using TCC.Lib.Helpers;
using TCC.Lib.ObjectStorage;
using TCC.Lib.Options;
using TCC.Lib.PrepareBlocks;
using TCC.Lib.Storage;

namespace TCC.Lib
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public async Task<OperationSummary> Compress(CompressOption option)
_logger.LogInformation("Starting compression job");
var po = ParallelizeOption(option);

IObjectStorageRemoteServer uploader = await option.CreateRemoteServerAsync(_cancellationTokenSource.Token);
IRemoteStorage uploader = await option.GetRemoteStorageAsync(_logger, _cancellationTokenSource.Token);

var operationBlocks = await buffer
.AsAsyncStream(_cancellationTokenSource.Token)
Expand Down Expand Up @@ -93,12 +93,9 @@ public async Task<OperationSummary> Compress(CompressOption option)
return ops;
}

private async Task<OperationCompressionBlock> UploadBlockInternal(IObjectStorageRemoteServer uploader, CompressOption option, OperationCompressionBlock block, CancellationToken token)
private async Task<OperationCompressionBlock> UploadBlockInternal(IRemoteStorage uploader, CompressOption option, OperationCompressionBlock block, CancellationToken token)
{
if (string.IsNullOrEmpty(option.AzBlobUrl)
|| string.IsNullOrEmpty(option.AzBlobContainer)
|| string.IsNullOrEmpty(option.AzSaS)
|| option.UploadMode == null)
if (uploader is NoneRemoteStorage)
{
return block;
}
Expand Down
Loading

0 comments on commit b0abe6b

Please sign in to comment.