Jump to content

MindAttic.Storage

From MindAttic Docs
Revision as of 20:27, 29 November 2026 by Docs Sync (talk | contribs) (md2wiki: sync from MindAttic.Docs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

MindAttic.Storage

Drop-in media storage for ASP.NET Core apps: one upload API, a catalog row per file, and a media endpoint that serves from local disk or hands large files straight to Azure Blob Storage.

.NET-10-512BD4.svg ASP.NET%20Core-library-512BD4.svg EF%20Core-SQL%20Server-CC2927.svg Azure-Blob%20Storage-0078D4.svg version-1.0.0-2f7a4f.svg

 your app                      MindAttic.Storage                       storage
 ─────────                     ─────────────────                       ───────
 UploadAsync(stream, ...) ──>  IMediaStore ──> hash (SHA-256) in flight
                                  │
                                  ├─ LocalDiskMediaStore   <= 2 MB ──> inline bytes in the MediaItem row
                                  │                        >  2 MB ──> MediaRoot/{uid}/{file}
                                  └─ AzureBlobMediaStore   any size ──> container/{uid}/{file}
                                  │
                                  └─ MediaItem row (uid, tenant, folder, type, size, sha256, ...)

 GET /_media/{uid} ──> signer present? ──> 302 to SAS, public or CDN URL  (Range and seeking by Azure)
                       otherwise       ──> stream through the app (ETag, Last-Modified, Range)

Two NuGet packages: MindAttic.Storage (the abstraction, the EF Core model, the local-disk store and the endpoint) and MindAttic.Storage.Azure (the Azure Blob backend and URL signer).

Why

  • Add uploads to an app with two registration lines and one endpoint mapping.
  • Swap local disk for Azure Blob Storage without touching the code that uploads or links to media.
  • Serve video properly: with Azure, the browser gets a signed URL and the storage service handles Range requests, resuming and seeking, so the bytes never pass through your app.
  • Stream gigabyte uploads without buffering them in memory; size and SHA-256 are computed in one pass.
  • Recover from mistakes: deletes are soft, and the stored file stays where it is.
  • Refer to every file by one stable GUID, so storage layout can always be rebuilt from the catalog row.

Features

  • IMediaStore: UploadAsync, ListAsync (by tenant, folder and media type), GetMetaAsync (catalog row only, no payload), GetAsync (row plus content stream) and DeleteAsync.
  • MediaItem catalog row with uid, optional tenant id, media type, folder, file name, content type, size, SHA-256, inline bytes or blob URI, width, height, notes, soft-delete flag, timestamps, an Extra field and a row version. MediaItemTypeConfiguration maps it with a unique index on uid and indexes on hash and on tenant, folder and file name.
  • Local disk store: payloads up to InlineThresholdBytes (2 MB by default) are kept inline in the database row; larger ones spill to MediaRoot/{uid}/{file}. ThresholdSpillStream makes that decision without knowing the length up front.
  • Azure Blob store: streams every upload to {uid:N}/{file} in the container with configurable block size, creating the container if needed. Authenticates with a connection string or with DefaultAzureCredential against a blob service URI.
  • URL signer (IMediaUrlSigner): a plain or CDN-fronted URL when the container allows public reads, a key-signed SAS with a connection string, or a user-delegation SAS with Entra credentials. Anything else falls back to streaming.
  • /_media/{uid} endpoint: redirects to a signed URL when a signer is registered, redirects to a stored absolute http or https URL, and otherwise streams the content with range processing, an ETag from the SHA-256, Last-Modified and a private Cache-Control max-age. Every response carries X-Content-Type-Options: nosniff. Images, text, video, audio and PDF are served inline; other types download with their file name. Active content that a browser could run script from (text/html, image/svg+xml, XML and +xml types, JavaScript, and any content type that does not parse) is never rendered inline: it downloads as an attachment with Content-Security-Policy: sandbox, so an uploaded page cannot run on your origin. The Azure store stamps Content-Disposition: attachment on such blobs and the signer adds it to their SAS URLs, so they download straight from storage too.
  • File-name sanitizing that strips path segments (both / and \) and characters invalid on Windows, the same way on every OS. Blob paths of rows stored before this rule are still rebuilt with the earlier, OS-dependent rule (MediaNaming.LegacySanitizeFileName), so existing media keeps resolving.
  • No orphans on failure: if an upload is cancelled, the copy fails or the catalog save fails, the spill file or the partially written blob is removed.

Quick start

The packages are published to a local feed (C:\LocalNuGet, see nuget.config). Reference them from your web project:

<PackageReference Include="MindAttic.Storage" Version="1.0.0" />
<PackageReference Include="MindAttic.Storage.Azure" Version="1.0.0" />

MindAttic apps keep the package reference and, in Debug builds, add a project reference to a sibling checkout of this repo, so local changes are picked up without packing:

<PackageReference Include="MindAttic.Storage" Version="1.0.0" />
<ProjectReference Include="..\MindAttic.Storage\src\MindAttic.Storage\MindAttic.Storage.csproj"
                  Condition="'$(Configuration)' == 'Debug' AND Exists('..\MindAttic.Storage\src\MindAttic.Storage\MindAttic.Storage.csproj')" />

Adjust the ..\ depth to where your project sits relative to the MindAttic.Storage folder.

Add the catalog table to your DbContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ApplyConfiguration(new MediaItemTypeConfiguration());
}

Register a store and map the endpoint:

// Local disk (default MediaRoot is ./media next to the app)
builder.Services.AddMedia<AppDbContext>(o => o.MediaRoot = @"D:\Data\media");

// or Azure Blob Storage, replacing the local store and adding the URL signer
builder.Services.AddMediaAzure<AppDbContext>(o =>
{
    o.BlobServiceUri = new Uri("https://<account>.blob.core.windows.net");
    o.ContainerName = "media";
});

app.MapMediaEndpoints();

Upload and link:

var item = await store.UploadAsync(file.OpenReadStream(), file.FileName, file.ContentType, folder: "covers");
var url = $"/_media/{item.Uid}";

Add an EF Core migration in your app for the MediaItem table. The column mapping uses varbinary(max) and nvarchar(max), so it targets SQL Server.

Configuration

MediaStoreOptions (local disk, and the base of the Azure options):

Option Default Meaning
MediaRoot media next to the app Folder for spilled payloads
InlineThresholdBytes 2 MB Payloads at or below this size stay inline in the row
SignedUrlLifetime 1 hour How long a signed URL stays valid
CacheMaxAgeSeconds 3600 max-age on streamed responses; 0 disables caching
CopyBufferSize library default Buffer size for the copy-and-hash loop

AzureMediaOptions adds:

Option Default Meaning
BlobServiceUri none Account endpoint, authenticated with DefaultAzureCredential
ConnectionString none Takes precedence over the URI and enables key-signed SAS
ContainerName media Blob container
PublicRead false Container allows anonymous reads; the signer hands out plain URLs, which a CDN can cache
PublicBaseUri none CDN or custom-domain origin used in place of the blob endpoint
UploadBlockSizeBytes 8 MB Block size for streamed uploads

Testing

dotnet test MindAttic.Storage.slnx

The NUnit suite covers the local disk store, blob naming, the spill stream and the /_media endpoint, using the EF Core in-memory provider. The Azure integration tests run against the Azurite emulator and are skipped when it is not listening:

npx azurite --silent --location ./azurite --blobHost 127.0.0.1 --blobPort 10000

Project layout

Path What it is
src/MindAttic.Storage IMediaStore, IMediaUrlSigner, MediaItem and its EF configuration, LocalDiskMediaStore, ThresholdSpillStream, the endpoint and DI extensions
src/MindAttic.Storage.Azure AzureBlobMediaStore, AzureBlobUrlSigner, container factory, naming, options and DI extensions
src/MindAttic.Storage.Tests NUnit tests, including the Azurite integration tests

Limitations

  • No migrations ship with the library; the host app owns its schema.
  • The column types in MediaItemTypeConfiguration are SQL Server specific.
  • Deletes are soft only. Reclaiming storage from deleted items is a separate operation the library does not provide.
  • The Azure store always writes to blob storage; the inline threshold applies to the local disk store.

Formerly MindAttic.Media

This repo was MindAttic.Media until it was renamed in 2026-11. The packages were renamed and their versions reset; the API is the same, including the public type names (IMediaStore, MediaItem, AddMedia, AddMediaAzure, MapMediaEndpoints) and the /_media endpoint. Only the namespaces change from MindAttic.Media to MindAttic.Storage.

Old package New package
MindAttic.Media 2.0.0 MindAttic.Storage 1.0.0
MindAttic.Media.Azure 2.0.0 MindAttic.Storage.Azure 1.0.0

Source: github.com/mindattic/MindAttic.Storage. docs/MIGRATION.md has the full mapping, the steps to move a consumer, and the consumers already moved.

Documentation

  • AGENTS.md: entry point for AI agents working in this repo; it points at the shared MindAttic agent standard.

License

This repository has no LICENSE file; all rights are reserved.

Part of MindAttic — see more projects at github.com/mindattic.

Pages in this repository

  • MindAttic.Storage — Project Bible — MindAttic.Storage is drop-in media storage for ASP.NET Core apps: one upload API, one catalog row per file, and a /_media/{uid} endpoint…
  • Migrating from MindAttic.Media — MindAttic.Media was renamed to MindAttic.Storage on 2026-11-24 (feature M.MDIA-T23). The API is unchanged: the public type names…
  • MindAttic.Storage — Release charter — Drop-in media storage for ASP.NET Core apps (formerly MindAttic.Media): one upload API, a catalog row per file, and the /_media/{uid}…
From MindAttic.Storage/README.md via MindAttic.Docs; synced automatically, so edit the repo, not this page.