TS.FileMimeTypeControl 1.0.1

dotnet add package TS.FileMimeTypeControl --version 1.0.1                
NuGet\Install-Package TS.FileMimeTypeControl -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TS.FileMimeTypeControl" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TS.FileMimeTypeControl --version 1.0.1                
#r "nuget: TS.FileMimeTypeControl, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TS.FileMimeTypeControl as a Cake Addin
#addin nuget:?package=TS.FileMimeTypeControl&version=1.0.1

// Install TS.FileMimeTypeControl as a Cake Tool
#tool nuget:?package=TS.FileMimeTypeControl&version=1.0.1                

File Mime Type Control NuGet Package

With this package, you can type-check the IFormFile files you receive upon request for jpg and png. It checks byte and returns false if it fails.

Usage

bool checkfileForJpg = file.CheckForJpg(); //returns false if it fails
bool checkfileForPng = file.CheckForPng(); //returns false if it fails

Resource Code

public static class ExtensionMethods
{
    public static bool CheckForJpg(this IFormFile file)
    {
        using (var stream = new MemoryStream())
        {
            file.CopyTo(stream);
            byte[] fileBytes = stream.ToArray();
            string jpgValue = fileBytes[0].ToString() + fileBytes[1].ToString() + fileBytes[2].ToString();


            if (jpgValue != "255216255")
            {
                return false;
            }
        }
        return true;
    }

    public static bool CheckForPng(this IFormFile file)
    {
        using (var stream = new MemoryStream())
        {
            file.CopyTo(stream);
            byte[] fileBytes = stream.ToArray();
            string jpgValue = 
                fileBytes[0].ToString() + 
                fileBytes[1].ToString() + 
                fileBytes[2].ToString() + 
                fileBytes[3].ToString();


            if (jpgValue != "137807871")
            {
                return false;
            }
        }
        return true;
    }
}

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 109 5/11/2024
1.0.0 95 5/11/2024