@page "/watermarks/{Id:int}" @page "/watermarks/add" @using ErsatzTV.Application.Images @using ErsatzTV.Application.Watermarks @using ErsatzTV.FFmpeg.State @implements IDisposable @inject NavigationManager NavigationManager @inject ILogger Logger @inject ISnackbar Snackbar @inject IMediator Mediator
@if (_editContext is not null) { @(IsEdit ? "Edit Watermark" : "Add Watermark") None Permanent Intermittent Custom Channel Logo Upload Image Bottom Right Bottom Middle Bottom Left Left Middle Top Left Top Middle Top Right Right Middle Scaled Actual Size 5 minutes 10 minutes 15 minutes 20 minutes 30 minutes 60 minutes @(IsEdit ? "Save Changes" : "Add Watermark") }
@code { private readonly CancellationTokenSource _cts = new(); [Parameter] public int Id { get; set; } private WatermarkEditViewModel _model = new(); private EditContext _editContext; private ValidationMessageStore _messageStore; public void Dispose() { _cts.Cancel(); _cts.Dispose(); } protected override async Task OnParametersSetAsync() { if (IsEdit) { Option watermark = await Mediator.Send(new GetWatermarkById(Id), _cts.Token); watermark.Match( watermarkViewModel => _model = new WatermarkEditViewModel(watermarkViewModel), () => NavigationManager.NavigateTo("404")); } else { _model = new WatermarkEditViewModel { Name = string.Empty, Mode = ChannelWatermarkMode.Permanent, Image = string.Empty, Location = WatermarkLocation.BottomRight, Size = WatermarkSize.Scaled, Width = 15, HorizontalMargin = 5, VerticalMargin = 5, FrequencyMinutes = 15, DurationSeconds = 15, Opacity = 100, PlaceWithinSourceContent = false }; } _editContext = new EditContext(_model); _messageStore = new ValidationMessageStore(_editContext); } private bool IsEdit => Id != 0; private async Task HandleSubmitAsync() { _messageStore.Clear(); if (_editContext.Validate()) { Seq errorMessage = IsEdit ? (await Mediator.Send(_model.ToUpdate(), _cts.Token)).LeftToSeq() : (await Mediator.Send(_model.ToCreate(), _cts.Token)).LeftToSeq(); errorMessage.HeadOrNone().Match( error => { Snackbar.Add("Unexpected error saving watermark"); Logger.LogError("Unexpected error saving watermark: {Error}", error.Value); }, () => NavigationManager.NavigateTo("/watermarks")); } } private async Task UploadWatermark(InputFileChangeEventArgs e) { try { Either maybeCacheFileName = await Mediator.Send( new SaveArtworkToDisk(e.File.OpenReadStream(10 * 1024 * 1024), ArtworkKind.Watermark), _cts.Token); maybeCacheFileName.Match( relativeFileName => { _model.Image = relativeFileName; _messageStore.Clear(); _editContext.Validate(); StateHasChanged(); }, error => { Snackbar.Add($"Unexpected error saving watermark: {error.Value}", Severity.Error); Logger.LogError("Unexpected error saving watermark: {Error}", error.Value); }); } catch (IOException) { Snackbar.Add("Watermark exceeds maximum allowed file size of 10 MB", Severity.Error); Logger.LogError("Watermark exceeds maximum allowed file size of 10 MB"); } catch (Exception ex) { Snackbar.Add($"Unexpected error saving watermark: {ex.Message}", Severity.Error); Logger.LogError("Unexpected error saving watermark: {Error}", ex.Message); } } }