Path: redb-route/redb.Route/Definitions/AttachmentDefinitions.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/Definitions/AttachmentDefinitions.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Where an attachment step takes its content from: an expression, the body, or a file. The body and file forms are
/// the common cases spelled out so a route needs no expression for them.
///
public enum AttachmentSource
{
/// The value of an expression.
Expression,
/// The message body; the body itself stays.
Body,
/// A file, read from disk on every send.
File
}
///
/// Leaf definition that adds an attachment to the In message. The name and the content type are literal; the file
/// name and the file path are literal or an expression, chosen by the overload (never guessed from the text).
///
///
/// Content of an expression or the body: byte[] as is, string as UTF-8, a completed
/// shared (re-readable), another read into memory, an
/// as is. A file is not read here: the attachment opens it on every read.
///
public sealed class AddAttachmentDefinition : ProcessorDefinition
{
/// The content source.
public AttachmentSource Source { get; }
/// The attachment name; for a file it may be null, the file name then.
public string? Name { get; }
/// Content () or file path ().
public IExpression? Content { get; }
/// File name, or null (a file attachment uses the file's name).
public IExpression? FileName { get; }
/// Content type, or null for the default of the source.
public string? ContentType { get; }
private AddAttachmentDefinition(AttachmentSource source, string? name, IExpression? content, IExpression? fileName, string? contentType)
{
if (name is not null)
ArgumentException.ThrowIfNullOrWhiteSpace(name);
if (contentType is not null)
ArgumentException.ThrowIfNullOrWhiteSpace(contentType);
Source = source;
Name = name;
Content = content;
FileName = fileName;
ContentType = contentType;
}
/// An attachment with the value of .
public static AddAttachmentDefinition FromExpression(string name, IExpression content, IExpression? fileName = null, string? contentType = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentNullException.ThrowIfNull(content);
return new(AttachmentSource.Expression, name, content, fileName, contentType);
}
/// An attachment with the message body as content.
public static AddAttachmentDefinition FromBody(string name, IExpression? fileName = null, string? contentType = null)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
return new(AttachmentSource.Body, name, null, fileName, contentType);
}
/// An attachment with the content of the file at .
public static AddAttachmentDefinition FromFile(IExpression path, string? name = null, string? contentType = null)
{
ArgumentNullException.ThrowIfNull(path);
return new(AttachmentSource.File, name, path, null, contentType);
}
/// A literal as an expression: what the string overloads of the DSL pass.
internal static IExpression? Literal(string? text) => text is null ? null : new ConstantExpression(text);
///
public override IProcessor CreateProcessor(IRouteContext context)
{
var name = Name;
var fileName = FileName;
var contentType = ContentType;
var source = Source;
var content = Content;
return new DelegateProcessor(async (exchange, ct) =>
{
var message = exchange.In;
AttachmentContent attachmentContent;
string defaultType;
var resolvedName = name;
var resolvedFileName = fileName is null ? null : Evaluate(fileName, exchange, "file name");
switch (source)
{
case AttachmentSource.File:
{
var path = Evaluate(content!, exchange, "file path");
attachmentContent = AttachmentContent.FromFile(path);
var bare = Path.GetFileName(path);
resolvedName ??= bare;
resolvedFileName ??= bare;
defaultType = Attachment.DefaultContentType;
break;
}
case AttachmentSource.Body:
(attachmentContent, defaultType) = await ToContent(message.Body, "the body", ct).ConfigureAwait(false);
defaultType = message.ContentType ?? defaultType;
break;
default:
(attachmentContent, defaultType) = await ToContent(content!.Evaluate