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

66 lines
1.6 KiB
C#
Raw Normal View History

2022-08-30 21:54:12 -05:00
using MimeTypes;
2022-09-16 13:51:34 -05:00
using NftFaucetRadzen.Components;
2022-08-28 01:04:38 -05:00
using NftFaucetRadzen.Models;
2022-08-30 21:09:30 -05:00
using NftFaucetRadzen.Plugins;
2022-08-28 00:40:10 -05:00
namespace NftFaucetRadzen.Pages;
2022-09-29 18:31:14 -05:00
public partial class CreateTokenDialog : BasicComponent
2022-08-28 00:40:10 -05:00
{
2022-08-28 01:04:38 -05:00
private NewFileModel Model { get; set; } = new NewFileModel();
private bool ModelIsValid => IsValid();
private void OnSavePressed()
{
if (!IsValid())
return;
2022-08-30 21:09:30 -05:00
var token = new Token
{
Id = Guid.NewGuid(),
Name = Model.Name,
Description = Model.Description,
CreatedAt = DateTime.Now,
Image = new TokenMedia
{
FileName = Model.FileName,
2022-08-30 21:54:12 -05:00
FileType = DetermineFileType(Model.FileName),
2022-08-30 21:09:30 -05:00
FileData = Model.FileData,
2022-08-30 21:54:12 -05:00
FileSize = Model.FileSize!.Value,
2022-08-30 21:09:30 -05:00
},
};
DialogService.Close(token);
2022-08-28 01:04:38 -05:00
}
2022-08-30 21:54:12 -05:00
private string DetermineFileType(string fileName)
{
var extension = fileName.Split('.', StringSplitOptions.RemoveEmptyEntries).Last();
if (!MimeTypeMap.TryGetMimeType(extension, out var mimeType))
{
return "application/octet-stream";
}
return mimeType;
}
2022-08-28 01:04:38 -05:00
private bool IsValid()
{
if (string.IsNullOrWhiteSpace(Model.Name))
return false;
if (string.IsNullOrWhiteSpace(Model.Description))
return false;
if (string.IsNullOrEmpty(Model.FileData))
return false;
if (string.IsNullOrEmpty(Model.FileName))
return false;
if (Model.FileSize is null or 0)
return false;
return true;
}
2022-08-28 00:40:10 -05:00
}