Path: redb-route/redb.Route.GenericFile/GenericFileProducer.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.GenericFile/GenericFileProducer.cs
----
using System.Diagnostics;
using System.Text;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Telemetry;
namespace redb.Route.GenericFile;
///
/// Template-method base class for all file-based producers (local file, SFTP, FTP).
/// Owns the complete write flow: resolve body → resolve path → handle existing file →
/// write via temp → atomic rename → cleanup on failure.
/// Subclasses provide protocol-specific hooks (connection, chmod, jail check).
///
/// Concrete options type inheriting .
public abstract class GenericFileProducer : IProducer
where TOptions : GenericFileEndpointOptions
{
private ILogger? _logger;
/// Endpoint this producer belongs to.
protected IEndpoint ProducerEndpoint { get; }
/// Endpoint options.
protected TOptions Options { get; }
/// File operations (protocol-specific adapter).
protected IFileOperations Operations { get; }
/// Logger resolved from the component.
protected ILogger? Logger => _logger ??= (ProducerEndpoint.Component as ComponentBase)?.Logger;
/// Base directory path for this producer.
protected abstract string BasePath { get; }
/// Header name for the produced file path (e.g. "redbFile.NameProduced").
protected abstract string FileNameProducedHeader { get; }
/// Creates a generic file producer.
protected GenericFileProducer(IEndpoint endpoint, TOptions options, IFileOperations operations)
{
ArgumentNullException.ThrowIfNull(endpoint);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(operations);
ProducerEndpoint = endpoint;
Options = options;
Operations = operations;
}
// ═══════════════════════════════════════════════════════════════════
// PROCESS (template method)
// ═══════════════════════════════════════════════════════════════════
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
var body = ResolveBody(exchange);
var targetPath = ResolveTargetPath(exchange);
var targetDir = Operations.GetParentPath(targetPath);
using var activity = RouteTelemetryExtensions.StartTransportSpan(
(ProducerEndpoint.Component as ComponentBase)?.Context,
$"file write {ProducerEndpoint.Component.Scheme}", ActivityKind.Producer,
"redb.system", ProducerEndpoint.Component.Scheme,
ProducerEndpoint.Uri.NormalizedKey,
destination: targetPath,
operation: "write");
try
{
await WriteAsync(exchange, body, targetPath, targetDir, ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException || !ct.IsCancellationRequested)
{
// Only our own token cancelling the write is a stop; any other failure marks the span.
activity.RecordFailure(ex);
throw;
}
}
private async Task WriteAsync(IExchange exchange, object? body, string targetPath, string targetDir, CancellationToken ct)
{
// Validate path (virtual — jail check for remote)
ValidatePath(targetPath);
// Before-write hook (connect if remote, auto-create directory)
await BeforeWriteAsync(targetDir, ct).ConfigureAwait(false);
// Determine the path to write to (may be temp file)
var writePath = ResolveTempPath(targetPath);
var isTemp = !string.Equals(writePath, targetPath, StringComparison.Ordinal);
try
{
// Handle existing file
if (await Operations.ExistsAsync(targetPath, ct).ConfigureAwait(false))
{
switch (Options.FileExist)
{
case GenericFileExistStrategy.Fail:
throw new IOException(
$"File already exists and FileExist=Fail: {targetPath}");
case GenericFileExistStrategy.Ignore:
exchange.In.Headers[FileNameProducedHeader] = targetPath;
await OnWriteCompletedAsync(exchange, targetPath, ct).ConfigureAwait(false);
return;
case GenericFileExistStrategy.Append:
await AppendToFile(targetPath, body, ct).ConfigureAwait(false);
exchange.In.Headers[FileNameProducedHeader] = targetPath;
await OnWriteCompletedAsync(exchange, targetPath, ct).ConfigureAwait(false);
return;
case GenericFileExistStrategy.Move:
await MoveExistingFile(targetPath, ct).ConfigureAwait(false);
break;
case GenericFileExistStrategy.TryRename:
var renamePath = targetPath + "." + DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmssfff");
try
{
await Operations.MoveAsync(targetPath, renamePath, false, ct).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger?.LogWarning(ex, "TryRename failed for {Path}", targetPath);
}
break;
case GenericFileExistStrategy.Override:
default:
if (Options.EagerDeleteTargetFile)
{
try
{
await Operations.DeleteAsync(targetPath, ct).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger?.LogWarning(ex, "EagerDelete failed for {Path}", targetPath);
}
}
break;
}
}
// Write the file
await WriteBody(writePath, body, ct).ConfigureAwait(false);
// Atomic rename from temp to target
if (isTemp)
{
// Delete target if it appeared during write
if (await Operations.ExistsAsync(targetPath, ct).ConfigureAwait(false))
{
try
{
await Operations.DeleteAsync(targetPath, ct).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger?.LogWarning(ex, "Failed to delete target before rename {Path}", targetPath);
}
}
await Operations.MoveAsync(writePath, targetPath, true, ct).ConfigureAwait(false);
}
exchange.In.Headers[FileNameProducedHeader] = targetPath;
// After-write hook (chmod, keepLastModified, etc.)
await AfterWriteAsync(exchange, targetPath, ct).ConfigureAwait(false);
// Completion hook (disconnect if configured, etc.)
await OnWriteCompletedAsync(exchange, targetPath, ct).ConfigureAwait(false);
}
catch
{
// Clean up temp file on failure
if (isTemp)
{
try
{
if (await Operations.ExistsAsync(writePath, ct).ConfigureAwait(false))
await Operations.DeleteAsync(writePath, ct).ConfigureAwait(false);
}
catch (Exception cleanupEx)
{
Logger?.LogDebug(cleanupEx, "Failed to clean up temp file {Path}", writePath);
}
}
throw;
}
}
// ═══════════════════════════════════════════════════════════════════
// LIFECYCLE
// ═══════════════════════════════════════════════════════════════════
///
public virtual Task Start(CancellationToken ct = default)
{
_logger ??= (ProducerEndpoint.Component as ComponentBase)?.Logger;
Logger?.LogInformation("{Producer} started", EndpointUri.ForLog(ProducerEndpoint.Uri.NormalizedKey, Logger));
return Task.CompletedTask;
}
///
public virtual Task Stop(CancellationToken ct = default)
{
Logger?.LogInformation("{Producer} stopped", EndpointUri.ForLog(ProducerEndpoint.Uri.NormalizedKey, Logger));
return Task.CompletedTask;
}
// ═══════════════════════════════════════════════════════════════════
// SHARED LOGIC
// ═══════════════════════════════════════════════════════════════════
/// Extracts the body from the exchange (Out preferred, falls back to In).
protected object? ResolveBody(IExchange exchange)
{
var body = exchange.HasOut ? exchange.Out!.Body : exchange.In.Body;
if (body == null && !Options.AllowNullBody)
{
throw new InvalidOperationException(
"Exchange body is null and AllowNullBody=false. Set AllowNullBody=true to write empty files.");
}
return body;
}
/// Resolves the target file path from the FileName option or exchange headers.
protected virtual string ResolveTargetPath(IExchange exchange)
{
string fileName;
if (Options.FileName != null)
{
fileName = Options.FileName.Value.Resolve(exchange) ?? "output";
}
else
{
// Try to get filename from incoming exchange headers
var headerFileName = exchange.In.Headers.TryGetValue(FileNameProducedHeader.Replace("Produced", ""), out var hfn)
? hfn?.ToString()
: null;
fileName = headerFileName ?? $"redb-{Guid.NewGuid():N}";
}
return Operations.CombinePath(BasePath, fileName);
}
///
/// Computes the temp file path if TempPrefix or TempFileName is configured. The file keys in TempFileName
/// (${file:name}.part) read the target file, the one being written; its size and time are not known yet and
/// write nothing.
///
protected string ResolveTempPath(string targetPath)
{
if (!string.IsNullOrEmpty(Options.TempFileName))
{
var dir = Operations.GetParentPath(targetPath);
var target = new GenericFileFacts(Operations.GetFileName(targetPath), targetPath, BasePath, Operations);
return Operations.CombinePath(dir, GenericFileUtils.SubstituteFileTokens(target, Options.TempFileName));
}
if (!string.IsNullOrEmpty(Options.TempPrefix))
{
var dir = Operations.GetParentPath(targetPath);
var name = Operations.GetFileName(targetPath);
return Operations.CombinePath(dir, Options.TempPrefix + name);
}
return targetPath;
}
/// Writes the body to the given path.
protected async Task WriteBody(string path, object? body, CancellationToken ct)
{
if (body == null)
{
await Operations.WriteAsync(path, Array.Empty(), ct).ConfigureAwait(false);
}
else if (body is byte[] bytes)
{
await Operations.WriteAsync(path, bytes, ct).ConfigureAwait(false);
}
else if (body is Stream stream)
{
await Operations.WriteAsync(path, stream, ct).ConfigureAwait(false);
}
else
{
var text = BodyText.Of(body);
var encoding = ResolveEncoding();
var textBytes = encoding.GetBytes(text);
await Operations.WriteAsync(path, textBytes, ct).ConfigureAwait(false);
}
}
/// Appends the body to the given path.
protected async Task AppendToFile(string path, object? body, CancellationToken ct)
{
var encoding = ResolveEncoding();
if (body is byte[] bytes)
{
await Operations.AppendAsync(path, bytes, ct).ConfigureAwait(false);
}
else if (body is Stream stream)
{
using var ms = new MemoryStream();
await stream.CopyToAsync(ms, ct).ConfigureAwait(false);
await Operations.AppendAsync(path, ms.ToArray(), ct).ConfigureAwait(false);
}
else
{
var text = body is null ? "" : BodyText.Of(body);
await Operations.AppendTextAsync(path, text, encoding, ct).ConfigureAwait(false);
}
if (!string.IsNullOrEmpty(Options.AppendChars))
{
await Operations.AppendTextAsync(path, Options.AppendChars, encoding, ct).ConfigureAwait(false);
}
}
/// Moves an existing file at the target path to a backup name.
protected virtual async Task MoveExistingFile(string path, CancellationToken ct)
{
var backupPath = path + ".bak";
await Operations.MoveAsync(path, backupPath, true, ct).ConfigureAwait(false);
}
/// Resolves encoding from the Charset option with UTF-8 fallback.
protected Encoding ResolveEncoding()
{
try
{
return Encoding.GetEncoding(Options.Charset);
}
catch
{
return Encoding.UTF8;
}
}
// ═══════════════════════════════════════════════════════════════════
// VIRTUAL HOOKS
// ═══════════════════════════════════════════════════════════════════
///
/// Validates the target path before writing. Override for jail-directory checks.
///
protected virtual void ValidatePath(string targetPath) { }
///
/// Called before writing. Override to connect to remote server, auto-create directory with chmod, etc.
/// Default implementation auto-creates the target directory if configured.
///
protected virtual async Task BeforeWriteAsync(string targetDir, CancellationToken ct)
{
if (Options.AutoCreate)
{
await Operations.CreateDirectoryAsync(targetDir, ct).ConfigureAwait(false);
}
}
///
/// Called after a successful write. Override for chmod, keepLastModified, content-type inference.
///
protected virtual Task AfterWriteAsync(IExchange exchange, string targetPath, CancellationToken ct)
=> Task.CompletedTask;
///
/// Called after write or early return (Ignore, Append). Override to disconnect if configured.
///
protected virtual Task OnWriteCompletedAsync(IExchange exchange, string targetPath, CancellationToken ct)
=> Task.CompletedTask;
}