2022-01-18 17:20:43 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2022-01-18 17:20:43 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/StickerFormat.h"
|
|
|
|
|
2022-01-25 12:58:04 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
2022-01-18 17:20:43 +01:00
|
|
|
namespace td {
|
|
|
|
|
2022-07-14 12:58:33 +02:00
|
|
|
StickerFormat get_sticker_format(const td_api::object_ptr<td_api::StickerFormat> &format) {
|
|
|
|
CHECK(format != nullptr);
|
|
|
|
switch (format->get_id()) {
|
|
|
|
case td_api::stickerFormatWebp::ID:
|
2022-07-13 22:02:46 +02:00
|
|
|
return StickerFormat::Webp;
|
2022-07-14 12:58:33 +02:00
|
|
|
case td_api::stickerFormatTgs::ID:
|
2022-07-13 22:02:46 +02:00
|
|
|
return StickerFormat::Tgs;
|
2022-07-14 12:58:33 +02:00
|
|
|
case td_api::stickerFormatWebm::ID:
|
2022-07-13 22:02:46 +02:00
|
|
|
return StickerFormat::Webm;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return StickerFormat::Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 18:45:58 +01:00
|
|
|
StickerFormat get_sticker_format_by_mime_type(Slice mime_type) {
|
2022-01-18 17:20:43 +01:00
|
|
|
if (mime_type == "application/x-tgsticker") {
|
|
|
|
return StickerFormat::Tgs;
|
|
|
|
}
|
|
|
|
if (mime_type == "image/webp") {
|
|
|
|
return StickerFormat::Webp;
|
|
|
|
}
|
2022-01-25 12:58:04 +01:00
|
|
|
if (mime_type == "video/webm") {
|
|
|
|
return StickerFormat::Webm;
|
|
|
|
}
|
2022-01-18 17:20:43 +01:00
|
|
|
return StickerFormat::Unknown;
|
|
|
|
}
|
|
|
|
|
2022-02-17 18:45:58 +01:00
|
|
|
StickerFormat get_sticker_format_by_extension(Slice extension) {
|
|
|
|
if (extension == "tgs") {
|
|
|
|
return StickerFormat::Tgs;
|
|
|
|
}
|
|
|
|
if (extension == "webp") {
|
|
|
|
return StickerFormat::Webp;
|
|
|
|
}
|
|
|
|
if (extension == "webm") {
|
|
|
|
return StickerFormat::Webm;
|
|
|
|
}
|
|
|
|
return StickerFormat::Unknown;
|
|
|
|
}
|
|
|
|
|
2022-07-14 12:58:33 +02:00
|
|
|
td_api::object_ptr<td_api::StickerFormat> get_sticker_format_object(StickerFormat sticker_format) {
|
2022-01-25 12:58:04 +01:00
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
LOG(ERROR) << "Have a sticker of unknown format";
|
2022-07-14 12:58:33 +02:00
|
|
|
return td_api::make_object<td_api::stickerFormatWebp>();
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webp:
|
2022-07-14 12:58:33 +02:00
|
|
|
return td_api::make_object<td_api::stickerFormatWebp>();
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Tgs:
|
2022-07-14 12:58:33 +02:00
|
|
|
return td_api::make_object<td_api::stickerFormatTgs>();
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
2022-07-14 12:58:33 +02:00
|
|
|
return td_api::make_object<td_api::stickerFormatWebm>();
|
2022-01-25 12:58:04 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:20:43 +01:00
|
|
|
string get_sticker_format_mime_type(StickerFormat sticker_format) {
|
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
case StickerFormat::Webp:
|
|
|
|
return "image/webp";
|
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return "application/x-tgsticker";
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
|
|
|
return "video/webm";
|
2022-01-18 17:20:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice get_sticker_format_extension(StickerFormat sticker_format) {
|
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
return Slice();
|
|
|
|
case StickerFormat::Webp:
|
|
|
|
return Slice(".webp");
|
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return Slice(".tgs");
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
|
|
|
return Slice(".webm");
|
2022-01-18 17:20:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Slice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 15:01:29 +02:00
|
|
|
PhotoFormat get_sticker_format_photo_format(StickerFormat sticker_format) {
|
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
case StickerFormat::Webp:
|
|
|
|
return PhotoFormat::Webp;
|
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return PhotoFormat::Tgs;
|
|
|
|
case StickerFormat::Webm:
|
|
|
|
return PhotoFormat::Webm;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return PhotoFormat::Webp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:20:43 +01:00
|
|
|
bool is_sticker_format_animated(StickerFormat sticker_format) {
|
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
return false;
|
|
|
|
case StickerFormat::Webp:
|
|
|
|
return false;
|
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return true;
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
|
|
|
return true;
|
2022-01-18 17:20:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_sticker_format_vector(StickerFormat sticker_format) {
|
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
return false;
|
|
|
|
case StickerFormat::Webp:
|
|
|
|
return false;
|
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return true;
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
|
|
|
return false;
|
2022-01-18 17:20:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 22:49:46 +02:00
|
|
|
int64 get_max_sticker_file_size(StickerFormat sticker_format, StickerType sticker_type, bool for_thumbnail) {
|
2022-08-14 14:04:08 +02:00
|
|
|
bool is_custom_emoji = sticker_type == StickerType::CustomEmoji;
|
2022-01-18 17:20:43 +01:00
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
case StickerFormat::Webp:
|
2022-08-14 14:04:08 +02:00
|
|
|
return for_thumbnail ? (1 << 17) : (is_custom_emoji ? (1 << 17) : (1 << 19));
|
2022-01-18 17:20:43 +01:00
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return for_thumbnail ? (1 << 15) : (1 << 16);
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
2022-08-14 14:04:08 +02:00
|
|
|
return for_thumbnail ? (1 << 15) : (is_custom_emoji ? (1 << 16) : (1 << 18));
|
2022-01-18 17:20:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, StickerFormat sticker_format) {
|
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
return string_builder << "unknown";
|
|
|
|
case StickerFormat::Webp:
|
|
|
|
return string_builder << "WEBP";
|
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return string_builder << "TGS";
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
|
|
|
return string_builder << "WEBM";
|
2022-01-18 17:20:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return string_builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|