Skip to content

Commit

Permalink
Fix #45 & #46 - Fixed header crashing and added support for configura…
Browse files Browse the repository at this point in the history
…ble max file size upload (#47)
  • Loading branch information
joao4all authored Mar 21, 2024
1 parent 66d5507 commit e85b888
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
18 changes: 14 additions & 4 deletions reqifviewer.Tests/Pages/Index/IndexPageTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

namespace ReqifViewer.Tests.Pages.Index
{
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using Bunit;

using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using Moq;
Expand All @@ -39,7 +42,7 @@ namespace ReqifViewer.Tests.Pages.Index
using ReqIFSharp.Extensions.Services;

using reqifviewer.Pages.Index;

using reqifviewer.Utilities;
using TestContext = Bunit.TestContext;

/// <summary>
Expand All @@ -50,15 +53,21 @@ public class IndexPageTestFixture
{
private Mock<IReqIFLoaderService> reqIfLoaderService;
private TestContext context;
private const long MaxFileSize = 5 * 1024 * 1024;
private IConfiguration configuration;
private const double MaxFileSizeInMb = 5;

[SetUp]
public void SetUp()
{
this.context = new TestContext();
this.reqIfLoaderService = new Mock<IReqIFLoaderService>();

this.configuration = new ConfigurationBuilder()
.AddInMemoryCollection([new KeyValuePair<string, string>(Constants.MaxUploadFileSizeInMbConfigurationKey, MaxFileSizeInMb.ToString(CultureInfo.InvariantCulture))])
.Build();

this.context.Services.AddSingleton(this.reqIfLoaderService.Object);
this.context.Services.AddSingleton(this.configuration);
}

[TearDown]
Expand All @@ -72,16 +81,17 @@ public async Task VerifyComponent()
{
var renderer = this.context.RenderComponent<IndexPage>();
var uploadComponent = renderer.FindComponent<InputFile>();
var maxFileSizeInBytes = (long)(MaxFileSizeInMb * 1024 * 1024);

var file = new Mock<IBrowserFile>();
file.Setup(x => x.Size).Returns(MaxFileSize + 1);
file.Setup(x => x.Size).Returns(maxFileSizeInBytes + 1);
file.Setup(x => x.Name).Returns("file.reqif");
file.Setup(x => x.OpenReadStream(It.IsAny<long>(), It.IsAny<CancellationToken>())).Returns(new MemoryStream());

await renderer.InvokeAsync(() => uploadComponent.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs([file.Object])));
file.Verify(x => x.OpenReadStream(It.IsAny<long>(), It.IsAny<CancellationToken>()), Times.Never);

file.Setup(x => x.Size).Returns(MaxFileSize - 1);
file.Setup(x => x.Size).Returns(maxFileSizeInBytes - 1);
await renderer.InvokeAsync(() => uploadComponent.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs([file.Object])));
file.Verify(x => x.OpenReadStream(It.IsAny<long>(), It.IsAny<CancellationToken>()), Times.Once);

Expand Down
2 changes: 1 addition & 1 deletion reqifviewer/Pages/Index/AboutComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ limitations under the License.
<div class="row my-4">
<div class="col-md-12">
<RadzenCard>
<RadzenTextArea Style="height: 300px" Value="@license"></RadzenTextArea>
<RadzenTextArea Style="height: 300px; width: 100%;" Value="@license" ReadOnly="true"></RadzenTextArea>
</RadzenCard>
</div>
</div>
Expand Down
23 changes: 17 additions & 6 deletions reqifviewer/Pages/Index/IndexPage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace reqifviewer.Pages.Index
{
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.Configuration;

using ReqIFSharp;
using ReqIFSharp.Extensions.Services;
Expand All @@ -35,6 +36,9 @@ namespace reqifviewer.Pages.Index
using System.Threading.Tasks;
using System.Threading;
using System;
using System.Globalization;

using Utilities;

/// <summary>
/// The code behind for the <see cref="IndexPage"/> component
Expand All @@ -47,6 +51,12 @@ public partial class IndexPage : ComponentBase, IDisposable
[Inject]
public IReqIFLoaderService ReqIfLoaderService { get; set; }

/// <summary>
/// Gets or sets the <see cref="IConfiguration"/>
/// </summary>
[Inject]
public IConfiguration Configuration { get; set; }

/// <summary>
/// Gets or sets the error message that is displayed in the component
/// </summary>
Expand All @@ -68,9 +78,9 @@ public partial class IndexPage : ComponentBase, IDisposable
private const string UploadsDirectory = "wwwroot/uploads/";

/// <summary>
/// The directory where uploaded files are stored
/// The maximum file size to upload in megabytes
/// </summary>
private const long MaxFileSizeInBytes = 5 * 1024 * 1024;
private double MaxUploadFileSizeInMb => double.Parse(this.Configuration.GetSection(Constants.MaxUploadFileSizeInMbConfigurationKey).Value!, CultureInfo.InvariantCulture);

/// <summary>
/// Gets or sets the <see cref="ReqIF"/> file path
Expand Down Expand Up @@ -131,9 +141,11 @@ protected override void OnInitialized()
/// </returns>
private async Task HandleSelection(InputFileChangeEventArgs e)
{
if (e.File.Size > MaxFileSizeInBytes)
var maxUploadFileSizeInBytes = (long)(this.MaxUploadFileSizeInMb * 1024 * 1024);

if (e.File.Size > maxUploadFileSizeInBytes)
{
this.ErrorMessage = $"The max file size is {MaxFileSizeInBytes/(1024*1024)} MB";
this.ErrorMessage = $"The max file size is {this.MaxUploadFileSizeInMb} MB";
return;
}

Expand All @@ -152,7 +164,7 @@ private async Task HandleSelection(InputFileChangeEventArgs e)

await using (var fileStream = new FileStream(this.ReqIfFilePath, FileMode.Create))
{
await e.File.OpenReadStream(MaxFileSizeInBytes).CopyToAsync(fileStream);
await e.File.OpenReadStream(maxUploadFileSizeInBytes).CopyToAsync(fileStream);
}

this.reqifisAvailable = true;
Expand Down Expand Up @@ -212,7 +224,6 @@ private async Task OnCancel()
if (this.cancellationTokenSource != null)
{
await this.cancellationTokenSource.CancelAsync();
TryDeleteFile(this.ReqIfFilePath);
this.IsLoading = false;
await this.InvokeAsync(this.StateHasChanged);
}
Expand Down
1 change: 1 addition & 0 deletions reqifviewer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static async Task Main(string[] args)
builder.Services.AddScoped<IReqIFLoaderService, ReqIFLoaderService>();
builder.Services.AddGoogleAnalytics("295704041");
builder.Services.AddBlazorStrap();
builder.Services.AddHttpClient();

var app = builder.Build();

Expand Down
33 changes: 33 additions & 0 deletions reqifviewer/Utilities/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="Constants.cs" company="RHEA System S.A.">
//
// Copyright 2023-2024 RHEA System S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// -------------------------------------------------------------------------------------------------

namespace reqifviewer.Utilities
{
/// <summary>
/// Contains constant values that can be shared across the application
/// </summary>
public static class Constants
{
/// <summary>
/// The name of the configuration key used to retrieve the max upload file size, in megabytes
/// </summary>
public const string MaxUploadFileSizeInMbConfigurationKey = "MaxUploadFileSizeInMb";
}
}
3 changes: 3 additions & 0 deletions reqifviewer/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"DetailedErrors": true
}
1 change: 1 addition & 0 deletions reqifviewer/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"MaxUploadFileSizeInMb": 500,
"AllowedHosts": "*",
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
Expand Down

0 comments on commit e85b888

Please sign in to comment.