Browse Source

Some bugfixes with asset uploads.

pull/118/head
Sebastian Stehle 9 years ago
parent
commit
3b958cd87e
  1. 35
      src/Squidex.Infrastructure/FileExtensions.cs
  2. 2
      src/Squidex/Controllers/Api/Assets/AssetsController.cs
  3. 3
      src/Squidex/app/shared/components/asset.component.ts
  4. 18
      tests/Squidex.Infrastructure.Tests/FileExtensionsTests.cs

35
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]}";
}
}
}

2
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);
}

3
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);
});
}
}

18
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);
}
}
}

Loading…
Cancel
Save