Path: redb-route/redb.Route/Expressions/ExpressionResolver.Attachments.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/Expressions/ExpressionResolver.Attachments.cs ---- using System.Text; using redb.Route.Abstractions; namespace redb.Route.Expressions; /// /// Partial class ExpressionResolver: the message attachments in the language, with the names of Camel Simple. /// attachments is the list of the In message (attachments.size, attachments[0].fileName); /// attachmentContent(name), attachmentContentAsText(name), attachmentContentType(name) and /// attachmentHeader(name, header) read the first attachment with that name, null when there is none. /// /// /// Expressions are evaluated synchronously, so the content functions read the whole content synchronously: they suit /// small parts (a text note, a short XML). Large files are for the attachment steps and the connectors. /// public static partial class ExpressionResolver { private const string AttachmentsRoot = "attachments"; /// Whether is the attachments root or a path under it. private static bool IsAttachmentsPath(string name) => name.StartsWith(AttachmentsRoot, StringComparison.Ordinal) && (name.Length == AttachmentsRoot.Length || name[AttachmentsRoot.Length] is '.' or '['); /// /// Resolves attachments and a path under it: .size / .count, an index [i], and the /// members of an attachment (name, fileName, contentType, contentId, length). /// private static object? ResolveAttachmentsPath(IExchange exchange, string name) { var attachments = exchange.In.Attachments; var rest = name[AttachmentsRoot.Length..]; if (rest.Length == 0) return attachments; if (rest is ".size" or ".count" or ".Size" or ".Count") return attachments.Count; return ResolvePropertyPath(attachments.ToArray(), rest.TrimStart('.')); } private static Attachment? FirstAttachment(object? name, IExchange exchange, string function) { var text = name?.ToString(); if (string.IsNullOrEmpty(text)) throw new InvalidOperationException($"{function}() needs the name of an attachment."); return exchange.In.Attachments.Find(text); } /// attachmentContent(name): the content of the first attachment with that name as bytes, or null. public static object? Ast_AttachmentContent(object? name, IExchange exchange) => FirstAttachment(name, exchange, "attachmentContent")?.Content.ReadAllBytes(); /// /// attachmentContentAsText(name): the content of the first attachment with that name as text, decoded with /// the charset of its content type (UTF-8 when it has none), or null. /// public static object? Ast_AttachmentContentAsText(object? name, IExchange exchange) { var attachment = FirstAttachment(name, exchange, "attachmentContentAsText"); return attachment is null ? null : CharsetOf(attachment.ContentType).GetString(attachment.Content.ReadAllBytes()); } /// attachmentContentType(name): the content type of the first attachment with that name, or null. public static object? Ast_AttachmentContentType(object? name, IExchange exchange) => FirstAttachment(name, exchange, "attachmentContentType")?.ContentType; /// /// attachmentHeader(name, header): a MIME header of the first attachment with that name (case-insensitive), /// or null. /// public static object? Ast_AttachmentHeader(object? name, object? header, IExchange exchange) { var headerName = header?.ToString(); if (string.IsNullOrEmpty(headerName)) throw new InvalidOperationException("attachmentHeader() needs the name of an attachment and the name of a header."); var attachment = FirstAttachment(name, exchange, "attachmentHeader"); return attachment is not null && attachment.Headers.TryGetValue(headerName, out var value) ? value : null; } private static Encoding CharsetOf(string contentType) { if (System.Net.Http.Headers.MediaTypeHeaderValue.TryParse(contentType, out var parsed) && parsed.CharSet is { Length: > 0 } charset) { try { return Encoding.GetEncoding(charset.Trim('"')); } catch (ArgumentException ex) { throw new InvalidOperationException( $"attachmentContentAsText(): the attachment declares charset '{charset}', which this runtime does not know.", ex); } } return Encoding.UTF8; } }