Path: redb-route/redb.Route/Abstractions/AttachmentContent.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Abstractions/AttachmentContent.cs ---- using redb.Route.Core; namespace redb.Route.Abstractions; /// /// The content of an : a source that opens a new stream from the start on every read. Reading /// more than once is required, not optional: Multicast branches, redelivery and a resend over a fresh connection all /// read the same attachment again, so a forward-only stream is not accepted as a source. /// /// /// A source that holds a resource (a , a file to delete) implements /// ; the exchange that carries the message releases it like a disposable body. /// public abstract class AttachmentContent { /// Length of the content in bytes. public abstract long Length { get; } /// Opens a new read-only stream positioned at the start of the content. public abstract Stream OpenRead(); /// Opens a new read-only stream positioned at the start of the content. public virtual ValueTask OpenReadAsync(CancellationToken ct = default) => ValueTask.FromResult(OpenRead()); /// Reads the whole content into a new array. public virtual byte[] ReadAllBytes() { using var stream = OpenRead(); using var buffer = new MemoryStream(Length > int.MaxValue ? 0 : (int)Length); stream.CopyTo(buffer); return buffer.ToArray(); } /// Reads the whole content into a new array. public virtual async ValueTask ReadAllBytesAsync(CancellationToken ct = default) { var stream = await OpenReadAsync(ct).ConfigureAwait(false); await using (stream.ConfigureAwait(false)) { using var buffer = new MemoryStream(Length > int.MaxValue ? 0 : (int)Length); await stream.CopyToAsync(buffer, ct).ConfigureAwait(false); return buffer.ToArray(); } } /// /// An isolated copy for a checkpoint snapshot: the content is read into memory, so the copy outlives the source and /// later changes to it. /// public virtual AttachmentContent DeepCopy() => new BytesContent(ReadAllBytes()); /// Content held in memory. The array is shared, not copied, like a byte[] body. public static AttachmentContent FromBytes(byte[] bytes) { ArgumentNullException.ThrowIfNull(bytes); return new BytesContent(bytes); } /// Content of a file, read from disk on every open. /// Path of an existing file. /// true to delete the file when the exchange carrying the message is released. public static AttachmentContent FromFile(string path, bool deleteWithExchange = false) { ArgumentException.ThrowIfNullOrEmpty(path); var full = Path.GetFullPath(path); if (!File.Exists(full)) throw new FileNotFoundException("Attachment file does not exist.", full); return deleteWithExchange ? new OwnedFileContent(full) : new FileContent(full); } /// /// Content of a completed ; every open gets an independent reader. The cache is disposed /// with the exchange carrying the message. /// public static AttachmentContent FromStreamCache(StreamCache cache) { ArgumentNullException.ThrowIfNull(cache); if (!cache.CanRead) throw new ArgumentException("The StreamCache must be completed (CompleteWriting) before it becomes attachment content.", nameof(cache)); return new StreamCacheContent(cache); } private sealed class BytesContent(byte[] bytes) : AttachmentContent { public override long Length => bytes.LongLength; public override Stream OpenRead() => new MemoryStream(bytes, writable: false); public override byte[] ReadAllBytes() => (byte[])bytes.Clone(); public override ValueTask ReadAllBytesAsync(CancellationToken ct = default) => ValueTask.FromResult(ReadAllBytes()); public override AttachmentContent DeepCopy() => new BytesContent((byte[])bytes.Clone()); } private class FileContent(string path) : AttachmentContent { protected string FilePath => path; public override long Length => new FileInfo(path).Length; public override Stream OpenRead() => new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 81920, FileOptions.SequentialScan); public override ValueTask OpenReadAsync(CancellationToken ct = default) => ValueTask.FromResult(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 81920, FileOptions.Asynchronous | FileOptions.SequentialScan)); } private sealed class OwnedFileContent(string path) : FileContent(path), IAsyncDisposable { public ValueTask DisposeAsync() { File.Delete(FilePath); return ValueTask.CompletedTask; } } private sealed class StreamCacheContent(StreamCache cache) : AttachmentContent, IAsyncDisposable { public override long Length => cache.Length; public override Stream OpenRead() => cache.OpenReader(); public ValueTask DisposeAsync() => cache.DisposeAsync(); } }