refactor(*): Make IsInvalidFileName an extension method

This commit is contained in:
Josh Creek
2021-09-02 20:35:47 +01:00
parent ac1183dc1e
commit 7cf5abd3ec
2 changed files with 7 additions and 6 deletions
+3 -2
View File
@@ -1,20 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Creek.FileRepository.Helpers
{
/// <summary>
/// A helper class for string methods.
/// </summary>
internal class StringHelper
internal static class StringHelper
{
/// <summary>
/// Checks if a filename contains invalid characters.
/// </summary>
/// <param name="filename">The filename to be checked.</param>
/// <returns>Returns true if the filename contains invalid characters.</returns>
public static bool IsInvalidFileName(string filename)
public static bool IsInvalidFileName(this string filename)
{
char[] invalidCharacters = new char[] { '<', '>', ':', '\'', '/', '\\', '|', '?', '*' };
@@ -42,7 +42,7 @@ namespace Creek.FileRepository.Repositories
/// <returns>Returns the filename.</returns>
public async Task<string> CreateFileAsync(RepoFile file)
{
if (StringHelper.IsInvalidFileName(file.Filename))
if (file.Filename.IsInvalidFileName())
{
throw new ArgumentException($"The filename for'{nameof(file)}' contains invalid characters for a filename.", nameof(file));
}
@@ -64,7 +64,7 @@ namespace Creek.FileRepository.Repositories
{
throw new ArgumentException($"'{nameof(filename)}' cannot be null or empty.", nameof(filename));
}
else if (StringHelper.IsInvalidFileName(filename))
else if (filename.IsInvalidFileName())
{
throw new ArgumentException($"'{nameof(filename)}' contains invalid characters for a filename.", nameof(filename));
}
@@ -102,7 +102,7 @@ namespace Creek.FileRepository.Repositories
{
throw new ArgumentException($"'{nameof(filename)}' cannot be null or empty.", nameof(filename));
}
else if (StringHelper.IsInvalidFileName(filename))
else if (filename.IsInvalidFileName())
{
throw new ArgumentException($"'{nameof(filename)}' contains invalid characters for a filename.", nameof(filename));
}
@@ -140,7 +140,7 @@ namespace Creek.FileRepository.Repositories
/// <returns>Returns true if update was successful.</returns>
public async Task<bool> UpdateFileAsync(RepoFile file)
{
if (StringHelper.IsInvalidFileName(file.Filename))
if (file.Filename.IsInvalidFileName())
{
throw new ArgumentException($"The filename for'{nameof(file)}' contains invalid characters for a filename.", nameof(file));
}