Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions src/FileSizeFromBase64.NET/FileSizeFromBase64.NET.csproj
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>FileSizeFromBase64.NET</AssemblyName>
<RootNamespace>FileSizeFromBase64.NET</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kodjo Laurent Egbakou</Authors>
<Description>Get file size from base64 string</Description>
<PackageReadmeFile>nuget.md</PackageReadmeFile>
<PackageProjectUrl>https://github.com/lioncoding-oss/FileSizeFromBase64.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/lioncoding-oss/FileSizeFromBase64.NET</RepositoryUrl>
<PackageIcon>logo.png</PackageIcon>
<Version>2.0.1</Version>
<PackageReleaseNotes>See: https://github.com/lioncoding-oss/FileSizeFromBase64.NET </PackageReleaseNotes>
<PackageTags>FileSize, Base64, Paddings, MIME-type</PackageTags>
<Copyright>Copyright 2026</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>FileSizeFromBase64.NET</AssemblyName>
<RootNamespace>FileSizeFromBase64.NET</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Kodjo Laurent Egbakou</Authors>
<Description>Get file size from base64 string</Description>
<PackageReadmeFile>nuget.md</PackageReadmeFile>
<PackageProjectUrl>https://github.com/lioncoding-oss/FileSizeFromBase64.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/lioncoding-oss/FileSizeFromBase64.NET</RepositoryUrl>
<PackageIcon>logo.png</PackageIcon>
<Version>2.0.1</Version>
<PackageReleaseNotes>See: https://github.com/lioncoding-oss/FileSizeFromBase64.NET</PackageReleaseNotes>
<PackageTags>FileSize, Base64, Paddings, MIME-type</PackageTags>
<Copyright>Copyright 2026</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<DocumentationFile>bin\Release\FileSizeFromBase64.NET.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<DocumentationFile>bin\Release\FileSizeFromBase64.NET.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\nuget\readme.txt" PackagePath="readme.txt" Pack="true" />
<None Include="..\..\nuget\nuget.md" Pack="true" PackagePath="" />
<None Include="..\..\art\logo.png" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\nuget\readme.txt" PackagePath="readme.txt" Pack="true"/>
<None Include="..\..\nuget\nuget.md" Pack="true" PackagePath=""/>
<None Include="..\..\art\logo.png" Pack="true" PackagePath=""/>
</ItemGroup>

</Project>
</Project>
49 changes: 19 additions & 30 deletions src/FileSizeFromBase64.NET/FileSizeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,40 @@ public static class FileSizeHelpers
/// <param name="applyPaddingsRules">Indicate if the padding management is required or not. Default is false</param>
/// <param name="unitsOfMeasurement">The unit of measure of the file size returned by the method. The default unit of measure is Byte.</param>
/// <returns>The size of the file represented by the base64 string.</returns>
public static double GetFileSizeFromBase64String(string base64String, bool applyPaddingsRules = false, UnitsOfMeasurement unitsOfMeasurement = UnitsOfMeasurement.Byte)
public static double GetFileSizeFromBase64String(
string base64String,
bool applyPaddingsRules = false,
UnitsOfMeasurement unitsOfMeasurement = UnitsOfMeasurement.Byte)
{
if (string.IsNullOrEmpty(base64String)) return 0;
if (string.IsNullOrEmpty(base64String)) return 0d;

var base64Length = base64String
.AsSpan()
[(base64String.IndexOf(',', StringComparison.Ordinal) + 1)..]
.Length;
ReadOnlySpan<char> base64ReadOnlySpan = base64String.AsSpan();

// RFC 2397 https://www.rfc-editor.org/rfc/rfc2397.html data:[<mediatype>][;base64],<data>
if (base64ReadOnlySpan.StartsWith("data:".AsSpan()))
{
var commaIndex = base64String.IndexOf(',');
if (commaIndex >= 0)
base64ReadOnlySpan = base64ReadOnlySpan[(commaIndex + 1)..];
}

var base64Length = base64ReadOnlySpan.Length;

var fileSizeInByte = Math.Ceiling((double)base64Length / 4) * 3;

if (applyPaddingsRules && base64Length >= 2)
{
ReadOnlySpan<char> paddings = base64String.AsSpan()[^2..];
ReadOnlySpan<char> paddings = base64ReadOnlySpan[^2..];

fileSizeInByte = paddings switch
{
_ when paddings.Equals("==", StringComparison.Ordinal) => fileSizeInByte - 2,
_ when paddings[1].Equals('=') => fileSizeInByte - 1,
_ when paddings[0] == '=' && paddings[1] == '=' => fileSizeInByte - 2,
_ when paddings[1] == '=' => fileSizeInByte - 1,
_ => fileSizeInByte
};
}

return fileSizeInByte > 0 ? fileSizeInByte / (int)unitsOfMeasurement : 0;
}
}

/// <summary>
/// Unit of measurement.
/// </summary>
public enum UnitsOfMeasurement
{
/// <summary>
/// B.
/// </summary>
Byte = 1,

/// <summary>
/// KB.
/// </summary>
KiloByte = 1_024,

/// <summary>
/// MB.
/// </summary>
MegaByte = 1_048_576
}
}
22 changes: 22 additions & 0 deletions src/FileSizeFromBase64.NET/UnitsOfMeasurement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace FileSizeFromBase64.NET;

/// <summary>
/// Unit of measurement.
/// </summary>
public enum UnitsOfMeasurement
{
/// <summary>
/// B.
/// </summary>
Byte = 1,

/// <summary>
/// KB.
/// </summary>
KiloByte = 1_024,

/// <summary>
/// MB.
/// </summary>
MegaByte = 1_048_576
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>false</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>false</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="FileSizeFromBase64.NET" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1"/>
</ItemGroup>

<ItemGroup>
<None Update="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
<ItemGroup>
<None Update="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\FileSizeFromBase64.NET\FileSizeFromBase64.NET.csproj"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace FileSizeFromBase64.Benchmarks;

[Config(typeof(Config))]
[MediumRunJob]
[MemoryDiagnoser]
public class FileSizeHelpersBenchmark
{
private sealed class Config : ManualConfig
Expand All @@ -21,6 +22,7 @@ public Config()

AddJob(baseJob.WithNuGet("FileSizeFromBase64.NET", "1.0.0").WithId("v1.0.0").WithBaseline(true));
AddJob(baseJob.WithNuGet("FileSizeFromBase64.NET", "2.0.0").WithId("v2.0.0"));
AddJob(baseJob.WithNuGet("FileSizeFromBase64.NET", "2.0.1").WithId("v2.0.1"));

AddDiagnoser(MemoryDiagnoser.Default);
SummaryStyle = SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FileSizeFromBase64.NET" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\FileSizeFromBase64.NET\FileSizeFromBase64.NET.csproj"/>
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CA1707</NoWarn>
</PropertyGroup>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CA1707</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.v3" Version="3.2.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1"/>
<PackageReference Include="Shouldly" Version="4.3.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="xunit.v3" Version="3.2.2"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\FileSizeFromBase64.NET\FileSizeFromBase64.NET.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\FileSizeFromBase64.NET\FileSizeFromBase64.NET.csproj"/>
</ItemGroup>

<ItemGroup>
<None Update="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
</Project>