Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1255 from TelegramBots/develop
Release v19 stable
- Loading branch information
Showing
57 changed files
with
294 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,38 @@ | ||
using System.IO; | ||
using System.Reflection; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Telegram.Bot.Converters; | ||
|
||
internal class InputFileConverter : JsonConverter | ||
internal class InputFileConverter : JsonConverter<InputFile?> | ||
{ | ||
public override bool CanConvert(Type objectType) => | ||
objectType.GetTypeInfo().IsSubclassOf(typeof(IInputFile)); | ||
|
||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) | ||
public override void WriteJson(JsonWriter writer, InputFile? value, JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value switch | ||
{ | ||
InputFileId file => file.Id, | ||
InputFileUrl file => file.Url, | ||
InputFile file => $"attach://{file.FileName}", | ||
_ => throw new NotSupportedException("File Type not supported") | ||
InputFileId file => file.Id, | ||
InputFileUrl file => file.Url, | ||
InputFileStream file => $"attach://{file.FileName}", | ||
_ => throw new NotSupportedException("File Type not supported"), | ||
}); | ||
} | ||
|
||
public override object ReadJson( | ||
public override InputFile? ReadJson( | ||
JsonReader reader, | ||
Type objectType, | ||
object? existingValue, | ||
InputFile? existingValue, | ||
bool hasExistingValue, | ||
JsonSerializer serializer) | ||
{ | ||
var value = JToken.ReadFrom(reader).Value<string>(); | ||
|
||
if (value is null) { return null!; } | ||
if (value.StartsWith("attach://", StringComparison.InvariantCulture)) | ||
if (value is null) { return null; } | ||
if (value.StartsWith("attach://", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return new InputFile(Stream.Null, value.Substring(9)); | ||
return new InputFileStream(Stream.Null, value.Substring(9)); | ||
} | ||
|
||
return Uri.IsWellFormedUriString(value, UriKind.Absolute) | ||
? new InputFileUrl(value) | ||
return Uri.TryCreate(value, UriKind.Absolute, out var url) | ||
? new InputFileUrl(url) | ||
: new InputFileId(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.