mirror of
https://github.com/jcreek/Creek.FileRepository.git
synced 2026-07-13 02:43:43 +00:00
refactor(*): Make IsInvalidFileName an extension method
This commit is contained in:
@@ -1,20 +1,21 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Creek.FileRepository.Helpers
|
namespace Creek.FileRepository.Helpers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A helper class for string methods.
|
/// A helper class for string methods.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class StringHelper
|
internal static class StringHelper
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if a filename contains invalid characters.
|
/// Checks if a filename contains invalid characters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">The filename to be checked.</param>
|
/// <param name="filename">The filename to be checked.</param>
|
||||||
/// <returns>Returns true if the filename contains invalid characters.</returns>
|
/// <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[] { '<', '>', ':', '\'', '/', '\\', '|', '?', '*' };
|
char[] invalidCharacters = new char[] { '<', '>', ':', '\'', '/', '\\', '|', '?', '*' };
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace Creek.FileRepository.Repositories
|
|||||||
/// <returns>Returns the filename.</returns>
|
/// <returns>Returns the filename.</returns>
|
||||||
public async Task<string> CreateFileAsync(RepoFile file)
|
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));
|
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));
|
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));
|
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));
|
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));
|
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>
|
/// <returns>Returns true if update was successful.</returns>
|
||||||
public async Task<bool> UpdateFileAsync(RepoFile file)
|
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));
|
throw new ArgumentException($"The filename for'{nameof(file)}' contains invalid characters for a filename.", nameof(file));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user