diff --git a/src/Squidex.Infrastructure/FileExtensions.cs b/src/Squidex.Infrastructure/FileExtensions.cs index 25e3673e1..956d93312 100644 --- a/src/Squidex.Infrastructure/FileExtensions.cs +++ b/src/Squidex.Infrastructure/FileExtensions.cs @@ -6,12 +6,22 @@ // All rights reserved. // ========================================================================== +using System; using System.IO; namespace Squidex.Infrastructure { public static class FileExtensions { + private static readonly string[] Extensions = + { + "bytes", + "kB", + "MB", + "GB", + "TB" + }; + public static string FileType(this string fileName) { try @@ -25,5 +35,30 @@ namespace Squidex.Infrastructure return "blob"; } } + + public static string ToReadableSize(this long value) + { + if (value < 0) + { + return string.Empty; + } + + var d = (double)value; + var u = 0; + var s = 1024; + + while ((d >= s || -d >= s) && u < Extensions.Length - 1) + { + d /= s; + u++; + } + + if (u >= Extensions.Length - 1) + { + u = Extensions.Length - 1; + } + + return $"{Math.Round(d, 1)} {Extensions[u]}"; + } } } diff --git a/src/Squidex/Controllers/Api/Assets/AssetsController.cs b/src/Squidex/Controllers/Api/Assets/AssetsController.cs index 8d538f3ca..fb52fba74 100644 --- a/src/Squidex/Controllers/Api/Assets/AssetsController.cs +++ b/src/Squidex/Controllers/Api/Assets/AssetsController.cs @@ -264,7 +264,7 @@ namespace Squidex.Controllers.Api.Assets if (formFile.Length > assetsConfig.MaxSize) { - var error = new ValidationError($"File size cannot be longer than ${assetsConfig.MaxSize}."); + var error = new ValidationError($"File size cannot be longer than {assetsConfig.MaxSize.ToReadableSize()}."); throw new ValidationException("Cannot create asset.", error); } diff --git a/src/Squidex/app/shared/components/asset.component.ts b/src/Squidex/app/shared/components/asset.component.ts index 60ac9f9ae..85540c6cf 100644 --- a/src/Squidex/app/shared/components/asset.component.ts +++ b/src/Squidex/app/shared/components/asset.component.ts @@ -92,6 +92,7 @@ export class AssetComponent extends AppComponentBase implements OnInit { } }, error => { this.notifyError(error); + this.emitFailed(error); }); } else { this.updateAsset(this.asset, false); @@ -109,8 +110,8 @@ export class AssetComponent extends AppComponentBase implements OnInit { this.setProgress(dto); } }, error => { + this.notifyError(error); this.setProgress(); - this.emitFailed(error); }); } } diff --git a/tests/Squidex.Infrastructure.Tests/FileExtensionsTests.cs b/tests/Squidex.Infrastructure.Tests/FileExtensionsTests.cs index dfd7dfd8a..efa528035 100644 --- a/tests/Squidex.Infrastructure.Tests/FileExtensionsTests.cs +++ b/tests/Squidex.Infrastructure.Tests/FileExtensionsTests.cs @@ -34,5 +34,23 @@ namespace Squidex.Infrastructure Assert.Equal("blob", actual); } + + [Theory] + [InlineData(-1, "")] + [InlineData(-2, "")] + [InlineData(0, "0 bytes")] + [InlineData(50, "50 bytes")] + [InlineData(1024, "1 kB")] + [InlineData(870400, "850 kB")] + [InlineData(1572864, "1.5 MB")] + [InlineData(4294967296, "4 GB")] + [InlineData(3408486046105, "3.1 TB")] + [InlineData(3490289711212134, "3174.4 TB")] + public void Should_calculate_file_size(long bytes, string expected) + { + var actual = bytes.ToReadableSize(); + + Assert.Equal(expected, actual); + } } }