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(exchange), $"attachment '{resolvedName}'", ct) .ConfigureAwait(false); break; } message.Attachments.Add(new Attachment { Name = resolvedName!, FileName = resolvedFileName, ContentType = contentType ?? defaultType, Content = attachmentContent }); }); } private static async Task<(AttachmentContent Content, string DefaultType)> ToContent(object? value, string what, CancellationToken ct) { switch (value) { case null: throw new InvalidOperationException($"Cannot attach {what}: it is null."); case AttachmentContent ready: return (ready, Attachment.DefaultContentType); case byte[] bytes: return (AttachmentContent.FromBytes(bytes), Attachment.DefaultContentType); case string text: return (AttachmentContent.FromBytes(System.Text.Encoding.UTF8.GetBytes(text)), "text/plain; charset=utf-8"); case StreamCache { CanRead: true } cache: return (AttachmentContent.FromStreamCache(cache), Attachment.DefaultContentType); case Stream stream: { using var buffer = new MemoryStream(); await stream.CopyToAsync(buffer, ct).ConfigureAwait(false); return (AttachmentContent.FromBytes(buffer.ToArray()), Attachment.DefaultContentType); } default: throw new InvalidOperationException( $"Cannot attach {what} of type '{value.GetType().FullName}': use byte[], string, Stream or AttachmentContent."); } } private static string Evaluate(IExpression expression, IExchange exchange, string field) => expression.Evaluate(exchange)?.ToString() is { Length: > 0 } value ? value : throw new InvalidOperationException($"The attachment {field} evaluated to nothing."); } /// Leaf definition that removes the attachments with a name from the In message. public sealed class RemoveAttachmentDefinition : ProcessorDefinition { /// The name of the attachments to remove. public string Name { get; } /// Creates the definition. public RemoveAttachmentDefinition(string name) { ArgumentException.ThrowIfNullOrWhiteSpace(name); Name = name; } /// public override IProcessor CreateProcessor(IRouteContext context) => new DelegateProcessor(exchange => exchange.In.Attachments.Remove(Name)); } /// Leaf definition that removes every attachment of the In message. public sealed class RemoveAttachmentsDefinition : ProcessorDefinition { /// public override IProcessor CreateProcessor(IRouteContext context) => new DelegateProcessor(exchange => exchange.In.Attachments.Clear()); }