[CHANGE] Remove anon accounts && added simple caching for urls

This commit is contained in:
max
2025-09-17 23:44:02 +02:00
parent 0056a14f79
commit 8a64d6fc64
15 changed files with 235 additions and 50 deletions

22
.gitignore vendored
View File

@@ -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,16 +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/Library/
# Manager.App
[Ll]ibrary/
[Cc]ache/

View File

@@ -1,4 +1,6 @@
@using Manager.App.Services.System
@inject ISnackbar SnackbarService @inject ISnackbar SnackbarService
@inject CacheService Cache
<ForcedLoadingOverlay Visible="_isLoading"/> <ForcedLoadingOverlay Visible="_isLoading"/>
@@ -19,16 +21,15 @@
case AccountImportSteps.Authenticate: case AccountImportSteps.Authenticate:
<MudStack Spacing="2"> <MudStack Spacing="2">
<MudPaper Elevation="0" Outlined Class="pa-2"> <MudPaper Elevation="0" Outlined Class="pa-2">
<MudSwitch @bind-Value="@IsAnonymous" Color="Color.Info">Anonymous client</MudSwitch>
<MudTextField @bind-Value="@DefaultUserAgent" Required Label="User agent" <MudTextField @bind-Value="@DefaultUserAgent" Required Label="User agent"
HelperText="Use an WEB client user agent."/> HelperText="Use an WEB user agent."/>
</MudPaper> </MudPaper>
<MudStack Row Spacing="2" Style="height: 100%"> <MudStack Row Spacing="2" Style="height: 100%">
<MudPaper Elevation="0" Outlined Class="pa-2" Style="width: 50%;"> <MudPaper Elevation="0" Outlined Class="pa-2" Style="width: 50%;">
<MudText>Import cookies</MudText> <MudText>Import cookies</MudText>
<MudText Typo="Typo.caption">@($"{ImportCookies.Count} cookie(s) imported")</MudText> <MudText Typo="Typo.caption">@($"{ImportCookies.Count} cookie(s) imported")</MudText>
<MudForm @bind-IsValid="@_cookieImportTextValid" Disabled="@(IsAnonymous)"> <MudForm @bind-IsValid="@_cookieImportTextValid">
<MudTextField @bind-Value="@_cookieDomain" Immediate Required Label="Domain" <MudTextField @bind-Value="@_cookieDomain" Immediate Required Label="Domain"
RequiredError="Domain is required."/> RequiredError="Domain is required."/>
<MudTextField Class="my-2" Lines="4" AutoGrow @bind-Value="@_cookieText" Immediate <MudTextField Class="my-2" Lines="4" AutoGrow @bind-Value="@_cookieText" Immediate
@@ -76,7 +77,7 @@
<MudPaper Elevation="0"> <MudPaper Elevation="0">
@if (banner != null) @if (banner != null)
{ {
<MudImage Src="@banner.Url" Height="250" Style="width: 100%;"/> <MudImage Src="@Cache.CreateCacheUrl(banner.Url)" Height="250" Style="width: 100%;"/>
} }
else else
{ {
@@ -85,7 +86,7 @@
<MudStack Row Spacing="3" Class="px-4"> <MudStack Row Spacing="3" Class="px-4">
@if (avatar != null) @if (avatar != null)
{ {
<MudImage Src="@avatar.Url" Class="mt-n5" Height="100" Width="100"/> <MudImage Src="@Cache.CreateCacheUrl(avatar.Url)" Class="mt-n5" Height="100" Width="100"/>
} }
else else
{ {

View File

@@ -10,7 +10,6 @@ namespace Manager.App.Components.Dialogs
{ {
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; } [CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
[Parameter] public string DefaultUserAgent { get; set; } = ""; [Parameter] public string DefaultUserAgent { get; set; } = "";
private bool IsAnonymous { get; set; }
private ClientPrep? PreparingClient { get; set; } private ClientPrep? PreparingClient { get; set; }
private CookieCollection ImportCookies { get; set; } = []; private CookieCollection ImportCookies { get; set; } = [];
private bool _isLoading; private bool _isLoading;
@@ -22,12 +21,7 @@ namespace Manager.App.Components.Dialogs
private bool CanSave() private bool CanSave()
{ {
if (IsAnonymous || PreparingClient?.YouTubeClient?.State?.LoggedIn == true) return PreparingClient?.YouTubeClient?.State?.LoggedIn == true;
{
return true;
}
return false;
} }
private bool CanContinue() private bool CanContinue()
@@ -35,13 +29,13 @@ namespace Manager.App.Components.Dialogs
switch (_steps) switch (_steps)
{ {
case AccountImportSteps.Authenticate: case AccountImportSteps.Authenticate:
if (IsAnonymous || ImportCookies.Count != 0) if (ImportCookies.Count != 0)
{ {
return true; return true;
} }
break; break;
case AccountImportSteps.Validate: case AccountImportSteps.Validate:
if (IsAnonymous || PreparingClient?.YouTubeClient?.State?.LoggedIn == true) if (PreparingClient?.YouTubeClient?.State?.LoggedIn == true)
{ {
return true; return true;
} }
@@ -96,7 +90,6 @@ namespace Manager.App.Components.Dialogs
{ {
PreparingClient?.YouTubeClient?.Dispose(); PreparingClient?.YouTubeClient?.Dispose();
PreparingClient = null; PreparingClient = null;
IsAnonymous = false;
ImportCookies.Clear(); ImportCookies.Clear();
_steps = AccountImportSteps.Authenticate; _steps = AccountImportSteps.Authenticate;
StateHasChanged(); StateHasChanged();
@@ -150,10 +143,6 @@ namespace Manager.App.Components.Dialogs
{ {
_isLoading = true; _isLoading = true;
PreparingClient = new ClientPrep(); PreparingClient = new ClientPrep();
if (IsAnonymous)
{
ImportCookies.Clear();
}
var clientResult = await YouTubeClient.CreateAsync(ImportCookies, DefaultUserAgent); var clientResult = await YouTubeClient.CreateAsync(ImportCookies, DefaultUserAgent);
if (clientResult.IsSuccess) if (clientResult.IsSuccess)
{ {

View File

@@ -13,7 +13,7 @@ public partial class Channels : ComponentBase
private async Task<TableData<ChannelEntity>> ServerReload(TableState state, CancellationToken token) private async Task<TableData<ChannelEntity>> ServerReload(TableState state, CancellationToken token)
{ {
var results = await LibraryService.GetChannelAccountsAsync(state.PageSize, state.Page * state.PageSize, 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 }; return !results.IsSuccess ? new TableData<ChannelEntity>() : new TableData<ChannelEntity> { Items = results.Value, TotalItems = results.Total };
} }
@@ -42,12 +42,11 @@ public partial class Channels : ComponentBase
} }
else else
{ {
Snackbar.Add($"Client {clientPrep.Channel?.Handle ?? clientPrep.YouTubeClient.Id} saved!", Severity.Success);
}
if (_table != null) if (_table != null)
{ {
await _table.ReloadServerData(); await _table.ReloadServerData();
} }
Snackbar.Add($"Client {clientPrep.Channel?.Handle ?? clientPrep.YouTubeClient.Id} saved!", Severity.Success);
}
} }
} }

View File

@@ -27,7 +27,9 @@ public partial class Services : ComponentBase
private List<ServiceEvent> GetInitialEvents() private List<ServiceEvent> GetInitialEvents()
{ {
var totalToGet = 1000 / _backgroundServices.Count; var totalToGet = 1000 / _backgroundServices.Count;
var initial = _backgroundServices.SelectMany(x => x.ProgressEvents.Items.TakeLast(totalToGet)); var initial = _backgroundServices
.SelectMany(x => x.ProgressEvents.Items.TakeLast(totalToGet))
.OrderBy(x => x.DateUtc);
return initial.ToList(); return initial.ToList();
} }

View 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)
{
if (string.IsNullOrWhiteSpace(url))
{
return BadRequest("No url given.");
}
var cacheResult = await cacheService.CacheFromUrl(url);
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);
}
}

View File

@@ -32,6 +32,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="cache\" />
<Folder Include="Library\" /> <Folder Include="Library\" />
<Folder Include="Logs\Debug\" /> <Folder Include="Logs\Debug\" />
</ItemGroup> </ItemGroup>

View File

@@ -10,6 +10,8 @@ builder.Services.AddRazorComponents()
AppContext.SetSwitch("System.Net.Http.EnableActivityPropagation", false); AppContext.SetSwitch("System.Net.Http.EnableActivityPropagation", false);
builder.Services.AddControllers();
/* Manager */ /* Manager */
builder.SetupLogging(); builder.SetupLogging();
builder.SetupSettings(); builder.SetupSettings();
@@ -32,6 +34,7 @@ app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseAntiforgery(); app.UseAntiforgery();
app.MapControllers();
app.MapRazorComponents<App>() app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode(); .AddInteractiveServerRenderMode();

View File

@@ -30,8 +30,9 @@ public abstract class ExtendedBackgroundService(string name, string description,
await _resumeSignal.Task.WaitAsync(stoppingToken); await _resumeSignal.Task.WaitAsync(stoppingToken);
} }
await Task.Delay(ExecuteInterval, stoppingToken);
await ExecuteServiceAsync(stoppingToken); await ExecuteServiceAsync(stoppingToken);
await Task.Delay(ExecuteInterval, stoppingToken);
} }
} }
catch (Exception e) catch (Exception e)

View File

@@ -13,5 +13,5 @@ public interface ILibraryService
public Task<Result> SaveChannelAsync(ChannelEntity channel, CancellationToken cancellationToken = default); public Task<Result> SaveChannelAsync(ChannelEntity channel, CancellationToken cancellationToken = default);
public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default); public Task<Result<LibraryInformation>> GetLibraryInfoAsync(CancellationToken cancellationToken = default);
public Task<ListResult<ChannelEntity>> GetChannelAccountsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default); public Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default);
} }

View File

@@ -168,7 +168,7 @@ public class LibraryService : ILibraryService
} }
} }
public async Task<ListResult<ChannelEntity>> GetChannelAccountsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default) public async Task<ListResult<ChannelEntity>> GetChannelsAsync(int total = 20, int offset = 0, CancellationToken cancellationToken = default)
{ {
try try
{ {

View File

@@ -0,0 +1,151 @@
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.FromDays(1))
{
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.");
}
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)");
dbContext.RemoveRange(toRemove);
var deleted = await dbContext.SaveChangesAsync(cancellationToken);
LogEvent($"Removed {deleted}/{totalToRemove} items");
}
}
public record CacheFile(byte[] Data, string? ContentType, string? OriginalFileName);

View File

@@ -10,7 +10,7 @@ namespace Manager.App.Services.System;
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger) public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger)
: ExtendedBackgroundService(nameof(ClientService), "Managing YouTube clients", logger, TimeSpan.FromMinutes(10)) : ExtendedBackgroundService(nameof(ClientService), "Managing YouTube clients", logger, TimeSpan.FromMinutes(10))
{ {
private readonly List<YouTubeClient> _clients = []; private readonly List<YouTubeClient> _loadedClients = [];
private CancellationToken _cancellationToken; private CancellationToken _cancellationToken;
private ILibraryService? _libraryService; private ILibraryService? _libraryService;

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

View File

@@ -6,7 +6,7 @@ namespace Manager.YouTube;
public static class NetworkService public static class NetworkService
{ {
public const string Origin = "https://www.youtube.com"; public const string Origin = "https://www.youtube.com";
private static readonly HttpClient HttpClient = new HttpClient(); private static readonly HttpClient HttpClient = new();
public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false) public static async Task<Result<string>> MakeRequestAsync(HttpRequestMessage request, YouTubeClient client, bool skipAuthenticationHeader = false)
{ {
@@ -43,7 +43,7 @@ public static class NetworkService
return ResultError.Fail($"Failed to get file to download, response code: {response.StatusCode}."); return ResultError.Fail($"Failed to get file to download, response code: {response.StatusCode}.");
} }
var data = await response.Content.ReadAsByteArrayAsync();; 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); return new DownloadResult(data, response.Content.Headers.ContentType?.MediaType, response.Content.Headers.ContentDisposition?.FileName?.Trim('"'), response.Content.Headers.ContentLength ?? 0);
} }