Files
YouTube-Manager/Manager.App/Components/Dialogs/AccountDialog.razor.cs

161 lines
5.3 KiB
C#

using System.Net;
using System.Net.Mime;
using System.Text;
using Manager.App.Models.Library;
using Manager.YouTube;
using Manager.YouTube.Constants;
using Manager.YouTube.Parsers;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using MudBlazor;
namespace Manager.App.Components.Dialogs
{
public partial class AccountDialog : ComponentBase
{
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
[Parameter] public string DefaultUserAgent { get; set; } = "";
private ClientChannel? ClientChannel { get; set; }
private CookieCollection ImportCookies { get; set; } = [];
private IEnumerable<string> MissingCookies => CookieConstants.RequiredCookiesNames.Where(req => !ImportCookies.Select(c => c.Name).ToHashSet().Contains(req)).ToList();
private bool _isLoading;
private AccountImportSteps _steps = AccountImportSteps.Authenticate;
private string _cookieText = "";
private bool CanSave()
{
return ClientChannel?.YouTubeClient?.State?.LoggedIn == true;
}
private bool CanContinue()
{
switch (_steps)
{
case AccountImportSteps.Authenticate:
if (ImportCookies.Count != 0)
{
return true;
}
break;
case AccountImportSteps.Validate:
if (ClientChannel?.YouTubeClient?.State?.LoggedIn == true)
{
return true;
}
break;
}
return false;
}
private async Task OnNextStep()
{
switch (_steps)
{
case AccountImportSteps.Authenticate:
if (CanContinue())
{
_steps = AccountImportSteps.Validate;
await BuildClient();
await InvokeAsync(StateHasChanged);
return;
}
SnackbarService.Add("Cannot continue!", Severity.Warning);
break;
case AccountImportSteps.Validate:
if (CanSave())
{
MudDialog?.Close(DialogResult.Ok(ClientChannel));
await InvokeAsync(StateHasChanged);
return;
}
SnackbarService.Add("Cannot save!", Severity.Warning);
break;
}
await InvokeAsync(StateHasChanged);
}
private async Task UploadFiles(IBrowserFile? file)
{
if (file == null)
{
SnackbarService.Add("File is null!", Severity.Error);
return;
}
if (file.ContentType != MediaTypeNames.Text.Plain)
{
SnackbarService.Add($"File uploaded with unsupported content type: {file.ContentType}", Severity.Warning);
return;
}
_isLoading = true;
var streamReader = new StreamReader(file.OpenReadStream(), Encoding.UTF8);
_cookieText = await streamReader.ReadToEndAsync();
_isLoading = false;
await InvokeAsync(StateHasChanged);
}
private async Task ParseCookies()
{
if (string.IsNullOrEmpty(_cookieText))
{
return;
}
try
{
ImportCookies.Clear();
var parsedCookies = await CookieTxtParser.ParseAsync(new MemoryStream(Encoding.UTF8.GetBytes(_cookieText)), CookieConstants.RequiredCookiesNames.ToHashSet());
ImportCookies.Add(parsedCookies);
_cookieText = string.Empty;
}
catch (Exception e)
{
SnackbarService.Add($"Parsing cookies failed: {e.Message}", Severity.Error);
}
StateHasChanged();
}
private void ClearPreparedClient()
{
ClientChannel?.YouTubeClient?.Dispose();
ClientChannel = null;
ImportCookies.Clear();
_steps = AccountImportSteps.Authenticate;
StateHasChanged();
}
private async Task BuildClient()
{
_isLoading = true;
ClientChannel = new ClientChannel();
var clientResult = await YouTubeClient.CreateAsync(ImportCookies, DefaultUserAgent);
if (clientResult.IsSuccess)
{
ClientChannel.YouTubeClient = clientResult.Value;
}
if (ClientChannel.YouTubeClient == null)
{
SnackbarService.Add("Failed to get client!", Severity.Error);
_isLoading = false;
return;
}
var accountResult = await ClientChannel.YouTubeClient.GetChannelByIdAsync(ClientChannel.YouTubeClient.Id);
if (accountResult.IsSuccess)
{
ClientChannel.Channel = accountResult.Value;
}
_isLoading = false;
await InvokeAsync(StateHasChanged);
}
}
public enum AccountImportSteps
{
Authenticate,
Validate
}
}