2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Fix sending of .tgs stickers.

This commit is contained in:
John Preston
2019-07-03 13:03:01 +02:00
parent da48a78f7c
commit 7034df49e9
5 changed files with 72 additions and 39 deletions

View File

@@ -78,13 +78,8 @@ PreparedFileThumbnail PrepareFileThumbnail(QImage &&original) {
PreparedFileThumbnail PrepareAnimatedStickerThumbnail(
const QString &file,
const QByteArray &bytes) {
return PrepareFileThumbnail(Lottie::ReadThumbnail([&] {
if (!bytes.isEmpty()) {
return bytes;
}
auto f = QFile(file);
return f.open(QIODevice::ReadOnly) ? f.readAll() : QByteArray();
}()));
return PrepareFileThumbnail(
Lottie::ReadThumbnail(Lottie::ReadContent(bytes, file)));
}
bool FileThumbnailUploadRequired(const QString &filemime, int32 filesize) {
@@ -683,14 +678,23 @@ bool FileLoadTask::CheckForImage(
const QByteArray &content,
std::unique_ptr<FileMediaInformation> &result) {
auto animated = false;
auto image = ([&filepath, &content, &animated] {
auto image = [&] {
if (filepath.endsWith(qstr(".tgs"), Qt::CaseInsensitive)) {
auto image = Lottie::ReadThumbnail(
Lottie::ReadContent(content, filepath));
if (!image.isNull()) {
animated = true;
result->filemime = qstr("application/x-tgsticker");
}
return image;
}
if (!content.isEmpty()) {
return App::readImage(content, nullptr, false, &animated);
} else if (!filepath.isEmpty()) {
return App::readImage(filepath, nullptr, false, &animated);
}
return QImage();
})();
}();
return FillImageInformation(std::move(image), animated, result);
}
@@ -712,6 +716,7 @@ bool FileLoadTask::FillImageInformation(
void FileLoadTask::process() {
const auto stickerMime = qsl("image/webp");
const auto animatedStickerMime = qsl("application/x-tgsticker");
_result = std::make_shared<FileLoadResult>(
id(),
@@ -754,7 +759,7 @@ void FileLoadTask::process() {
if (auto image = base::get_if<FileMediaInformation::Image>(
&_information->media)) {
fullimage = base::take(image->data);
if (auto opaque = (filemime != stickerMime)) {
if (filemime != stickerMime && filemime != animatedStickerMime) {
fullimage = Images::prepareOpaque(std::move(fullimage));
}
isAnimation = image->animated;
@@ -773,7 +778,7 @@ void FileLoadTask::process() {
}
const auto mimeType = Core::MimeTypeForData(_content);
filemime = mimeType.name();
if (filemime != stickerMime) {
if (filemime != stickerMime && filemime != animatedStickerMime) {
fullimage = Images::prepareOpaque(std::move(fullimage));
}
if (filemime == "image/jpeg") {
@@ -830,10 +835,6 @@ void FileLoadTask::process() {
QByteArray goodThumbnailBytes;
QVector<MTPDocumentAttribute> attributes(1, MTP_documentAttributeFilename(MTP_string(filename)));
const auto checkAnimatedSticker = filename.endsWith(qstr(".tgs"), Qt::CaseInsensitive);
if (checkAnimatedSticker) {
filemime = "application/x-tgsticker";
}
auto thumbnail = PreparedFileThumbnail();
@@ -872,8 +873,6 @@ void FileLoadTask::process() {
}
thumbnail = PrepareFileThumbnail(std::move(video->thumbnail));
} else if (checkAnimatedSticker) {
thumbnail = PrepareAnimatedStickerThumbnail(_filepath, _content);
}
}
@@ -882,7 +881,20 @@ void FileLoadTask::process() {
attributes.push_back(MTP_documentAttributeImageSize(MTP_int(w), MTP_int(h)));
if (ValidateThumbDimensions(w, h)) {
if (isAnimation) {
isSticker = (filemime == stickerMime
|| filemime == animatedStickerMime)
&& (w > 0)
&& (h > 0)
&& (w <= StickerMaxSize)
&& (h <= StickerMaxSize)
&& (filesize < Storage::kMaxStickerInMemory);
if (isSticker) {
attributes.push_back(MTP_documentAttributeSticker(
MTP_flags(0),
MTP_string(QString()),
MTP_inputStickerSetEmpty(),
MTPMaskCoords()));
} else if (isAnimation) {
attributes.push_back(MTP_documentAttributeAnimated());
} else if (_type != SendMediaType::File) {
auto thumb = (w > 100 || h > 100) ? fullimage.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation) : fullimage;
@@ -915,21 +927,6 @@ void FileLoadTask::process() {
filesize = _result->filesize = filedata.size();
}
}
isSticker = !isAnimation
&& (filemime == stickerMime)
&& (w > 0)
&& (h > 0)
&& (w <= StickerMaxSize)
&& (h <= StickerMaxSize)
&& (filesize < Storage::kMaxStickerInMemory);
if (isSticker) {
attributes.push_back(MTP_documentAttributeSticker(
MTP_flags(0),
MTP_string(QString()),
MTP_inputStickerSetEmpty(),
MTPMaskCoords()));
}
thumbnail = PrepareFileThumbnail(std::move(fullimage));
}
}
@@ -937,7 +934,7 @@ void FileLoadTask::process() {
std::move(thumbnail),
filemime,
filesize,
isSticker || checkAnimatedSticker);
isSticker);
if (_type == SendMediaType::Photo && photo.type() == mtpc_photoEmpty) {
_type = SendMediaType::File;