Skip to content

Commit a54a0ac

Browse files
committed
Support InputFile deserialization
1 parent 869a63a commit a54a0ac

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/Telegram.Bot/Extensions/FormatExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static string ToMarkdown(string message, MessageEntity[]? entities)
6161
else if (nextEntity.Type is MessageEntityType.DateTime)
6262
{
6363
closing.md = string.IsNullOrEmpty(nextEntity.DateTimeFormat) ? "" : $"&format={nextEntity.DateTimeFormat}";
64-
closing.md = $"](tg://time?unix={((DateTimeOffset)nextEntity.UnixTime!).ToUnixTimeSeconds()}{closing.md})";
64+
closing.md = $"](tg://time?unix={((DateTimeOffset)nextEntity.UnixTime!.Value).ToUnixTimeSeconds()}{closing.md})";
6565
}
6666
}
6767
else if (md[0] == '>')
@@ -184,7 +184,7 @@ public static string ToHtml(string message, MessageEntity[]? entities)
184184
else if (nextEntity.Type is MessageEntityType.DateTime)
185185
{
186186
tag = string.IsNullOrEmpty(nextEntity.DateTimeFormat) ? null : $" format=\"{nextEntity.DateTimeFormat}\"";
187-
tag = $"<tg-time unix=\"{((DateTimeOffset)nextEntity.UnixTime!).ToUnixTimeSeconds()}\"{tag}>";
187+
tag = $"<tg-time unix=\"{((DateTimeOffset)nextEntity.UnixTime!.Value).ToUnixTimeSeconds()}\"{tag}>";
188188
}
189189
else
190190
tag = $"<{tag}>";
@@ -640,7 +640,7 @@ private static ReplyMarkup ParseHtmlKeyboard(ReadOnlySpan<char> keyboard)
640640
while (true)
641641
{
642642
if (CheckHtmlArg(ref keyboard, "style=\"", out var style))
643-
button.Style = Enum.Parse<KeyboardButtonStyle>(style, true);
643+
button.Style = Enum.Parse<KeyboardButtonStyle>(style, ignoreCase: true);
644644
else if (CheckHtmlArg(ref keyboard, "icon=\"", out var emojiId))
645645
button.IconCustomEmojiId = emojiId;
646646
else

src/Telegram.Bot/PassportDecrypter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading;
44
using System.Threading.Tasks;
55

6-
#pragma warning disable CA2208, MA0015
6+
#pragma warning disable CA2208, MA0015, MA0076
77
#pragma warning disable CA1850, CA1835
88

99
namespace Telegram.Bot.Types.Passport

src/Telegram.Bot/Serialization/InputFileConverter.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using System.Threading;
2+
using Telegram.Bot.Exceptions;
3+
4+
#pragma warning disable CS1591
25

36
namespace Telegram.Bot.Serialization;
47

5-
internal sealed class InputFileConverter : JsonConverter<InputFile?>
8+
public sealed class InputFileConverter : JsonConverter<InputFile?>
69
{
7-
internal static readonly AsyncLocal<List<InputFileStream>?> Attachments = new();
10+
public static string? AllowLocalFilesUnder { get; set; } // if not empty, should end with a path separator
11+
public static readonly AsyncLocal<List<InputFileStream>?> Attachments = new();
812

913
public override bool CanConvert(Type typeToConvert) => typeof(InputFile).IsAssignableFrom(typeToConvert);
1014

@@ -18,7 +22,17 @@ internal sealed class InputFileConverter : JsonConverter<InputFile?>
1822
if (value is null)
1923
return null;
2024
if (value.StartsWith("attach://", StringComparison.OrdinalIgnoreCase))
21-
return new InputFileStream(Stream.Null, value[9..]);
25+
if (Attachments.Value?.Find(a => a.FileName?.StartsWith(value, StringComparison.Ordinal) == true) is { } ifs)
26+
return new InputFileStream(ifs.Content, ifs.FileName![(value.Length + 1)..]);
27+
else
28+
return new InputFileStream(Stream.Null, value[9..]);
29+
else if (value.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
30+
if (AllowLocalFilesUnder == null)
31+
throw new ApiRequestException("file:// URI not allowed", 400);
32+
else if (Path.GetFullPath(new Uri(value).LocalPath) is { } path && path.StartsWith(AllowLocalFilesUnder, StringComparison.OrdinalIgnoreCase))
33+
return new InputFileStream(File.OpenRead(path), Path.GetFileName(path));
34+
else
35+
throw new ApiRequestException("file:// path is not under allowed folder", 400);
2236

2337
return Uri.TryCreate(value, UriKind.Absolute, out var url)
2438
? new InputFileUrl(url)

src/Telegram.Bot/Telegram.Bot.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ $(ReleaseNotes)</Description>
5858
<NoWarn>$(NoWarn);CA1510</NoWarn> <!-- Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing
5959
a new exception instance -->
6060
<NoWarn>$(NoWarn);MA0046</NoWarn> <!-- The delegate must return void -->
61-
<NoWarn>$(NoWarn);MA0001;MA0048;MA0051;MA0111;MA0113</NoWarn>
61+
<NoWarn>$(NoWarn);MA0001;MA0042;MA0048;MA0051;MA0111;MA0113</NoWarn>
6262
<NoWarn>$(NoWarn);IDE0079;IDE0130</NoWarn>
6363
<NoWarn>$(NoWarn);CS0419</NoWarn> <!-- Ambiguous reference in cref attribute -->
6464
<NoWarn>$(NoWarn);IL2026</NoWarn> <!-- Using member 'method' which has 'RequiresUnreferencedCodeAttribute'... it's ok we use JsonSerializerOptions with type resolver context -->

0 commit comments

Comments
 (0)