r2-rework WIP

This commit is contained in:
IsWaffle 2024-04-26 21:17:08 -04:00
parent 9f0369bbd6
commit ec8ddeb513
8 changed files with 211 additions and 31 deletions

View File

@ -10,6 +10,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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.Core" Version="4.0.8" />
<PackageReference Include="FubarCoder.RestSharp.Portable.HttpClient" Version="4.0.8" /> <PackageReference Include="FubarCoder.RestSharp.Portable.HttpClient" Version="4.0.8" />
<PackageReference Include="GoFileSharp" Version="1.0.2" /> <PackageReference Include="GoFileSharp" Version="1.0.2" />

View 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
}
}

View File

@ -39,6 +39,8 @@ namespace EftPatchHelper.Model
/// </summary> /// </summary>
public bool UploadToMega = false; public bool UploadToMega = false;
public bool UplaodToR2 = false;
/// <summary> /// <summary>
/// Whether or not to upload to all sftp site listing /// Whether or not to upload to all sftp site listing
/// </summary> /// </summary>

View 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;
}
}

View File

@ -57,6 +57,21 @@ namespace EftPatchHelper.Model
[JsonPropertyName("goFileFolderId")] [JsonPropertyName("goFileFolderId")]
public string GoFileFolderId { get; set; } = ""; 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")] [JsonPropertyName("sftpUploads")]
public List<SftpUploadInfo> SftpUploads { get; set; } = new(); public List<SftpUploadInfo> SftpUploads { get; set; } = new();
@ -114,6 +129,17 @@ namespace EftPatchHelper.Model
return true; 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() public bool Validate()
{ {
if (string.IsNullOrWhiteSpace(TargetEftVersion)) return false; if (string.IsNullOrWhiteSpace(TargetEftVersion)) return false;

View File

@ -20,40 +20,50 @@ namespace EftPatchHelper.Tasks
_options = options; _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) => var r2Uplaod = new R2Upload(_settings.R2ConnectedDomainUrl, _settings.R2ServiceUrl, _settings.R2AccessKeyId,
{ _settings.R2SecretKeyId, _settings.R2BucketName);
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(); return true;
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 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) private Release? MakeRelease(RepositoryApi repo)
{ {
AnsiConsole.Write("Adding release to gitea ... "); AnsiConsole.Write("Adding release to gitea ... ");
@ -111,7 +121,7 @@ namespace EftPatchHelper.Tasks
var release = MakeRelease(repo).ValidateOrExit<Release>(); var release = MakeRelease(repo).ValidateOrExit<Release>();
UploadAsset(fileInfo, release, repo); //UploadAsset(fileInfo, release, repo);
} }
} }
} }

View File

@ -81,6 +81,11 @@ namespace EftPatchHelper.Tasks
_options.UploadToGoFile = new ConfirmationPrompt("Upload to GoFile?").Show(AnsiConsole.Console); _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) if (_settings.SftpUploads.Count > 0)
{ {
_options.UploadToSftpSites = _options.UploadToSftpSites =

View File

@ -48,6 +48,14 @@ namespace EftPatchHelper.Tasks
AnsiConsole.WriteLine("Added GoFile"); 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) if (_settings.SftpUploads.Count > 0 && _options.UploadToSftpSites)
{ {
foreach (var sftpInfo in _settings.SftpUploads) foreach (var sftpInfo in _settings.SftpUploads)