MindAttic.Storage: Difference between revisions
md2wiki: sync from MindAttic.Docs |
md2wiki: sync from MindAttic.Docs |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
= MindAttic.Storage = | = MindAttic.Storage = | ||
Drop-in media storage for ASP.NET Core apps: one upload API, a catalog row per file, and a media endpoint that | 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. | ||
https://img.shields.io/badge/.NET-10-512BD4.svg https://img.shields.io/badge/ASP.NET%20Core-library-512BD4.svg https://img.shields.io/badge/EF%20Core-SQL%20Server-CC2927 | https://img.shields.io/badge/.NET-10-512BD4.svg https://img.shields.io/badge/ASP.NET%20Core-library-512BD4.svg https://img.shields.io/badge/EF%20Core-SQL%20Server-CC2927.svg https://img.shields.io/badge/version-1.0.0-2f7a4f.svg | ||
<pre class="md-code lang-text"> your app MindAttic.Storage storage | <pre class="md-code lang-text"> your app MindAttic.Storage storage | ||
| Line 11: | Line 11: | ||
├─ LocalDiskMediaStore <= 2 MB ──> inline bytes in the MediaItem row | ├─ LocalDiskMediaStore <= 2 MB ──> inline bytes in the MediaItem row | ||
│ > 2 MB ──> MediaRoot/{uid}/{file} | │ > 2 MB ──> MediaRoot/{uid}/{file} | ||
│ | │ | ||
└─ MediaItem row (uid, tenant, folder, type, size, sha256, ...) | └─ MediaItem row (uid, tenant, folder, type, size, sha256, ...) | ||
GET /_media/{uid} | GET /_media/{uid} ──> stream through the app (ETag, Last-Modified, Range, Cache-Control)</pre> | ||
The supported package is <code>MindAttic.Storage</code>: the abstraction, the EF Core model, the local-disk store (<code>AddMedia</code>) and the endpoint. | |||
<blockquote> | |||
<strong>Azure is frozen.</strong> MindAttic's Azure resources were decommissioned on 2026-11-26. <code>MindAttic.Storage.Azure</code> 1.0.0 (<code>AddMediaAzure</code>, 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 <code>AddMedia</code>; Prose and IdeaCms, the two apps still on it, switch to <code>AddMedia<TContext></code> with a local <code>MediaRoot</code> as described in [[MindAttic.Storage/MIGRATION#Switching off MindAttic.Storage.Azure|MIGRATION.md: Switching off MindAttic.Storage.Azure]]. | |||
</blockquote> | |||
== Scope == | == Scope == | ||
| Line 27: | Line 29: | ||
* Add uploads to an app with two registration lines and one endpoint mapping. | * 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: | * 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. | * 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. | * Recover from mistakes: deletes are soft, and the stored file stays where it is. | ||
| Line 38: | Line 40: | ||
* <strong><code>MediaItem</code> catalog row</strong> 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 <code>Extra</code> field and a row version. <code>MediaItemTypeConfiguration</code> maps it with a unique index on uid and indexes on hash and on tenant, folder and file name. | * <strong><code>MediaItem</code> catalog row</strong> 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 <code>Extra</code> field and a row version. <code>MediaItemTypeConfiguration</code> maps it with a unique index on uid and indexes on hash and on tenant, folder and file name. | ||
* <strong>Local disk store:</strong> payloads up to <code>InlineThresholdBytes</code> (2 MB by default) are kept inline in the database row; larger ones spill to <code>MediaRoot/{uid}/{file}</code>. <code>ThresholdSpillStream</code> makes that decision without knowing the length up front. | * <strong>Local disk store:</strong> payloads up to <code>InlineThresholdBytes</code> (2 MB by default) are kept inline in the database row; larger ones spill to <code>MediaRoot/{uid}/{file}</code>. <code>ThresholdSpillStream</code> makes that decision without knowing the length up front. | ||
* <strong><code>/_media/{uid}</code> endpoint:</strong> streams the content with range processing, an ETag from the SHA-256, Last-Modified and a private <code>Cache-Control</code> max-age. Every response carries <code>X-Content-Type-Options: nosniff</code>. 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 (<code>text/html</code>, <code>image/svg+xml</code>, XML and <code>+xml</code> types, JavaScript, and any content type that does not parse) is never rendered inline: it downloads as an attachment with <code>Content-Security-Policy: sandbox</code>, so an uploaded page cannot run on your origin. | |||
* <strong><code>/_media/{uid}</code> endpoint:</strong> | |||
* <strong>File-name sanitizing</strong> that strips path segments (both <code>/</code> and <code>\</code>) 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 (<code>MediaNaming.LegacySanitizeFileName</code>), so existing media keeps resolving. | * <strong>File-name sanitizing</strong> that strips path segments (both <code>/</code> and <code>\</code>) 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 (<code>MediaNaming.LegacySanitizeFileName</code>), so existing media keeps resolving. | ||
* <strong>No orphans on failure:</strong> if an upload is cancelled, the copy fails or the catalog save fails, the spill file or | * <strong>No orphans on failure:</strong> if an upload is cancelled, the copy fails or the catalog save fails, the spill file is removed. | ||
* <strong>Frozen: Azure Blob backend</strong> (<code>MindAttic.Storage.Azure</code> 1.0.0, kept only for existing consumers). <code>AddMediaAzure</code> replaces the local store with <code>AzureBlobMediaStore</code> and registers <code>AzureBlobUrlSigner</code> as the <code>IMediaUrlSigner</code>; when a signer is registered, <code>/_media/{uid}</code> 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 [[MindAttic.Storage/MIGRATION#Switching off MindAttic.Storage.Azure|Switching off MindAttic.Storage.Azure]] to leave it. | |||
== Quick start == | == Quick start == | ||
The | The package is published to a local feed (<code>C:\LocalNuGet</code>, see <code>nuget.config</code>). Reference it from your web project: | ||
<pre class="md-code lang-xml"><PackageReference Include="MindAttic.Storage | <pre class="md-code lang-xml"><PackageReference Include="MindAttic.Storage" Version="1.0.0" /></pre> | ||
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: | 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: | ||
| Line 70: | Line 70: | ||
<pre class="md-code lang-csharp">// Local disk (default MediaRoot is ./media next to the app) | <pre class="md-code lang-csharp">// Local disk (default MediaRoot is ./media next to the app) | ||
builder.Services.AddMedia<AppDbContext>(o => o.MediaRoot = @"D:\Data\media"); | builder.Services.AddMedia<AppDbContext>(o => o.MediaRoot = @"D:\Data\media"); | ||
app.MapMediaEndpoints();</pre> | app.MapMediaEndpoints();</pre> | ||
| Line 89: | Line 82: | ||
== Configuration == | == Configuration == | ||
<code>MediaStoreOptions</code> | <code>MediaStoreOptions</code>, set through <code>AddMedia</code>: | ||
{| class="wikitable" | {| class="wikitable" | ||
| Line 103: | Line 96: | ||
| 2 MB | | 2 MB | ||
| Payloads at or below this size stay inline in the row | | Payloads at or below this size stay inline in the row | ||
|- | |- | ||
| <code>CacheMaxAgeSeconds</code> | | <code>CacheMaxAgeSeconds</code> | ||
| Line 115: | Line 104: | ||
| library default | | library default | ||
| Buffer size for the copy-and-hash loop | | Buffer size for the copy-and-hash loop | ||
|- | |||
| <code>SignedUrlLifetime</code> | |||
| 1 hour | |||
| Frozen Azure path only: how long a signed URL stays valid; removed in 2.0.0 | |||
|} | |} | ||
<code>AzureMediaOptions</code> adds: | Frozen: <code>AzureMediaOptions</code> (<code>MindAttic.Storage.Azure</code>, no new work) adds: | ||
{| class="wikitable" | {| class="wikitable" | ||
| Line 153: | Line 146: | ||
<pre class="md-code lang-powershell">dotnet test MindAttic.Storage.slnx</pre> | <pre class="md-code lang-powershell">dotnet test MindAttic.Storage.slnx</pre> | ||
The NUnit suite covers the local disk store, | The NUnit suite covers the local disk store, <code>AddMedia</code> registration, the spill stream and the <code>/_media</code> 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 == | == Project layout == | ||
| Line 167: | Line 158: | ||
|- | |- | ||
| <code>src/MindAttic.Storage.Azure</code> | | <code>src/MindAttic.Storage.Azure</code> | ||
| <code>AzureBlobMediaStore</code>, <code>AzureBlobUrlSigner</code>, container factory, naming, options and DI extensions | | Frozen: <code>AzureBlobMediaStore</code>, <code>AzureBlobUrlSigner</code>, container factory, naming, options and DI extensions; removed in 2.0.0 | ||
|- | |- | ||
| <code>src/MindAttic.Storage.Tests</code> | | <code>src/MindAttic.Storage.Tests</code> | ||
| NUnit tests, including the Azurite integration tests | | NUnit tests, including the frozen package's Azurite integration tests | ||
|} | |} | ||
| Line 178: | Line 169: | ||
* The column types in <code>MediaItemTypeConfiguration</code> are SQL Server specific. | * The column types in <code>MediaItemTypeConfiguration</code> are SQL Server specific. | ||
* Deletes are soft only. Reclaiming storage from deleted items is a separate operation the library does not provide. | * 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 == | == Formerly MindAttic.Media == | ||
| Line 209: | Line 200: | ||
== Pages in this repository == | == Pages in this repository == | ||
* [[MindAttic.Storage/BIBLE|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… | * [[MindAttic.Storage/BIBLE|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… | ||
* [[MindAttic.Storage/CODE_LEDGER|Code ledger: MindAttic.Storage]] — Every tracked source file and the ticket in this repo's tree that accounts for it (MAT-T484; method:… | |||
* [[MindAttic.Storage/MIGRATION|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/MIGRATION|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|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}… | * [[MindAttic.Storage/RELEASE|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}… | ||
* [[MindAttic.Storage/SPEC|MindAttic.Storage — Product specification]] — MindAttic.Storage | * [[MindAttic.Storage/SPEC|MindAttic.Storage — Product specification]] — In docs/ECOSYSTEM.md MindAttic.Storage owns the files concern: apps store and serve uploaded files through it. | ||
<div class="md2wiki-source">From [https://github.com/mindattic/MindAttic.Storage/blob/main/docs/overview.md MindAttic.Storage/docs/overview.md] via MindAttic.Docs; synced automatically, so edit the repo, not this page.</div> | <div class="md2wiki-source">From [https://github.com/mindattic/MindAttic.Storage/blob/main/docs/overview.md MindAttic.Storage/docs/overview.md] via MindAttic.Docs; synced automatically, so edit the repo, not this page.</div> | ||
[[Category:MindAttic.Storage]] | [[Category:MindAttic.Storage]] | ||
Latest revision as of 15:49, 1 December 2026
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.
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.Azure1.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 useAddMedia; Prose and IdeaCms, the two apps still on it, switch toAddMedia<TContext>with a localMediaRootas 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) andDeleteAsync.MediaItemcatalog 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, anExtrafield and a row version.MediaItemTypeConfigurationmaps 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 toMediaRoot/{uid}/{file}.ThresholdSpillStreammakes 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 privateCache-Controlmax-age. Every response carriesX-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+xmltypes, JavaScript, and any content type that does not parse) is never rendered inline: it downloads as an attachment withContent-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.Azure1.0.0, kept only for existing consumers).AddMediaAzurereplaces the local store withAzureBlobMediaStoreand registersAzureBlobUrlSigneras theIMediaUrlSigner; 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
MediaItemTypeConfigurationare 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
- 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…
- Code ledger: MindAttic.Storage — Every tracked source file and the ticket in this repo's tree that accounts for it (MAT-T484; method:…
- 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}…
- MindAttic.Storage — Product specification — In docs/ECOSYSTEM.md MindAttic.Storage owns the files concern: apps store and serve uploaded files through it.