mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-05 07:36:53 +08:00
feat(telegram): enhance file handling by supporting stickers and photos
This commit is contained in:
@@ -46,17 +46,40 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
|||||||
|
|
||||||
var responseContent = await response.Content.ReadAsStringAsync();
|
var responseContent = await response.Content.ReadAsStringAsync();
|
||||||
var responseObj = JsonSerializer.Deserialize<TelegramResponse>(responseContent);
|
var responseObj = JsonSerializer.Deserialize<TelegramResponse>(responseContent);
|
||||||
if (responseObj == null || !responseObj.Ok || responseObj.Result?.Document == null)
|
if (responseObj == null || !responseObj.Ok)
|
||||||
{
|
{
|
||||||
throw new ApplicationException($"上传文件到 Telegram 失败: {responseContent}");
|
throw new ApplicationException($"上传文件到 Telegram 失败: {responseContent}");
|
||||||
}
|
}
|
||||||
|
|
||||||
var fileId = responseObj.Result.Document.FileId;
|
string fileId;
|
||||||
|
string fileUniqueId;
|
||||||
|
|
||||||
|
if (responseObj.Result?.Document != null)
|
||||||
|
{
|
||||||
|
fileId = responseObj.Result.Document.FileId;
|
||||||
|
fileUniqueId = responseObj.Result.Document.FileUniqueId;
|
||||||
|
}
|
||||||
|
else if (responseObj.Result?.Sticker != null)
|
||||||
|
{
|
||||||
|
fileId = responseObj.Result.Sticker.FileId;
|
||||||
|
fileUniqueId = responseObj.Result.Sticker.FileUniqueId;
|
||||||
|
}
|
||||||
|
else if (responseObj.Result?.Photo != null && responseObj.Result.Photo.Length > 0)
|
||||||
|
{
|
||||||
|
// 取最大尺寸的照片
|
||||||
|
var largestPhoto = responseObj.Result.Photo.OrderByDescending(p => p.FileSize).First();
|
||||||
|
fileId = largestPhoto.FileId;
|
||||||
|
fileUniqueId = largestPhoto.FileUniqueId;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ApplicationException($"无法从 Telegram 响应中提取文件信息: {responseContent}");
|
||||||
|
}
|
||||||
|
|
||||||
var metadata = new TelegramFileMetadata
|
var metadata = new TelegramFileMetadata
|
||||||
{
|
{
|
||||||
FileId = fileId,
|
FileId = fileId,
|
||||||
FileUniqueId = responseObj.Result.Document.FileUniqueId,
|
FileUniqueId = fileUniqueId,
|
||||||
MessageId = responseObj.Result.MessageId,
|
MessageId = responseObj.Result.MessageId,
|
||||||
ChatId = chatId,
|
ChatId = chatId,
|
||||||
OriginalFileName = fileName,
|
OriginalFileName = fileName,
|
||||||
@@ -246,6 +269,10 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
|||||||
[JsonPropertyName("message_id")] public int MessageId { get; set; }
|
[JsonPropertyName("message_id")] public int MessageId { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("document")] public TelegramDocument? Document { get; set; }
|
[JsonPropertyName("document")] public TelegramDocument? Document { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("sticker")] public TelegramSticker? Sticker { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("photo")] public TelegramPhoto[]? Photo { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TelegramDocument
|
private class TelegramDocument
|
||||||
@@ -261,6 +288,31 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
|||||||
[JsonPropertyName("file_size")] public int FileSize { get; set; }
|
[JsonPropertyName("file_size")] public int FileSize { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class TelegramSticker
|
||||||
|
{
|
||||||
|
[JsonPropertyName("file_id")] public string FileId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("file_unique_id")] public string FileUniqueId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("width")] public int Width { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("height")] public int Height { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("file_size")] public int FileSize { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TelegramPhoto
|
||||||
|
{
|
||||||
|
[JsonPropertyName("file_id")] public string FileId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("file_unique_id")] public string FileUniqueId { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[JsonPropertyName("width")] public int Width { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("height")] public int Height { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("file_size")] public int FileSize { get; set; }
|
||||||
|
}
|
||||||
// 存储关于上传文件的元数据
|
// 存储关于上传文件的元数据
|
||||||
private class TelegramFileMetadata
|
private class TelegramFileMetadata
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user