puspensi.general.validation 1.0.1

dotnet add package puspensi.general.validation --version 1.0.1
                    
NuGet\Install-Package puspensi.general.validation -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="puspensi.general.validation" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="puspensi.general.validation" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="puspensi.general.validation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add puspensi.general.validation --version 1.0.1
                    
#r "nuget: puspensi.general.validation, 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.
#:package puspensi.general.validation@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=puspensi.general.validation&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=puspensi.general.validation&version=1.0.1
                    
Install as a Cake Tool

puspensi.general.validation

A comprehensive .NET Standard 2.0 library for file validation and MIME type detection, providing extension methods for IFormFile and MemoryStream objects.

🚀 Features

  • File Type Validation: Validate PDF, Word documents, images, spreadsheets, presentations, and compressed archives
  • Security Validation: Check PDF files for potentially malicious JavaScript content
  • MIME Type Detection: Advanced content-based file type detection using MimeDetective
  • Dual Support: Works with both IFormFile (ASP.NET Core uploads) and MemoryStream objects
  • Lightweight: Targets .NET Standard 2.0 for broad compatibility

📦 Installation

Install the package via NuGet Package Manager:

Install-Package puspensi.general.validation

Or via .NET CLI:

dotnet add package puspensi.general.validation

Or via PackageReference in your .csproj file:

<PackageReference Include="puspensi.general.validation" Version="1.0.0" />

🔧 Dependencies

  • Microsoft.AspNetCore.Http (>= 2.3.9)
  • Mime-Detective (>= 24.5.1)
  • Mime-Detective.Definitions.Exhaustive (>= 24.5.1)

📖 Usage

Basic File Validation

using puspensi.general.validation;
using Microsoft.AspNetCore.Http;

// In your controller action
[HttpPost]
public IActionResult UploadFile(IFormFile file)
{
    // Check if file is of any allowed type
    if (!file.AllowedFile())
    {
        return BadRequest("File type not allowed");
    }
    
    // Specific validations
    if (file.IsPdfFile())
    {
        // Handle PDF file
        if (file.IsPdfSecured())
        {
            // PDF is secure (no JavaScript)
            // Process the file...
        }
        else
        {
            return BadRequest("PDF contains potentially malicious content");
        }
    }
    
    return Ok("File uploaded successfully");
}

Working with MemoryStream

using puspensi.general.validation;
using System.IO;

public void ProcessFileFromMemory(MemoryStream fileStream)
{
    if (fileStream.IsImageFile())
    {
        // Handle image file
        Console.WriteLine("Processing image file...");
    }
    else if (fileStream.IsDocumentFile())
    {
        // Handle document file
        Console.WriteLine("Processing document file...");
    }
    else if (fileStream.IsCompressFile())
    {
        // Handle compressed archive
        Console.WriteLine("Processing archive file...");
    }
}

File Type Specific Validation

// Check for Word documents
if (file.IsWordFile())
{
    // Supports: DOC, DOCX, ODT, MS Works
}

// Check for images
if (file.IsImageFile())
{
    // Supports: JPEG, PNG, GIF, BMP, TIFF, WebP, SVG, PostScript
}

// Check for spreadsheets and presentations (part of IsDocumentFile)
if (file.IsDocumentFile())
{
    // Includes PDFs, Word docs, Excel files, PowerPoint, etc.
}

// Check for compressed files
if (file.IsCompressFile())
{
    // Supports: ZIP, RAR, 7Z, TAR, GZIP, BZIP2
}

📋 API Reference

Extension Methods for IFormFile

Method Description Returns
AllowedFile() Checks if file is of any allowed type (PDF, document, Word, image, or compressed) bool
IsPdfFile() Validates if file is a PDF based on content type bool
IsPdfSecured() Checks if PDF file is secure (contains no JavaScript) bool
IsWordFile() Validates Word documents (DOC, DOCX, ODT, Works) bool
IsImageFile() Validates image files (JPEG, PNG, GIF, BMP, TIFF, WebP, SVG, PostScript) bool
IsDocumentFile() Validates document files (PDF, Word, spreadsheets, presentations) bool
IsCompressFile() Validates compressed archives (ZIP, RAR, 7Z, TAR, GZIP, BZIP2) bool

Extension Methods for MemoryStream

All the above methods are also available for MemoryStream objects with the same signatures and functionality. MemoryStream validation uses advanced content analysis for more accurate detection.

🗂️ Supported File Types

PDF Files

  • application/pdf

Word Documents

  • application/msword (DOC)
  • application/vnd.openxmlformats-officedocument.wordprocessingml.document (DOCX)
  • application/vnd.oasis.opendocument.text (ODT)
  • application/vnd.ms-works (Works)

Spreadsheets

  • application/vnd.ms-excel (XLS)
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (XLSX)
  • application/vnd.oasis.opendocument.spreadsheet (ODS)
  • text/csv (CSV)

Presentations

  • application/vnd.ms-powerpoint (PPT)
  • application/vnd.openxmlformats-officedocument.presentationml.presentation (PPTX)
  • application/vnd.oasis.opendocument.presentation (ODP)

Images

  • image/jpeg (JPEG/JPG)
  • image/png (PNG)
  • image/gif (GIF)
  • image/bmp (BMP)
  • image/tiff (TIFF)
  • image/webp (WebP)
  • image/svg+xml (SVG)
  • application/postscript (PostScript)

Text Documents

  • text/plain (TXT)
  • application/rtf (RTF)

Compressed Archives

  • application/zip (ZIP)
  • application/x-rar-compressed (RAR)
  • application/x-7z-compressed (7Z)
  • application/x-tar (TAR)
  • application/gzip (GZIP)
  • application/x-bzip2 (BZIP2)

🔒 Security Features

The IsPdfSecured() method provides basic security validation for PDF files by checking for:

  • JavaScript content (/s /javascript)
  • JavaScript execution patterns (/js ()

Note: This is a basic security check and should not be considered a complete security solution. Always implement additional security measures for file uploads in production environments.

🎯 Use Cases

  • File Upload Validation: Validate user uploads in web applications
  • Content Management: Ensure only allowed file types are processed
  • Document Processing: Identify and route different document types
  • Security Screening: Basic malware detection for PDF files
  • File Organization: Categorize files by type for storage systems

🏗️ Framework Compatibility

  • .NET Standard 2.0
  • .NET Framework 4.6.1+
  • .NET Core 2.0+
  • .NET 5.0+
  • .NET 6.0+
  • .NET 7.0+
  • .NET 8.0+

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🔧 Advanced Usage

Custom Validation Logic

public bool IsAllowedDocument(IFormFile file)
{
    // Custom validation combining multiple checks
    return file.IsPdfFile() && file.IsPdfSecured() || 
           file.IsWordFile() || 
           file.IsImageFile();
}

Error Handling

public async Task<IActionResult> ValidateAndProcessFile(IFormFile file)
{
    try
    {
        if (!file.AllowedFile())
        {
            return BadRequest(new { error = "File type not supported" });
        }

        if (file.IsPdfFile() && !file.IsPdfSecured())
        {
            return BadRequest(new { error = "PDF file contains potentially unsafe content" });
        }

        // Process file...
        return Ok(new { message = "File processed successfully" });
    }
    catch (Exception ex)
    {
        return StatusCode(500, new { error = "File validation failed", details = ex.Message });
    }
}

⚠️ Important Notes

  1. Content vs Header Validation: IFormFile methods rely on the ContentType header, while MemoryStream methods analyze actual file content for more accurate detection.

  2. Performance: Content-based detection (MemoryStream methods) is more accurate but slightly slower than header-based validation.

  3. Security: The PDF security check is basic. Implement additional security measures for production use.

  4. Licensing: The MimeDetective library is configured for personal/non-commercial use. Check licensing requirements for commercial applications.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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 98 5/4/2026
1.0.0 144 1/30/2026