2016-04-09 05:57:55 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-09 05:57:55 +00:00
|
|
|
*/
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/serialize_document.h"
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/serialize_common.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "storage/serialize_peer.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "data/data_session.h"
|
2020-06-08 17:24:36 +00:00
|
|
|
#include "data/stickers/data_stickers.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2020-05-22 13:50:04 +00:00
|
|
|
namespace Serialize {
|
2016-04-09 05:57:55 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-05-22 13:50:04 +00:00
|
|
|
constexpr auto kVersionTag = int32(0x7FFFFFFF);
|
2022-12-15 15:10:51 +00:00
|
|
|
constexpr auto kVersion = 5;
|
2020-05-22 13:50:04 +00:00
|
|
|
|
2016-04-09 05:57:55 +00:00
|
|
|
enum StickerSetType {
|
|
|
|
StickerSetTypeEmpty = 0,
|
|
|
|
StickerSetTypeID = 1,
|
|
|
|
StickerSetTypeShortName = 2,
|
2022-07-08 17:04:31 +00:00
|
|
|
StickerSetTypeEmoji = 3,
|
|
|
|
StickerSetTypeMasks = 4,
|
2016-04-09 05:57:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void Document::writeToStream(QDataStream &stream, DocumentData *document) {
|
2022-05-10 14:22:28 +00:00
|
|
|
stream
|
|
|
|
<< quint64(document->id)
|
|
|
|
<< quint64(document->_access)
|
|
|
|
<< qint32(document->date)
|
|
|
|
<< document->_fileReference
|
|
|
|
<< qint32(kVersionTag)
|
|
|
|
<< qint32(kVersion)
|
|
|
|
<< document->filename()
|
|
|
|
<< document->mimeString()
|
|
|
|
<< qint32(document->_dc)
|
|
|
|
// FileSize: Right now any file size fits 32 bit.
|
|
|
|
<< qint32(uint32(document->size))
|
|
|
|
<< qint32(document->dimensions.width())
|
|
|
|
<< qint32(document->dimensions.height())
|
|
|
|
<< qint32(document->type);
|
2021-07-08 16:42:57 +00:00
|
|
|
if (const auto sticker = document->sticker()) {
|
2022-07-08 17:04:31 +00:00
|
|
|
stream << sticker->alt;
|
|
|
|
if (sticker->setType == Data::StickersType::Emoji) {
|
|
|
|
stream << qint32(StickerSetTypeEmoji);
|
|
|
|
} else if (sticker->setType == Data::StickersType::Masks) {
|
|
|
|
stream << qint32(StickerSetTypeMasks);
|
|
|
|
} else if (sticker->set.id) {
|
2016-04-09 05:57:55 +00:00
|
|
|
stream << qint32(StickerSetTypeID);
|
2021-07-08 16:42:57 +00:00
|
|
|
} else {
|
2016-04-09 05:57:55 +00:00
|
|
|
stream << qint32(StickerSetTypeEmpty);
|
|
|
|
}
|
|
|
|
}
|
2022-01-24 22:07:59 +00:00
|
|
|
stream << qint32(document->getDuration());
|
2022-04-22 16:23:47 +00:00
|
|
|
if (document->type == StickerDocument) {
|
2022-07-15 14:52:36 +00:00
|
|
|
const auto premium = document->isPremiumSticker()
|
|
|
|
|| document->isPremiumEmoji();
|
|
|
|
stream << qint32(premium ? 1 : 0);
|
2022-12-15 15:10:51 +00:00
|
|
|
stream << qint32(document->emojiUsesTextColor() ? 1 : 0);
|
2022-04-22 16:23:47 +00:00
|
|
|
}
|
2020-04-16 12:37:26 +00:00
|
|
|
writeImageLocation(stream, document->thumbnailLocation());
|
2020-05-22 13:50:04 +00:00
|
|
|
stream << qint32(document->thumbnailByteSize());
|
|
|
|
writeImageLocation(stream, document->videoThumbnailLocation());
|
2021-07-02 10:39:08 +00:00
|
|
|
stream
|
|
|
|
<< qint32(document->videoThumbnailByteSize())
|
|
|
|
<< qint32(document->inlineThumbnailIsPath() ? 1 : 0)
|
|
|
|
<< document->inlineThumbnailBytes();
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 10:49:10 +00:00
|
|
|
DocumentData *Document::readFromStreamHelper(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
int streamAppVersion,
|
|
|
|
QDataStream &stream,
|
|
|
|
const StickerSetInfo *info) {
|
2016-04-09 05:57:55 +00:00
|
|
|
quint64 id, access;
|
|
|
|
QString name, mime;
|
2020-05-22 13:50:04 +00:00
|
|
|
qint32 date, dc, size, width, height, type, versionTag, version = 0;
|
2018-07-13 16:49:46 +00:00
|
|
|
QByteArray fileReference;
|
2016-04-09 05:57:55 +00:00
|
|
|
stream >> id >> access >> date;
|
2016-07-25 19:54:40 +00:00
|
|
|
if (streamAppVersion >= 9061) {
|
2018-07-16 19:43:44 +00:00
|
|
|
if (streamAppVersion >= 1003013) {
|
2018-07-13 16:49:46 +00:00
|
|
|
stream >> fileReference;
|
|
|
|
}
|
2020-05-22 13:50:04 +00:00
|
|
|
stream >> versionTag;
|
|
|
|
if (versionTag == kVersionTag) {
|
|
|
|
stream >> version;
|
|
|
|
}
|
2016-07-08 13:56:53 +00:00
|
|
|
} else {
|
2020-05-22 13:50:04 +00:00
|
|
|
versionTag = 0;
|
2016-07-08 13:56:53 +00:00
|
|
|
version = 0;
|
|
|
|
}
|
2022-05-10 14:22:28 +00:00
|
|
|
stream
|
|
|
|
>> name
|
|
|
|
>> mime
|
|
|
|
>> dc
|
|
|
|
>> size // FileSize: Right now any file size fits 32 bit.
|
|
|
|
>> width
|
|
|
|
>> height
|
|
|
|
>> type;
|
2016-04-09 05:57:55 +00:00
|
|
|
|
|
|
|
QVector<MTPDocumentAttribute> attributes;
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
attributes.push_back(MTP_documentAttributeFilename(MTP_string(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 duration = -1;
|
2022-04-21 11:06:50 +00:00
|
|
|
qint32 isPremiumSticker = 0;
|
2022-12-15 15:10:51 +00:00
|
|
|
qint32 useTextColor = 0;
|
2016-04-09 05:57:55 +00:00
|
|
|
if (type == StickerDocument) {
|
|
|
|
QString alt;
|
|
|
|
qint32 typeOfSet;
|
|
|
|
stream >> alt >> typeOfSet;
|
2022-07-08 17:04:31 +00:00
|
|
|
if (version >= 3) {
|
|
|
|
stream >> duration;
|
|
|
|
if (version >= 4) {
|
|
|
|
stream >> isPremiumSticker;
|
2022-12-15 15:10:51 +00:00
|
|
|
if (version >= 5) {
|
|
|
|
stream >> useTextColor;
|
|
|
|
}
|
2022-07-08 17:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 05:57:55 +00:00
|
|
|
if (typeOfSet == StickerSetTypeEmpty) {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(alt), MTP_inputStickerSetEmpty(), MTPMaskCoords()));
|
2016-04-09 05:57:55 +00:00
|
|
|
} else if (info) {
|
2020-06-08 17:24:36 +00:00
|
|
|
if (info->setId == Data::Stickers::DefaultSetId
|
|
|
|
|| info->setId == Data::Stickers::CloudRecentSetId
|
2021-03-25 11:05:33 +00:00
|
|
|
|| info->setId == Data::Stickers::CloudRecentAttachedSetId
|
2020-06-08 17:24:36 +00:00
|
|
|
|| info->setId == Data::Stickers::FavedSetId
|
|
|
|
|| info->setId == Data::Stickers::CustomSetId) {
|
2016-04-09 05:57:55 +00:00
|
|
|
typeOfSet = StickerSetTypeEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (typeOfSet) {
|
|
|
|
case StickerSetTypeID: {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(alt), MTP_inputStickerSetID(MTP_long(info->setId), MTP_long(info->accessHash)), MTPMaskCoords()));
|
2016-04-09 05:57:55 +00:00
|
|
|
} break;
|
2022-07-08 17:04:31 +00:00
|
|
|
case StickerSetTypeMasks: {
|
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(MTPDdocumentAttributeSticker::Flag::f_mask), MTP_string(alt), MTP_inputStickerSetID(MTP_long(info->setId), MTP_long(info->accessHash)), MTPMaskCoords()));
|
|
|
|
} break;
|
|
|
|
case StickerSetTypeEmoji: {
|
2022-12-15 15:10:51 +00:00
|
|
|
if (version < 5) {
|
|
|
|
// We didn't store useTextColor yet, can't use.
|
|
|
|
stream.setStatus(QDataStream::ReadCorruptData);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-07-08 17:04:31 +00:00
|
|
|
using Flag = MTPDdocumentAttributeCustomEmoji::Flag;
|
|
|
|
attributes.push_back(MTP_documentAttributeCustomEmoji(
|
2022-12-15 15:10:51 +00:00
|
|
|
MTP_flags((isPremiumSticker ? Flag(0) : Flag::f_free)
|
|
|
|
| (useTextColor ? Flag::f_text_color : Flag(0))),
|
2022-07-08 17:04:31 +00:00
|
|
|
MTP_string(alt),
|
|
|
|
MTP_inputStickerSetID(
|
|
|
|
MTP_long(info->setId),
|
2022-07-08 18:07:14 +00:00
|
|
|
MTP_long(info->accessHash))));
|
2016-04-09 05:57:55 +00:00
|
|
|
} break;
|
|
|
|
case StickerSetTypeEmpty:
|
|
|
|
default: {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(alt), MTP_inputStickerSetEmpty(), MTPMaskCoords()));
|
2016-04-09 05:57:55 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stream >> duration;
|
|
|
|
if (type == AnimatedDocument) {
|
|
|
|
attributes.push_back(MTP_documentAttributeAnimated());
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 13:50:04 +00:00
|
|
|
std::optional<ImageLocation> videoThumb;
|
|
|
|
qint32 thumbnailByteSize = 0, videoThumbnailByteSize = 0;
|
2021-07-02 10:39:08 +00:00
|
|
|
qint32 inlineThumbnailIsPath = 0;
|
|
|
|
QByteArray inlineThumbnailBytes;
|
2020-05-22 13:50:04 +00:00
|
|
|
const auto thumb = readImageLocation(streamAppVersion, stream);
|
|
|
|
if (version >= 1) {
|
|
|
|
stream >> thumbnailByteSize;
|
|
|
|
videoThumb = readImageLocation(streamAppVersion, stream);
|
|
|
|
stream >> videoThumbnailByteSize;
|
2021-07-02 10:39:08 +00:00
|
|
|
if (version >= 2) {
|
|
|
|
stream >> inlineThumbnailIsPath >> inlineThumbnailBytes;
|
|
|
|
}
|
2020-05-22 13:50:04 +00:00
|
|
|
} else {
|
|
|
|
videoThumb = ImageLocation();
|
|
|
|
}
|
2016-04-09 05:57:55 +00:00
|
|
|
if (width > 0 && height > 0) {
|
|
|
|
if (duration >= 0) {
|
2017-03-23 16:11:35 +00:00
|
|
|
auto flags = MTPDdocumentAttributeVideo::Flags(0);
|
2017-04-02 10:25:54 +00:00
|
|
|
if (type == RoundVideoDocument) {
|
|
|
|
flags |= MTPDdocumentAttributeVideo::Flag::f_round_message;
|
|
|
|
}
|
2017-03-23 16:11:35 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeVideo(MTP_flags(flags), MTP_int(duration), MTP_int(width), MTP_int(height)));
|
2016-04-09 05:57:55 +00:00
|
|
|
} else {
|
|
|
|
attributes.push_back(MTP_documentAttributeImageSize(MTP_int(width), MTP_int(height)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 13:50:04 +00:00
|
|
|
if ((stream.status() != QDataStream::Ok)
|
|
|
|
|| (!dc && !access)
|
2019-04-03 20:05:29 +00:00
|
|
|
|| !thumb
|
2022-05-02 11:36:34 +00:00
|
|
|
|| !videoThumb) {
|
|
|
|
stream.setStatus(QDataStream::ReadCorruptData);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto storage = std::get_if<StorageFileLocation>(
|
|
|
|
&thumb->file().data);
|
|
|
|
if (thumb->valid()
|
|
|
|
&& (!storage || !storage->isDocumentThumbnail())) {
|
2019-04-03 20:05:29 +00:00
|
|
|
stream.setStatus(QDataStream::ReadCorruptData);
|
|
|
|
// We can't convert legacy thumbnail location to modern, because
|
|
|
|
// size letter ('s' or 'm') is lost, it was not saved in legacy.
|
2016-04-09 05:57:55 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-06-10 10:49:10 +00:00
|
|
|
return session->data().document(
|
2018-01-17 16:21:01 +00:00
|
|
|
id,
|
|
|
|
access,
|
2018-07-13 16:49:46 +00:00
|
|
|
fileReference,
|
2018-01-17 16:21:01 +00:00
|
|
|
date,
|
|
|
|
attributes,
|
|
|
|
mime,
|
2021-07-02 10:39:08 +00:00
|
|
|
InlineImageLocation{
|
|
|
|
inlineThumbnailBytes,
|
|
|
|
(inlineThumbnailIsPath == 1),
|
|
|
|
},
|
2020-05-22 13:50:04 +00:00
|
|
|
ImageWithLocation{
|
|
|
|
.location = *thumb,
|
|
|
|
.bytesCount = thumbnailByteSize
|
|
|
|
},
|
|
|
|
ImageWithLocation{
|
|
|
|
.location = *videoThumb,
|
|
|
|
.bytesCount = videoThumbnailByteSize
|
|
|
|
},
|
2022-04-21 11:06:50 +00:00
|
|
|
(isPremiumSticker == 1),
|
2018-01-17 16:21:01 +00:00
|
|
|
dc,
|
2022-05-10 14:22:28 +00:00
|
|
|
int64(uint32(size)));
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 10:49:10 +00:00
|
|
|
DocumentData *Document::readStickerFromStream(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
int streamAppVersion,
|
|
|
|
QDataStream &stream,
|
|
|
|
const StickerSetInfo &info) {
|
|
|
|
return readFromStreamHelper(session, streamAppVersion, stream, &info);
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 10:49:10 +00:00
|
|
|
DocumentData *Document::readFromStream(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
int streamAppVersion,
|
|
|
|
QDataStream &stream) {
|
|
|
|
return readFromStreamHelper(session, streamAppVersion, stream, nullptr);
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Document::sizeInStream(DocumentData *document) {
|
|
|
|
int result = 0;
|
|
|
|
|
2021-07-02 10:39:08 +00:00
|
|
|
// id + access + date
|
|
|
|
result += sizeof(quint64) + sizeof(quint64) + sizeof(qint32);
|
|
|
|
// file_reference + version tag + version
|
|
|
|
result += bytearraySize(document->_fileReference) + sizeof(qint32) * 2;
|
2016-04-09 05:57:55 +00:00
|
|
|
// + namelen + name + mimelen + mime + dc + size
|
2017-11-05 11:00:48 +00:00
|
|
|
result += stringSize(document->filename()) + stringSize(document->mimeString()) + sizeof(qint32) + sizeof(qint32);
|
2016-04-09 05:57:55 +00:00
|
|
|
// + width + height
|
|
|
|
result += sizeof(qint32) + sizeof(qint32);
|
|
|
|
// + type
|
|
|
|
result += sizeof(qint32);
|
|
|
|
|
|
|
|
if (auto sticker = document->sticker()) { // type == StickerDocument
|
|
|
|
// + altlen + alt + type-of-set
|
|
|
|
result += stringSize(sticker->alt) + sizeof(qint32);
|
|
|
|
} else {
|
|
|
|
// + duration
|
|
|
|
result += sizeof(qint32);
|
|
|
|
}
|
2020-04-15 14:06:34 +00:00
|
|
|
// + thumb loc
|
2020-04-16 12:37:26 +00:00
|
|
|
result += Serialize::imageLocationSize(document->thumbnailLocation());
|
2021-07-02 10:39:08 +00:00
|
|
|
result += sizeof(qint32); // thumbnail_byte_size
|
|
|
|
result += Serialize::imageLocationSize(document->videoThumbnailLocation());
|
|
|
|
result += sizeof(qint32); // video_thumbnail_byte_size
|
|
|
|
|
|
|
|
result += sizeof(qint32) + Serialize::bytearraySize(document->inlineThumbnailBytes());
|
2016-04-09 05:57:55 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Serialize
|