nft-faucet/NftFaucetRadzen/Pages/CreateUploadPage.razor.cs

129 lines
4.8 KiB
C#
Raw Normal View History

2022-09-18 21:10:58 -05:00
using System.Text;
2022-08-28 19:05:50 -05:00
using Microsoft.AspNetCore.Components;
2022-09-18 21:10:58 -05:00
using Newtonsoft.Json;
2022-08-28 19:05:50 -05:00
using NftFaucetRadzen.Components;
using NftFaucetRadzen.Components.CardList;
2022-09-18 21:10:58 -05:00
using NftFaucetRadzen.Models;
2022-08-28 21:17:40 -05:00
using NftFaucetRadzen.Plugins;
2022-08-28 19:05:50 -05:00
using NftFaucetRadzen.Plugins.UploadPlugins;
using Radzen;
namespace NftFaucetRadzen.Pages;
public partial class CreateUploadPage : BasicComponent
{
2022-09-16 13:51:34 -05:00
[Parameter] public IToken Token { get; set; }
2022-08-30 21:09:30 -05:00
2022-08-28 19:05:50 -05:00
protected override void OnInitialized()
{
2022-09-17 20:41:56 -05:00
RefreshCards();
base.OnInitialized();
2022-08-28 19:05:50 -05:00
}
2022-08-31 11:32:23 -05:00
private CardListItem[] UploaderCards { get; set; }
2022-08-28 21:17:40 -05:00
private Guid[] SelectedUploaderIds { get; set; }
private IUploader SelectedUploader => AppState?.Storage?.Uploaders?.FirstOrDefault(x => x.Id == SelectedUploaderIds?.FirstOrDefault());
2022-09-18 19:38:34 -05:00
private bool IsUploading { get; set; }
2022-08-28 19:05:50 -05:00
2022-09-17 20:41:56 -05:00
private void RefreshCards()
{
UploaderCards = AppState.Storage.Uploaders.Select(MapCardListItem).ToArray();
}
2022-08-28 19:05:50 -05:00
private CardListItem MapCardListItem(IUploader uploader)
2022-09-17 20:41:56 -05:00
{
var configuration = uploader.GetConfiguration();
return new CardListItem
2022-08-28 19:05:50 -05:00
{
Id = uploader.Id,
ImageLocation = uploader.ImageName != null ? "./images/" + uploader.ImageName : null,
Header = uploader.Name,
2022-09-18 19:54:37 -05:00
Properties = uploader.GetProperties(),
2022-08-28 19:05:50 -05:00
IsDisabled = !uploader.IsSupported,
2022-09-18 00:02:38 -05:00
SelectionIcon = uploader.IsConfigured ? CardListItemSelectionIcon.Checkmark : CardListItemSelectionIcon.Warning,
2022-08-28 19:05:50 -05:00
Badges = new[]
{
(Settings?.RecommendedUploaders?.Contains(uploader.Id) ?? false)
? new CardListItemBadge {Style = BadgeStyle.Success, Text = "Recommended"}
: null,
!uploader.IsSupported
? new CardListItemBadge {Style = BadgeStyle.Light, Text = "Not Supported"}
: null,
}.Where(x => x != null).ToArray(),
2022-09-17 20:41:56 -05:00
Configuration = configuration == null
? null
: new CardListItemConfiguration
{
Objects = configuration.Objects,
ConfigureAction = x =>
{
var result = configuration.ConfigureAction(x);
RefreshCards();
return result;
},
},
2022-08-28 19:05:50 -05:00
};
2022-09-17 20:41:56 -05:00
}
2022-08-28 19:05:50 -05:00
private async Task OnSavePressed()
2022-08-30 21:09:30 -05:00
{
2022-09-18 19:38:34 -05:00
IsUploading = true;
RefreshMediator.NotifyStateHasChangedSafe();
2022-09-18 21:10:58 -05:00
var imageLocationResult = await SelectedUploader.Upload(Token.Image.FileName, Token.Image.FileType, Base64DataToBytes(Token.Image.FileData));
if (imageLocationResult.IsFailure)
{
IsUploading = false;
RefreshMediator.NotifyStateHasChangedSafe();
NotificationService.Notify(NotificationSeverity.Error, "Uploading image failed", imageLocationResult.Error);
return;
}
var imageLocation = imageLocationResult.Value;
var tokenMetadata = GenerateTokenMetadata(Token, imageLocation);
var tokenMetadataBytes = Encoding.UTF8.GetBytes(tokenMetadata);
var tokenLocationResult = await SelectedUploader.Upload($"{Token.Id}.json", "application/json", tokenMetadataBytes);
if (tokenLocationResult.IsFailure)
{
IsUploading = false;
RefreshMediator.NotifyStateHasChangedSafe();
NotificationService.Notify(NotificationSeverity.Error, "Uploading metadata failed", tokenLocationResult.Error);
return;
}
2022-09-18 19:38:34 -05:00
IsUploading = false;
RefreshMediator.NotifyStateHasChangedSafe();
2022-09-18 21:10:58 -05:00
NotificationService.Notify(NotificationSeverity.Success, "Upload succeeded", tokenLocationResult.Value.OriginalString);
var uploadLocation = new TokenUploadLocation
2022-08-30 21:09:30 -05:00
{
2022-09-18 21:10:58 -05:00
Id = Guid.NewGuid(),
TokenId = Token.Id,
2022-09-18 21:10:58 -05:00
Name = SelectedUploader.ShortName,
Location = tokenLocationResult.Value.OriginalString,
CreatedAt = DateTime.Now,
UploaderId = SelectedUploader.Id,
};
DialogService.Close(uploadLocation);
}
private static byte[] Base64DataToBytes(string fileData)
{
var index = fileData.IndexOf(';');
var encoded = fileData.Substring(index + 8);
return Convert.FromBase64String(encoded);
}
private static string GenerateTokenMetadata(IToken token, Uri imageLocation)
{
var tokenMetadata = new TokenMetadata
2022-08-30 21:09:30 -05:00
{
2022-09-18 21:10:58 -05:00
Name = token.Name,
Description = token.Description,
Image = imageLocation.OriginalString,
ExternalUrl = "https://darkcodi.github.io/nft-faucet/",
};
var metadataJson = JsonConvert.SerializeObject(tokenMetadata, Formatting.Indented);
return metadataJson;
2022-08-28 19:05:50 -05:00
}
}