Jump to content

MindAttic.Storage

From MindAttic Docs

MindAttic.Storage

Drop-in media storage for ASP.NET Core apps: one upload API, a catalog row per file on your own database, the file itself on local disk, and a media endpoint that streams it back with Range, ETag and caching.

.NET-10-512BD4.svg ASP.NET%20Core-library-512BD4.svg EF%20Core-SQL%20Server-CC2927.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}
                                  │
                                  └─ MediaItem row (uid, tenant, folder, type, size, sha256, ...)

 GET /_media/{uid} ──> stream through the app (ETag, Last-Modified, Range, Cache-Control)

The supported package is MindAttic.Storage: the abstraction, the EF Core model, the local-disk store (AddMedia) and the endpoint.

Azure is frozen. MindAttic's Azure resources were decommissioned on 2026-11-26. MindAttic.Storage.Azure 1.0.0 (AddMediaAzure, the Azure Blob store and its URL signer) stays on the feed only for the consumers that still reference it; it gets no new work and is removed in 2.0.0 (MS-154). New apps use AddMedia; Prose and IdeaCms, the two apps still on it, switch to AddMedia<TContext> with a local MediaRoot as described in MIGRATION.md: Switching off MindAttic.Storage.Azure.

Scope

The complete product, built and planned, is specified in docs/SPEC.md; the current finish line is docs/RELEASE.md. MS-57 documents the local-disk path and freezes Azure; MS-154 then removes the Azure package once Prose and IdeaCms have switched, finishing the product as 2.0.0.

Why

  • Add uploads to an app with two registration lines and one endpoint mapping.
  • Keep small files in the database row and large ones on local disk, decided while the upload streams.
  • Serve video properly: the endpoint answers Range requests, so the browser can resume and seek.
  • 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.
  • /_media/{uid} endpoint: 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.
  • 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 is removed.
  • Frozen: Azure Blob backend (MindAttic.Storage.Azure 1.0.0, kept only for existing consumers). AddMediaAzure replaces the local store with AzureBlobMediaStore and registers AzureBlobUrlSigner as the IMediaUrlSigner; when a signer is registered, /_media/{uid} redirects to a signed or public blob URL instead of streaming, and a row whose stored location is an absolute http or https URL is redirected to it. No new work goes into this path; see Switching off MindAttic.Storage.Azure to leave it.

Quick start

The package is published to a local feed (C:\LocalNuGet, see nuget.config). Reference it from your web project:

<PackageReference Include="MindAttic.Storage" 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");

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, set through AddMedia:

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
CacheMaxAgeSeconds 3600 max-age on streamed responses; 0 disables caching
CopyBufferSize library default Buffer size for the copy-and-hash loop
SignedUrlLifetime 1 hour Frozen Azure path only: how long a signed URL stays valid; removed in 2.0.0

Frozen: AzureMediaOptions (MindAttic.Storage.Azure, no new work) 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, AddMedia registration, the spill stream and the /_media endpoint, using the EF Core in-memory provider. The frozen Azure package keeps its naming and Azurite integration tests until 2.0.0 removes it; the integration tests are skipped when no Azurite emulator is listening.

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 Frozen: AzureBlobMediaStore, AzureBlobUrlSigner, container factory, naming, options and DI extensions; removed in 2.0.0
src/MindAttic.Storage.Tests NUnit tests, including the frozen package's 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.
  • Local disk only for new work: the frozen Azure store always writes to blob storage and ignores the inline threshold.

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

From MindAttic.Storage/docs/overview.md via MindAttic.Docs; synced automatically, so edit the repo, not this page.