| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using TeleFlow.Telegram.Schema.Types; |
| | | 4 | | |
| | | 5 | | namespace TeleFlow.Telegram.Internal; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Detects whether a Telegram request payload can contain files that must be sent as multipart content. |
| | | 9 | | /// This protects the common JSON-only path from unnecessary recursive object scans. |
| | | 10 | | /// It is used before sending outgoing Bot API requests such as sendMessage, sendPhoto, and media groups. |
| | | 11 | | /// </summary> |
| | | 12 | | internal static class TelegramRequestUploadDetector |
| | | 13 | | { |
| | 1 | 14 | | private static readonly ConcurrentDictionary<Type, bool> CapabilityCache = new(); |
| | | 15 | | |
| | | 16 | | // Type-level proof used before touching object values. |
| | | 17 | | // false means "cannot contain InputFile"; true means "maybe, inspect the actual value". |
| | | 18 | | public static bool MayContainInputFile(Type type) |
| | | 19 | | { |
| | 163 | 20 | | ArgumentNullException.ThrowIfNull(type); |
| | | 21 | | |
| | 163 | 22 | | return CapabilityCache.GetOrAdd( |
| | 163 | 23 | | type, |
| | 180 | 24 | | static candidate => MayContainInputFile(candidate, [])); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public static bool ContainsInputFile(object value) |
| | | 28 | | { |
| | 13 | 29 | | ArgumentNullException.ThrowIfNull(value); |
| | 13 | 30 | | return ContainsInputFile(value, new HashSet<object>(ReferenceEqualityComparer.Instance)); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | // Top-level method properties can be InputFile directly, or a generated union wrapper whose |
| | | 34 | | // selected case is InputFile. Both are sent as normal multipart file fields. |
| | | 35 | | public static bool TryGetDirectInputFile(object value, out InputFile inputFile) |
| | | 36 | | { |
| | 19 | 37 | | ArgumentNullException.ThrowIfNull(value); |
| | | 38 | | |
| | 19 | 39 | | if (value is InputFile direct) |
| | | 40 | | { |
| | 5 | 41 | | inputFile = direct; |
| | 5 | 42 | | return true; |
| | | 43 | | } |
| | | 44 | | |
| | 14 | 45 | | var unionCase = GetActiveUnionCase(value); |
| | 14 | 46 | | if (unionCase?.Value is InputFile nested) |
| | | 47 | | { |
| | 2 | 48 | | inputFile = nested; |
| | 2 | 49 | | return true; |
| | | 50 | | } |
| | | 51 | | |
| | 12 | 52 | | inputFile = null!; |
| | 12 | 53 | | return false; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Generated Telegram union wrappers have no [JsonPropertyName] fields of their own. |
| | | 57 | | // Exactly one non-null public property is treated as the active union case. |
| | | 58 | | public static TelegramActiveUnionCase? GetActiveUnionCase(object value) |
| | | 59 | | { |
| | 32 | 60 | | ArgumentNullException.ThrowIfNull(value); |
| | | 61 | | |
| | 32 | 62 | | var metadata = TelegramRequestTypeMetadataCache.Get(value.GetType()); |
| | 32 | 63 | | if (metadata.TelegramProperties.Length > 0) |
| | | 64 | | { |
| | 3 | 65 | | return null; |
| | | 66 | | } |
| | | 67 | | |
| | 29 | 68 | | var activeCases = metadata.ReadableProperties |
| | 69 | 69 | | .Select(property => new TelegramActiveUnionCase(property.Name, property.GetValue(value))) |
| | 69 | 70 | | .Where(static item => item.Value is not null) |
| | 29 | 71 | | .ToArray(); |
| | | 72 | | |
| | 29 | 73 | | return activeCases.Length == 1 ? activeCases[0] : null; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | private static bool ContainsInputFile(object? value, HashSet<object> visited) |
| | | 77 | | { |
| | 187 | 78 | | if (value is null) |
| | | 79 | | { |
| | 107 | 80 | | return false; |
| | | 81 | | } |
| | | 82 | | |
| | 80 | 83 | | if (value is InputFile) |
| | | 84 | | { |
| | 9 | 85 | | return true; |
| | | 86 | | } |
| | | 87 | | |
| | 71 | 88 | | var type = value.GetType(); |
| | 71 | 89 | | if (!MayContainInputFile(type)) |
| | | 90 | | { |
| | 32 | 91 | | return false; |
| | | 92 | | } |
| | | 93 | | |
| | 39 | 94 | | var metadata = TelegramRequestTypeMetadataCache.Get(type); |
| | 39 | 95 | | if (metadata.IsScalar || value is Stream) |
| | | 96 | | { |
| | 0 | 97 | | return false; |
| | | 98 | | } |
| | | 99 | | |
| | 39 | 100 | | if (!type.IsValueType && !visited.Add(value)) |
| | | 101 | | { |
| | 0 | 102 | | return false; |
| | | 103 | | } |
| | | 104 | | |
| | 39 | 105 | | if (value is IEnumerable enumerable and not string) |
| | | 106 | | { |
| | 18 | 107 | | foreach (var item in enumerable) |
| | | 108 | | { |
| | 6 | 109 | | if (ContainsInputFile(item, visited)) |
| | | 110 | | { |
| | 2 | 111 | | return true; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | 2 | 115 | | return false; |
| | | 116 | | } |
| | | 117 | | |
| | 389 | 118 | | foreach (var property in metadata.ReadableProperties) |
| | | 119 | | { |
| | 168 | 120 | | if (ContainsInputFile(property.GetValue(value), visited)) |
| | | 121 | | { |
| | 17 | 122 | | return true; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | 18 | 126 | | return false; |
| | 2 | 127 | | } |
| | | 128 | | |
| | | 129 | | private static bool MayContainInputFile(Type type, HashSet<Type> visiting) |
| | | 130 | | { |
| | 281 | 131 | | var candidate = Nullable.GetUnderlyingType(type) ?? type; |
| | | 132 | | |
| | 281 | 133 | | if (candidate == typeof(InputFile)) |
| | | 134 | | { |
| | 8 | 135 | | return true; |
| | | 136 | | } |
| | | 137 | | |
| | 273 | 138 | | if (TelegramRequestTypeMetadataCache.IsScalarType(candidate) || |
| | 273 | 139 | | typeof(Stream).IsAssignableFrom(candidate)) |
| | | 140 | | { |
| | 205 | 141 | | return false; |
| | | 142 | | } |
| | | 143 | | |
| | 68 | 144 | | if (candidate == typeof(object) || candidate.IsGenericParameter) |
| | | 145 | | { |
| | 0 | 146 | | return true; |
| | | 147 | | } |
| | | 148 | | |
| | 68 | 149 | | if (TryGetEnumerableElementType(candidate, out var elementType)) |
| | | 150 | | { |
| | 10 | 151 | | return elementType is null || MayContainInputFile(elementType, visiting); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | // Interfaces, abstract classes, and replaceable classes are conservative by design: |
| | | 155 | | // the declared type may be safe, but a runtime implementation/subclass may contain InputFile. |
| | 58 | 156 | | if (candidate.IsInterface || candidate.IsAbstract || !candidate.IsSealed) |
| | | 157 | | { |
| | 0 | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | // This guard is for recursive type graphs. Runtime object cycles are handled by |
| | | 162 | | // ContainsInputFile with reference equality tracking. |
| | 58 | 163 | | if (!visiting.Add(candidate)) |
| | | 164 | | { |
| | 0 | 165 | | return false; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | try |
| | | 169 | | { |
| | 605 | 170 | | foreach (var property in TelegramRequestTypeMetadataCache.Get(candidate).ReadableProperties) |
| | | 171 | | { |
| | 254 | 172 | | if (MayContainInputFile(property.PropertyType, visiting)) |
| | | 173 | | { |
| | 19 | 174 | | return true; |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | 39 | 178 | | return false; |
| | | 179 | | } |
| | | 180 | | finally |
| | | 181 | | { |
| | 58 | 182 | | visiting.Remove(candidate); |
| | 58 | 183 | | } |
| | 58 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static bool TryGetEnumerableElementType(Type type, out Type? elementType) |
| | | 187 | | { |
| | 68 | 188 | | if (type == typeof(string) || !typeof(IEnumerable).IsAssignableFrom(type)) |
| | | 189 | | { |
| | 58 | 190 | | elementType = null; |
| | 58 | 191 | | return false; |
| | | 192 | | } |
| | | 193 | | |
| | 10 | 194 | | if (type.IsArray) |
| | | 195 | | { |
| | 1 | 196 | | elementType = type.GetElementType(); |
| | 1 | 197 | | return true; |
| | | 198 | | } |
| | | 199 | | |
| | 9 | 200 | | var enumerableType = type |
| | 9 | 201 | | .GetInterfaces() |
| | 9 | 202 | | .Append(type) |
| | 9 | 203 | | .FirstOrDefault(static candidate => |
| | 29 | 204 | | candidate.IsGenericType && |
| | 29 | 205 | | candidate.GetGenericTypeDefinition() == typeof(IEnumerable<>)); |
| | | 206 | | |
| | 9 | 207 | | elementType = enumerableType?.GetGenericArguments()[0]; |
| | 9 | 208 | | return true; |
| | | 209 | | } |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Represents the selected case of a generated Telegram union wrapper. |
| | | 214 | | /// Request content builders use it to serialize unions as the active value expected by Telegram. |
| | | 215 | | /// </summary> |
| | | 216 | | internal sealed record TelegramActiveUnionCase(string Name, object? Value); |