Compare commits
40 Commits
aeb9adf930
...
developmen
Author | SHA1 | Date | |
---|---|---|---|
|
79ea2badf1 | ||
|
5250b9f3f9 | ||
|
9e173258ed | ||
|
ab532ac6dc | ||
|
8a64d6fc64 | ||
|
0056a14f79 | ||
|
e82736a45f | ||
|
0f83cf1ddc | ||
|
b1e5b0dc68 | ||
|
9be6f5be89 | ||
|
ef6ca0ee07 | ||
|
03631cd0c8 | ||
|
9ff4fcded2 | ||
|
2593d02a73 | ||
|
d183803390 | ||
|
a5e55e563e | ||
|
b3861b7cd9 | ||
|
680b6d2cc9 | ||
|
b2c9fc2c52 | ||
|
b2c6003203 | ||
|
3db61b599d | ||
|
c528ad9bb3 | ||
|
d0eca248bb | ||
|
fa0c617c9a | ||
|
f334c87fbb | ||
|
55322f8792 | ||
|
92e5bb7f1f | ||
|
431a103fac | ||
|
a8cfbbe0db | ||
|
f7bfee5de2 | ||
|
dd19fc27d9 | ||
|
4bee6e6d35 | ||
|
a8b0291ebf | ||
|
d98a99d145 | ||
|
c30e503642 | ||
|
dbf6938a7e | ||
|
0ab0a029c4 | ||
|
2f018d131e | ||
|
d501015b82 | ||
|
98fe4b9391 |
21
.gitignore
vendored
21
.gitignore
vendored
@@ -307,10 +307,6 @@ node_modules/
|
|||||||
*.dsw
|
*.dsw
|
||||||
*.dsp
|
*.dsp
|
||||||
|
|
||||||
# Visual Studio 6 technical files
|
|
||||||
*.ncb
|
|
||||||
*.aps
|
|
||||||
|
|
||||||
# Visual Studio LightSwitch build output
|
# Visual Studio LightSwitch build output
|
||||||
**/*.HTMLClient/GeneratedArtifacts
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
**/*.DesktopClient/GeneratedArtifacts
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
@@ -404,14 +400,8 @@ FodyWeavers.xsd
|
|||||||
*.sln.iml
|
*.sln.iml
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
##
|
|
||||||
## Visual studio for Mac
|
|
||||||
##
|
|
||||||
|
|
||||||
|
|
||||||
# globs
|
# globs
|
||||||
Makefile.in
|
Makefile.in
|
||||||
*.userprefs
|
|
||||||
*.usertasks
|
*.usertasks
|
||||||
config.make
|
config.make
|
||||||
config.status
|
config.status
|
||||||
@@ -470,15 +460,12 @@ ehthumbs_vista.db
|
|||||||
# Recycle Bin used on file shares
|
# Recycle Bin used on file shares
|
||||||
$RECYCLE.BIN/
|
$RECYCLE.BIN/
|
||||||
|
|
||||||
# Windows Installer files
|
|
||||||
*.cab
|
|
||||||
*.msi
|
|
||||||
*.msix
|
|
||||||
*.msm
|
|
||||||
*.msp
|
|
||||||
|
|
||||||
# Windows shortcuts
|
# Windows shortcuts
|
||||||
*.lnk
|
*.lnk
|
||||||
|
|
||||||
# Vim temporary swap files
|
# Vim temporary swap files
|
||||||
*.swp
|
*.swp
|
||||||
|
|
||||||
|
# Manager.App
|
||||||
|
[Ll]ibrary/
|
||||||
|
[Cc]ache/
|
||||||
|
@@ -2,21 +2,23 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>Application</title>
|
<title>YouTube Manager server</title>
|
||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
<base href="/"/>
|
<base href="/"/>
|
||||||
<link rel="stylesheet" href="app.css"/>
|
<link rel="stylesheet" href="app.css"/>
|
||||||
|
<link href="Manager.App.styles.css" rel="stylesheet" />
|
||||||
<link rel="icon" type="image/png" href="favicon.png"/>
|
<link rel="icon" type="image/png" href="favicon.png"/>
|
||||||
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet"/>
|
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet"/>
|
||||||
<link href="_content/MudBlazor/MudBlazor.min.css?v=@Metadata.Version" rel="stylesheet"/>
|
<link href="_content/MudBlazor/MudBlazor.min.css?v=@Metadata.Version" rel="stylesheet"/>
|
||||||
<HeadOutlet/>
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<Routes @rendermode="InteractiveServer"/>
|
<Routes @rendermode="InteractiveServer"/>
|
||||||
<script src="_framework/blazor.web.js"></script>
|
<script src="_framework/blazor.web.js"></script>
|
||||||
<script src="_content/MudBlazor/MudBlazor.min.js?v=@Metadata.Version"></script>
|
<script src="_content/MudBlazor/MudBlazor.min.js?v=@Metadata.Version"></script>
|
||||||
|
<script src="js/tz.js"></script>
|
||||||
|
<script src="js/eventConsole.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
|
||||||
|
<MudText>SAPISID Hash generator</MudText>
|
||||||
|
<MudStack Row Spacing="2">
|
||||||
|
<MudTextField HelperText="Datasync id" @bind-Value="@DatasyncId"/>
|
||||||
|
<MudTextField HelperText="Time" Mask="@(new PatternMask("0000000000"))" @bind-Value="@Time"/>
|
||||||
|
<MudTextField HelperText="SAPISID" @bind-Value="@SecureCookie"/>
|
||||||
|
<MudTextField HelperText="Origin" @bind-Value="@Origin"/>
|
||||||
|
</MudStack>
|
||||||
|
<MudTextField HelperText="Hash" ReadOnly @bind-Value="@OutputHash"/>
|
||||||
|
<MudStack Row Spacing="2">
|
||||||
|
<MudButton OnClick="Hash">Generate</MudButton>
|
||||||
|
<MudButton OnClick="Clear">Clear</MudButton>
|
||||||
|
</MudStack>
|
@@ -0,0 +1,29 @@
|
|||||||
|
using Manager.YouTube.Util;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
|
||||||
|
namespace Manager.App.Components.Application.Dev;
|
||||||
|
|
||||||
|
public partial class AuthenticationHasher : ComponentBase
|
||||||
|
{
|
||||||
|
private const string DefaultOrigin = "https://www.youtube.com";
|
||||||
|
public string DatasyncId { get; set; } = "";
|
||||||
|
public string Time { get; set; } = "";
|
||||||
|
public string SecureCookie { get; set; } = "";
|
||||||
|
public string Origin { get; set; } = DefaultOrigin;
|
||||||
|
|
||||||
|
public string OutputHash { get; set; } = "";
|
||||||
|
|
||||||
|
private void Clear()
|
||||||
|
{
|
||||||
|
DatasyncId = "";
|
||||||
|
Time = "";
|
||||||
|
SecureCookie = "";
|
||||||
|
Origin = DefaultOrigin;
|
||||||
|
OutputHash = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Hash()
|
||||||
|
{
|
||||||
|
OutputHash = AuthenticationUtilities.GetSapisidHash(DatasyncId, SecureCookie, Origin, Time);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,23 @@
|
|||||||
|
<MudOverlay Absolute="Absolute" DarkBackground LockScroll @bind-Visible="Visible" ZIndex="ZIndex">
|
||||||
|
<MudStack AlignItems="AlignItems.Center">
|
||||||
|
<MudProgressCircular Indeterminate/>
|
||||||
|
<MudText>@Message</MudText>
|
||||||
|
@if (CancellationTokenSource != null)
|
||||||
|
{
|
||||||
|
<MudButton OnClick="() => CancellationTokenSource.Cancel()" Disabled="CancellationTokenSource.IsCancellationRequested">Cancel operation</MudButton>
|
||||||
|
}
|
||||||
|
</MudStack>
|
||||||
|
</MudOverlay>
|
||||||
|
|
||||||
|
@code {
|
||||||
|
[Parameter]
|
||||||
|
public bool Visible { get; set; }
|
||||||
|
[Parameter]
|
||||||
|
public string Message { get; set; } = "Loading...";
|
||||||
|
[Parameter]
|
||||||
|
public bool Absolute { get; set; }
|
||||||
|
[Parameter]
|
||||||
|
public int ZIndex { get; set; } = 9999;
|
||||||
|
[Parameter]
|
||||||
|
public CancellationTokenSource? CancellationTokenSource { get; set; }
|
||||||
|
}
|
21
Manager.App/Components/Application/System/EventConsole.razor
Normal file
21
Manager.App/Components/Application/System/EventConsole.razor
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
@inject IJSRuntime JsRuntime
|
||||||
|
@implements IDisposable
|
||||||
|
|
||||||
|
<MudPaper Elevation="Elevation" Class="@Class" Style="@Style">
|
||||||
|
<MudStack Class="ml-2 mb-2" Spacing="2" Row>
|
||||||
|
<MudStack Spacing="1">
|
||||||
|
<MudText Typo="Typo.h5">Live service events</MudText>
|
||||||
|
<MudText Typo="Typo.caption">@($"{_serviceEvents.Count} events")</MudText>
|
||||||
|
</MudStack>
|
||||||
|
<MudSwitch @bind-Value="@_autoScroll">Auto-scroll</MudSwitch>
|
||||||
|
</MudStack>
|
||||||
|
<div @ref="@_consoleContainer" class="console-container" @onwheel="OnUserScroll">
|
||||||
|
<Virtualize @ref="_virtualize" TItem="ServiceEvent" ItemsProvider="VirtualizedItemsProvider" Context="serviceEvent">
|
||||||
|
<div class="log-line">
|
||||||
|
@TimeZoneInfo.ConvertTime(serviceEvent.DateUtc, _timeZone)
|
||||||
|
<span class="log-severity @GetLogClass(serviceEvent)">@serviceEvent.Severity</span> [<span style="color: #1565c0">@serviceEvent.Source</span>]
|
||||||
|
<span style="color: snow">@serviceEvent.Message</span>
|
||||||
|
</div>
|
||||||
|
</Virtualize>
|
||||||
|
</div>
|
||||||
|
</MudPaper>
|
165
Manager.App/Components/Application/System/EventConsole.razor.cs
Normal file
165
Manager.App/Components/Application/System/EventConsole.razor.cs
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
using DotBased.Logging;
|
||||||
|
using Manager.App.Services;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
using Microsoft.AspNetCore.Components.Web;
|
||||||
|
using Microsoft.AspNetCore.Components.Web.Virtualization;
|
||||||
|
using Microsoft.JSInterop;
|
||||||
|
|
||||||
|
namespace Manager.App.Components.Application.System;
|
||||||
|
|
||||||
|
public partial class EventConsole : ComponentBase
|
||||||
|
{
|
||||||
|
private const int BatchDelayMs = 2000;
|
||||||
|
private List<ServiceEvent> _serviceEvents = [];
|
||||||
|
private readonly List<ServiceEvent> _batchBuffer = [];
|
||||||
|
private readonly SemaphoreSlim _batchLock = new(1, 1);
|
||||||
|
private ElementReference _consoleContainer;
|
||||||
|
private bool _autoScroll = true;
|
||||||
|
private CancellationTokenSource _cts = new();
|
||||||
|
private TimeZoneInfo _timeZone = TimeZoneInfo.Local;
|
||||||
|
private Virtualize<ServiceEvent>? _virtualize;
|
||||||
|
|
||||||
|
[Parameter]
|
||||||
|
public List<ServiceEvent> InitialEvents { get; set; } = [];
|
||||||
|
[Parameter]
|
||||||
|
public IAsyncEnumerable<ServiceEvent>? AsyncEnumerable { get; set; }
|
||||||
|
|
||||||
|
[Parameter]
|
||||||
|
public int Elevation { get; set; }
|
||||||
|
[Parameter]
|
||||||
|
public string? Class { get; set; }
|
||||||
|
[Parameter]
|
||||||
|
public string? Style { get; set; }
|
||||||
|
|
||||||
|
protected override async Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
_serviceEvents.AddRange(InitialEvents);
|
||||||
|
var jsTimeZone = await JsRuntime.InvokeAsync<string>("getUserTimeZone");
|
||||||
|
if (!string.IsNullOrEmpty(jsTimeZone))
|
||||||
|
{
|
||||||
|
_timeZone = TimeZoneInfo.FindSystemTimeZoneById(jsTimeZone);
|
||||||
|
}
|
||||||
|
_ = Task.Run(() => ReadEventStreamsAsync(_cts.Token));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ReadEventStreamsAsync(CancellationToken token)
|
||||||
|
{
|
||||||
|
if (AsyncEnumerable == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await foreach (var serviceEvent in AsyncEnumerable.WithCancellation(token))
|
||||||
|
{
|
||||||
|
await _batchLock.WaitAsync(token);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_batchBuffer.Add(serviceEvent);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_batchLock.Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = BatchUpdateUi();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetLogClass(ServiceEvent serviceEvent) =>
|
||||||
|
serviceEvent.Severity switch
|
||||||
|
{
|
||||||
|
LogSeverity.Info => "log-info",
|
||||||
|
LogSeverity.Warning => "log-warning",
|
||||||
|
LogSeverity.Error => "log-error",
|
||||||
|
LogSeverity.Debug => "log-debug",
|
||||||
|
LogSeverity.Trace => "log-trace",
|
||||||
|
LogSeverity.Fatal => "log-fatal",
|
||||||
|
LogSeverity.Verbose => "log-error",
|
||||||
|
_ => "log-info"
|
||||||
|
};
|
||||||
|
|
||||||
|
private DateTime _lastBatchUpdate = DateTime.MinValue;
|
||||||
|
private bool _updateScheduled;
|
||||||
|
|
||||||
|
private async Task BatchUpdateUi()
|
||||||
|
{
|
||||||
|
if (_updateScheduled) return;
|
||||||
|
_updateScheduled = true;
|
||||||
|
|
||||||
|
while (!_cts.Token.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
var elapsed = (DateTime.UtcNow - _lastBatchUpdate).TotalMilliseconds;
|
||||||
|
if (elapsed < BatchDelayMs)
|
||||||
|
{
|
||||||
|
await Task.Delay(BatchDelayMs - (int)elapsed, _cts.Token);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<ServiceEvent> batch;
|
||||||
|
await _batchLock.WaitAsync();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_batchBuffer.Count == 0) continue;
|
||||||
|
batch = new List<ServiceEvent>(_batchBuffer);
|
||||||
|
_batchBuffer.Clear();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_batchLock.Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var serviceEvent in batch.Where(serviceEvent => !_serviceEvents.Contains(serviceEvent)))
|
||||||
|
{
|
||||||
|
_serviceEvents.Add(serviceEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
_lastBatchUpdate = DateTime.UtcNow;
|
||||||
|
|
||||||
|
if (_virtualize != null)
|
||||||
|
{
|
||||||
|
await _virtualize.RefreshDataAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_autoScroll)
|
||||||
|
{
|
||||||
|
await JsRuntime.InvokeVoidAsync("scrollToBottom", _consoleContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
await InvokeAsync(StateHasChanged);
|
||||||
|
|
||||||
|
_updateScheduled = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnUserScroll(WheelEventArgs e)
|
||||||
|
{
|
||||||
|
_ = UpdateAutoScroll();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task UpdateAutoScroll()
|
||||||
|
{
|
||||||
|
if (_consoleContainer.Context != null)
|
||||||
|
{
|
||||||
|
var scrollInfo = await JsRuntime.InvokeAsync<ScrollInfo>("getScrollInfo", _consoleContainer);
|
||||||
|
_autoScroll = scrollInfo.ScrollTop + scrollInfo.ClientHeight >= scrollInfo.ScrollHeight - 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ValueTask<ItemsProviderResult<ServiceEvent>> VirtualizedItemsProvider(ItemsProviderRequest request)
|
||||||
|
{
|
||||||
|
var items = _serviceEvents.Skip(request.StartIndex).Take(request.Count);
|
||||||
|
return ValueTask.FromResult(new ItemsProviderResult<ServiceEvent>(items, _serviceEvents.Count));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_batchLock.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ScrollInfo
|
||||||
|
{
|
||||||
|
public double ScrollTop { get; set; }
|
||||||
|
public double ScrollHeight { get; set; }
|
||||||
|
public double ClientHeight { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,51 @@
|
|||||||
|
.console-container {
|
||||||
|
background-color: #1e1e1e;
|
||||||
|
color: #9c9898;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 8px;
|
||||||
|
flex: 1;
|
||||||
|
overflow-y: auto;
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-severity{
|
||||||
|
display: inline-block;
|
||||||
|
width: 8ch;
|
||||||
|
text-align: left;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-line {
|
||||||
|
display: flex;
|
||||||
|
justify-content: start;
|
||||||
|
align-items: center;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-info {
|
||||||
|
color: #3f6b81;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-warning {
|
||||||
|
color: #f8f802;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-error {
|
||||||
|
color: #f44747;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-debug {
|
||||||
|
color: #e110ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-trace {
|
||||||
|
color: #535353;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-fatal {
|
||||||
|
color: #af1e1e;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-verbose {
|
||||||
|
color: #8085ff;
|
||||||
|
}
|
197
Manager.App/Components/Dialogs/AccountDialog.razor
Normal file
197
Manager.App/Components/Dialogs/AccountDialog.razor
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
@using Manager.App.Services.System
|
||||||
|
@inject ISnackbar SnackbarService
|
||||||
|
@inject CacheService Cache
|
||||||
|
|
||||||
|
<ForcedLoadingOverlay Visible="_isLoading"/>
|
||||||
|
|
||||||
|
@{
|
||||||
|
var client = ClientChannel?.YouTubeClient;
|
||||||
|
var clientState = client?.State;
|
||||||
|
var channel = ClientChannel?.Channel;
|
||||||
|
var avatar = channel?.AvatarImages.FirstOrDefault();
|
||||||
|
var banner = channel?.BannerImages.FirstOrDefault();
|
||||||
|
}
|
||||||
|
<MudDialog>
|
||||||
|
<TitleContent>
|
||||||
|
<MudText Typo="Typo.h6">Add new account</MudText>
|
||||||
|
</TitleContent>
|
||||||
|
<DialogContent>
|
||||||
|
@switch (_steps)
|
||||||
|
{
|
||||||
|
case AccountImportSteps.Authenticate:
|
||||||
|
<MudStack Spacing="2">
|
||||||
|
<MudPaper Elevation="0" Outlined Class="pa-2">
|
||||||
|
<MudTextField @bind-Value="@DefaultUserAgent" Required Label="User agent"
|
||||||
|
HelperText="Use an WEB user agent."/>
|
||||||
|
</MudPaper>
|
||||||
|
|
||||||
|
<MudStack Row Spacing="2" Style="height: 100%">
|
||||||
|
<MudPaper Elevation="0" Outlined Class="pa-2" Style="width: 50%;">
|
||||||
|
<MudText>Import cookies</MudText>
|
||||||
|
<MudText Typo="Typo.caption">@($"{ImportCookies.Count} cookie(s) imported")</MudText>
|
||||||
|
<MudForm @bind-IsValid="@_cookieImportTextValid">
|
||||||
|
<MudTextField @bind-Value="@_cookieDomain" Immediate Required Label="Domain"
|
||||||
|
RequiredError="Domain is required."/>
|
||||||
|
<MudTextField Class="my-2" Lines="4" AutoGrow @bind-Value="@_cookieText" Immediate
|
||||||
|
Required Label="Cookies" Variant="Variant.Outlined"
|
||||||
|
Placeholder="EXAMPLE: Cookie1=Value1; Cookie2=Value2;"
|
||||||
|
Validation="@(new Func<string, string?>(ValidateCookieText))"/>
|
||||||
|
<MudButton Variant="Variant.Outlined" Disabled="@(!_cookieImportTextValid)"
|
||||||
|
OnClick="ParseCookies">Import
|
||||||
|
</MudButton>
|
||||||
|
</MudForm>
|
||||||
|
</MudPaper>
|
||||||
|
|
||||||
|
<MudDataGrid Items="ImportCookies" Dense Elevation="0" Outlined Style="width: 50%;">
|
||||||
|
<Header>
|
||||||
|
<MudStack Class="ma-2">
|
||||||
|
<MudText>Cookies</MudText>
|
||||||
|
</MudStack>
|
||||||
|
</Header>
|
||||||
|
<Columns>
|
||||||
|
<TemplateColumn Title="Name">
|
||||||
|
<CellTemplate>
|
||||||
|
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Name"
|
||||||
|
Immediate/>
|
||||||
|
</CellTemplate>
|
||||||
|
</TemplateColumn>
|
||||||
|
<TemplateColumn Title="Domain">
|
||||||
|
<CellTemplate>
|
||||||
|
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Domain"
|
||||||
|
Immediate/>
|
||||||
|
</CellTemplate>
|
||||||
|
</TemplateColumn>
|
||||||
|
<TemplateColumn Title="Value">
|
||||||
|
<CellTemplate>
|
||||||
|
<MudTextField Variant="Variant.Text" @bind-Value="@context.Item.Value"
|
||||||
|
Immediate/>
|
||||||
|
</CellTemplate>
|
||||||
|
</TemplateColumn>
|
||||||
|
</Columns>
|
||||||
|
</MudDataGrid>
|
||||||
|
</MudStack>
|
||||||
|
</MudStack>
|
||||||
|
break;
|
||||||
|
case AccountImportSteps.Validate:
|
||||||
|
<MudStack Spacing="3">
|
||||||
|
<MudPaper Elevation="0">
|
||||||
|
@if (banner != null)
|
||||||
|
{
|
||||||
|
<MudImage Src="@Cache.CreateCacheUrl(banner.Url)" Height="250" Style="width: 100%;"/>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div Class="account-banner" Style="height: 250px; width: 100%;"></div>
|
||||||
|
}
|
||||||
|
<MudStack Row Spacing="3" Class="px-4">
|
||||||
|
@if (avatar != null)
|
||||||
|
{
|
||||||
|
<MudImage Src="@Cache.CreateCacheUrl(avatar.Url)" Class="mt-n5" Height="100" Width="100"/>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div Class="avatar-pattern mt-n6" Style="height: 100px; width: 100px;"></div>
|
||||||
|
}
|
||||||
|
<MudStack Spacing="0">
|
||||||
|
<MudText Typo="Typo.h5">@(channel?.ChannelName ?? client?.Id)</MudText>
|
||||||
|
<MudText Typo="Typo.caption">@(string.IsNullOrWhiteSpace(channel?.Description) ? "No description!" : channel.Description)</MudText>
|
||||||
|
</MudStack>
|
||||||
|
</MudStack>
|
||||||
|
</MudPaper>
|
||||||
|
|
||||||
|
<MudSimpleTable Bordered Dense Elevation="0" Outlined Square Hover>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Account id:</td>
|
||||||
|
<td>@client?.Id</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Account name:</td>
|
||||||
|
<td>@channel?.ChannelName</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Account handle:</td>
|
||||||
|
<td>@channel?.Handle</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Logged in:</td>
|
||||||
|
<td style="@($"color: {(clientState?.LoggedIn ?? false ? "green" : "red")}")">@clientState?.LoggedIn</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>YouTube Premium:</td>
|
||||||
|
<td style="@($"color: {(clientState?.IsPremiumUser ?? false ? "green" : "red")}")">@clientState?.IsPremiumUser</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>User agent:</td>
|
||||||
|
<td>@client?.UserAgent</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>InnerTube client:</td>
|
||||||
|
<td>@clientState?.InnerTubeClient</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>InnerTube client version:</td>
|
||||||
|
<td>@clientState?.InnerTubeClientVersion</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>InnerTube API key:</td>
|
||||||
|
<td>@clientState?.InnertubeApiKey</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Language:</td>
|
||||||
|
<td>@clientState?.InnerTubeContext?.InnerTubeClient?.HLanguage</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</MudSimpleTable>
|
||||||
|
</MudStack>
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<MudStack Spacing="2" Row>
|
||||||
|
<MudButton Color="Color.Error" OnClick="() => MudDialog?.Cancel()" Variant="Variant.Outlined">Cancel
|
||||||
|
</MudButton>
|
||||||
|
<MudButton Color="Color.Info" OnClick="ClearPreparedClient" Variant="Variant.Outlined">Reset</MudButton>
|
||||||
|
<MudButton Color="Color.Primary" OnClick="OnNextStep" Disabled="@(!CanContinue())" Variant="Variant.Outlined">@(_steps == AccountImportSteps.Validate ? "Save" : "Next")</MudButton>
|
||||||
|
</MudStack>
|
||||||
|
</DialogActions>
|
||||||
|
</MudDialog>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.account-banner {
|
||||||
|
width: 100%;
|
||||||
|
height: 180px;
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-end;
|
||||||
|
padding: 1rem;
|
||||||
|
border-radius: 12px;
|
||||||
|
|
||||||
|
/* Pattern background */
|
||||||
|
background: repeating-linear-gradient(
|
||||||
|
135deg,
|
||||||
|
#1976d2, /* MudBlazor Primary */
|
||||||
|
#1976d2 20px,
|
||||||
|
#1565c0 20px,
|
||||||
|
#1565c0 40px
|
||||||
|
);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.avatar-pattern {
|
||||||
|
width: 80px;
|
||||||
|
height: 80px;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
color: white;
|
||||||
|
|
||||||
|
/* Patterned background */
|
||||||
|
background: repeating-linear-gradient(
|
||||||
|
45deg,
|
||||||
|
#1976d2,
|
||||||
|
#1976d2 10px,
|
||||||
|
#1565c0 10px,
|
||||||
|
#1565c0 20px
|
||||||
|
);
|
||||||
|
}
|
||||||
|
</style>
|
174
Manager.App/Components/Dialogs/AccountDialog.razor.cs
Normal file
174
Manager.App/Components/Dialogs/AccountDialog.razor.cs
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
using System.Net;
|
||||||
|
using Manager.App.Models.Library;
|
||||||
|
using Manager.YouTube;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
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 bool _isLoading;
|
||||||
|
private AccountImportSteps _steps = AccountImportSteps.Authenticate;
|
||||||
|
|
||||||
|
private bool _cookieImportTextValid;
|
||||||
|
private string _cookieText = "";
|
||||||
|
private string _cookieDomain = ".youtube.com";
|
||||||
|
|
||||||
|
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 void ParseCookies()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ImportCookies.Clear();
|
||||||
|
ImportCookies.Add(ParseCookieHeader(_cookieText, _cookieDomain));
|
||||||
|
_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 static string? ValidateCookieText(string text)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(text))
|
||||||
|
return "Cookies are required";
|
||||||
|
|
||||||
|
var pairs = text.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
foreach (var pair in pairs)
|
||||||
|
{
|
||||||
|
if (!pair.Contains('=')) return "Invalid.";
|
||||||
|
var key = pair[..pair.IndexOf('=')].Trim();
|
||||||
|
if (string.IsNullOrEmpty(key)) return "Invalid.";
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CookieCollection ParseCookieHeader(string cookieHeader, string domain = "")
|
||||||
|
{
|
||||||
|
var collection = new CookieCollection();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(cookieHeader))
|
||||||
|
return collection;
|
||||||
|
|
||||||
|
var cookies = cookieHeader.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
foreach (var cookieStr in cookies)
|
||||||
|
{
|
||||||
|
var parts = cookieStr.Split('=', 2);
|
||||||
|
if (parts.Length != 2) continue;
|
||||||
|
|
||||||
|
var name = parts[0].Trim();
|
||||||
|
var value = parts[1].Trim();
|
||||||
|
|
||||||
|
var cookie = new Cookie(name, value)
|
||||||
|
{
|
||||||
|
Path = "/",
|
||||||
|
Domain = domain
|
||||||
|
};
|
||||||
|
collection.Add(cookie);
|
||||||
|
}
|
||||||
|
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
@@ -16,5 +16,7 @@
|
|||||||
</MudTooltip>
|
</MudTooltip>
|
||||||
}
|
}
|
||||||
</MudAppBar>
|
</MudAppBar>
|
||||||
|
<div style="display: flex; flex-direction: column; flex: 1; padding: 20px; min-height: 0;">
|
||||||
@Body
|
@Body
|
||||||
|
</div>
|
||||||
</CascadingValue>
|
</CascadingValue>
|
@@ -7,7 +7,7 @@ public partial class ApplicationLayout
|
|||||||
[CascadingParameter]
|
[CascadingParameter]
|
||||||
public BaseLayout? BaseLayout { get; set; }
|
public BaseLayout? BaseLayout { get; set; }
|
||||||
public bool DrawerOpen { get; set; } = true;
|
public bool DrawerOpen { get; set; } = true;
|
||||||
public string AppText { get; set; } = "YouTube Import";
|
public string AppText { get; set; } = "YouTube Manager";
|
||||||
|
|
||||||
private void ToggleDrawerOpen()
|
private void ToggleDrawerOpen()
|
||||||
{
|
{
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
@inherits LayoutComponentBase
|
@inherits LayoutComponentBase
|
||||||
|
|
||||||
<MudThemeProvider @ref="@_themeProvider" IsDarkModeChanged="@OnDarkThemeChanged" Theme="@_mudTheme" IsDarkMode="@DarkTheme"/>
|
<MudThemeProvider @ref="@_themeProvider" IsDarkModeChanged="@OnDarkThemeChanged" Theme="@_mudTheme" IsDarkMode="@DarkTheme"/>
|
||||||
<MudPopoverProvider />
|
<MudPopoverProvider @rendermode="InteractiveServer"/>
|
||||||
<MudDialogProvider/>
|
<MudDialogProvider @rendermode="InteractiveServer"/>
|
||||||
<MudSnackbarProvider/>
|
<MudSnackbarProvider @rendermode="InteractiveServer"/>
|
||||||
|
|
||||||
<CascadingValue Value="this">
|
<CascadingValue Value="this">
|
||||||
<MudLayout>
|
<MudLayout>
|
||||||
<MudMainContent>
|
<MudMainContent Style="display: flex; flex-direction: column; height: 100vh;">
|
||||||
@Body
|
@Body
|
||||||
</MudMainContent>
|
</MudMainContent>
|
||||||
</MudLayout>
|
</MudLayout>
|
||||||
|
@@ -1,7 +1,9 @@
|
|||||||
|
|
||||||
<MudNavMenu>
|
<MudNavMenu>
|
||||||
<MudNavLink Href="/" Match="NavLinkMatch.Prefix">Home</MudNavLink>
|
<MudNavLink Href="/" Icon="@Icons.Material.Filled.Home" Match="NavLinkMatch.All">Home</MudNavLink>
|
||||||
<MudNavLink Href="/Accounts" Match="NavLinkMatch.All">Accounts</MudNavLink>
|
<MudNavLink Href="/Channels" Icon="@Icons.Material.Filled.SupervisorAccount" Match="NavLinkMatch.All">Channels</MudNavLink>
|
||||||
<MudNavLink Href="/Library" Match="NavLinkMatch.All">Library</MudNavLink>
|
<MudNavLink Href="/Library" Icon="@Icons.Material.Filled.LocalLibrary" Match="NavLinkMatch.All">Library</MudNavLink>
|
||||||
<MudNavLink Href="/Playlists" Match="NavLinkMatch.All">Playlists</MudNavLink>
|
<MudNavLink Href="/Playlists" Icon="@Icons.Material.Filled.ViewList" Match="NavLinkMatch.All">Playlists</MudNavLink>
|
||||||
|
<MudNavLink Href="/Development" Icon="@Icons.Material.Filled.DeveloperMode" Match="NavLinkMatch.All">Development</MudNavLink>
|
||||||
|
<MudNavLink Href="/Services" Icon="@Icons.Material.Filled.MiscellaneousServices" Match="NavLinkMatch.All">Services</MudNavLink>
|
||||||
</MudNavMenu>
|
</MudNavMenu>
|
46
Manager.App/Components/Pages/Channels.razor
Normal file
46
Manager.App/Components/Pages/Channels.razor
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
@page "/Channels"
|
||||||
|
@using Manager.App.Models.Settings
|
||||||
|
@using Manager.App.Services.System
|
||||||
|
@using Microsoft.Extensions.Options
|
||||||
|
|
||||||
|
@inject ILibraryService LibraryService
|
||||||
|
@inject IDialogService DialogService
|
||||||
|
@inject IOptions<LibrarySettings> LibraryOptions
|
||||||
|
@inject ClientService ClientService
|
||||||
|
@inject ISnackbar Snackbar
|
||||||
|
|
||||||
|
<PageTitle>Channels</PageTitle>
|
||||||
|
|
||||||
|
|
||||||
|
<MudStack Spacing="2">
|
||||||
|
<MudPaper Elevation="0" Outlined>
|
||||||
|
<MudStack Row Class="ma-2">
|
||||||
|
<MudButton IconSize="Size.Small" StartIcon="@Icons.Material.Filled.Add" Variant="Variant.Outlined" OnClick="OnAddAccountDialogAsync">Add account</MudButton>
|
||||||
|
</MudStack>
|
||||||
|
</MudPaper>
|
||||||
|
|
||||||
|
<MudTable @ref="@_table" ServerData="ServerReload">
|
||||||
|
<ToolBarContent>
|
||||||
|
<MudText Typo="Typo.h6">Channels</MudText>
|
||||||
|
</ToolBarContent>
|
||||||
|
<HeaderContent>
|
||||||
|
<MudTh>Name</MudTh>
|
||||||
|
<MudTh>Channel id</MudTh>
|
||||||
|
<MudTh>Has login</MudTh>
|
||||||
|
</HeaderContent>
|
||||||
|
<RowTemplate>
|
||||||
|
<MudTd>@context.Name</MudTd>
|
||||||
|
<MudTd>@context.Id</MudTd>
|
||||||
|
<MudTd>@(context.ClientAccount != null)</MudTd>
|
||||||
|
</RowTemplate>
|
||||||
|
<NoRecordsContent>
|
||||||
|
<MudText>No channels found</MudText>
|
||||||
|
</NoRecordsContent>
|
||||||
|
<LoadingContent>
|
||||||
|
<MudText>Loading...</MudText>
|
||||||
|
</LoadingContent>
|
||||||
|
<PagerContent>
|
||||||
|
<MudTablePager/>
|
||||||
|
</PagerContent>
|
||||||
|
</MudTable>
|
||||||
|
</MudStack>
|
67
Manager.App/Components/Pages/Channels.razor.cs
Normal file
67
Manager.App/Components/Pages/Channels.razor.cs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
using Manager.App.Components.Dialogs;
|
||||||
|
using Manager.App.Models.Library;
|
||||||
|
using Manager.Data.Entities.LibraryContext;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
using MudBlazor;
|
||||||
|
|
||||||
|
namespace Manager.App.Components.Pages;
|
||||||
|
|
||||||
|
public partial class Channels : ComponentBase
|
||||||
|
{
|
||||||
|
private readonly DialogOptions _dialogOptions = new() { BackdropClick = false, CloseButton = true, FullWidth = true, MaxWidth = MaxWidth.ExtraLarge };
|
||||||
|
private MudTable<ChannelEntity>? _table;
|
||||||
|
|
||||||
|
private async Task<TableData<ChannelEntity>> ServerReload(TableState state, CancellationToken token)
|
||||||
|
{
|
||||||
|
var results = await LibraryService.GetChannelsAsync(state.PageSize, state.Page * state.PageSize, token);
|
||||||
|
return !results.IsSuccess ? new TableData<ChannelEntity>() : new TableData<ChannelEntity> { Items = results.Value, TotalItems = results.Total };
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task OnAddAccountDialogAsync()
|
||||||
|
{
|
||||||
|
var libSettings = LibraryOptions.Value;
|
||||||
|
var parameters = new DialogParameters<AccountDialog> { { x => x.DefaultUserAgent, libSettings.DefaultUserAgent } };
|
||||||
|
var dialog = await DialogService.ShowAsync<AccountDialog>("Add account", parameters, _dialogOptions);
|
||||||
|
var result = await dialog.Result;
|
||||||
|
|
||||||
|
if (result == null || result.Canceled || result.Data == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientChannel = (ClientChannel)result.Data;
|
||||||
|
if (clientChannel?.YouTubeClient == null)
|
||||||
|
{
|
||||||
|
Snackbar.Add("No YouTube client received.", Severity.Error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var savedClientResult = await ClientService.SaveClientAsync(clientChannel.YouTubeClient);
|
||||||
|
if (savedClientResult.IsSuccess)
|
||||||
|
{
|
||||||
|
if (_table != null)
|
||||||
|
{
|
||||||
|
await _table.ReloadServerData();
|
||||||
|
}
|
||||||
|
Snackbar.Add($"Client {clientChannel.Channel?.Handle ?? clientChannel.YouTubeClient.Id} saved!", Severity.Success);
|
||||||
|
ClientService.AddClient(clientChannel.YouTubeClient);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Snackbar.Add($"Failed to store client: {savedClientResult.Error?.Description ?? "Unknown!"}", Severity.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clientChannel.Channel == null)
|
||||||
|
{
|
||||||
|
Snackbar.Add("No channel information received!", Severity.Warning);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var saveChannelResult = await LibraryService.SaveChannelAsync(clientChannel.Channel);
|
||||||
|
if (!saveChannelResult.IsSuccess)
|
||||||
|
{
|
||||||
|
Snackbar.Add("Failed to save channel information", Severity.Warning);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
Manager.App/Components/Pages/Development.razor
Normal file
9
Manager.App/Components/Pages/Development.razor
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
@page "/Development"
|
||||||
|
@using Manager.App.Components.Application.Dev
|
||||||
|
<PageTitle>Development page</PageTitle>
|
||||||
|
|
||||||
|
<MudTabs Outlined Position="Position.Left" PanelClass="pa-4" ApplyEffectsToContainer Style="height: 100%">
|
||||||
|
<MudTabPanel Text="Authentication">
|
||||||
|
<AuthenticationHasher />
|
||||||
|
</MudTabPanel>
|
||||||
|
</MudTabs>
|
8
Manager.App/Components/Pages/Development.razor.cs
Normal file
8
Manager.App/Components/Pages/Development.razor.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
|
||||||
|
namespace Manager.App.Components.Pages;
|
||||||
|
|
||||||
|
public partial class Development : ComponentBase
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
40
Manager.App/Components/Pages/Library.razor
Normal file
40
Manager.App/Components/Pages/Library.razor
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
@page "/Library"
|
||||||
|
|
||||||
|
@inject ISnackbar Snackbar
|
||||||
|
@inject ILibraryService LibraryService
|
||||||
|
|
||||||
|
<PageTitle>Library information</PageTitle>
|
||||||
|
<ForcedLoadingOverlay Visible="_loading" CancellationTokenSource="@_cancellationTokenSource"/>
|
||||||
|
|
||||||
|
@if (_libraryInformation != null)
|
||||||
|
{
|
||||||
|
<MudSimpleTable Bordered Dense Elevation="0" Outlined Square Hover>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Library path:</td>
|
||||||
|
<td>@_libraryInformation.LibraryPath</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Created at (UTC):</td>
|
||||||
|
<td>@_libraryInformation.CreatedAtUtc</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Last modified (UTC):</td>
|
||||||
|
<td>@_libraryInformation.LastModifiedUtc</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Library size:</td>
|
||||||
|
<td>@($"{Suffix.BytesToSizeSuffix(_libraryInformation.TotalSizeBytes)} ({_libraryInformation.TotalSizeBytes} bytes)")</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>Total media:</td>
|
||||||
|
<td>@_libraryInformation.TotalMedia</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Total channels:</td>
|
||||||
|
<td>@_libraryInformation.TotalChannels</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</MudSimpleTable>
|
||||||
|
}
|
37
Manager.App/Components/Pages/Library.razor.cs
Normal file
37
Manager.App/Components/Pages/Library.razor.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using Manager.App.Models.Library;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
using MudBlazor;
|
||||||
|
|
||||||
|
namespace Manager.App.Components.Pages;
|
||||||
|
|
||||||
|
public partial class Library : ComponentBase
|
||||||
|
{
|
||||||
|
private LibraryInformation? _libraryInformation;
|
||||||
|
private bool _loading;
|
||||||
|
private CancellationTokenSource _cancellationTokenSource = new();
|
||||||
|
|
||||||
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||||
|
{
|
||||||
|
if (firstRender)
|
||||||
|
{
|
||||||
|
if (_cancellationTokenSource.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
_loading = true;
|
||||||
|
await InvokeAsync(StateHasChanged);
|
||||||
|
var result = await LibraryService.GetLibraryInfoAsync(_cancellationTokenSource.Token);
|
||||||
|
if (result is { IsSuccess: true, Value: not null })
|
||||||
|
{
|
||||||
|
_libraryInformation = result.Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Snackbar.Add($"Failed to get library info. Error: {result.Error?.Description}", Severity.Error);
|
||||||
|
}
|
||||||
|
_loading = false;
|
||||||
|
await InvokeAsync(StateHasChanged);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
Manager.App/Components/Pages/Services.razor
Normal file
42
Manager.App/Components/Pages/Services.razor
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
@page "/Services"
|
||||||
|
@using Manager.App.Services.System
|
||||||
|
@using Manager.App.Components.Application.System
|
||||||
|
@implements IDisposable
|
||||||
|
|
||||||
|
@inject BackgroundServiceRegistry ServiceRegistry
|
||||||
|
|
||||||
|
<PageTitle>Services</PageTitle>
|
||||||
|
|
||||||
|
<MudDataGrid T="ExtendedBackgroundService" Items="@_backgroundServices" Filterable QuickFilter="@QuickFilter">
|
||||||
|
<ToolBarContent>
|
||||||
|
<MudText Typo="Typo.h6">Services</MudText>
|
||||||
|
<MudSpacer/>
|
||||||
|
<MudTextField T="string" @bind-Value="@_searchText" Immediate
|
||||||
|
Placeholder="Search" Adornment="Adornment.Start"
|
||||||
|
AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium"/>
|
||||||
|
</ToolBarContent>
|
||||||
|
<Columns>
|
||||||
|
<PropertyColumn Property="x => x.Name" Title="Service"/>
|
||||||
|
<PropertyColumn Property="x => x.Description" Title="Description"/>
|
||||||
|
<PropertyColumn Property="x => x.State" Title="Status"/>
|
||||||
|
<PropertyColumn Property="x => x.ExecuteInterval" Title="Execute interval"/>
|
||||||
|
<TemplateColumn>
|
||||||
|
<CellTemplate>
|
||||||
|
<MudStack Row Spacing="2">
|
||||||
|
<MudButton Disabled="@(context.Item?.State == ServiceState.Paused)"
|
||||||
|
OnClick="@(() => { context.Item?.Pause(); })" Variant="Variant.Outlined">Pause
|
||||||
|
</MudButton>
|
||||||
|
<MudButton Disabled="@(context.Item?.State == ServiceState.Running)"
|
||||||
|
OnClick="@(() => { context.Item?.Resume(); })" Variant="Variant.Outlined">Resume
|
||||||
|
</MudButton>
|
||||||
|
</MudStack>
|
||||||
|
</CellTemplate>
|
||||||
|
</TemplateColumn>
|
||||||
|
</Columns>
|
||||||
|
<PagerContent>
|
||||||
|
<MudDataGridPager T="ExtendedBackgroundService"/>
|
||||||
|
</PagerContent>
|
||||||
|
</MudDataGrid>
|
||||||
|
|
||||||
|
<EventConsole AsyncEnumerable="@GetEventAsyncEnumerable()" InitialEvents="@GetInitialEvents()"
|
||||||
|
Elevation="0" Class="mt-3" Style="flex: 1; display: flex; flex-direction: column; min-height: 350px;"/>
|
41
Manager.App/Components/Pages/Services.razor.cs
Normal file
41
Manager.App/Components/Pages/Services.razor.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using Manager.App.Extensions;
|
||||||
|
using Manager.App.Services;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
|
||||||
|
namespace Manager.App.Components.Pages;
|
||||||
|
|
||||||
|
public partial class Services : ComponentBase
|
||||||
|
{
|
||||||
|
private string _searchText = "";
|
||||||
|
private List<ExtendedBackgroundService> _backgroundServices = [];
|
||||||
|
private readonly CancellationTokenSource _cts = new();
|
||||||
|
|
||||||
|
protected override void OnInitialized()
|
||||||
|
{
|
||||||
|
_backgroundServices = ServiceRegistry.GetServices();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Func<ExtendedBackgroundService, bool> QuickFilter
|
||||||
|
=> x => string.IsNullOrWhiteSpace(_searchText) || $"{x.Name} {x.Description} {x.State} {x.ExecuteInterval}".Contains(_searchText);
|
||||||
|
|
||||||
|
private IAsyncEnumerable<ServiceEvent> GetEventAsyncEnumerable()
|
||||||
|
{
|
||||||
|
var asyncEnumerators = _backgroundServices.Select(x => x.ProgressEvents.GetStreamAsync());
|
||||||
|
return AsyncEnumerableExtensions.Merge(asyncEnumerators, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ServiceEvent> GetInitialEvents()
|
||||||
|
{
|
||||||
|
var totalToGet = 1000 / _backgroundServices.Count;
|
||||||
|
var initial = _backgroundServices
|
||||||
|
.SelectMany(x => x.ProgressEvents.Items.TakeLast(totalToGet))
|
||||||
|
.OrderBy(x => x.DateUtc);
|
||||||
|
return initial.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_cts.Cancel();
|
||||||
|
_cts.Dispose();
|
||||||
|
}
|
||||||
|
}
|
@@ -4,3 +4,4 @@
|
|||||||
<FocusOnNavigate RouteData="routeData" Selector="h1"/>
|
<FocusOnNavigate RouteData="routeData" Selector="h1"/>
|
||||||
</Found>
|
</Found>
|
||||||
</Router>
|
</Router>
|
||||||
|
<HeadOutlet />
|
@@ -1,13 +1,14 @@
|
|||||||
@using System.Net.Http
|
@using Microsoft.AspNetCore.Components.Forms
|
||||||
@using System.Net.Http.Json
|
|
||||||
@using Microsoft.AspNetCore.Components.Forms
|
|
||||||
@using Microsoft.AspNetCore.Components.Routing
|
@using Microsoft.AspNetCore.Components.Routing
|
||||||
@using Microsoft.AspNetCore.Components.Web
|
@using Microsoft.AspNetCore.Components.Web
|
||||||
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
||||||
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
||||||
@using Microsoft.JSInterop
|
@using Microsoft.JSInterop
|
||||||
|
@using DotBased.Utilities
|
||||||
@using Manager.App
|
@using Manager.App
|
||||||
@using Manager.App.Components
|
@using Manager.App.Components
|
||||||
|
@using Manager.App.Components.Application
|
||||||
|
@using Manager.App.Services
|
||||||
|
|
||||||
@* MudBlazor *@
|
@* MudBlazor *@
|
||||||
@using MudBlazor
|
@using MudBlazor
|
18
Manager.App/Constants/LibraryConstants.cs
Normal file
18
Manager.App/Constants/LibraryConstants.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
namespace Manager.App.Constants;
|
||||||
|
|
||||||
|
public static class LibraryConstants
|
||||||
|
{
|
||||||
|
public static class Directories
|
||||||
|
{
|
||||||
|
public const string SubDirMedia = "Media";
|
||||||
|
public const string SubDirChannels = "Channels";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class FileTypes
|
||||||
|
{
|
||||||
|
public const string ChannelAvatar = "channel/avatar";
|
||||||
|
public const string ChannelBanner = "channel/banner";
|
||||||
|
public const string VideoThumbnail = "video/thumbnail";
|
||||||
|
public const string VideoCaption = "video/caption";
|
||||||
|
}
|
||||||
|
}
|
27
Manager.App/Controllers/CacheController.cs
Normal file
27
Manager.App/Controllers/CacheController.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using Manager.App.Services.System;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Manager.App.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/v1/[controller]")]
|
||||||
|
public class CacheController(ILogger<CacheController> logger, CacheService cacheService) : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> Cache([FromQuery(Name = "url")] string url, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(url))
|
||||||
|
{
|
||||||
|
return BadRequest("No url given.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacheResult = await cacheService.CacheFromUrl(url, cancellationToken);
|
||||||
|
if (!cacheResult.IsSuccess)
|
||||||
|
{
|
||||||
|
logger.LogError("Cache request failed. {ErrorMessage}", cacheResult.Error?.Description);
|
||||||
|
return StatusCode(500, cacheResult.Error?.Description);
|
||||||
|
}
|
||||||
|
|
||||||
|
return File(cacheResult.Value.Data, cacheResult.Value.ContentType ?? string.Empty, cacheResult.Value.OriginalFileName);
|
||||||
|
}
|
||||||
|
}
|
@@ -3,6 +3,7 @@ using DotBased.Logging.MEL;
|
|||||||
using DotBased.Logging.Serilog;
|
using DotBased.Logging.Serilog;
|
||||||
using Manager.App.Models.Settings;
|
using Manager.App.Models.Settings;
|
||||||
using Manager.App.Services;
|
using Manager.App.Services;
|
||||||
|
using Manager.App.Services.System;
|
||||||
using Manager.Data.Contexts;
|
using Manager.Data.Contexts;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@@ -24,6 +25,8 @@ public static class DependencyInjection
|
|||||||
options.UseSqlite($"Data Source={dbPath}");
|
options.UseSqlite($"Data Source={dbPath}");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
builder.RegisterExtendedBackgroundServices();
|
||||||
|
|
||||||
builder.Services.AddScoped<ILibraryService, LibraryService>();
|
builder.Services.AddScoped<ILibraryService, LibraryService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,4 +86,20 @@ public static class DependencyInjection
|
|||||||
builder.Logging.SetMinimumLevel(isDevelopment ? LogLevel.Trace : LogLevel.Information);
|
builder.Logging.SetMinimumLevel(isDevelopment ? LogLevel.Trace : LogLevel.Information);
|
||||||
builder.Logging.AddDotBasedLoggerProvider(LogService.Options);
|
builder.Logging.AddDotBasedLoggerProvider(LogService.Options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void RegisterExtendedBackgroundServices(this WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
var assembly = typeof(Program).Assembly;
|
||||||
|
|
||||||
|
foreach (var exBgService in assembly.GetTypes()
|
||||||
|
.Where(t => typeof(ExtendedBackgroundService).IsAssignableFrom(t)
|
||||||
|
&& t is { IsClass: true, IsAbstract: false }))
|
||||||
|
{
|
||||||
|
builder.Services.AddSingleton(exBgService);
|
||||||
|
builder.Services.AddSingleton(typeof(ExtendedBackgroundService), sp => (ExtendedBackgroundService)sp.GetRequiredService(exBgService));
|
||||||
|
builder.Services.AddSingleton<IHostedService>(sp => (IHostedService)sp.GetRequiredService(exBgService));
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<BackgroundServiceRegistry>();
|
||||||
|
}
|
||||||
}
|
}
|
48
Manager.App/Extensions/AsyncEnumerableExtensions.cs
Normal file
48
Manager.App/Extensions/AsyncEnumerableExtensions.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
using System.Threading.Channels;
|
||||||
|
|
||||||
|
namespace Manager.App.Extensions;
|
||||||
|
|
||||||
|
public static class AsyncEnumerableExtensions
|
||||||
|
{
|
||||||
|
public static async IAsyncEnumerable<T> Merge<T>(IEnumerable<IAsyncEnumerable<T>> sources, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var channel = Channel.CreateUnbounded<T>( new UnboundedChannelOptions { SingleReader = true, SingleWriter = false });
|
||||||
|
|
||||||
|
var writerTasks = sources.Select(source => Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await foreach (var item in source.WithCancellation(cancellationToken))
|
||||||
|
{
|
||||||
|
await channel.Writer.WriteAsync(item, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
channel.Writer.TryComplete(ex);
|
||||||
|
}
|
||||||
|
}, cancellationToken)).ToArray();
|
||||||
|
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.WhenAll(writerTasks);
|
||||||
|
channel.Writer.TryComplete();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
channel.Writer.TryComplete();
|
||||||
|
}
|
||||||
|
}, cancellationToken);
|
||||||
|
|
||||||
|
await foreach (var item in channel.Reader.ReadAllAsync(cancellationToken))
|
||||||
|
{
|
||||||
|
yield return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -10,7 +10,11 @@
|
|||||||
<PackageReference Include="DotBased" Version="1.0.0" />
|
<PackageReference Include="DotBased" Version="1.0.0" />
|
||||||
<PackageReference Include="DotBased.Logging.MEL" Version="1.0.0" />
|
<PackageReference Include="DotBased.Logging.MEL" Version="1.0.0" />
|
||||||
<PackageReference Include="DotBased.Logging.Serilog" Version="1.0.0" />
|
<PackageReference Include="DotBased.Logging.Serilog" Version="1.0.0" />
|
||||||
<PackageReference Include="MudBlazor" Version="8.10.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.19">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="MudBlazor" Version="8.11.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@@ -18,6 +22,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<_ContentIncludedByDefault Remove="wwwroot\bootstrap\bootstrap.min.css" />
|
<_ContentIncludedByDefault Remove="wwwroot\bootstrap\bootstrap.min.css" />
|
||||||
<_ContentIncludedByDefault Remove="wwwroot\bootstrap\bootstrap.min.css.map" />
|
<_ContentIncludedByDefault Remove="wwwroot\bootstrap\bootstrap.min.css.map" />
|
||||||
|
<_ContentIncludedByDefault Remove="wwwroot\js\console.js" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -26,4 +31,10 @@
|
|||||||
<ProjectReference Include="..\Manager.YouTube\Manager.YouTube.csproj" />
|
<ProjectReference Include="..\Manager.YouTube\Manager.YouTube.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="cache\" />
|
||||||
|
<Folder Include="Library\" />
|
||||||
|
<Folder Include="Logs\Debug\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
30
Manager.App/Models/System/ListResult.cs
Normal file
30
Manager.App/Models/System/ListResult.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using DotBased.Monads;
|
||||||
|
|
||||||
|
namespace Manager.App.Models.System;
|
||||||
|
|
||||||
|
public class ListResult<TResult> : Result<List<TResult>>
|
||||||
|
{
|
||||||
|
protected ListResult(List<TResult> result, int total) : base(result)
|
||||||
|
{
|
||||||
|
Total = total;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ListResult(Exception exception) : base(exception)
|
||||||
|
{
|
||||||
|
Total = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ListResult(ResultError error) : base(error)
|
||||||
|
{
|
||||||
|
Total = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Total { get; set; }
|
||||||
|
|
||||||
|
public static implicit operator ListResult<TResult>(ResultError error) => new(error);
|
||||||
|
public static implicit operator ListResult<TResult>(Exception exception) => new(exception);
|
||||||
|
public static implicit operator ListResult<TResult>(List<TResult> result) => new(result, 0);
|
||||||
|
public static implicit operator ListResult<TResult>(ListResultReturn<TResult> result) => new(result.List, result.Total);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record ListResultReturn<TResult>(List<TResult> List, int Total);
|
@@ -8,6 +8,9 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
|
|
||||||
|
AppContext.SetSwitch("System.Net.Http.EnableActivityPropagation", false);
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
/* Manager */
|
/* Manager */
|
||||||
builder.SetupLogging();
|
builder.SetupLogging();
|
||||||
@@ -31,6 +34,7 @@ app.UseHttpsRedirection();
|
|||||||
|
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
app.UseAntiforgery();
|
app.UseAntiforgery();
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
app.MapRazorComponents<App>()
|
app.MapRazorComponents<App>()
|
||||||
.AddInteractiveServerRenderMode();
|
.AddInteractiveServerRenderMode();
|
||||||
|
64
Manager.App/Services/CircularBuffer.cs
Normal file
64
Manager.App/Services/CircularBuffer.cs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
using System.Threading.Channels;
|
||||||
|
|
||||||
|
namespace Manager.App.Services;
|
||||||
|
|
||||||
|
public class CircularBuffer <T>
|
||||||
|
{
|
||||||
|
private readonly T[] _buffer;
|
||||||
|
private readonly Channel<T> _channel;
|
||||||
|
private readonly object _lock = new();
|
||||||
|
|
||||||
|
public int Capacity { get; }
|
||||||
|
public int Head { get; private set; }
|
||||||
|
public int Count { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
|
public CircularBuffer(int capacity)
|
||||||
|
{
|
||||||
|
if (capacity <= 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(capacity));
|
||||||
|
}
|
||||||
|
|
||||||
|
Capacity = capacity;
|
||||||
|
_buffer = new T[Capacity];
|
||||||
|
_channel = Channel.CreateBounded<T>(new BoundedChannelOptions(Capacity)
|
||||||
|
{
|
||||||
|
SingleReader = false,
|
||||||
|
SingleWriter = false,
|
||||||
|
FullMode = BoundedChannelFullMode.DropOldest
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(T item)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
_buffer[Head] = item;
|
||||||
|
Head = (Head + 1) % _buffer.Length;
|
||||||
|
|
||||||
|
if (Count < _buffer.Length)
|
||||||
|
{
|
||||||
|
Count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_channel.Writer.TryWrite(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<T> Items
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
for (var i = 0; i < Count; i++)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
yield return _buffer[(Head - Count + i + _buffer.Length) % _buffer.Length];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IAsyncEnumerable<T> GetStreamAsync() => _channel.Reader.ReadAllAsync();
|
||||||
|
}
|
97
Manager.App/Services/ExtendedBackgroundService.cs
Normal file
97
Manager.App/Services/ExtendedBackgroundService.cs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
using DotBased.Logging;
|
||||||
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||||
|
|
||||||
|
namespace Manager.App.Services;
|
||||||
|
|
||||||
|
public abstract class ExtendedBackgroundService(string name, string description, ILogger logger, TimeSpan? executeInterval = null)
|
||||||
|
: BackgroundService
|
||||||
|
{
|
||||||
|
private TaskCompletionSource _resumeSignal = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
|
public ServiceState State { get; private set; } = ServiceState.Stopped;
|
||||||
|
public CircularBuffer<ServiceEvent> ProgressEvents { get; } = new(500);
|
||||||
|
public string Name { get; } = name;
|
||||||
|
public string Description { get; set; } = description;
|
||||||
|
public TimeSpan ExecuteInterval { get; set; } = executeInterval ?? TimeSpan.FromMinutes(1);
|
||||||
|
|
||||||
|
protected sealed override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
State = ServiceState.Running;
|
||||||
|
logger.LogInformation("Initializing background service: {ServiceName}", Name);
|
||||||
|
await InitializeAsync(stoppingToken);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.LogInformation("Running background service: {ServiceName}", Name);
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (State == ServiceState.Paused)
|
||||||
|
{
|
||||||
|
_resumeSignal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
|
await _resumeSignal.Task.WaitAsync(stoppingToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ExecuteServiceAsync(stoppingToken);
|
||||||
|
|
||||||
|
await Task.Delay(ExecuteInterval, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
if (e is not OperationCanceledException)
|
||||||
|
{
|
||||||
|
State = ServiceState.Faulted;
|
||||||
|
logger.LogError(e,"Background service {ServiceName} faulted!", Name);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
logger.LogInformation(e,"Service {ServiceName} received cancellation", Name);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
State = ServiceState.Stopped;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void LogEvent(string message, LogSeverity severity = LogSeverity.Info) => ProgressEvents.Add(new ServiceEvent(string.Intern(Name), message, DateTime.UtcNow, severity));
|
||||||
|
|
||||||
|
public void Pause()
|
||||||
|
{
|
||||||
|
if (State == ServiceState.Running)
|
||||||
|
{
|
||||||
|
State = ServiceState.Paused;
|
||||||
|
LogEvent("Service paused.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Resume()
|
||||||
|
{
|
||||||
|
if (State == ServiceState.Paused)
|
||||||
|
{
|
||||||
|
State = ServiceState.Running;
|
||||||
|
_resumeSignal.TrySetResult();
|
||||||
|
LogEvent("Service resumed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Task InitializeAsync(CancellationToken stoppingToken);
|
||||||
|
protected abstract Task ExecuteServiceAsync(CancellationToken stoppingToken);
|
||||||
|
|
||||||
|
public override bool Equals(object? obj)
|
||||||
|
{
|
||||||
|
return obj is ExtendedBackgroundService bgService && bgService.Name.Equals(Name, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return Name.GetHashCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ServiceState
|
||||||
|
{
|
||||||
|
Stopped,
|
||||||
|
Faulted,
|
||||||
|
Running,
|
||||||
|
Paused
|
||||||
|
}
|
||||||
|
|
||||||
|
public record struct ServiceEvent(string Source, string Message, DateTime DateUtc, LogSeverity Severity);
|
@@ -1,6 +1,19 @@
|
|||||||
|
using DotBased.Monads;
|
||||||
|
using Manager.App.Models.Library;
|
||||||
|
using Manager.App.Models.System;
|
||||||
|
using Manager.Data.Entities.LibraryContext;
|
||||||
|
using Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
namespace Manager.App.Services;
|
namespace Manager.App.Services;
|
||||||
|
|
||||||
public interface ILibraryService
|
public interface ILibraryService
|
||||||
{
|
{
|
||||||
|
public Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel);
|
||||||
|
public Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default);
|
||||||
|
public Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default);
|
||||||
|
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
public Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
@@ -1,7 +1,12 @@
|
|||||||
using DotBased.Monads;
|
using DotBased.Monads;
|
||||||
|
using Manager.App.Constants;
|
||||||
using Manager.App.Models.Library;
|
using Manager.App.Models.Library;
|
||||||
using Manager.App.Models.Settings;
|
using Manager.App.Models.Settings;
|
||||||
|
using Manager.App.Models.System;
|
||||||
|
using Manager.App.Services.System;
|
||||||
using Manager.Data.Contexts;
|
using Manager.Data.Contexts;
|
||||||
|
using Manager.Data.Entities.LibraryContext;
|
||||||
|
using Manager.YouTube.Models.Innertube;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
@@ -13,22 +18,270 @@ public class LibraryService : ILibraryService
|
|||||||
private readonly LibrarySettings _librarySettings;
|
private readonly LibrarySettings _librarySettings;
|
||||||
private readonly IDbContextFactory<LibraryDbContext> _dbContextFactory;
|
private readonly IDbContextFactory<LibraryDbContext> _dbContextFactory;
|
||||||
private readonly DirectoryInfo _libraryDirectory;
|
private readonly DirectoryInfo _libraryDirectory;
|
||||||
private const string SubDirMedia = "Media";
|
private readonly CacheService _cacheService;
|
||||||
private const string SubDirChannels = "Channels";
|
|
||||||
|
|
||||||
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory)
|
public LibraryService(ILogger<LibraryService> logger, IOptions<LibrarySettings> librarySettings, IDbContextFactory<LibraryDbContext> contextFactory, CacheService cacheService)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_librarySettings = librarySettings.Value;
|
_librarySettings = librarySettings.Value;
|
||||||
_dbContextFactory = contextFactory;
|
_dbContextFactory = contextFactory;
|
||||||
_libraryDirectory = Directory.CreateDirectory(Path.Combine(_librarySettings.Path, SubDirMedia));
|
_cacheService = cacheService;
|
||||||
_libraryDirectory = Directory.CreateDirectory(Path.Combine(_librarySettings.Path, SubDirChannels));
|
_libraryDirectory = Directory.CreateDirectory(_librarySettings.Path);
|
||||||
|
logger.LogDebug("Library directory: {LibraryWorkingDir}", _libraryDirectory.FullName);
|
||||||
|
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirMedia));
|
||||||
|
Directory.CreateDirectory(Path.Combine(_librarySettings.Path, LibraryConstants.Directories.SubDirChannels));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result<LibraryInformation>> GetLibraryInfoAsync()
|
public async Task<Result> FetchChannelImagesAsync(InnertubeChannel innertubeChannel)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
await using var context = await _dbContextFactory.CreateDbContextAsync();
|
||||||
//TODO: Get library info
|
|
||||||
return ResultError.Fail("Not implemented!");
|
await AddWebImagesAsync(context, innertubeChannel.AvatarImages, innertubeChannel.Id, "avatars", LibraryConstants.FileTypes.ChannelAvatar, LibraryConstants.Directories.SubDirChannels);
|
||||||
|
await AddWebImagesAsync(context, innertubeChannel.BannerImages, innertubeChannel.Id, "banners", LibraryConstants.FileTypes.ChannelBanner, LibraryConstants.Directories.SubDirChannels);
|
||||||
|
|
||||||
|
if (!context.ChangeTracker.HasChanges())
|
||||||
|
{
|
||||||
|
_logger.LogInformation("No changes detected. Skipping.");
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task AddWebImagesAsync(LibraryDbContext context, List<WebImage> images, string foreignKey, string libSubDir, string fileType, string subDir)
|
||||||
|
{
|
||||||
|
foreach (var image in images)
|
||||||
|
{
|
||||||
|
if (context.Files.Any(f => image.Url.Equals(f.OriginalUrl, StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacheResult = await _cacheService.CacheFromUrl(image.Url);
|
||||||
|
if (!cacheResult.IsSuccess)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Failed to get image {ImageUrl}", image.Url);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var cachedFile = cacheResult.Value;
|
||||||
|
|
||||||
|
var fileId = Guid.NewGuid();
|
||||||
|
var fileName = cachedFile.OriginalFileName ?? $"{fileId}.{cachedFile.ContentType?.Split('/').Last() ?? "unknown"}";
|
||||||
|
var relativePath = Path.Combine(foreignKey, libSubDir, $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}_{fileName}");
|
||||||
|
var savePath = Path.Combine(_libraryDirectory.FullName, subDir, relativePath);
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(savePath) ?? savePath);
|
||||||
|
await using var fileStream = File.Create(savePath);
|
||||||
|
await fileStream.WriteAsync(cachedFile.Data.AsMemory(0, cachedFile.Data.Length));
|
||||||
|
|
||||||
|
var file = new FileEntity
|
||||||
|
{
|
||||||
|
Id = fileId,
|
||||||
|
OriginalUrl = image.Url,
|
||||||
|
OriginalFileName = cachedFile.OriginalFileName,
|
||||||
|
ForeignKey = foreignKey,
|
||||||
|
FileType = fileType,
|
||||||
|
RelativePath = relativePath.Replace('\\', '/'),
|
||||||
|
MimeType = cachedFile.ContentType,
|
||||||
|
SizeBytes = cachedFile.Data.Length,
|
||||||
|
Height = image.Height,
|
||||||
|
Width = image.Width
|
||||||
|
};
|
||||||
|
|
||||||
|
await context.Files.AddAsync(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result> SaveClientAsync(ClientAccountEntity client, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var updateEntity = false;
|
||||||
|
var dbClient = context.ClientAccounts.FirstOrDefault(c => c.Id == client.Id);
|
||||||
|
if (dbClient == null)
|
||||||
|
{
|
||||||
|
dbClient = client;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
updateEntity = true;
|
||||||
|
dbClient.HttpCookies = client.HttpCookies;
|
||||||
|
dbClient.UserAgent = client.UserAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updateEntity)
|
||||||
|
{
|
||||||
|
context.ClientAccounts.Update(dbClient);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.ClientAccounts.Add(dbClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
var savedResult= await context.SaveChangesAsync(cancellationToken);
|
||||||
|
return savedResult <= 0 ? ResultError.Fail("Could not save changes!") : Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<ChannelEntity>> GetChannelByIdAsync(string id, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Channel id cannot be null or empty!");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var channel = await context.Channels
|
||||||
|
.Include(c => c.ClientAccount)
|
||||||
|
.ThenInclude(p => p!.HttpCookies)
|
||||||
|
.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
|
||||||
|
|
||||||
|
if (channel == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Channel not found!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result> SaveChannelAsync(InnertubeChannel innertubeChannel, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var imagesResult = await FetchChannelImagesAsync(innertubeChannel);
|
||||||
|
if (!imagesResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Failed to fetch channel images!");
|
||||||
|
}
|
||||||
|
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var channelResult = await GetChannelByIdAsync(innertubeChannel.Id, cancellationToken);
|
||||||
|
|
||||||
|
ChannelEntity? channelEntity;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (channelResult.IsSuccess)
|
||||||
|
{
|
||||||
|
channelEntity = channelResult.Value;
|
||||||
|
channelEntity.Name = innertubeChannel.ChannelName;
|
||||||
|
channelEntity.Handle = innertubeChannel.Handle;
|
||||||
|
channelEntity.Description = innertubeChannel.Description;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
channelEntity = new ChannelEntity
|
||||||
|
{
|
||||||
|
Id = innertubeChannel.Id,
|
||||||
|
Name = innertubeChannel.ChannelName,
|
||||||
|
Handle = innertubeChannel.Handle,
|
||||||
|
Description = innertubeChannel.Description
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context.Channels.Any(c => c.Id == innertubeChannel.Id))
|
||||||
|
{
|
||||||
|
context.Channels.Update(channelEntity);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.Channels.Add(channelEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
var changed = await context.SaveChangesAsync(cancellationToken);
|
||||||
|
return changed <= 0 ? ResultError.Fail("Failed to save channel!") : Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var libInfo = new LibraryInformation
|
||||||
|
{
|
||||||
|
LibraryPath = _libraryDirectory.FullName,
|
||||||
|
CreatedAtUtc = _libraryDirectory.CreationTimeUtc,
|
||||||
|
LastModifiedUtc = _libraryDirectory.LastWriteTimeUtc,
|
||||||
|
TotalChannels = await context.Channels.CountAsync(cancellationToken: cancellationToken),
|
||||||
|
TotalMedia = await context.Media.CountAsync(cancellationToken: cancellationToken),
|
||||||
|
TotalSizeBytes = GetDirectorySize(_libraryDirectory)
|
||||||
|
};
|
||||||
|
return libInfo;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var orderedAccounts = context.Channels.Include(x => x.ClientAccount).Where(x => x.ClientAccount != null).OrderBy(x => x.Id);
|
||||||
|
return new ListResultReturn<ChannelEntity>(orderedAccounts.Skip(offset).Take(total).ToList(),orderedAccounts.Count());
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long GetDirectorySize(DirectoryInfo dir)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var size = dir.EnumerateFiles("*", SearchOption.AllDirectories).Select(f => f.Length).Sum();
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Error while getting directory size.");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResultError HandleException(Exception exception)
|
||||||
|
{
|
||||||
|
if (exception is OperationCanceledException)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Library service operation cancelled");
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogError(exception, "Failed to get library information");
|
||||||
|
return ResultError.Fail("Failed to get library information");
|
||||||
}
|
}
|
||||||
}
|
}
|
9
Manager.App/Services/System/BackgroundServiceRegistry.cs
Normal file
9
Manager.App/Services/System/BackgroundServiceRegistry.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Manager.App.Services.System;
|
||||||
|
|
||||||
|
public class BackgroundServiceRegistry(IEnumerable<ExtendedBackgroundService> backgroundServices)
|
||||||
|
{
|
||||||
|
public List<ExtendedBackgroundService> GetServices()
|
||||||
|
{
|
||||||
|
return backgroundServices.ToList();
|
||||||
|
}
|
||||||
|
}
|
184
Manager.App/Services/System/CacheService.cs
Normal file
184
Manager.App/Services/System/CacheService.cs
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
using DotBased.Logging;
|
||||||
|
using DotBased.Monads;
|
||||||
|
using Manager.Data.Contexts;
|
||||||
|
using Manager.Data.Entities.Cache;
|
||||||
|
using Manager.YouTube;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
|
||||||
|
namespace Manager.App.Services.System;
|
||||||
|
|
||||||
|
public class CacheService(ILogger<CacheService> logger, IHostEnvironment environment) : ExtendedBackgroundService(nameof(CacheService), "Manages caching.", logger, TimeSpan.FromHours(5))
|
||||||
|
{
|
||||||
|
private DirectoryInfo? _cacheDirectory;
|
||||||
|
private PooledDbContextFactory<CacheDbContext>? _dbContextFactory;
|
||||||
|
private const string DataSubDir = "data";
|
||||||
|
|
||||||
|
protected override Task InitializeAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
_cacheDirectory = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "cache"));
|
||||||
|
_cacheDirectory.Create();
|
||||||
|
Directory.CreateDirectory(Path.Combine(_cacheDirectory.FullName, DataSubDir));
|
||||||
|
LogEvent($"Cache directory: {_cacheDirectory.FullName}");
|
||||||
|
|
||||||
|
var dbContextOptionsBuilder = new DbContextOptionsBuilder<CacheDbContext>();
|
||||||
|
dbContextOptionsBuilder.UseSqlite($"Data Source={Path.Combine(_cacheDirectory.FullName, "cache_index.db")}");
|
||||||
|
_dbContextFactory = new PooledDbContextFactory<CacheDbContext>(dbContextOptionsBuilder.Options);
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
if (environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
LogEvent("Development mode detected, skipping cache cleaning...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await ClearCacheAsync(stoppingToken);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
logger.LogError(e, "Error in execution of service.");
|
||||||
|
LogEvent($"Service execution failed. {e.Message}", LogSeverity.Error);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string CreateCacheUrl(string originalUrl) => $"/api/v1/cache?url={originalUrl}";
|
||||||
|
|
||||||
|
public async Task<Result<CacheFile>> CacheFromUrl(string url, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(url))
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Url is empty.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_cacheDirectory == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Cache directory is not initialized.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_dbContextFactory == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Context factory is not initialized.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var urlKeyBytes = SHA1.HashData(Encoding.UTF8.GetBytes(url));
|
||||||
|
var urlKey = Convert.ToHexString(urlKeyBytes);
|
||||||
|
var cacheEntity = await context.Cache.FirstOrDefaultAsync(c => c.Id == urlKey, cancellationToken: cancellationToken);
|
||||||
|
if (cacheEntity == null)
|
||||||
|
{
|
||||||
|
var downloadResult = await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, url));
|
||||||
|
if (!downloadResult.IsSuccess)
|
||||||
|
{
|
||||||
|
LogEvent($"Failed to download from url: {url}");
|
||||||
|
return ResultError.Fail("Download failed.");
|
||||||
|
}
|
||||||
|
var download = downloadResult.Value;
|
||||||
|
await using var downloadFile = File.Create(Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{urlKey}.cache"));
|
||||||
|
await downloadFile.WriteAsync(download.Data.AsMemory(0, download.Data.Length), cancellationToken);
|
||||||
|
|
||||||
|
cacheEntity = new CacheEntity
|
||||||
|
{
|
||||||
|
Id = urlKey,
|
||||||
|
CachedAtUtc = DateTime.UtcNow,
|
||||||
|
ContentLength = download.ContentLength,
|
||||||
|
ContentType = download.ContentType,
|
||||||
|
OriginalFileName = download.FileName,
|
||||||
|
};
|
||||||
|
|
||||||
|
context.Cache.Add(cacheEntity);
|
||||||
|
var saved = await context.SaveChangesAsync(cancellationToken);
|
||||||
|
if (saved <= 0)
|
||||||
|
{
|
||||||
|
LogEvent($"Cache entity {cacheEntity.Id} could not be saved.", LogSeverity.Error);
|
||||||
|
return ResultError.Fail("Failed to save to cache db.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CacheFile(download.Data, download.ContentType, download.FileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
var filePath = Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{urlKey}.cache");
|
||||||
|
var buffer = await File.ReadAllBytesAsync(filePath, cancellationToken);
|
||||||
|
if (buffer.Length == 0)
|
||||||
|
{
|
||||||
|
LogEvent($"Failed to read data from disk. File: {filePath}", LogSeverity.Error);
|
||||||
|
return ResultError.Fail($"Error reading data from disk. File: {filePath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CacheFile(buffer.ToArray(), cacheEntity.ContentType, cacheEntity.OriginalFileName);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e, "Cache error.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ClearCacheAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (_dbContextFactory == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("No DbContext factory configured.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_cacheDirectory == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("No cache directory configured.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var toRemove = dbContext.Cache.Where(c => c.CachedAtUtc < DateTime.UtcNow.AddDays(-1));
|
||||||
|
if (!toRemove.Any())
|
||||||
|
{
|
||||||
|
LogEvent("No items found to purge from cache.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var totalToRemove = toRemove.Count();
|
||||||
|
LogEvent($"Found {totalToRemove} cache items that are older than 1 day(s)");
|
||||||
|
|
||||||
|
var deleted = new List<CacheEntity>();
|
||||||
|
foreach (var entity in toRemove)
|
||||||
|
{
|
||||||
|
var pathToFile = Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{entity.Id}.cache");
|
||||||
|
if (!File.Exists(pathToFile))
|
||||||
|
{
|
||||||
|
deleted.Add(entity);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.Delete(pathToFile);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
logger.LogError(e, "Failed to delete cache entity with id: {EntityId}. Skipping cache entity...", entity.Id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
deleted.Add(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
dbContext.RemoveRange(deleted);
|
||||||
|
var dbDeleted = await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
if (dbDeleted < deleted.Count)
|
||||||
|
{
|
||||||
|
LogEvent("Could not delete all files from cache.", LogSeverity.Warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
LogEvent($"Removed {dbDeleted}/{totalToRemove} items");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public record CacheFile(byte[] Data, string? ContentType, string? OriginalFileName);
|
112
Manager.App/Services/System/ClientService.cs
Normal file
112
Manager.App/Services/System/ClientService.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using System.Net;
|
||||||
|
using DotBased.Logging;
|
||||||
|
using DotBased.Monads;
|
||||||
|
using Manager.Data.Entities.LibraryContext;
|
||||||
|
using Manager.YouTube;
|
||||||
|
|
||||||
|
namespace Manager.App.Services.System;
|
||||||
|
|
||||||
|
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger)
|
||||||
|
: ExtendedBackgroundService(nameof(ClientService), "Managing YouTube clients", logger, TimeSpan.FromMinutes(10))
|
||||||
|
{
|
||||||
|
private readonly YouTubeClientCollection _loadedClients = [];
|
||||||
|
private ILibraryService? _libraryService;
|
||||||
|
|
||||||
|
protected override Task InitializeAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
stoppingToken.Register(CancellationRequested);
|
||||||
|
using var scope = scopeFactory.CreateScope();
|
||||||
|
_libraryService = scope.ServiceProvider.GetRequiredService<ILibraryService>();
|
||||||
|
LogEvent("Initializing service...");
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
LogEvent($"Saving {_loadedClients.Count} loaded client(s)");
|
||||||
|
foreach (var client in _loadedClients)
|
||||||
|
{
|
||||||
|
await SaveClientAsync(client, cancellationToken: stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void CancellationRequested()
|
||||||
|
{
|
||||||
|
foreach (var client in _loadedClients)
|
||||||
|
{
|
||||||
|
await SaveClientAsync(client);
|
||||||
|
client.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result> AddClientByIdAsync(string id, CancellationToken stoppingToken = default)
|
||||||
|
{
|
||||||
|
if (_libraryService == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Library service is not initialized!.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientResult = await _libraryService.GetChannelByIdAsync(id, stoppingToken);
|
||||||
|
if (!clientResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return clientResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientAcc = clientResult.Value.ClientAccount;
|
||||||
|
if (clientAcc == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Client account is not initialized!.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var cookieCollection = new CookieCollection();
|
||||||
|
foreach (var httpCookie in clientAcc.HttpCookies)
|
||||||
|
{
|
||||||
|
var cookie = new Cookie
|
||||||
|
{
|
||||||
|
Name = httpCookie.Name,
|
||||||
|
Value = httpCookie.Value,
|
||||||
|
Domain = httpCookie.Domain,
|
||||||
|
Path = httpCookie.Path,
|
||||||
|
Secure = httpCookie.Secure,
|
||||||
|
HttpOnly = httpCookie.HttpOnly,
|
||||||
|
Expires = httpCookie.ExpiresUtc ?? DateTime.MinValue
|
||||||
|
};
|
||||||
|
cookieCollection.Add(cookie);
|
||||||
|
}
|
||||||
|
var ytClientResult = await YouTubeClient.CreateAsync(cookieCollection, clientAcc.UserAgent ?? "");
|
||||||
|
if (!ytClientResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return ytClientResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
AddClient(ytClientResult.Value);
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddClient(YouTubeClient client)
|
||||||
|
{
|
||||||
|
if (_loadedClients.Contains(client))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_loadedClients.Add(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result> SaveClientAsync(YouTubeClient client, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (_libraryService == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Library service is not initialized!.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(client.Id))
|
||||||
|
{
|
||||||
|
LogEvent("Failed to store client no ID!", LogSeverity.Warning);
|
||||||
|
return ResultError.Fail("Client does not have an ID, cannot save to library database!");
|
||||||
|
}
|
||||||
|
|
||||||
|
var saveResult = await _libraryService.SaveClientAsync(new ClientAccountEntity { Id = client.Id, UserAgent = client.UserAgent }, cancellationToken);
|
||||||
|
return saveResult;
|
||||||
|
}
|
||||||
|
}
|
@@ -3,11 +3,11 @@
|
|||||||
"Logging": {
|
"Logging": {
|
||||||
"Severity": "Debug",
|
"Severity": "Debug",
|
||||||
"SeverityFilters":{
|
"SeverityFilters":{
|
||||||
"Microsoft": "Debug",
|
"Microsoft": "Info",
|
||||||
"Microsoft.Hosting.Lifetime": "Debug",
|
"Microsoft.Hosting.Lifetime": "Debug",
|
||||||
"Microsoft.AspNetCore": "Warning",
|
"Microsoft.AspNetCore": "Warning",
|
||||||
"Microsoft.AspNetCore.Authentication": "Debug",
|
"Microsoft.AspNetCore.Authentication": "Debug",
|
||||||
"MudBlazor": "Debug"
|
"MudBlazor": "Info"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
16
Manager.App/wwwroot/js/eventConsole.js
Normal file
16
Manager.App/wwwroot/js/eventConsole.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
window.scrollToBottom = (element) => {
|
||||||
|
if (element) {
|
||||||
|
requestAnimationFrame(function () {
|
||||||
|
element.scroll({ top: element.scrollHeight, behavior: 'smooth' });
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.getScrollInfo = (element) => {
|
||||||
|
if (!element) return null;
|
||||||
|
return {
|
||||||
|
scrollTop: element.scrollTop,
|
||||||
|
scrollHeight: element.scrollHeight,
|
||||||
|
clientHeight: element.clientHeight
|
||||||
|
};
|
||||||
|
};
|
3
Manager.App/wwwroot/js/tz.js
Normal file
3
Manager.App/wwwroot/js/tz.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
function getUserTimeZone() {
|
||||||
|
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
}
|
112
Manager.Data/Contexts/AuditInterceptor.cs
Normal file
112
Manager.Data/Contexts/AuditInterceptor.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using Manager.Data.Entities.Audit;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||||
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||||
|
|
||||||
|
namespace Manager.Data.Contexts;
|
||||||
|
|
||||||
|
public class AuditInterceptor : SaveChangesInterceptor
|
||||||
|
{
|
||||||
|
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
|
||||||
|
{
|
||||||
|
AddAudit(eventData.Context);
|
||||||
|
return base.SavingChanges(eventData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
|
||||||
|
DbContextEventData eventData,
|
||||||
|
InterceptionResult<int> result,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
AddAudit(eventData.Context);
|
||||||
|
return base.SavingChangesAsync(eventData, result, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddAudit(DbContext? context)
|
||||||
|
{
|
||||||
|
if (context == null) return;
|
||||||
|
|
||||||
|
var entries = context.ChangeTracker.Entries()
|
||||||
|
.Where(e => e.State is EntityState.Modified or EntityState.Deleted or EntityState.Added && Attribute.IsDefined(e.Entity.GetType(),
|
||||||
|
typeof(AuditableAttribute)));
|
||||||
|
|
||||||
|
var audits = new List<EntityAudit>();
|
||||||
|
|
||||||
|
foreach (var entry in entries)
|
||||||
|
{
|
||||||
|
var primaryKey = entry.Properties.First(p => p.Metadata.IsPrimaryKey()).CurrentValue?.ToString();
|
||||||
|
|
||||||
|
var declaredProperties = entry.Entity.GetType()
|
||||||
|
.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
|
||||||
|
.Where(p => !Attribute.IsDefined(p.DeclaringType!, typeof(NoAuditAttribute), false))
|
||||||
|
.Select(p => p.Name)
|
||||||
|
.ToHashSet();
|
||||||
|
|
||||||
|
var allowedProperties = entry.Properties.Where(p => declaredProperties.Contains(p.Metadata.Name));
|
||||||
|
|
||||||
|
switch (entry.State)
|
||||||
|
{
|
||||||
|
case EntityState.Added:
|
||||||
|
audits.AddRange(allowedProperties
|
||||||
|
.Where(p => p.CurrentValue != null)
|
||||||
|
.Select(p => CreateAudit(entry, p, entry.State, primaryKey))
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case EntityState.Modified:
|
||||||
|
audits.AddRange(allowedProperties
|
||||||
|
.Where(p => p.IsModified)
|
||||||
|
.Select(p => CreateAudit(entry, p, entry.State, primaryKey))
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case EntityState.Deleted:
|
||||||
|
audits.AddRange(allowedProperties
|
||||||
|
.Select(p => CreateAudit(entry, p, entry.State, primaryKey))
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (audits.Count != 0)
|
||||||
|
{
|
||||||
|
context.Set<EntityAudit>().AddRange(audits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private EntityAudit CreateAudit(EntityEntry entry, PropertyEntry prop, EntityState changeType, string? primaryKey)
|
||||||
|
{
|
||||||
|
return new EntityAudit
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
EntityName = entry.Entity.GetType().Name,
|
||||||
|
EntityId = primaryKey ?? "Unknown",
|
||||||
|
PropertyName = prop.Metadata.Name,
|
||||||
|
OldValue = SerializeValue(prop.OriginalValue),
|
||||||
|
NewValue = SerializeValue(prop.CurrentValue),
|
||||||
|
ModifiedUtc = DateTime.UtcNow,
|
||||||
|
ChangedBy = "SYSTEM",
|
||||||
|
ChangeType = changeType
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
|
||||||
|
{
|
||||||
|
WriteIndented = false,
|
||||||
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||||
|
};
|
||||||
|
|
||||||
|
private string? SerializeValue(object? value)
|
||||||
|
{
|
||||||
|
if (value == null) return null;
|
||||||
|
|
||||||
|
var type = value.GetType();
|
||||||
|
|
||||||
|
if (type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(decimal))
|
||||||
|
{
|
||||||
|
return value.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonSerializer.Serialize(value, _jsonSerializerOptions);
|
||||||
|
}
|
||||||
|
}
|
25
Manager.Data/Contexts/CacheDbContext.cs
Normal file
25
Manager.Data/Contexts/CacheDbContext.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using Manager.Data.Entities.Cache;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Manager.Data.Contexts;
|
||||||
|
|
||||||
|
public sealed class CacheDbContext : DbContext
|
||||||
|
{
|
||||||
|
public CacheDbContext(DbContextOptions<CacheDbContext> options) : base(options)
|
||||||
|
{
|
||||||
|
Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<CacheEntity> Cache { get; set; }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<CacheEntity>(ce =>
|
||||||
|
{
|
||||||
|
ce.ToTable("cache");
|
||||||
|
ce.HasKey(x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
}
|
||||||
|
}
|
38
Manager.Data/Contexts/DateInterceptor.cs
Normal file
38
Manager.Data/Contexts/DateInterceptor.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
using Manager.Data.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||||
|
|
||||||
|
namespace Manager.Data.Contexts;
|
||||||
|
|
||||||
|
public class DateInterceptor : SaveChangesInterceptor
|
||||||
|
{
|
||||||
|
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
|
||||||
|
{
|
||||||
|
UpdateEntryDates(eventData.Context);
|
||||||
|
return base.SavingChanges(eventData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result,
|
||||||
|
CancellationToken cancellationToken = new())
|
||||||
|
{
|
||||||
|
UpdateEntryDates(eventData.Context);
|
||||||
|
return base.SavingChangesAsync(eventData, result, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateEntryDates(DbContext? context)
|
||||||
|
{
|
||||||
|
if (context == null) return;
|
||||||
|
|
||||||
|
var entries = context.ChangeTracker.Entries().Where(x => x is { Entity: DateTimeBase, State: EntityState.Added or EntityState.Modified });
|
||||||
|
|
||||||
|
foreach (var entity in entries)
|
||||||
|
{
|
||||||
|
((DateTimeBase)entity.Entity).LastModifiedUtc = DateTime.UtcNow;
|
||||||
|
|
||||||
|
if (entity.State == EntityState.Added)
|
||||||
|
{
|
||||||
|
((DateTimeBase)entity.Entity).CreatedAtUtc = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,3 +1,4 @@
|
|||||||
|
using Manager.Data.Entities.Audit;
|
||||||
using Manager.Data.Entities.LibraryContext;
|
using Manager.Data.Entities.LibraryContext;
|
||||||
using Manager.Data.Entities.LibraryContext.Join;
|
using Manager.Data.Entities.LibraryContext.Join;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -10,18 +11,33 @@ public sealed class LibraryDbContext : DbContext
|
|||||||
{
|
{
|
||||||
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
||||||
ChangeTracker.LazyLoadingEnabled = false;
|
ChangeTracker.LazyLoadingEnabled = false;
|
||||||
|
Database.EnsureCreated();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DbSet<EntityAudit> Histories { get; set; }
|
||||||
|
|
||||||
public DbSet<CaptionEntity> Captions { get; set; }
|
public DbSet<CaptionEntity> Captions { get; set; }
|
||||||
public DbSet<ChannelEntity> Channels { get; set; }
|
public DbSet<ChannelEntity> Channels { get; set; }
|
||||||
public DbSet<ClientAccountEntity> Accounts { get; set; }
|
public DbSet<ClientAccountEntity> ClientAccounts { get; set; }
|
||||||
public DbSet<HttpCookieEntity> HttpCookies { get; set; }
|
public DbSet<HttpCookieEntity> HttpCookies { get; set; }
|
||||||
public DbSet<MediaEntity> Media { get; set; }
|
public DbSet<MediaEntity> Media { get; set; }
|
||||||
public DbSet<MediaFormatEntity> MediaFormats { get; set; }
|
public DbSet<MediaFormatEntity> MediaFormats { get; set; }
|
||||||
public DbSet<PlaylistEntity> Playlists { get; set; }
|
public DbSet<PlaylistEntity> Playlists { get; set; }
|
||||||
|
public DbSet<FileEntity> Files { get; set; }
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
{
|
||||||
|
optionsBuilder.AddInterceptors(new DateInterceptor(), new AuditInterceptor());
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
|
modelBuilder.Entity<EntityAudit>(ea =>
|
||||||
|
{
|
||||||
|
ea.HasKey(a => a.Id);
|
||||||
|
ea.ToTable("audits");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity<CaptionEntity>(ce =>
|
modelBuilder.Entity<CaptionEntity>(ce =>
|
||||||
{
|
{
|
||||||
ce.ToTable("captions");
|
ce.ToTable("captions");
|
||||||
@@ -32,17 +48,30 @@ public sealed class LibraryDbContext : DbContext
|
|||||||
{
|
{
|
||||||
channel.ToTable("channels");
|
channel.ToTable("channels");
|
||||||
channel.HasKey(x => x.Id);
|
channel.HasKey(x => x.Id);
|
||||||
|
channel.HasMany(x => x.Media)
|
||||||
|
.WithOne()
|
||||||
|
.HasForeignKey(x => x.ChannelId);
|
||||||
|
channel.HasMany(x => x.Playlists)
|
||||||
|
.WithOne()
|
||||||
|
.HasForeignKey(x => x.ChannelId);
|
||||||
|
channel.HasOne(x => x.ClientAccount)
|
||||||
|
.WithOne()
|
||||||
|
.HasForeignKey<ClientAccountEntity>(e => e.Id);
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity<ClientAccountEntity>(cae =>
|
modelBuilder.Entity<ClientAccountEntity>(cae =>
|
||||||
{
|
{
|
||||||
cae.ToTable("client_accounts");
|
cae.ToTable("client_accounts");
|
||||||
cae.HasKey(x => x.Id);
|
cae.HasKey(x => x.Id);
|
||||||
|
cae.HasMany(x => x.HttpCookies)
|
||||||
|
.WithOne()
|
||||||
|
.HasForeignKey(x => x.ClientId);
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity<HttpCookieEntity>(httpce =>
|
modelBuilder.Entity<HttpCookieEntity>(httpce =>
|
||||||
{
|
{
|
||||||
httpce.ToTable("http_cookies");
|
httpce.ToTable("http_cookies");
|
||||||
|
httpce.HasKey(x => x.Name);
|
||||||
});
|
});
|
||||||
|
|
||||||
modelBuilder.Entity<MediaEntity>(me =>
|
modelBuilder.Entity<MediaEntity>(me =>
|
||||||
@@ -69,6 +98,12 @@ public sealed class LibraryDbContext : DbContext
|
|||||||
ple.HasKey(x => x.Id);
|
ple.HasKey(x => x.Id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<FileEntity>(file =>
|
||||||
|
{
|
||||||
|
file.ToTable("files");
|
||||||
|
file.HasKey(x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
/* Join tables */
|
/* Join tables */
|
||||||
|
|
||||||
modelBuilder.Entity<PlaylistMedia>(pmj =>
|
modelBuilder.Entity<PlaylistMedia>(pmj =>
|
||||||
|
@@ -4,7 +4,8 @@ public static class DataConstants
|
|||||||
{
|
{
|
||||||
public static class DbContext
|
public static class DbContext
|
||||||
{
|
{
|
||||||
public const int DefaultDbStringSize = 100;
|
public const int DefaultDbStringSize = 500;
|
||||||
public const int DefaultDbDescriptionStringSize = 500;
|
public const int DefaultDbDescriptionStringSize = 5500;
|
||||||
|
public const int DefaultDbUrlSize = 10000;
|
||||||
}
|
}
|
||||||
}
|
}
|
9
Manager.Data/Entities/Audit/AuditableAttribute.cs
Normal file
9
Manager.Data/Entities/Audit/AuditableAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Make all properties in the entity audible, if they are changed this will be stored as a history in the db.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
|
public class AuditableAttribute : Attribute
|
||||||
|
{
|
||||||
|
}
|
23
Manager.Data/Entities/Audit/EntityAudit.cs
Normal file
23
Manager.Data/Entities/Audit/EntityAudit.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
|
public class EntityAudit
|
||||||
|
{
|
||||||
|
public required Guid Id { get; set; }
|
||||||
|
[MaxLength(200)]
|
||||||
|
public required string EntityName { get; set; }
|
||||||
|
[MaxLength(200)]
|
||||||
|
public required string EntityId { get; set; }
|
||||||
|
[MaxLength(200)]
|
||||||
|
public required string PropertyName { get; set; }
|
||||||
|
[MaxLength(1000)]
|
||||||
|
public string? OldValue { get; set; }
|
||||||
|
[MaxLength(1000)]
|
||||||
|
public string? NewValue { get; set; }
|
||||||
|
public DateTime ModifiedUtc { get; set; } = DateTime.UtcNow;
|
||||||
|
[MaxLength(200)]
|
||||||
|
public string? ChangedBy { get; set; }
|
||||||
|
public EntityState ChangeType { get; set; }
|
||||||
|
}
|
9
Manager.Data/Entities/Audit/NoAuditAttribute.cs
Normal file
9
Manager.Data/Entities/Audit/NoAuditAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies to ignore the properties in the entity to not audit.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
|
public class NoAuditAttribute : Attribute
|
||||||
|
{
|
||||||
|
}
|
@@ -1,7 +1,10 @@
|
|||||||
|
using Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
namespace Manager.Data.Entities;
|
namespace Manager.Data.Entities;
|
||||||
|
|
||||||
|
[NoAudit]
|
||||||
public abstract class DateTimeBase
|
public abstract class DateTimeBase
|
||||||
{
|
{
|
||||||
public DateTime CreatedAtUtc { get; set; } = DateTime.UtcNow;
|
public DateTime CreatedAtUtc { get; set; }
|
||||||
public DateTime LastModifiedUtc { get; set; } = DateTime.UtcNow;
|
public DateTime LastModifiedUtc { get; set; }
|
||||||
}
|
}
|
@@ -1,8 +1,10 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext;
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
public class CaptionEntity
|
[Auditable]
|
||||||
|
public class CaptionEntity : DateTimeBase
|
||||||
{
|
{
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public required string MediaId { get; set; }
|
public required string MediaId { get; set; }
|
||||||
|
@@ -1,17 +1,20 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext;
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
|
[Auditable]
|
||||||
public class ChannelEntity : DateTimeBase
|
public class ChannelEntity : DateTimeBase
|
||||||
{
|
{
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public required string Id { get; set; }
|
public required string Id { get; set; }
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public string? Handle { get; set; }
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbDescriptionStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbDescriptionStringSize)]
|
||||||
public string? Description { get; set; }
|
public string? Description { get; set; }
|
||||||
public DateTime JoinedDate { get; set; }
|
public List<MediaEntity> Media { get; set; } = [];
|
||||||
public long Subscribers { get; set; }
|
public List<PlaylistEntity> Playlists { get; set; } = [];
|
||||||
public long TotalVideos { get; set; }
|
public ClientAccountEntity? ClientAccount { get; set; }
|
||||||
public long TotalViews { get; set; }
|
|
||||||
}
|
}
|
@@ -1,12 +1,14 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext;
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
|
[Auditable]
|
||||||
public class ClientAccountEntity : DateTimeBase
|
public class ClientAccountEntity : DateTimeBase
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public string Name { get; set; } = "";
|
public required string Id { get; set; }
|
||||||
public List<PlaylistEntity> Playlists { get; set; } = [];
|
|
||||||
public List<HttpCookieEntity> HttpCookies { get; set; } = [];
|
public List<HttpCookieEntity> HttpCookies { get; set; } = [];
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public string? UserAgent { get; set; }
|
||||||
}
|
}
|
25
Manager.Data/Entities/LibraryContext/FileEntity.cs
Normal file
25
Manager.Data/Entities/LibraryContext/FileEntity.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
|
public class FileEntity : DateTimeBase
|
||||||
|
{
|
||||||
|
public required Guid Id { get; set; }
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public required string ForeignKey { get; set; }
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public required string FileType { get; set; }
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public required string RelativePath { get; set; }
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public string? MimeType { get; set; }
|
||||||
|
public long SizeBytes { get; set; }
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbUrlSize)]
|
||||||
|
public string? OriginalUrl { get; set; }
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public string? OriginalFileName { get; set; }
|
||||||
|
|
||||||
|
public int? Width { get; set; }
|
||||||
|
public int? Height { get; set; }
|
||||||
|
public long? LenghtMilliseconds { get; set; }
|
||||||
|
}
|
@@ -1,9 +1,13 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Manager.Data.Entities.Audit;
|
||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext;
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
|
[NoAudit]
|
||||||
public class HttpCookieEntity : DateTimeBase
|
public class HttpCookieEntity : DateTimeBase
|
||||||
{
|
{
|
||||||
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
public required string ClientId { get; set; }
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public required string Name { get; set; }
|
public required string Name { get; set; }
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
@@ -12,11 +16,7 @@ public class HttpCookieEntity : DateTimeBase
|
|||||||
public string? Domain { get; set; }
|
public string? Domain { get; set; }
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public string? Path { get; set; }
|
public string? Path { get; set; }
|
||||||
public DateTimeOffset? ExpiresUtc { get; set; }
|
public DateTime? ExpiresUtc { get; set; }
|
||||||
public bool Secure { get; set; }
|
public bool Secure { get; set; }
|
||||||
public bool HttpOnly { get; set; }
|
public bool HttpOnly { get; set; }
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
|
||||||
public string? SameSite { get; set; }
|
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
|
||||||
public required string ClientId { get; set; }
|
|
||||||
}
|
}
|
||||||
|
@@ -2,13 +2,10 @@ using System.ComponentModel.DataAnnotations;
|
|||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext.Join;
|
namespace Manager.Data.Entities.LibraryContext.Join;
|
||||||
|
|
||||||
public class PlaylistMedia
|
public class PlaylistMedia : DateTimeBase
|
||||||
{
|
{
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public required string PlaylistId { get; set; }
|
public required string PlaylistId { get; set; }
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public required string MediaId { get; set; }
|
public required string MediaId { get; set; }
|
||||||
|
|
||||||
public DateTime DateAddedUtc { get; set; }
|
|
||||||
public DateTime DateModifiedUtc { get; set; }
|
|
||||||
}
|
}
|
@@ -1,8 +1,10 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Manager.Data.Entities.Audit;
|
||||||
using Manager.Data.Entities.LibraryContext.Join;
|
using Manager.Data.Entities.LibraryContext.Join;
|
||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext;
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
|
[Auditable]
|
||||||
public class MediaEntity : DateTimeBase
|
public class MediaEntity : DateTimeBase
|
||||||
{
|
{
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
@@ -17,7 +19,6 @@ public class MediaEntity : DateTimeBase
|
|||||||
public List<MediaFormatEntity> Formats { get; set; } = [];
|
public List<MediaFormatEntity> Formats { get; set; } = [];
|
||||||
public List<CaptionEntity> Captions { get; set; } = [];
|
public List<CaptionEntity> Captions { get; set; } = [];
|
||||||
public List<PlaylistMedia> PlaylistMedias { get; set; } = [];
|
public List<PlaylistMedia> PlaylistMedias { get; set; } = [];
|
||||||
|
|
||||||
public MediaExternalState ExternalState { get; set; } = MediaExternalState.Online;
|
public MediaExternalState ExternalState { get; set; } = MediaExternalState.Online;
|
||||||
public bool IsDownloaded { get; set; }
|
public bool IsDownloaded { get; set; }
|
||||||
public MediaState State { get; set; } = MediaState.Indexed;
|
public MediaState State { get; set; } = MediaState.Indexed;
|
||||||
|
@@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations;
|
|||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext;
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
public class MediaFormatEntity
|
public class MediaFormatEntity : DateTimeBase
|
||||||
{
|
{
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
public required string MediaId { get; set; }
|
public required string MediaId { get; set; }
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Manager.Data.Entities.Audit;
|
||||||
using Manager.Data.Entities.LibraryContext.Join;
|
using Manager.Data.Entities.LibraryContext.Join;
|
||||||
|
|
||||||
namespace Manager.Data.Entities.LibraryContext;
|
namespace Manager.Data.Entities.LibraryContext;
|
||||||
|
|
||||||
|
[Auditable]
|
||||||
public class PlaylistEntity : DateTimeBase
|
public class PlaylistEntity : DateTimeBase
|
||||||
{
|
{
|
||||||
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
|
||||||
|
@@ -11,8 +11,12 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.19" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.8" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.19">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.19" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
9
Manager.YouTube/Models/AdditionalJsonData.cs
Normal file
9
Manager.YouTube/Models/AdditionalJsonData.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Models;
|
||||||
|
|
||||||
|
public class AdditionalJsonData
|
||||||
|
{
|
||||||
|
[JsonExtensionData]
|
||||||
|
public Dictionary<string, object> AdditionalData { get; set; } = [];
|
||||||
|
}
|
@@ -2,17 +2,42 @@ using System.Text.Json.Serialization;
|
|||||||
|
|
||||||
namespace Manager.YouTube.Models.Innertube;
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
public class ClientState
|
public class ClientState : AdditionalJsonData
|
||||||
{
|
{
|
||||||
[JsonExtensionData]
|
public bool IsPremiumUser { get; set; }
|
||||||
public Dictionary<string, object> AdditionalData { get; set; } = [];
|
|
||||||
|
|
||||||
[JsonPropertyName("INNERTUBE_API_KEY")]
|
[JsonPropertyName("INNERTUBE_API_KEY")]
|
||||||
public string? InnertubeApiKey { get; set; }
|
public string? InnertubeApiKey { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("LINK_API_KEY")]
|
||||||
|
public string? LinkApiKey { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("VOZ_API_KEY")]
|
||||||
|
public string? VozApiKey { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("SIGNIN_URL")]
|
[JsonPropertyName("SIGNIN_URL")]
|
||||||
public string? SigninUrl { get; set; }
|
public string? SigninUrl { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("INNERTUBE_CLIENT_NAME")]
|
||||||
|
public string? InnerTubeClient { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("INNERTUBE_CLIENT_VERSION")]
|
||||||
|
public string? InnerTubeClientVersion { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("LOGGED_IN")]
|
||||||
|
public bool LoggedIn { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("WEB_PLAYER_CONTEXT_CONFIGS")]
|
||||||
|
public WebPlayerContextConfig? WebPlayerContextConfig { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("USER_ACCOUNT_NAME")]
|
||||||
|
public string? UserAccountName { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("SERVER_VERSION")]
|
||||||
|
public string? ServerVersion { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("INNERTUBE_CONTEXT")]
|
||||||
|
public InnerTubeContext? InnerTubeContext { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("SBOX_SETTINGS")]
|
[JsonPropertyName("SBOX_SETTINGS")]
|
||||||
public SBoxSettings? SBoxSettings { get; set; }
|
public SBoxSettings? SBoxSettings { get; set; }
|
||||||
}
|
}
|
15
Manager.YouTube/Models/Innertube/InnerTubeClient.cs
Normal file
15
Manager.YouTube/Models/Innertube/InnerTubeClient.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
public class InnerTubeClient : AdditionalJsonData
|
||||||
|
{
|
||||||
|
[JsonPropertyName("hl")]
|
||||||
|
public string? HLanguage { get; set; }
|
||||||
|
[JsonPropertyName("gl")]
|
||||||
|
public string? GLanguage { get; set; }
|
||||||
|
[JsonPropertyName("remoteHost")]
|
||||||
|
public string? RemoteHost { get; set; }
|
||||||
|
[JsonPropertyName("rolloutToken")]
|
||||||
|
public string? RolloutToken { get; set; }
|
||||||
|
}
|
9
Manager.YouTube/Models/Innertube/InnerTubeContext.cs
Normal file
9
Manager.YouTube/Models/Innertube/InnerTubeContext.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
public class InnerTubeContext : AdditionalJsonData
|
||||||
|
{
|
||||||
|
[JsonPropertyName("client")]
|
||||||
|
public InnerTubeClient? InnerTubeClient { get; set; }
|
||||||
|
}
|
15
Manager.YouTube/Models/Innertube/InnertubeChannel.cs
Normal file
15
Manager.YouTube/Models/Innertube/InnertubeChannel.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
public class InnertubeChannel
|
||||||
|
{
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public bool NoIndex { get; set; }
|
||||||
|
public bool Unlisted { get; set; }
|
||||||
|
public bool FamilySafe { get; set; }
|
||||||
|
public string? ChannelName { get; set; }
|
||||||
|
public string? Handle { get; set; }
|
||||||
|
public string? Description { get; set; }
|
||||||
|
public List<string> AvailableCountries { get; set; } = [];
|
||||||
|
public List<WebImage> AvatarImages { get; set; } = [];
|
||||||
|
public List<WebImage> BannerImages { get; set; } = [];
|
||||||
|
}
|
8
Manager.YouTube/Models/Innertube/WebImage.cs
Normal file
8
Manager.YouTube/Models/Innertube/WebImage.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
public class WebImage
|
||||||
|
{
|
||||||
|
public int Width { get; set; }
|
||||||
|
public int Height { get; set; }
|
||||||
|
public string Url { get; set; } = "";
|
||||||
|
}
|
9
Manager.YouTube/Models/Innertube/WebPlayerContext.cs
Normal file
9
Manager.YouTube/Models/Innertube/WebPlayerContext.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
public class WebPlayerContext : AdditionalJsonData
|
||||||
|
{
|
||||||
|
[JsonPropertyName("datasyncId")]
|
||||||
|
public string? DatasyncId { get; set; }
|
||||||
|
}
|
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
public class WebPlayerContextConfig : AdditionalJsonData
|
||||||
|
{
|
||||||
|
[JsonPropertyName("WEB_PLAYER_CONTEXT_CONFIG_ID_KEVLAR_WATCH")]
|
||||||
|
public WebPlayerContext? WebPlayerContext { get; set; }
|
||||||
|
}
|
@@ -1,59 +1,57 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using DotBased.Monads;
|
using DotBased.Monads;
|
||||||
using Manager.YouTube.Models.Innertube;
|
|
||||||
using Manager.YouTube.Parsers;
|
|
||||||
using Manager.YouTube.Util;
|
using Manager.YouTube.Util;
|
||||||
|
|
||||||
namespace Manager.YouTube;
|
namespace Manager.YouTube;
|
||||||
|
|
||||||
public static class NetworkService
|
public static class NetworkService
|
||||||
{
|
{
|
||||||
public static async Task<Result<ClientState>> GetClientStateAsync(YouTubeClient client)
|
public const string Origin = "https://www.youtube.com";
|
||||||
{
|
private static readonly HttpClient HttpClient = new();
|
||||||
var origin = "https://www.youtube.com/";
|
|
||||||
var httpRequest = new HttpRequestMessage
|
|
||||||
{
|
|
||||||
Method = HttpMethod.Get,
|
|
||||||
RequestUri = new Uri(origin)
|
|
||||||
};
|
|
||||||
httpRequest.Headers.IfModifiedSince = new DateTimeOffset(DateTime.UtcNow);
|
|
||||||
httpRequest.Headers.UserAgent.ParseAdd(client.UserAgent);
|
|
||||||
|
|
||||||
if (client.SapisidCookie != null)
|
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false)
|
||||||
{
|
{
|
||||||
httpRequest.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.SapisidCookie.Value, origin);
|
request.Headers.Add("Origin", Origin);
|
||||||
httpRequest.Headers.Add("Origin", origin);
|
request.Headers.UserAgent.ParseAdd(client.UserAgent);
|
||||||
|
if (client.SapisidCookie != null && !skipAuthenticationHeader)
|
||||||
|
{
|
||||||
|
request.Headers.Authorization = AuthenticationUtilities.GetSapisidHashHeader(client.GetDatasyncId(), client.SapisidCookie.Value, Origin);
|
||||||
}
|
}
|
||||||
|
|
||||||
var http = client.GetHttpClient();
|
|
||||||
if (http == null)
|
|
||||||
{
|
|
||||||
return ResultError.Fail("Unable to get http client!");
|
|
||||||
}
|
|
||||||
|
|
||||||
var response = await http.SendAsync(httpRequest);
|
|
||||||
if (!response.IsSuccessStatusCode)
|
|
||||||
{
|
|
||||||
var responseResult = await response.Content.ReadAsStringAsync();
|
|
||||||
return Result<ClientState>.Fail(ResultError.Fail(responseResult));
|
|
||||||
}
|
|
||||||
var responseHtml = await response.Content.ReadAsStringAsync();
|
|
||||||
var clientStateResult = HtmlParser.GetJsonFromScriptFunction(responseHtml, "ytcfg.set");
|
|
||||||
if (clientStateResult is { IsSuccess: false, Error: not null })
|
|
||||||
{
|
|
||||||
return clientStateResult.Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClientState? clientState;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
clientState = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value);
|
var response = await client.HttpClient.SendAsync(request);
|
||||||
|
var contentString = await response.Content.ReadAsStringAsync();
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
return ResultError.Fail(contentString);
|
||||||
|
}
|
||||||
|
return contentString;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return ResultError.Error(e, "Error while parsing JSON!");
|
return ResultError.Error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientState == null ? ResultError.Fail("Unable to parse client state!") : clientState;
|
public static async Task<Result<DownloadResult>> DownloadBytesAsync(HttpRequestMessage request, YouTubeClient? client = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = client != null ? await client.HttpClient.SendAsync(request) : await HttpClient.SendAsync(request);
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
return ResultError.Fail($"Failed to get file to download, response code: {response.StatusCode}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var data = await response.Content.ReadAsByteArrayAsync();
|
||||||
|
|
||||||
|
return new DownloadResult(data, response.Content.Headers.ContentType?.MediaType, response.Content.Headers.ContentDisposition?.FileName?.Trim('"'), response.Content.Headers.ContentLength ?? 0);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public record DownloadResult(byte[] Data, string? ContentType, string? FileName, long ContentLength);
|
@@ -1,4 +1,3 @@
|
|||||||
using System.Text.RegularExpressions;
|
|
||||||
using DotBased.Monads;
|
using DotBased.Monads;
|
||||||
using HtmlAgilityPack;
|
using HtmlAgilityPack;
|
||||||
|
|
||||||
@@ -6,34 +5,61 @@ namespace Manager.YouTube.Parsers;
|
|||||||
|
|
||||||
public static class HtmlParser
|
public static class HtmlParser
|
||||||
{
|
{
|
||||||
public static Result<string> GetJsonFromScriptFunction(string html, string functionName)
|
public static Result<(string, bool)> GetStateJson(string html)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(html))
|
if (string.IsNullOrWhiteSpace(html))
|
||||||
{
|
{
|
||||||
return ResultError.Fail("html cannot be empty!");
|
return ResultError.Fail("html cannot be empty!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(functionName))
|
|
||||||
{
|
|
||||||
return ResultError.Fail("No function names provided!");
|
|
||||||
}
|
|
||||||
|
|
||||||
var htmlDocument = new HtmlDocument();
|
var htmlDocument = new HtmlDocument();
|
||||||
htmlDocument.LoadHtml(html);
|
htmlDocument.LoadHtml(html);
|
||||||
|
|
||||||
var scriptNode = htmlDocument.DocumentNode.SelectSingleNode($"//script[contains(., '{functionName}')]");
|
const string setFunction = "ytcfg.set({";
|
||||||
|
var scriptNode = htmlDocument.DocumentNode.SelectSingleNode($"//script[contains(., '{setFunction}')]");
|
||||||
if (string.IsNullOrWhiteSpace(scriptNode.InnerText))
|
if (string.IsNullOrWhiteSpace(scriptNode.InnerText))
|
||||||
return ResultError.Fail($"Could not find {functionName} in html script nodes!");
|
return ResultError.Fail($"Could not find {setFunction} in html script nodes!");
|
||||||
|
|
||||||
var regexPattern = $@"{Regex.Escape(functionName)}\(([^)]+)\);";
|
var json = ExtractJson(scriptNode.InnerText, "ytcfg.set(");
|
||||||
var match = Regex.Match(scriptNode.InnerText, regexPattern);
|
|
||||||
|
|
||||||
if (match.Success)
|
if (string.IsNullOrWhiteSpace(json))
|
||||||
{
|
{
|
||||||
var jsonString = match.Groups[1].Value.Trim();
|
return ResultError.Fail($"Could not find {setFunction} in html script nodes!");
|
||||||
return jsonString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ResultError.Fail($"Unable to parse {functionName} JSON!");
|
var isPremiumUser = html.Contains("logo-type=\"YOUTUBE_PREMIUM_LOGO\"", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
return (json, isPremiumUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
static string? ExtractJson(string input, string marker)
|
||||||
|
{
|
||||||
|
var start = input.IndexOf(marker, StringComparison.Ordinal);
|
||||||
|
if (start < 0) return null;
|
||||||
|
|
||||||
|
start += marker.Length;
|
||||||
|
|
||||||
|
// Skip until first '{'
|
||||||
|
while (start < input.Length && input[start] != '{')
|
||||||
|
start++;
|
||||||
|
|
||||||
|
if (start >= input.Length) return null;
|
||||||
|
|
||||||
|
var depth = 0;
|
||||||
|
var i = start;
|
||||||
|
|
||||||
|
for (; i < input.Length; i++)
|
||||||
|
{
|
||||||
|
if (input[i] == '{') depth++;
|
||||||
|
else if (input[i] == '}')
|
||||||
|
{
|
||||||
|
depth--;
|
||||||
|
if (depth != 0) continue;
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return input[start..i];
|
||||||
}
|
}
|
||||||
}
|
}
|
83
Manager.YouTube/Parsers/Json/ChannelJsonParser.cs
Normal file
83
Manager.YouTube/Parsers/Json/ChannelJsonParser.cs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using DotBased.Monads;
|
||||||
|
using Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Parsers.Json;
|
||||||
|
|
||||||
|
public static class ChannelJsonParser
|
||||||
|
{
|
||||||
|
public static Result<InnertubeChannel> ParseJsonToChannelData(string json)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var doc = JsonDocument.Parse(json);
|
||||||
|
var rootDoc = doc.RootElement;
|
||||||
|
|
||||||
|
var channelMetadata = rootDoc
|
||||||
|
.GetProperty("metadata")
|
||||||
|
.GetProperty("channelMetadataRenderer");
|
||||||
|
|
||||||
|
var channelId = channelMetadata.GetProperty("externalId").GetString();
|
||||||
|
if (channelId == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("No channel id found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var channel = new InnertubeChannel
|
||||||
|
{
|
||||||
|
Id = channelId,
|
||||||
|
ChannelName = channelMetadata.GetProperty("title").ToString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
var microformat = rootDoc.GetProperty("microformat").GetProperty("microformatDataRenderer");
|
||||||
|
|
||||||
|
channel.AvailableCountries = microformat
|
||||||
|
.GetProperty("availableCountries")
|
||||||
|
.EnumerateArray()
|
||||||
|
.Select(e => e.GetString())
|
||||||
|
.OfType<string>().ToList();
|
||||||
|
channel.Description = microformat.GetProperty("description").GetString();
|
||||||
|
channel.NoIndex = microformat.GetProperty("noindex").GetBoolean();
|
||||||
|
channel.Unlisted = microformat.GetProperty("unlisted").GetBoolean();
|
||||||
|
channel.FamilySafe = microformat.GetProperty("familySafe").GetBoolean();
|
||||||
|
|
||||||
|
var avatarThumbnails = channelMetadata.GetProperty("avatar")
|
||||||
|
.GetProperty("thumbnails")
|
||||||
|
.EnumerateArray();
|
||||||
|
channel.AvatarImages = JsonParser.ParseImages(avatarThumbnails);
|
||||||
|
|
||||||
|
var headerContent = rootDoc
|
||||||
|
.GetProperty("header")
|
||||||
|
.GetProperty("pageHeaderRenderer")
|
||||||
|
.GetProperty("content");
|
||||||
|
|
||||||
|
channel.Handle = headerContent
|
||||||
|
.GetProperty("pageHeaderViewModel")
|
||||||
|
.GetProperty("metadata")
|
||||||
|
.GetProperty("contentMetadataViewModel")
|
||||||
|
.GetProperty("metadataRows")
|
||||||
|
.EnumerateArray()
|
||||||
|
.FirstOrDefault()
|
||||||
|
.GetProperty("metadataParts")
|
||||||
|
.EnumerateArray()
|
||||||
|
.FirstOrDefault()
|
||||||
|
.GetProperty("text")
|
||||||
|
.GetProperty("content").GetString();
|
||||||
|
|
||||||
|
var bannerImages = headerContent
|
||||||
|
.GetProperty("pageHeaderViewModel")
|
||||||
|
.GetProperty("banner")
|
||||||
|
.GetProperty("imageBannerViewModel")
|
||||||
|
.GetProperty("image")
|
||||||
|
.GetProperty("sources")
|
||||||
|
.EnumerateArray();
|
||||||
|
channel.BannerImages = JsonParser.ParseImages(bannerImages);
|
||||||
|
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
Manager.YouTube/Parsers/Json/JsonAccountParser.cs
Normal file
43
Manager.YouTube/Parsers/Json/JsonAccountParser.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using DotBased.Monads;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Parsers.Json;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parsing functionality for the response from endpoint: /youtubei/v1/account/account_menu
|
||||||
|
/// </summary>
|
||||||
|
public static class JsonAccountParser
|
||||||
|
{
|
||||||
|
public static Result<string> ParseAccountId(string json)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var jsonDocument = JsonDocument.Parse(json);
|
||||||
|
|
||||||
|
var id = jsonDocument.RootElement
|
||||||
|
.GetProperty("actions")[0]
|
||||||
|
.GetProperty("openPopupAction")
|
||||||
|
.GetProperty("popup")
|
||||||
|
.GetProperty("multiPageMenuRenderer")
|
||||||
|
.GetProperty("header")
|
||||||
|
.GetProperty("activeAccountHeaderRenderer")
|
||||||
|
.GetProperty("manageAccountTitle")
|
||||||
|
.GetProperty("runs")
|
||||||
|
.EnumerateArray()
|
||||||
|
.FirstOrDefault()
|
||||||
|
.GetProperty("navigationEndpoint")
|
||||||
|
.GetProperty("browseEndpoint")
|
||||||
|
.GetProperty("browseId").GetString();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Unable to get account id!");
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
Manager.YouTube/Parsers/Json/JsonParser.cs
Normal file
12
Manager.YouTube/Parsers/Json/JsonParser.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using Manager.YouTube.Models.Innertube;
|
||||||
|
|
||||||
|
namespace Manager.YouTube.Parsers.Json;
|
||||||
|
|
||||||
|
public static class JsonParser
|
||||||
|
{
|
||||||
|
public static List<WebImage> ParseImages(JsonElement.ArrayEnumerator array) =>
|
||||||
|
array
|
||||||
|
.Select(image => new WebImage { Width = image.GetProperty("width").GetInt32(), Height = image.GetProperty("height").GetInt32(), Url = image.GetProperty("url").GetString() ?? "" })
|
||||||
|
.ToList();
|
||||||
|
}
|
@@ -9,15 +9,37 @@ public static class AuthenticationUtilities
|
|||||||
{
|
{
|
||||||
private const string HeaderScheme = "SAPISIDHASH";
|
private const string HeaderScheme = "SAPISIDHASH";
|
||||||
|
|
||||||
// Dave Thomas @ https://stackoverflow.com/a/32065323/9948300
|
// Dave Thomas & windy for updated answer @ https://stackoverflow.com/a/32065323/9948300
|
||||||
public static AuthenticationHeaderValue? GetSapisidHashHeader(string sapisid, string origin)
|
public static AuthenticationHeaderValue GetSapisidHashHeader(string datasyncId, string sapisid, string origin)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(sapisid) || string.IsNullOrWhiteSpace(origin))
|
var strHash = GetSapisidHash(datasyncId, sapisid, origin);
|
||||||
return null;
|
return new AuthenticationHeaderValue(HeaderScheme, strHash);
|
||||||
var time = GetTime();
|
}
|
||||||
var sha1 = HashString($"{time} {sapisid} {origin}");
|
|
||||||
var completeHash = $"{time}_{sha1}";
|
public static string GetSapisidHash(string datasyncId, string sapisid, string origin, string? time = null)
|
||||||
return new AuthenticationHeaderValue(HeaderScheme, completeHash);
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(datasyncId))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(datasyncId));
|
||||||
|
}
|
||||||
|
if (string.IsNullOrWhiteSpace(sapisid))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(sapisid));
|
||||||
|
}
|
||||||
|
if (string.IsNullOrWhiteSpace(origin))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(origin));
|
||||||
|
}
|
||||||
|
|
||||||
|
datasyncId = datasyncId.Replace("||", "");
|
||||||
|
sapisid = Uri.UnescapeDataString(sapisid);
|
||||||
|
if (string.IsNullOrWhiteSpace(time))
|
||||||
|
{
|
||||||
|
time = GetTime();
|
||||||
|
}
|
||||||
|
var sha1 = HashString($"{datasyncId} {time} {sapisid} {origin}");
|
||||||
|
var completeHash = $"{time}_{sha1}_u";
|
||||||
|
return completeHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string HashString(string stringData)
|
private static string HashString(string stringData)
|
||||||
|
@@ -1,59 +1,264 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using DotBased.Logging;
|
using System.Net.Mime;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
using DotBased.Monads;
|
||||||
using Manager.YouTube.Models.Innertube;
|
using Manager.YouTube.Models.Innertube;
|
||||||
|
using Manager.YouTube.Parsers;
|
||||||
|
using Manager.YouTube.Parsers.Json;
|
||||||
|
|
||||||
namespace Manager.YouTube;
|
namespace Manager.YouTube;
|
||||||
|
|
||||||
public sealed class YouTubeClient : IDisposable
|
public sealed class YouTubeClient : IDisposable
|
||||||
{
|
{
|
||||||
public string Id { get; private set; }
|
public string Id { get; private set; } = "";
|
||||||
public string AccountName { get; private set; }
|
public string? UserAgent { get; set; }
|
||||||
public string? UserAgent { get; private set; }
|
public bool IsAnonymous { get; }
|
||||||
public CookieContainer CookieContainer { get; }
|
public CookieContainer CookieContainer { get; } = new() { PerDomainCapacity = 50 };
|
||||||
public ClientState? ClientState { get; private set; }
|
public ClientState? State { get; private set; }
|
||||||
|
public List<string> DatasyncIds { get; } = [];
|
||||||
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
|
public Cookie? SapisidCookie => CookieContainer.GetAllCookies()["SAPISID"];
|
||||||
public HttpClient? GetHttpClient() => _httpClient;
|
public HttpClient HttpClient { get; }
|
||||||
|
|
||||||
private readonly ILogger? _logger;
|
private YouTubeClient(CookieCollection cookies, string userAgent)
|
||||||
private HttpClient? _httpClient;
|
|
||||||
|
|
||||||
public YouTubeClient(CookieContainer cookieContainer, string userAgent, ILogger? logger = null)
|
|
||||||
{
|
{
|
||||||
CookieContainer = cookieContainer;
|
if (string.IsNullOrWhiteSpace(userAgent))
|
||||||
_logger = logger;
|
{
|
||||||
|
throw new ArgumentNullException(nameof(userAgent));
|
||||||
|
}
|
||||||
UserAgent = userAgent;
|
UserAgent = userAgent;
|
||||||
SetupClient();
|
if (cookies.Count == 0)
|
||||||
|
{
|
||||||
|
Id = $"anon_{Guid.NewGuid()}";
|
||||||
|
IsAnonymous = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetupClient()
|
CookieContainer.Add(cookies);
|
||||||
{
|
HttpClient = new HttpClient(GetHttpClientHandler());
|
||||||
_logger?.Information("Building http client...");
|
}
|
||||||
_httpClient?.Dispose();
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads the given cookies and fetch client state.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cookies">The cookies to use for making requests. Empty collection for anonymous requests.</param>
|
||||||
|
/// <param name="userAgent">The user agent to use for the requests. Only WEB client is supported.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<Result<YouTubeClient>> CreateAsync(CookieCollection cookies, string userAgent)
|
||||||
|
{
|
||||||
|
var client = new YouTubeClient(cookies, userAgent);
|
||||||
|
var clientInitializeResult = await client.FetchClientDataAsync();
|
||||||
|
if (!clientInitializeResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return clientInitializeResult.Error ?? ResultError.Fail("Failed to initialize YouTube client!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpClientHandler GetHttpClientHandler()
|
||||||
|
{
|
||||||
var clientHandler = new HttpClientHandler
|
var clientHandler = new HttpClientHandler
|
||||||
{
|
{
|
||||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
||||||
UseCookies = true,
|
UseCookies = true,
|
||||||
CookieContainer = CookieContainer
|
CookieContainer = CookieContainer
|
||||||
};
|
};
|
||||||
_httpClient = new HttpClient(clientHandler);
|
return clientHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task GetStateAsync()
|
internal async Task<Result> FetchClientDataAsync()
|
||||||
{
|
{
|
||||||
var state = await NetworkService.GetClientStateAsync(this);
|
if (State is not { LoggedIn: true })
|
||||||
|
{
|
||||||
|
var state = await GetClientStateAsync();
|
||||||
if (!state.IsSuccess)
|
if (!state.IsSuccess)
|
||||||
{
|
{
|
||||||
_logger?.Warning("Error getting client state: {StateError}", state.Error);
|
return state;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientState = state.Value;
|
if (string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||||
_logger?.Information("Client state retrieved. With API key: {InnertubeApiKey}", ClientState.InnertubeApiKey);
|
{
|
||||||
|
var datasyncResult = await GetDatasyncIds();
|
||||||
|
if (!datasyncResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return datasyncResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var id in datasyncResult.Value)
|
||||||
|
{
|
||||||
|
if (DatasyncIds.Contains(id))
|
||||||
|
continue;
|
||||||
|
DatasyncIds.Add(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(Id))
|
||||||
|
{
|
||||||
|
var accountInfoResult = await GetCurrentAccountIdAsync();
|
||||||
|
if (!accountInfoResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return accountInfoResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
Id = accountInfoResult.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<Result> GetClientStateAsync()
|
||||||
|
{
|
||||||
|
var httpRequest = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Get,
|
||||||
|
RequestUri = new Uri(NetworkService.Origin)
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = await NetworkService.MakeRequestAsync(httpRequest, this, true);
|
||||||
|
if (!result.IsSuccess)
|
||||||
|
{
|
||||||
|
return result.Error ?? ResultError.Fail("Request failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
var clientStateResult = HtmlParser.GetStateJson(result.Value);
|
||||||
|
if (clientStateResult is { IsSuccess: false, Error: not null })
|
||||||
|
{
|
||||||
|
return clientStateResult.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
State = JsonSerializer.Deserialize<ClientState>(clientStateResult.Value.Item1);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return ResultError.Error(e, "Error while parsing JSON!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (State == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Unable to parse client state!");
|
||||||
|
}
|
||||||
|
|
||||||
|
State.IsPremiumUser = clientStateResult.Value.Item2;
|
||||||
|
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<InnertubeChannel>> GetChannelByIdAsync(string channelId)
|
||||||
|
{
|
||||||
|
if (State == null)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("No client state!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(channelId))
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Channel id is empty!");
|
||||||
|
}
|
||||||
|
|
||||||
|
var serializedContext = JsonSerializer.SerializeToNode(State.InnerTubeContext);
|
||||||
|
var payload = new JsonObject { { "context", serializedContext }, { "browseId", channelId } };
|
||||||
|
var requestMessage = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Post,
|
||||||
|
RequestUri = new Uri($"{NetworkService.Origin}/youtubei/v1/browse?key={State.InnertubeApiKey}"),
|
||||||
|
Content = new StringContent(payload.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json)
|
||||||
|
};
|
||||||
|
var responseResult = await NetworkService.MakeRequestAsync(requestMessage, this);
|
||||||
|
if (!responseResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return responseResult.Error ?? ResultError.Fail("Request failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ChannelJsonParser.ParseJsonToChannelData(responseResult.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetDatasyncId()
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(State?.WebPlayerContextConfig?.WebPlayerContext?.DatasyncId))
|
||||||
|
{
|
||||||
|
return State.WebPlayerContextConfig.WebPlayerContext.DatasyncId;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tempDatasyncId = "";
|
||||||
|
foreach (var datasyncId in DatasyncIds)
|
||||||
|
{
|
||||||
|
var split = datasyncId.Split("||", StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
switch (split.Length)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
case 2 when tempDatasyncId.Equals(split[1]):
|
||||||
|
continue;
|
||||||
|
case 2:
|
||||||
|
tempDatasyncId = split[1];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tempDatasyncId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_httpClient?.Dispose();
|
HttpClient.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<Result<string>> GetCurrentAccountIdAsync()
|
||||||
|
{
|
||||||
|
if (State is not { LoggedIn: true })
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Client not logged in!");
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpRequest = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Post,
|
||||||
|
RequestUri = new Uri($"{NetworkService.Origin}/youtubei/v1/account/account_menu")
|
||||||
|
};
|
||||||
|
var serializedContext = JsonSerializer.SerializeToNode(State.InnerTubeContext);
|
||||||
|
var payload = new JsonObject { { "context", serializedContext } };
|
||||||
|
httpRequest.Content = new StringContent(payload.ToJsonString(), Encoding.UTF8, MediaTypeNames.Application.Json);
|
||||||
|
var responseResult = await NetworkService.MakeRequestAsync(httpRequest, this);
|
||||||
|
|
||||||
|
if (!responseResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return responseResult.Error ?? ResultError.Fail("Request failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonAccountParser.ParseAccountId(responseResult.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<Result<string[]>> GetDatasyncIds()
|
||||||
|
{
|
||||||
|
if (State is not { LoggedIn: true } || CookieContainer.Count == 0)
|
||||||
|
{
|
||||||
|
return ResultError.Fail("Client is not logged in, requires logged in client for this endpoint (/getDatasyncIdsEndpoint).");
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpRequest = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Get,
|
||||||
|
RequestUri = new Uri($"{NetworkService.Origin}/getDatasyncIdsEndpoint")
|
||||||
|
};
|
||||||
|
var responseResult = await NetworkService.MakeRequestAsync(httpRequest, this);
|
||||||
|
if (!responseResult.IsSuccess)
|
||||||
|
{
|
||||||
|
return responseResult.Error ?? ResultError.Fail("Request failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
var datasyncIdsJson = JsonNode.Parse(responseResult.Value.Replace(")]}'", ""));
|
||||||
|
|
||||||
|
var isLoggedOut = datasyncIdsJson?["responseContext"]?["mainAppWebResponseContext"]?["loggedOut"]
|
||||||
|
.Deserialize<bool>() ?? true;
|
||||||
|
if (!isLoggedOut)
|
||||||
|
{
|
||||||
|
return datasyncIdsJson?["datasyncIds"].Deserialize<string[]>() ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResultError.Fail("Failed to get datasyncIds! Client not logged in.");
|
||||||
}
|
}
|
||||||
}
|
}
|
11
Manager.YouTube/YouTubeClientCollection.cs
Normal file
11
Manager.YouTube/YouTubeClientCollection.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace Manager.YouTube;
|
||||||
|
|
||||||
|
public class YouTubeClientCollection : KeyedCollection<string, YouTubeClient>
|
||||||
|
{
|
||||||
|
protected override string GetKeyForItem(YouTubeClient item)
|
||||||
|
{
|
||||||
|
return item.Id;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,6 +0,0 @@
|
|||||||
namespace Manager.YouTube;
|
|
||||||
|
|
||||||
public class YouTubeService()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
11
README.md
11
README.md
@@ -4,12 +4,15 @@ Server application for managing YouTube content and managing a local library.
|
|||||||
### Migrations
|
### Migrations
|
||||||
Using migrations using the .NET CLI
|
Using migrations using the .NET CLI
|
||||||
|
|
||||||
Add migration
|
Add migration (library)
|
||||||
```shell
|
```shell
|
||||||
dotnet ef migrations add <MigrationName> --project Manager.Data --context Manager.Data.Contexts.AppDbContext
|
dotnet ef migrations add <migration-name> --project Manager.Data --startup-project Manager.App --context Manager.Data.Contexts.LibraryDbContext
|
||||||
```
|
```
|
||||||
Update the database
|
Update the database
|
||||||
```shell
|
```shell
|
||||||
dotnet ef database update --project Manager.Data --context Manager.Data.Contexts.AppDbContext
|
dotnet ef database update --project Manager.Data --startup-project Manager.App --context Manager.Data.Contexts.LibraryDbContext
|
||||||
|
```
|
||||||
|
Remove migration
|
||||||
|
```shell
|
||||||
|
dotnet ef migrations remove --project Manager.Data --startup-project Manager.App --context Manager.Data.Contexts.LibraryDbContext
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user