Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Koloskov committed Sep 3, 2024
1 parent 15f027b commit 9bad415
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 25 deletions.
6 changes: 4 additions & 2 deletions Elib2EbookWeb/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@inherits LayoutComponentBase

<MudThemeProvider />
<MudPopoverProvider />
<MudThemeProvider @rendermode="InteractiveServer" />
<MudPopoverProvider @rendermode="InteractiveServer" />
<MudSnackbarProvider @rendermode="InteractiveServer" />


<div class="page">
<main>
Expand Down
2 changes: 1 addition & 1 deletion Elib2EbookWeb/Components/Layout/MainLayout.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ main {
position: absolute;
right: 0.75rem;
top: 0.5rem;
}
}
44 changes: 27 additions & 17 deletions Elib2EbookWeb/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
@page "/"
@using System.Text
@using Core.Configs
@using Core.Extensions
@using Core.Misc
@using Core.Misc.TempFolder
@using Elib2EbookWeb.Misc
@rendermode InteractiveServer
@inject IJSRuntime jsRuntime
@inject ISnackbar Snackbar

<PageTitle>Elib2Ebook</PageTitle>

<MudGrid>
<MudItem xs="12" sm="5">
<MudItem xs="12" sm="12" md="12" lg="5">
<MudPaper Class="pa-4">
<MudForm Spacing="10">
<MudTextField T="string" Immediate="true" Label="Ссылка на книгу" Required="true" RequiredError="Ссылка на книгу обязательна!" Variant="Variant.Outlined" TextChanged="@(value => _options.Url = new[] { value })"/>
<MudTextField T="string" Immediate="true" Label="Ссылка на книгу" Required="true" RequiredError="Ссылка на книгу обязательна!" Variant="Variant.Outlined" TextChanged="@(value => _options.Url = new[] { value })" AutoFocus="true"/>

<MudStack>
<MudToggleGroup T="string" SelectionMode="SelectionMode.MultiSelection" Color="Color.Tertiary" CheckMark @bind-Values="_options.Format">
Expand All @@ -29,15 +29,21 @@
</MudForm>
</MudPaper>
<MudPaper Class="pa-4 mt-4">
<MudButton Variant="Variant.Filled" Color="Color.Primary" DropShadow="false" OnClick="Generate">Сгенерировать</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Info" DropShadow="false" OnClick="Generate" Disabled="_onGenerate" FullWidth="true" Size="Size.Large">
@if (_onGenerate) {
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true"/>
} else {
<MudText>Сгенерировать</MudText>
}
</MudButton>
</MudPaper>
</MudItem>

@if (_logs.Length > 0) {
<MudItem xs="12" sm="7">
@if (_logger?.Builder.Length > 0) {
<MudItem xs="12" sm="12" md="12" lg="7">
<MudPaper Class="pa-4">
<pre>
@_logs.ToString()
<pre class="autoscrollable-wrapper" style="max-height: 400px">
@_logger.Builder.ToString()
</pre>
</MudPaper>
</MudItem>
Expand All @@ -46,44 +52,48 @@


@code {
private readonly StringBuilder _logs = new();
private readonly Options _options = new();
private SbLogger? _logger;
private bool _onGenerate;

private async Task Generate() {
_logs.Clear();
var logger = new SbLogger(_logs, StateHasChanged);
_onGenerate = true;
_logger = new SbLogger(StateHasChanged);

using var tempSave = TempFolderFactory.Create("Books", true);

_options.Timeout = 10;
_options.SavePath = tempSave.Path;

try {
using var getterConfig = BookGetterConfig.GetDefault(_options, logger);
using var getterConfig = BookGetterConfig.GetDefault(_options, _logger);
using var getter = GetterProvider.Get(getterConfig, _options.Url.First().AsUri());
await getter.Init();
await getter.Authorize();

foreach (var url in _options.Url) {
logger.LogInformation($"Начинаю генерацию книги {url.CoverQuotes()}");
_logger.LogInformation($"Начинаю генерацию книги {url.CoverQuotes()}");
try {
var book = await getter.Get(url.AsUri());
foreach (var format in _options.Format) {
await BuilderProvider.Get(format, _options, logger).Build(book);
await BuilderProvider.Get(format, _options, _logger).Build(book);
}

foreach (var fileName in Directory.GetFiles(tempSave.Path)) {
await using var file = File.OpenRead(fileName);
using var streamRef = new DotNetStreamReference(file);
await jsRuntime.InvokeVoidAsync("downloadFileFromStream", Path.GetFileName(file.Name), streamRef);
}


Snackbar.Add($"Генерация книги {url.CoverQuotes()} завершена", Severity.Success);
} catch (Exception ex) {
logger.LogInformation($"Генерация книги {url} завершилась с ошибкой. {ex}");
_logger.LogInformation($"Генерация книги {url} завершилась с ошибкой. {ex}");
}
}
} catch (Exception ex) {
logger.LogInformation(ex.Message);
_logger.LogInformation(ex.Message);
}

_onGenerate = false;
}
}
3 changes: 3 additions & 0 deletions Elib2EbookWeb/Elib2EbookWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
<Content Update="wwwroot\app.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
12 changes: 8 additions & 4 deletions Elib2EbookWeb/Misc/SbWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
namespace Elib2EbookWeb.Misc;

public class SbLogger : ILogger {
private readonly StringBuilder _builder;
public readonly StringBuilder Builder;
private readonly Action _action;

public SbLogger(StringBuilder builder, Action action) {
_builder = builder;
public SbLogger(Action action) : this(action, new StringBuilder()) {

}

public SbLogger(Action action, StringBuilder builder) {
_action = action;
Builder = builder;
}

public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default!;

public bool IsEnabled(LogLevel logLevel) => true;

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) {
_builder.AppendLine($"{formatter(state, exception)}");
Builder.AppendLine($"{formatter(state, exception)}");
_action();
}
}
13 changes: 12 additions & 1 deletion Elib2EbookWeb/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using Elib2EbookWeb.Components;
using MudBlazor;
using MudBlazor.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddMudServices();
builder.Services.AddMudServices(config => {
config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomCenter;

config.SnackbarConfiguration.PreventDuplicates = false;
config.SnackbarConfiguration.NewestOnTop = false;
config.SnackbarConfiguration.ShowCloseIcon = true;
config.SnackbarConfiguration.VisibleStateDuration = 10000;
config.SnackbarConfiguration.HideTransitionDuration = 500;
config.SnackbarConfiguration.ShowTransitionDuration = 500;
config.SnackbarConfiguration.SnackbarVariant = Variant.Filled;
});

var app = builder.Build();

Expand Down
7 changes: 7 additions & 0 deletions Elib2EbookWeb/wwwroot/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ h1:focus {
.darker-border-checkbox.form-check-input {
border-color: #929292;
}

.autoscrollable-wrapper {
overflow: auto;
max-height: 100%;
display: flex;
flex-direction: column-reverse;
}

0 comments on commit 9bad415

Please sign in to comment.