Skip to content

Commit

Permalink
Add file size helper extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mnadareski committed Dec 2, 2024
1 parent 0a131c5 commit 3141e1f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions BinaryObjectScanner.Test/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ namespace BinaryObjectScanner.Test
{
public class ExtensionsTests
{
#region FileSize

[Fact]
public void FileSize_Null_Invalid()
{
string? filename = null;
long actual = filename.FileSize();
Assert.Equal(-1, actual);
}

[Fact]
public void FileSize_Empty_Invalid()
{
string? filename = string.Empty;
long actual = filename.FileSize();
Assert.Equal(-1, actual);
}

#endregion

#region IterateWithAction

[Fact]
Expand Down
18 changes: 18 additions & 0 deletions BinaryObjectScanner/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace BinaryObjectScanner
{
internal static class Extensions
{
/// <summary>
/// Helper to get the filesize from a path
/// </summary>
/// <returns>Size of the file path, -1 on error</returns>
public static long FileSize(this string? filename)
{
// Invalid filenames are ignored
if (string.IsNullOrEmpty(filename))
return -1;

// Non-file paths are ignored
if (!File.Exists(filename))
return -1;

return new FileInfo(filename).Length;
}

/// <summary>
/// Wrap iterating through an enumerable with an action
/// </summary>
Expand Down

0 comments on commit 3141e1f

Please sign in to comment.