impl/r2 #10
@ -10,6 +10,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AWSSDK.S3" Version="3.7.307.24" />
|
||||
<PackageReference Include="FubarCoder.RestSharp.Portable.Core" Version="4.0.8" />
|
||||
<PackageReference Include="FubarCoder.RestSharp.Portable.HttpClient" Version="4.0.8" />
|
||||
<PackageReference Include="GoFileSharp" Version="1.0.2" />
|
||||
|
66
EftPatchHelper/EftPatchHelper/Helpers/R2Helper.cs
Normal file
66
EftPatchHelper/EftPatchHelper/Helpers/R2Helper.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System.Net;
|
||||
using Amazon.Runtime;
|
||||
using Amazon.S3;
|
||||
using EftPatchHelper.Model;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace EftPatchHelper.Helpers;
|
||||
|
||||
public class R2Helper
|
||||
{
|
||||
private readonly IAmazonS3? _client;
|
||||
public bool IsReady => _client != null;
|
||||
|
||||
private Settings _settings;
|
||||
|
||||
public R2Helper(Settings settings, Options options)
|
||||
{
|
||||
_settings = settings;
|
||||
|
||||
if (_settings.UsingR2() && options.UplaodToR2)
|
||||
{
|
||||
var creds = new BasicAWSCredentials(_settings.R2AccessKeyId, _settings.R2SecretKeyId);
|
||||
_client = new AmazonS3Client(creds, new AmazonS3Config
|
||||
{
|
||||
ServiceURL = _settings.R2ServiceUrl,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all content in the bucket
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> ClearBucketAsync()
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[blue]Getting bucket contents: {_settings.R2BucketName}[/]");
|
||||
var listBucketReponse = await _client.ListObjectsAsync(_settings.R2BucketName);
|
||||
|
||||
if (listBucketReponse.HttpStatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
AnsiConsole.MarkupLine("[red]failed to get bucket contents[/]");
|
||||
return false;
|
||||
}
|
||||
|
||||
AnsiConsole.MarkupLine("[blue]Removing old content");
|
||||
foreach (var s3Object in listBucketReponse.S3Objects)
|
||||
{
|
||||
var deleteRepsonse = await _client.DeleteObjectAsync(_settings.R2BucketName, s3Object.Key);
|
||||
|
||||
if (deleteRepsonse.HttpStatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[red]failed to delete {_settings.R2BucketName}::{s3Object.Key}[/]");
|
||||
return false;
|
||||
}
|
||||
|
||||
AnsiConsole.MarkupLine($"[green]{_settings.R2BucketName}::{s3Object.Key} removed[/]");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> UplaodToBucketAsync(FileInfo file, IProgress<double> progress = null)
|
||||
{
|
||||
// todo: this
|
||||
}
|
||||
}
|
@ -39,6 +39,8 @@ namespace EftPatchHelper.Model
|
||||
/// </summary>
|
||||
public bool UploadToMega = false;
|
||||
|
||||
public bool UplaodToR2 = false;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not to upload to all sftp site listing
|
||||
/// </summary>
|
||||
|
62
EftPatchHelper/EftPatchHelper/Model/R2Upload.cs
Normal file
62
EftPatchHelper/EftPatchHelper/Model/R2Upload.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using System.Net;
|
||||
using Amazon.Runtime;
|
||||
using Amazon.S3;
|
||||
using Amazon.S3.Model;
|
||||
using EftPatchHelper.Interfaces;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace EftPatchHelper.Model;
|
||||
|
||||
public class R2Upload : IFileUpload
|
||||
{
|
||||
public string DisplayName { get; set; }
|
||||
public string ServiceName { get; set; }
|
||||
public string HubEntryText { get; set; }
|
||||
public FileInfo UploadFileInfo { get; }
|
||||
public bool AddHubEntry { get; }
|
||||
|
||||
private readonly string _bucketName;
|
||||
private readonly string _connectedDomainUrl;
|
||||
private readonly IAmazonS3 _s3Client;
|
||||
|
||||
public R2Upload(string connectedDomainUrl, string serviceUrl, string accessKey, string secretKey, string bucketName)
|
||||
{
|
||||
_bucketName = bucketName;
|
||||
_connectedDomainUrl = connectedDomainUrl;
|
||||
|
||||
var creds = new BasicAWSCredentials(accessKey, secretKey);
|
||||
_s3Client = new AmazonS3Client(creds, new AmazonS3Config
|
||||
{
|
||||
ServiceURL = serviceUrl,
|
||||
});
|
||||
|
||||
AddHubEntry = false;
|
||||
}
|
||||
|
||||
public string GetLink()
|
||||
{
|
||||
return $"{_connectedDomainUrl}/{UploadFileInfo.Name}";
|
||||
}
|
||||
|
||||
public async Task<bool> UploadAsync(IProgress<double>? progress = null)
|
||||
{
|
||||
var uploadRequest = new PutObjectRequest
|
||||
{
|
||||
BucketName = _bucketName,
|
||||
FilePath = UploadFileInfo.FullName,
|
||||
DisablePayloadSigning = true,
|
||||
};
|
||||
|
||||
if (progress != null)
|
||||
{
|
||||
uploadRequest.StreamTransferProgress = (sender, progressArgs) =>
|
||||
{
|
||||
progress.Report(progressArgs.PercentDone);
|
||||
};
|
||||
}
|
||||
|
||||
var uploadResponse = await _s3Client.PutObjectAsync(uploadRequest);
|
||||
|
||||
return uploadResponse.HttpStatusCode == HttpStatusCode.OK;
|
||||
}
|
||||
}
|
@ -57,6 +57,21 @@ namespace EftPatchHelper.Model
|
||||
[JsonPropertyName("goFileFolderId")]
|
||||
public string GoFileFolderId { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("r2ConnectedDomainUrl")]
|
||||
public string R2ConnectedDomainUrl { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("r2ServiceUrl")]
|
||||
public string R2ServiceUrl { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("r2Bucketname")]
|
||||
public string R2BucketName { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("r2AccessKeyId")]
|
||||
public string R2AccessKeyId { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("r2SecretKeyId")]
|
||||
public string R2SecretKeyId { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("sftpUploads")]
|
||||
public List<SftpUploadInfo> SftpUploads { get; set; } = new();
|
||||
|
||||
@ -114,6 +129,17 @@ namespace EftPatchHelper.Model
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool UsingR2()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(R2ConnectedDomainUrl)) return false;
|
||||
if (string.IsNullOrWhiteSpace(R2ServiceUrl)) return false;
|
||||
if (string.IsNullOrWhiteSpace(R2BucketName)) return false;
|
||||
if (string.IsNullOrWhiteSpace(R2AccessKeyId)) return false;
|
||||
if (string.IsNullOrWhiteSpace(R2SecretKeyId)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(TargetEftVersion)) return false;
|
||||
|
@ -20,40 +20,50 @@ namespace EftPatchHelper.Tasks
|
||||
_options = options;
|
||||
}
|
||||
|
||||
private bool UploadAsset(FileInfo file, Release release, RepositoryApi repo)
|
||||
private bool UploadMirrorList(FileInfo file)
|
||||
{
|
||||
return AnsiConsole.Status().Spinner(Spinner.Known.Point).Start("Uploading Asset", (StatusContext context) =>
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[blue]Adding release asset: {file.Name.EscapeMarkup()}[/]");
|
||||
var r2Uplaod = new R2Upload(_settings.R2ConnectedDomainUrl, _settings.R2ServiceUrl, _settings.R2AccessKeyId,
|
||||
_settings.R2SecretKeyId, _settings.R2BucketName);
|
||||
|
||||
|
||||
|
||||
file.Refresh();
|
||||
|
||||
if (!file.Exists)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[red]File does not exist: {file.FullName}[/]");
|
||||
}
|
||||
|
||||
using var fileStream = file.OpenRead();
|
||||
|
||||
try
|
||||
{
|
||||
var attachment = repo.RepoCreateReleaseAttachment(_settings.GiteaReleaseRepoOwner, _settings.GiteaReleaseRepoName, release.Id, fileStream, file.Name);
|
||||
|
||||
AnsiConsole.MarkupLine("[green]Upload Complete[/]");
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AnsiConsole.MarkupLine("[red]Failed to upload asset[/]");
|
||||
|
||||
AnsiConsole.WriteException(ex);
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
// private bool UploadAsset(FileInfo file, Release release, RepositoryApi repo)
|
||||
// {
|
||||
// return AnsiConsole.Status().Spinner(Spinner.Known.Point).Start("Uploading Asset", (StatusContext context) =>
|
||||
// {
|
||||
// AnsiConsole.MarkupLine($"[blue]Adding release asset: {file.Name.EscapeMarkup()}[/]");
|
||||
//
|
||||
// file.Refresh();
|
||||
//
|
||||
// if (!file.Exists)
|
||||
// {
|
||||
// AnsiConsole.MarkupLine($"[red]File does not exist: {file.FullName}[/]");
|
||||
// }
|
||||
//
|
||||
// using var fileStream = file.OpenRead();
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// var attachment = repo.RepoCreateReleaseAttachment(_settings.GiteaReleaseRepoOwner, _settings.GiteaReleaseRepoName, release.Id, fileStream, file.Name);
|
||||
//
|
||||
// AnsiConsole.MarkupLine("[green]Upload Complete[/]");
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// AnsiConsole.MarkupLine("[red]Failed to upload asset[/]");
|
||||
//
|
||||
// AnsiConsole.WriteException(ex);
|
||||
//
|
||||
// return false;
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
private Release? MakeRelease(RepositoryApi repo)
|
||||
{
|
||||
AnsiConsole.Write("Adding release to gitea ... ");
|
||||
@ -111,7 +121,7 @@ namespace EftPatchHelper.Tasks
|
||||
|
||||
var release = MakeRelease(repo).ValidateOrExit<Release>();
|
||||
|
||||
UploadAsset(fileInfo, release, repo);
|
||||
//UploadAsset(fileInfo, release, repo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -81,6 +81,11 @@ namespace EftPatchHelper.Tasks
|
||||
_options.UploadToGoFile = new ConfirmationPrompt("Upload to GoFile?").Show(AnsiConsole.Console);
|
||||
}
|
||||
|
||||
if (_settings.UsingR2())
|
||||
{
|
||||
_options.UplaodToR2 = new ConfirmationPrompt("Upload to R2?").Show(AnsiConsole.Console);
|
||||
}
|
||||
|
||||
if (_settings.SftpUploads.Count > 0)
|
||||
{
|
||||
_options.UploadToSftpSites =
|
||||
|
@ -48,6 +48,14 @@ namespace EftPatchHelper.Tasks
|
||||
AnsiConsole.WriteLine("Added GoFile");
|
||||
}
|
||||
|
||||
if (_settings.UsingR2() && _options.UplaodToR2)
|
||||
{
|
||||
var r2 = new R2Upload(_settings.R2ConnectedDomainUrl, _settings.R2ServiceUrl, _settings.R2AccessKeyId,
|
||||
_settings.R2SecretKeyId, _settings.R2BucketName);
|
||||
_fileUploads.Add(r2);
|
||||
AnsiConsole.WriteLine($"Added R2: {_settings.R2BucketName}");
|
||||
}
|
||||
|
||||
if (_settings.SftpUploads.Count > 0 && _options.UploadToSftpSites)
|
||||
{
|
||||
foreach (var sftpInfo in _settings.SftpUploads)
|
||||
|
Loading…
x
Reference in New Issue
Block a user