[CHANGE] Remove anon accounts && added simple caching for urls
This commit is contained in:
22
.gitignore
vendored
22
.gitignore
vendored
@@ -307,10 +307,6 @@ node_modules/
|
||||
*.dsw
|
||||
*.dsp
|
||||
|
||||
# Visual Studio 6 technical files
|
||||
*.ncb
|
||||
*.aps
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
@@ -404,14 +400,8 @@ FodyWeavers.xsd
|
||||
*.sln.iml
|
||||
.idea
|
||||
|
||||
##
|
||||
## Visual studio for Mac
|
||||
##
|
||||
|
||||
|
||||
# globs
|
||||
Makefile.in
|
||||
*.userprefs
|
||||
*.usertasks
|
||||
config.make
|
||||
config.status
|
||||
@@ -470,16 +460,12 @@ ehthumbs_vista.db
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# Vim temporary swap files
|
||||
*.swp
|
||||
/Manager.App/Library/
|
||||
|
||||
# Manager.App
|
||||
[Ll]ibrary/
|
||||
[Cc]ache/
|
||||
|
@@ -1,4 +1,6 @@
|
||||
@using Manager.App.Services.System
|
||||
@inject ISnackbar SnackbarService
|
||||
@inject CacheService Cache
|
||||
|
||||
<ForcedLoadingOverlay Visible="_isLoading"/>
|
||||
|
||||
@@ -19,16 +21,15 @@
|
||||
case AccountImportSteps.Authenticate:
|
||||
<MudStack Spacing="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"
|
||||
HelperText="Use an WEB client 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" Disabled="@(IsAnonymous)">
|
||||
<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
|
||||
@@ -76,7 +77,7 @@
|
||||
<MudPaper Elevation="0">
|
||||
@if (banner != null)
|
||||
{
|
||||
<MudImage Src="@banner.Url" Height="250" Style="width: 100%;"/>
|
||||
<MudImage Src="@Cache.CreateCacheUrl(banner.Url)" Height="250" Style="width: 100%;"/>
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -85,7 +86,7 @@
|
||||
<MudStack Row Spacing="3" Class="px-4">
|
||||
@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
|
||||
{
|
||||
|
@@ -10,7 +10,6 @@ namespace Manager.App.Components.Dialogs
|
||||
{
|
||||
[CascadingParameter] private IMudDialogInstance? MudDialog { get; set; }
|
||||
[Parameter] public string DefaultUserAgent { get; set; } = "";
|
||||
private bool IsAnonymous { get; set; }
|
||||
private ClientPrep? PreparingClient { get; set; }
|
||||
private CookieCollection ImportCookies { get; set; } = [];
|
||||
private bool _isLoading;
|
||||
@@ -22,12 +21,7 @@ namespace Manager.App.Components.Dialogs
|
||||
|
||||
private bool CanSave()
|
||||
{
|
||||
if (IsAnonymous || PreparingClient?.YouTubeClient?.State?.LoggedIn == true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return PreparingClient?.YouTubeClient?.State?.LoggedIn == true;
|
||||
}
|
||||
|
||||
private bool CanContinue()
|
||||
@@ -35,13 +29,13 @@ namespace Manager.App.Components.Dialogs
|
||||
switch (_steps)
|
||||
{
|
||||
case AccountImportSteps.Authenticate:
|
||||
if (IsAnonymous || ImportCookies.Count != 0)
|
||||
if (ImportCookies.Count != 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case AccountImportSteps.Validate:
|
||||
if (IsAnonymous || PreparingClient?.YouTubeClient?.State?.LoggedIn == true)
|
||||
if (PreparingClient?.YouTubeClient?.State?.LoggedIn == true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -96,7 +90,6 @@ namespace Manager.App.Components.Dialogs
|
||||
{
|
||||
PreparingClient?.YouTubeClient?.Dispose();
|
||||
PreparingClient = null;
|
||||
IsAnonymous = false;
|
||||
ImportCookies.Clear();
|
||||
_steps = AccountImportSteps.Authenticate;
|
||||
StateHasChanged();
|
||||
@@ -150,10 +143,6 @@ namespace Manager.App.Components.Dialogs
|
||||
{
|
||||
_isLoading = true;
|
||||
PreparingClient = new ClientPrep();
|
||||
if (IsAnonymous)
|
||||
{
|
||||
ImportCookies.Clear();
|
||||
}
|
||||
var clientResult = await YouTubeClient.CreateAsync(ImportCookies, DefaultUserAgent);
|
||||
if (clientResult.IsSuccess)
|
||||
{
|
||||
|
@@ -13,7 +13,7 @@ public partial class Channels : ComponentBase
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
@@ -42,12 +42,11 @@ public partial class Channels : ComponentBase
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_table != null)
|
||||
{
|
||||
await _table.ReloadServerData();
|
||||
}
|
||||
Snackbar.Add($"Client {clientPrep.Channel?.Handle ?? clientPrep.YouTubeClient.Id} saved!", Severity.Success);
|
||||
}
|
||||
|
||||
if (_table != null)
|
||||
{
|
||||
await _table.ReloadServerData();
|
||||
}
|
||||
}
|
||||
}
|
@@ -27,7 +27,9 @@ public partial class Services : ComponentBase
|
||||
private List<ServiceEvent> GetInitialEvents()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
@@ -32,6 +32,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="cache\" />
|
||||
<Folder Include="Library\" />
|
||||
<Folder Include="Logs\Debug\" />
|
||||
</ItemGroup>
|
||||
|
@@ -10,6 +10,8 @@ builder.Services.AddRazorComponents()
|
||||
|
||||
AppContext.SetSwitch("System.Net.Http.EnableActivityPropagation", false);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
/* Manager */
|
||||
builder.SetupLogging();
|
||||
builder.SetupSettings();
|
||||
@@ -32,6 +34,7 @@ app.UseHttpsRedirection();
|
||||
|
||||
app.UseStaticFiles();
|
||||
app.UseAntiforgery();
|
||||
app.MapControllers();
|
||||
|
||||
app.MapRazorComponents<App>()
|
||||
.AddInteractiveServerRenderMode();
|
||||
|
@@ -29,9 +29,10 @@ public abstract class ExtendedBackgroundService(string name, string description,
|
||||
_resumeSignal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
await _resumeSignal.Task.WaitAsync(stoppingToken);
|
||||
}
|
||||
|
||||
await ExecuteServiceAsync(stoppingToken);
|
||||
|
||||
await Task.Delay(ExecuteInterval, stoppingToken);
|
||||
await ExecuteServiceAsync(stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@@ -13,5 +13,5 @@ public interface ILibraryService
|
||||
public Task<Result> SaveChannelAsync(ChannelEntity channel, 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);
|
||||
}
|
@@ -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
|
||||
{
|
||||
|
151
Manager.App/Services/System/CacheService.cs
Normal file
151
Manager.App/Services/System/CacheService.cs
Normal 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);
|
@@ -10,7 +10,7 @@ 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 List<YouTubeClient> _clients = [];
|
||||
private readonly List<YouTubeClient> _loadedClients = [];
|
||||
private CancellationToken _cancellationToken;
|
||||
private ILibraryService? _libraryService;
|
||||
|
||||
|
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);
|
||||
}
|
||||
}
|
@@ -6,7 +6,7 @@ namespace Manager.YouTube;
|
||||
public static class NetworkService
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -43,7 +43,7 @@ public static class NetworkService
|
||||
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user