Path: redb-route/redb.Route.File/FileProducer.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.File/FileProducer.cs
----
using redb.Route.Abstractions;
using redb.Route.GenericFile;
namespace redb.Route.File;
///
/// File producer. Inherits the complete write flow from
/// (temp file, atomic rename, file-exist strategies).
/// Adds content-type inference for the produced file.
///
public class FileProducer : GenericFileProducer
{
private readonly FileEndpoint _endpoint;
///
protected override string BasePath => _endpoint.DirectoryPath;
///
protected override string FileNameProducedHeader => FileHeaders.FileNameProduced;
/// Creates a file producer.
public FileProducer(FileEndpoint endpoint, FileEndpointOptions options)
: base(endpoint, options, new LocalFileOperations())
{
_endpoint = endpoint;
}
///
///
/// The file name usually comes from an incoming message, so it is not trusted: a relative
/// "../" name or an absolute path would otherwise write outside the endpoint directory
/// ( silently discards the base for absolute paths).
/// Set to false to allow it.
///
protected override void ValidatePath(string targetPath)
{
if (!Options.JailStartingDirectory)
return;
var fullTarget = Path.GetFullPath(targetPath);
var root = Path.GetFullPath(BasePath);
var comparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
if (!GenericFileUtils.IsWithinDirectory(root, fullTarget, Path.DirectorySeparatorChar, comparison))
{
throw new UnauthorizedAccessException(
$"Producer path '{targetPath}' resolves to '{fullTarget}', outside the endpoint directory '{root}'. " +
"Set jailStartingDirectory=false to allow writing outside the base directory.");
}
}
///
protected override 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(FileHeaders.FileName, out var hfn)
? hfn?.ToString()
: null;
fileName = headerFileName ?? $"redb-{Guid.NewGuid():N}";
}
return Operations.CombinePath(BasePath, fileName);
}
}