FileTypeChecker

repository·master·Indexed 41 days ago

https://github.com/ajmitev/filetypechecker

A .NET Standard 2.0+ library for secure file type validation using magic number (binary signature) detection. It provides the FileTypeValidator class and Stream extension methods to verify files across categories such as Images, Documents, Archives, Audio/Video, and Executables. Includes a companion package, FileTypeChecker.Web, for adding validation attributes to IFormFile models in ASP.NET Core applications.

Tokens
1.3K
Snippets
5
Records
6
Agent score
2%

What's inside FileTypeChecker

  1. Use FileTypeChecker in ASP.NET Core web applications

    master

    For ASP.NET Core, use the companion package FileTypeChecker.Web to add validation attributes to your IFormFile models. This allows for automatic validation during model binding.

    public class UploadModel
    {
        [AllowedFileTypes(FileType.Jpeg, FileType.Png)]
        [MaxFileSize(5 * 1024 * 1024)] // 5MB
        public IFormFile ProfileImage { get; set; }
    }
  2. Install FileTypeChecker

    master

    You can install FileTypeChecker using various .NET package management tools. The library requires .NET Standard 2.0+.

    # Using Package Manager
    Install-Package File.TypeChecker
    # Using .NET CLI
    dotnet add package File.TypeChecker
    <!-- Using PackageReference in .csproj -->
    <PackageReference Include="File.TypeChecker" Version="4.2.0" />
  3. Validate file categories and specific types

    master

    The library provides extension methods on Stream to quickly check if a file belongs to a specific category (like Images or Documents) or a specific format (like PNG or JPEG).

    using (var fileStream = File.OpenRead("image.jpg"))
    {
        // Check by category
        bool isImage = fileStream.IsImage();
        bool isDocument = fileStream.IsDocument();
        bool isArchive = fileStream.IsArchive();
        
        // Check specific type using generics
        bool isPng = fileStream.Is<PortableNetworkGraphic>();
        bool isJpeg = fileStream.Is<JointPhotographicExpertsGroup>();
    }
  4. Detect file types using FileTypeValidator

    master

    Use the FileTypeValidator class to identify files by their binary signatures (magic numbers) rather than their extensions. This is useful for verifying that a file is actually what its extension claims it to be.

    using (var fileStream = File.OpenRead("document.pdf"))
    {
        // Check if file type is recognizable
        if (FileTypeValidator.IsTypeRecognizable(fileStream))
        {
            // Get file type information
            IFileType fileType = FileTypeValidator.GetFileType(fileStream);
            Console.WriteLine($"Type: {fileType.Name}");
            Console.WriteLine($"Extension: {fileType.Extension}");
        }
    }
  5. Register custom file types

    master

    You can extend the library by implementing the FileType class and registering your custom format with the FileTypeValidator. This allows the library to recognize proprietary or niche binary signatures.

    // Register your own file type
    public class MyCustomType : FileType
    {
        public override string Name => "My Custom Format";
        public override string Extension => "mycustom";
        public override string MimeType => "application/x-mycustom";
        
        public override bool IsMatch(byte[] signature, Stream stream)
        {
            // Implement logic to match your specific binary signature
            return signature.Length >= 4 && 
                   signature[0] == 0x4D && signature[1] == 0x59 && 
                   signature[2] == 0x43 && signature[3] == 0x54;
        }
    }
    
    // Use it
    FileTypeValidator.RegisterType<MyCustomType>();
  6. Supported file type categories

    master

    FileTypeChecker supports over 30 file formats across several categories:

    • Images: PNG, JPEG, GIF, BMP, TIFF, WebP, ICO, PSD, HEIC
    • Documents: PDF, DOC/DOCX, XLS/XLSX, ODT, ODS, ODP, HTML, XML
    • Archives: ZIP, RAR, 7Z, TAR, GZIP, BZIP2
    • Audio/Video: MP3, MP4, M4V, AVI, WAV
    • Executables: EXE, ELF