This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code cleanup + fix upload param validation
- Loading branch information
Showing
9 changed files
with
191 additions
and
149 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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 | ||
}; | ||
} | ||
} | ||
} |
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.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 | ||
}; | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,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 }); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,9 @@ | ||
namespace TCC.Lib.Storage | ||
{ | ||
public class UploadResponse | ||
{ | ||
public string RemoteFilePath { get; set; } | ||
public bool IsSuccess { get; set; } | ||
public string ErrorMessage { get; set; } | ||
} | ||
} |
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.