SmooAI.File 2.2.4

dotnet add package SmooAI.File --version 2.2.4
                    
NuGet\Install-Package SmooAI.File -Version 2.2.4
                    
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="SmooAI.File" Version="2.2.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SmooAI.File" Version="2.2.4" />
                    
Directory.Packages.props
<PackageReference Include="SmooAI.File" />
                    
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 SmooAI.File --version 2.2.4
                    
#r "nuget: SmooAI.File, 2.2.4"
                    
#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 SmooAI.File@2.2.4
                    
#: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=SmooAI.File&version=2.2.4
                    
Install as a Cake Addin
#tool nuget:?package=SmooAI.File&version=2.2.4
                    
Install as a Cake Tool

SmooAI.File

Magic-byte MIME detection, typed validation errors, and stream helpers for .NET — because Content-Type headers lie.

.NET port of @smooai/file. Built on Mime-Detective for real MIME sniffing (not extension guessing). Wire-compatible semantics with the TypeScript, Python, Go, and Rust ports.

Install

dotnet add package SmooAI.File

Need S3 upload/download helpers? Add the companion package:

dotnet add package SmooAI.File.S3

Split on purpose — apps that only need MIME detection and validation don't pull in AWSSDK.S3.

Quick start

using SmooAI.File;

// From an ASP.NET Core upload
await using var upload = formFile.OpenReadStream();
var file = await SmooFile.CreateFromStreamAsync(upload, options =>
{
    options.Name             = formFile.FileName;
    options.ExpectedMimeType = formFile.ContentType;   // client-claimed
    options.MaxSizeBytes     = 10_000_000;
    options.AllowedMimeTypes = new[] { "image/png", "image/jpeg", "application/pdf" };
});

// One call — throws typed exceptions on violation
file.Validate();

// Detected MIME reflects what the bytes *actually* are
Console.WriteLine(file.Detected.MimeType);   // e.g. "image/png"

// Emit as base64 for an email attachment / data URL
var b64 = await file.ToBase64Async();

// Or persist
await file.SaveToFileAsync("/tmp/upload.bin");

Why magic-byte detection

File extensions and Content-Type headers are untrusted client input. file.docx may actually be a ZIP. A .php can masquerade as image/png. SmooFile.Detected.MimeType reflects the real bytes — and Validate(expectedMimeType: …) throws FileContentMismatchException the moment claim disagrees with content.

// Client uploads a PHP shell renamed to avatar.png
var file = await SmooFile.CreateFromStreamAsync(stream, opts =>
{
    opts.Name             = "avatar.png";
    opts.ExpectedMimeType = "image/png";
});

file.Validate(allowedMimes: new[] { "image/png", "image/jpeg" });
// -> throws FileContentMismatchException
//    Detected: "text/x-php", Expected: "image/png"

Validation errors

One base class — one catch block on the controller. All typed, all actionable, all 400-worthy:

Exception When
FileSizeException File exceeds maxSize
FileMimeException MIME not in allowedMimes
FileContentMismatchException Client-claimed MIME disagrees with magic-byte-detected MIME
FileValidationException Base class — catch this to handle all validation failures
try
{
    file.Validate();
}
catch (FileValidationException ex)
{
    return Results.Problem(ex.Message, statusCode: 400);
}

Creating from other sources

// From a byte buffer
var file = await SmooFile.CreateFromBytesAsync(bytes, opts => { opts.Name = "thing.bin"; });

// From a file path
var file = await SmooFile.CreateFromPathAsync("/tmp/upload.bin");

// From a URL (streams, doesn't buffer the full body into memory)
var file = await SmooFile.CreateFromUrlAsync("https://example.com/report.pdf");

License

MIT — © SmooAI

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.  net9.0 is compatible.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SmooAI.File:

Package Downloads
SmooAI.File.S3

S3 helpers for SmooAI.File — createPresignedUploadUrl, createFromS3, uploadToS3, saveToS3.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.4 112 4/24/2026