Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement solution with first validation #1

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
364 changes: 364 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

484 changes: 484 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions MultiformValidator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{002313D0-3399-4B1E-A821-B07EC2B9D2E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiformValidator", "src\MultiformValidator\MultiformValidator.csproj", "{8B02AABE-9655-41BA-9A8C-9361AE0C0E43}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{2AA7FB9F-683C-4C3D-AFA5-C431F1FD85DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiformValidator.Test", "tests\MultiformValidator.Test\MultiformValidator.Test.csproj", "{2927DA79-2CD4-4944-903E-5B128EDB1318}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8B02AABE-9655-41BA-9A8C-9361AE0C0E43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B02AABE-9655-41BA-9A8C-9361AE0C0E43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B02AABE-9655-41BA-9A8C-9361AE0C0E43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B02AABE-9655-41BA-9A8C-9361AE0C0E43}.Release|Any CPU.Build.0 = Release|Any CPU
{2927DA79-2CD4-4944-903E-5B128EDB1318}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2927DA79-2CD4-4944-903E-5B128EDB1318}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2927DA79-2CD4-4944-903E-5B128EDB1318}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2927DA79-2CD4-4944-903E-5B128EDB1318}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8B02AABE-9655-41BA-9A8C-9361AE0C0E43} = {002313D0-3399-4B1E-A821-B07EC2B9D2E1}
{2927DA79-2CD4-4944-903E-5B128EDB1318} = {2AA7FB9F-683C-4C3D-AFA5-C431F1FD85DB}
EndGlobalSection
EndGlobal
38 changes: 0 additions & 38 deletions main.cs

This file was deleted.

67 changes: 67 additions & 0 deletions src/MultiformValidator/Files/AudioValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Microsoft.Extensions.Logging;

namespace MultiformValidator.Files;

public class AudioValidator
{
private static readonly ILogger? Logger = LoggerSetup.Logger;
private static readonly string ERROR_WHILE_READING_FILE_MESSAGE = "An error occurred while reading the file: ";
private static readonly string ILLEGAL_ARGUMENT_MESSAGE = "The input value cannot be null.";
private static readonly string[] FILE_TYPES = ["mp3", "wav"];

/// <summary>
/// Validates whether the provided file is a valid audio file.
/// </summary>
/// <param name="file">The file to be validated.</param>
/// <param name="exclude">An optional list of file types to be excluded from validation.</param>
/// <returns>Returns <c>true</c> if the file is a valid audio and not in the exclusion list; otherwise, returns <c>false</c>.</returns>
/// <exception cref="InvalidOperationException">Thrown when the provided file is null.</exception>
public static bool IsValidAudio(FileInfo file, params string[] exclude)
{
if (file is null) throw new InvalidOperationException(ILLEGAL_ARGUMENT_MESSAGE);

try
{
byte[] fileBytes = File.ReadAllBytes(file.FullName);

if (exclude.Length == 0) return ValidateAllAudiosFileTypes(fileBytes);

var filteredList = FILE_TYPES.Except(exclude).ToArray();
return filteredList.Length != 0 && ValidateAllAudiosFileTypes(fileBytes, filteredList);
}
catch (IOException exception)
{
Logger?.LogError($"{ERROR_WHILE_READING_FILE_MESSAGE} {exception.Message}");
return false;
}
}

#region [private methods]
private static bool ValidateAllAudiosFileTypes(byte[] fileBytes, string[] filteredList)
{
bool isMp3Valid = filteredList.Contains("mp3") && IsMp3(fileBytes);
bool isWavValid = filteredList.Contains("wav") && IsWav(fileBytes);

return isMp3Valid || isWavValid;
}

private static bool ValidateAllAudiosFileTypes(byte[] fileByte)
{
return IsMp3(fileByte) || IsWav(fileByte);
}

private static bool IsMp3(byte[] fileBytes)
{
return fileBytes[0] == 0x49 && fileBytes[1] == 0x44 && fileBytes[2] == 0x33;
}

private static bool IsWav(byte[] fileBytes)
{
return fileBytes[0] == 0x52
&& fileBytes[1] == 0x49
&& fileBytes[2] == 0x46
&& fileBytes[3] == 0x46;
}

#endregion
}
76 changes: 76 additions & 0 deletions src/MultiformValidator/Files/FileValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.Extensions.Logging;

namespace MultiformValidator.Files;

public static class FileValidator
{
private static readonly ILogger? Logger = LoggerSetup.Logger;
private static readonly string ERROR_WHILE_READING_FILE_MESSAGE = "An error occurred while reading the file: ";
private static readonly string ILLEGAL_ARGUMENT_MESSAGE = "The input value cannot be null.";
private static readonly string[] FILE_TYPES = ["txt", "pdf"];


/// <summary>
/// Validates whether the specified file is a valid PDF.
/// </summary>
/// <param name="file">The file to be validated.</param>
/// <returns>
/// <c>true</c> if the file is a valid PDF; otherwise, <c>false</c>.
/// </returns>
public static bool IsValidFile(FileInfo file, params string[] exclude)
{
if (file == null) throw new InvalidOperationException(ILLEGAL_ARGUMENT_MESSAGE);

try
{
byte[] fileBytes = File.ReadAllBytes(file.FullName);

if (exclude.Length == 0) return ValidateAllFileTypes(fileBytes);

var filteredList = FILE_TYPES.Except(exclude).ToArray();
return filteredList.Length != 0 && ValidateAllFileTypes(fileBytes, filteredList);
}
catch (IOException exception)
{
Logger?.LogError($"{ERROR_WHILE_READING_FILE_MESSAGE} {exception.Message}");
return false;
}
}

#region [private methods]

private static bool ValidateAllFileTypes(byte[] fileBytes)
{
return IsPdf(fileBytes) || IsTxt(fileBytes);
}

private static bool ValidateAllFileTypes(byte[] fileBytes, string[] filteredList)
{
var isTxt = filteredList.Contains("txt") && IsTxt(fileBytes);
var isPdf = filteredList.Contains("pdf") && IsPdf(fileBytes);

return isTxt || isPdf;
}


private static bool IsTxt(byte[] fileBytes)
{
if (fileBytes.Length < 0) return false;
foreach (byte b in fileBytes)
{
if ((b < 0x20 || b > 0x7e) && b != 0x0a && b != 0x0d) return false;
}

return true;
}

private static bool IsPdf(byte[] fileBytes)
{
return fileBytes[0] == 0x25
&& fileBytes[1] == 0x50
&& fileBytes[2] == 0x44
&& fileBytes[3] == 0x46;
}

#endregion
}
84 changes: 84 additions & 0 deletions src/MultiformValidator/Files/ImageValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Microsoft.Extensions.Logging;

namespace MultiformValidator.Files;

public static class ImageValidator
{
private static readonly ILogger? Logger = LoggerSetup.Logger;
private static readonly string ERROR_WHILE_READING_FILE_MESSAGE = "An error occurred while reading the file: ";
private static readonly string ILLEGAL_ARGUMENT_MESSAGE = "The input value cannot be null.";
private static readonly string[] FILES_TYPES = ["gif", "ico", "png", "jpeg"];

public static bool IsValidImage(FileInfo file, params string[] exclude)
{
if (file is null) throw new InvalidOperationException(ILLEGAL_ARGUMENT_MESSAGE);

try
{
byte[] fileBytes = File.ReadAllBytes(file.FullName);

if (exclude.Length == 0) return ValidateAllImageFileTypes(fileBytes);

var filteredList = FILES_TYPES.Except(exclude).ToArray();
return filteredList.Length != 0 && ValidateAllImageFileTypes(fileBytes, filteredList);
}
catch (IOException exception)
{
Logger?.LogError($"{ERROR_WHILE_READING_FILE_MESSAGE} {exception.Message}");
return false;
}
}

#region [private methods]

private static bool ValidateAllImageFileTypes(byte[] fileBytes, string[] filteredList)
{
var isPng = filteredList.Contains("png") && IsPng(fileBytes);
var isIco = filteredList.Contains("ico") && IsIco(fileBytes);
var isJpeg = filteredList.Contains("jpeg") && IsJpeg(fileBytes);
var isGif = filteredList.Contains("gif") && IsGif(fileBytes);

return isPng || isIco || isJpeg || isGif;
}

private static bool ValidateAllImageFileTypes(byte[] fileBytes)
{
return IsGif(fileBytes) || IsIco(fileBytes) || IsJpeg(fileBytes) || IsPng(fileBytes);
}

private static bool IsPng(byte[] fileBytes)
{
return fileBytes[0] == 0x89
&& fileBytes[1] == 0x50
&& fileBytes[2] == 0x4E
&& fileBytes[3] == 0x47;
}


private static bool IsJpeg(byte[] fileBytes)
{
return fileBytes[0] == 0xFF
&& fileBytes[1] == 0xD8
&& fileBytes[2] == 0xFF;
}

private static bool IsIco(byte[] fileBytes)
{
return fileBytes[0] == 0x00
&& fileBytes[1] == 0x00
&& fileBytes[2] == 0x01;
}

private static bool IsGif(byte[] fileBytes)
{
return fileBytes[0] == 0x47
&& fileBytes[1] == 0x49
&& fileBytes[2] == 0x46
&& fileBytes[3] == 0x38
&& fileBytes[1] == 0x49
&& fileBytes[2] == 0x46
&& fileBytes[3] == 0x38;
}

#endregion
}
12 changes: 12 additions & 0 deletions src/MultiformValidator/LoggerSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Logging;

namespace MultiformValidator;

public static class LoggerSetup
{
public static ILogger? Logger { get; private set; }
public static void ConfigureLogger(ILogger logger)
{
Logger = logger;
}
}
13 changes: 13 additions & 0 deletions src/MultiformValidator/MultiformValidator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdasdasd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdasdasd
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam quis venenatis dui, id dictum ipsum. Donec fermentum tortor in velit posuere fermentum. Aliquam erat volutpat. Maecenas mattis sollicitudin leo, quis volutpat nulla scelerisque sit amet. Proin varius nibh est, in mattis magna porta quis. Nunc a viverra enim. Pellentesque ex ipsum, tempus nec sem vitae, imperdiet ullamcorper tellus. Nam elementum metus id ex dapibus, at gravida mi pretium. Nulla porttitor sed dolor quis blandit. Mauris id orci sem. Fusce hendrerit cursus libero ornare pulvinar. Maecenas vitae ullamcorper nibh, eget consequat ligula.
29 changes: 29 additions & 0 deletions tests/MultiformValidator.Test/MultiformValidator.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

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

</Project>
Loading