Path: redb-route/redb.Route/Processors/StreamCachingProcessor.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/Processors/StreamCachingProcessor.cs
----
using redb.Route.Abstractions;
using redb.Route.Configuration;
using redb.Route.Core;
namespace redb.Route.Processors;
///
/// Wraps a forward-only Stream body with a seekable ,
/// allowing downstream processors to re-read the body multiple times.
/// Non-Stream bodies pass through unchanged.
///
internal sealed class StreamCachingProcessor : IProcessor
{
private readonly IProcessor _next;
private readonly StreamCacheOptions _options;
/// Creates a stream caching processor.
/// The next processor in the pipeline.
/// Stream cache options (spool threshold, temp directory).
internal StreamCachingProcessor(IProcessor next, StreamCacheOptions options)
{
ArgumentNullException.ThrowIfNull(next);
ArgumentNullException.ThrowIfNull(options);
_next = next;
_options = options;
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
await StreamCacheBody.CacheAsync(exchange, _options, ct).ConfigureAwait(false);
await _next.Process(exchange, ct).ConfigureAwait(false);
}
}
///
/// Standalone (no-next) variant of stream caching, suitable for use as a
/// leaf inside a .
/// Wraps a forward-only body with a seekable ;
/// the pipeline advances to the next step automatically.
/// Non-Stream bodies pass through unchanged.
///
internal sealed class StreamCachingTransformer : IProcessor
{
private readonly StreamCacheOptions _options;
internal StreamCachingTransformer(StreamCacheOptions options)
{
ArgumentNullException.ThrowIfNull(options);
_options = options;
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
await StreamCacheBody.CacheAsync(exchange, _options, ct).ConfigureAwait(false);
}
}
/// The one place a stream body is replaced by its cache.
internal static class StreamCacheBody
{
///
/// Replaces a forward-only stream body with a and closes the source: the exchange owns its
/// body and disposes only what the body is, so a replaced stream nobody closes would hold its file or connection
/// until the garbage collector ran (a streamed file the consumer then fails to delete). Camel closes the source too.
///
public static async Task CacheAsync(IExchange exchange, StreamCacheOptions options, CancellationToken ct)
{
if (exchange.In.Body is not Stream stream || stream is StreamCache)
return;
var cache = new StreamCache(options);
try
{
await cache.CacheFromSourceAsync(stream, ct).ConfigureAwait(false);
}
catch
{
// Not cached: the body stays the source, and the half-written cache goes.
await cache.DisposeAsync().ConfigureAwait(false);
throw;
}
exchange.In.Body = cache;
await stream.DisposeAsync().ConfigureAwait(false);
}
}