2020-03-27 11:40:50 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/data_document_media.h"
|
|
|
|
|
|
|
|
#include "data/data_document.h"
|
2021-06-14 04:35:51 +00:00
|
|
|
#include "data/data_document_resolver.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_cloud_themes.h"
|
2020-04-09 08:15:47 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2020-04-10 14:19:43 +00:00
|
|
|
#include "data/data_auto_download.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
#include "media/clip/media_clip_reader.h"
|
|
|
|
#include "main/main_session.h"
|
2020-06-18 12:47:09 +00:00
|
|
|
#include "main/main_session_settings.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
#include "lottie/lottie_animation.h"
|
2022-08-29 12:34:21 +00:00
|
|
|
#include "lottie/lottie_frame_generator.h"
|
|
|
|
#include "ffmpeg/ffmpeg_frame_generator.h"
|
2020-04-10 14:19:43 +00:00
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "history/history.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
#include "window/themes/window_theme_preview.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/core_settings.h"
|
|
|
|
#include "core/application.h"
|
2020-04-09 08:15:47 +00:00
|
|
|
#include "storage/file_download.h"
|
2020-05-29 15:10:25 +00:00
|
|
|
#include "ui/image/image.h"
|
2020-03-27 11:40:50 +00:00
|
|
|
|
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
#include <QtGui/QImageReader>
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kReadAreaLimit = 12'032 * 9'024;
|
|
|
|
constexpr auto kWallPaperThumbnailLimit = 960;
|
|
|
|
constexpr auto kGoodThumbQuality = 87;
|
|
|
|
|
|
|
|
enum class FileType {
|
|
|
|
Video,
|
2022-01-21 14:30:12 +00:00
|
|
|
VideoSticker,
|
2020-03-27 11:40:50 +00:00
|
|
|
AnimatedSticker,
|
|
|
|
WallPaper,
|
2021-08-11 16:56:12 +00:00
|
|
|
WallPatternPNG,
|
|
|
|
WallPatternSVG,
|
2020-03-27 11:40:50 +00:00
|
|
|
Theme,
|
|
|
|
};
|
|
|
|
|
|
|
|
[[nodiscard]] bool MayHaveGoodThumbnail(not_null<DocumentData*> owner) {
|
|
|
|
return owner->isVideoFile()
|
|
|
|
|| owner->isAnimation()
|
|
|
|
|| owner->isWallPaper()
|
|
|
|
|| owner->isTheme()
|
2022-01-19 14:45:51 +00:00
|
|
|
|| (owner->sticker() && owner->sticker()->isAnimated());
|
2020-03-27 11:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] QImage PrepareGoodThumbnail(
|
|
|
|
const QString &path,
|
|
|
|
QByteArray data,
|
|
|
|
FileType type) {
|
2022-01-21 14:30:12 +00:00
|
|
|
if (type == FileType::Video || type == FileType::VideoSticker) {
|
2022-01-21 14:57:06 +00:00
|
|
|
auto result = ::Media::Clip::PrepareForSending(path, data);
|
|
|
|
if (result.isWebmSticker && type == FileType::Video) {
|
|
|
|
result.thumbnail = Images::Opaque(std::move(result.thumbnail));
|
|
|
|
}
|
|
|
|
return result.thumbnail;
|
2020-03-27 11:40:50 +00:00
|
|
|
} else if (type == FileType::AnimatedSticker) {
|
|
|
|
return Lottie::ReadThumbnail(Lottie::ReadContent(data, path));
|
|
|
|
} else if (type == FileType::Theme) {
|
|
|
|
return Window::Theme::GeneratePreview(data, path);
|
2021-08-11 16:56:12 +00:00
|
|
|
} else if (type == FileType::WallPatternSVG) {
|
|
|
|
return Images::Read({
|
|
|
|
.path = path,
|
|
|
|
.content = std::move(data),
|
|
|
|
.maxSize = QSize(
|
|
|
|
kWallPaperThumbnailLimit,
|
|
|
|
kWallPaperThumbnailLimit),
|
|
|
|
.gzipSvg = true,
|
|
|
|
}).image;
|
2020-03-27 11:40:50 +00:00
|
|
|
}
|
|
|
|
auto buffer = QBuffer(&data);
|
|
|
|
auto file = QFile(path);
|
|
|
|
auto device = data.isEmpty() ? static_cast<QIODevice*>(&file) : &buffer;
|
|
|
|
auto reader = QImageReader(device);
|
|
|
|
const auto size = reader.size();
|
|
|
|
if (!reader.canRead()
|
|
|
|
|| (size.width() * size.height() > kReadAreaLimit)) {
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
auto result = reader.read();
|
|
|
|
if (!result.width() || !result.height()) {
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
return (result.width() > kWallPaperThumbnailLimit
|
|
|
|
|| result.height() > kWallPaperThumbnailLimit)
|
|
|
|
? result.scaled(
|
|
|
|
kWallPaperThumbnailLimit,
|
|
|
|
kWallPaperThumbnailLimit,
|
|
|
|
Qt::KeepAspectRatio,
|
|
|
|
Qt::SmoothTransformation)
|
|
|
|
: result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-05-22 11:57:13 +00:00
|
|
|
VideoPreviewState::VideoPreviewState(DocumentMedia *media)
|
|
|
|
: _media(media)
|
|
|
|
, _usingThumbnail(media ? media->owner()->hasVideoThumbnail() : false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoPreviewState::automaticLoad(Data::FileOrigin origin) const {
|
|
|
|
Expects(_media != nullptr);
|
|
|
|
|
|
|
|
if (_usingThumbnail) {
|
|
|
|
_media->videoThumbnailWanted(origin);
|
|
|
|
} else {
|
|
|
|
_media->automaticLoad(origin, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
::Media::Clip::ReaderPointer VideoPreviewState::makeAnimation(
|
|
|
|
Fn<void(::Media::Clip::Notification)> callback) const {
|
|
|
|
Expects(_media != nullptr);
|
|
|
|
Expects(loaded());
|
|
|
|
|
|
|
|
return _usingThumbnail
|
|
|
|
? ::Media::Clip::MakeReader(
|
|
|
|
_media->videoThumbnailContent(),
|
|
|
|
std::move(callback))
|
|
|
|
: ::Media::Clip::MakeReader(
|
2020-10-13 16:43:18 +00:00
|
|
|
_media->owner()->location(),
|
|
|
|
_media->bytes(),
|
2020-05-22 11:57:13 +00:00
|
|
|
std::move(callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoPreviewState::usingThumbnail() const {
|
|
|
|
return _usingThumbnail;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoPreviewState::loading() const {
|
|
|
|
return _usingThumbnail
|
|
|
|
? _media->owner()->videoThumbnailLoading()
|
|
|
|
: _media
|
|
|
|
? _media->owner()->loading()
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoPreviewState::loaded() const {
|
|
|
|
return _usingThumbnail
|
|
|
|
? !_media->videoThumbnailContent().isEmpty()
|
|
|
|
: _media
|
|
|
|
? _media->loaded()
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
DocumentMedia::DocumentMedia(not_null<DocumentData*> owner)
|
2020-04-10 13:18:51 +00:00
|
|
|
: _owner(owner) {
|
2020-03-27 11:40:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 18:01:04 +00:00
|
|
|
// NB! Right now DocumentMedia can outlive Main::Session!
|
|
|
|
// In DocumentData::collectLocalData a shared_ptr is sent on_main.
|
|
|
|
// In case this is a problem the ~Gif code should be rewritten.
|
2020-03-27 11:40:50 +00:00
|
|
|
DocumentMedia::~DocumentMedia() = default;
|
|
|
|
|
2020-04-09 12:27:53 +00:00
|
|
|
not_null<DocumentData*> DocumentMedia::owner() const {
|
|
|
|
return _owner;
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
void DocumentMedia::goodThumbnailWanted() {
|
|
|
|
_flags |= Flag::GoodThumbnailWanted;
|
|
|
|
}
|
|
|
|
|
|
|
|
Image *DocumentMedia::goodThumbnail() const {
|
|
|
|
Expects((_flags & Flag::GoodThumbnailWanted) != 0);
|
|
|
|
|
|
|
|
if (!_goodThumbnail) {
|
|
|
|
ReadOrGenerateThumbnail(_owner);
|
|
|
|
}
|
|
|
|
return _goodThumbnail.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::setGoodThumbnail(QImage thumbnail) {
|
|
|
|
if (!(_flags & Flag::GoodThumbnailWanted)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-29 15:10:25 +00:00
|
|
|
_goodThumbnail = std::make_unique<Image>(std::move(thumbnail));
|
2020-07-01 08:03:34 +00:00
|
|
|
_owner->session().notifyDownloaderTaskFinished();
|
2020-03-27 11:40:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:09:29 +00:00
|
|
|
Image *DocumentMedia::thumbnailInline() const {
|
2021-07-02 10:13:48 +00:00
|
|
|
if (!_inlineThumbnail && !_owner->inlineThumbnailIsPath()) {
|
2020-05-30 18:37:03 +00:00
|
|
|
const auto bytes = _owner->inlineThumbnailBytes();
|
|
|
|
if (!bytes.isEmpty()) {
|
|
|
|
auto image = Images::FromInlineBytes(bytes);
|
|
|
|
if (image.isNull()) {
|
|
|
|
_owner->clearInlineThumbnailBytes();
|
|
|
|
} else {
|
|
|
|
_inlineThumbnail = std::make_unique<Image>(std::move(image));
|
|
|
|
}
|
2020-04-08 15:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return _inlineThumbnail.get();
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:13:48 +00:00
|
|
|
const QPainterPath &DocumentMedia::thumbnailPath() const {
|
|
|
|
if (_pathThumbnail.isEmpty() && _owner->inlineThumbnailIsPath()) {
|
|
|
|
const auto bytes = _owner->inlineThumbnailBytes();
|
|
|
|
if (!bytes.isEmpty()) {
|
|
|
|
_pathThumbnail = Images::PathFromInlineBytes(bytes);
|
|
|
|
if (_pathThumbnail.isEmpty()) {
|
|
|
|
_owner->clearInlineThumbnailBytes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _pathThumbnail;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:06:34 +00:00
|
|
|
Image *DocumentMedia::thumbnail() const {
|
|
|
|
return _thumbnail.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::thumbnailWanted(Data::FileOrigin origin) {
|
|
|
|
if (!_thumbnail) {
|
|
|
|
_owner->loadThumbnail(origin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize DocumentMedia::thumbnailSize() const {
|
|
|
|
if (const auto image = _thumbnail.get()) {
|
|
|
|
return image->size();
|
|
|
|
}
|
|
|
|
const auto &location = _owner->thumbnailLocation();
|
|
|
|
return { location.width(), location.height() };
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::setThumbnail(QImage thumbnail) {
|
2020-05-29 15:10:25 +00:00
|
|
|
_thumbnail = std::make_unique<Image>(std::move(thumbnail));
|
2020-07-01 08:03:34 +00:00
|
|
|
_owner->session().notifyDownloaderTaskFinished();
|
2020-04-15 14:06:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 11:15:09 +00:00
|
|
|
QByteArray DocumentMedia::videoThumbnailContent() const {
|
|
|
|
return _videoThumbnailBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize DocumentMedia::videoThumbnailSize() const {
|
|
|
|
const auto &location = _owner->videoThumbnailLocation();
|
|
|
|
return { location.width(), location.height() };
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::videoThumbnailWanted(Data::FileOrigin origin) {
|
|
|
|
if (_videoThumbnailBytes.isEmpty()) {
|
|
|
|
_owner->loadVideoThumbnail(origin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::setVideoThumbnail(QByteArray content) {
|
|
|
|
_videoThumbnailBytes = std::move(content);
|
2022-04-22 16:23:47 +00:00
|
|
|
_videoThumbnailBytes.detach();
|
2020-05-22 11:15:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 08:15:47 +00:00
|
|
|
void DocumentMedia::checkStickerLarge() {
|
|
|
|
if (_sticker) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto data = _owner->sticker();
|
|
|
|
if (!data) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-10 14:19:43 +00:00
|
|
|
automaticLoad(_owner->stickerSetOrigin(), nullptr);
|
2022-01-19 14:45:51 +00:00
|
|
|
if (data->isAnimated() || !loaded()) {
|
2020-04-09 08:15:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-10 13:18:51 +00:00
|
|
|
if (_bytes.isEmpty()) {
|
2020-04-09 08:15:47 +00:00
|
|
|
const auto &loc = _owner->location(true);
|
|
|
|
if (loc.accessEnable()) {
|
2020-05-29 15:10:25 +00:00
|
|
|
_sticker = std::make_unique<Image>(loc.name());
|
2020-04-09 08:15:47 +00:00
|
|
|
loc.accessDisable();
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-29 15:10:25 +00:00
|
|
|
_sticker = std::make_unique<Image>(_bytes);
|
2020-04-09 08:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:19:43 +00:00
|
|
|
void DocumentMedia::automaticLoad(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
const HistoryItem *item) {
|
|
|
|
if (_owner->status != FileReady || loaded() || _owner->cancelled()) {
|
|
|
|
return;
|
|
|
|
} else if (!item && !_owner->sticker() && !_owner->isAnimation()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto toCache = _owner->saveToCache();
|
2020-06-18 18:04:16 +00:00
|
|
|
if (!toCache && Core::App().settings().askDownloadPath()) {
|
2020-04-10 14:19:43 +00:00
|
|
|
// We need a filename, but we're supposed to ask user for it.
|
|
|
|
// No automatic download in this case.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto filename = toCache
|
|
|
|
? QString()
|
|
|
|
: DocumentFileNameForSave(_owner);
|
|
|
|
const auto shouldLoadFromCloud = !Data::IsExecutableName(filename)
|
|
|
|
&& (item
|
|
|
|
? Data::AutoDownload::Should(
|
|
|
|
_owner->session().settings().autoDownload(),
|
|
|
|
item->history()->peer,
|
|
|
|
_owner)
|
|
|
|
: Data::AutoDownload::Should(
|
|
|
|
_owner->session().settings().autoDownload(),
|
|
|
|
_owner));
|
|
|
|
const auto loadFromCloud = shouldLoadFromCloud
|
|
|
|
? LoadFromCloudOrLocal
|
|
|
|
: LoadFromLocalOnly;
|
|
|
|
_owner->save(
|
|
|
|
origin,
|
|
|
|
filename,
|
|
|
|
loadFromCloud,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
2020-05-22 18:01:04 +00:00
|
|
|
void DocumentMedia::collectLocalData(not_null<DocumentMedia*> local) {
|
|
|
|
if (const auto image = local->_goodThumbnail.get()) {
|
2020-05-29 15:10:25 +00:00
|
|
|
_goodThumbnail = std::make_unique<Image>(image->original());
|
2020-05-22 18:01:04 +00:00
|
|
|
}
|
|
|
|
if (const auto image = local->_inlineThumbnail.get()) {
|
2020-05-29 15:10:25 +00:00
|
|
|
_inlineThumbnail = std::make_unique<Image>(image->original());
|
2020-05-22 18:01:04 +00:00
|
|
|
}
|
|
|
|
if (const auto image = local->_thumbnail.get()) {
|
2020-05-29 15:10:25 +00:00
|
|
|
_thumbnail = std::make_unique<Image>(image->original());
|
2020-05-22 18:01:04 +00:00
|
|
|
}
|
|
|
|
if (const auto image = local->_sticker.get()) {
|
2020-05-29 15:10:25 +00:00
|
|
|
_sticker = std::make_unique<Image>(image->original());
|
2020-05-22 18:01:04 +00:00
|
|
|
}
|
|
|
|
_bytes = local->_bytes;
|
|
|
|
_videoThumbnailBytes = local->_videoThumbnailBytes;
|
|
|
|
_flags = local->_flags;
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:27:53 +00:00
|
|
|
void DocumentMedia::setBytes(const QByteArray &bytes) {
|
|
|
|
if (!bytes.isEmpty()) {
|
|
|
|
_bytes = bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray DocumentMedia::bytes() const {
|
|
|
|
return _bytes;
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:02:09 +00:00
|
|
|
bool DocumentMedia::loaded(bool check) const {
|
2020-04-10 13:18:51 +00:00
|
|
|
return !_bytes.isEmpty() || !_owner->filepath(check).isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
float64 DocumentMedia::progress() const {
|
2020-04-10 14:19:43 +00:00
|
|
|
return (_owner->uploading() || _owner->loading())
|
|
|
|
? _owner->progress()
|
2020-04-10 13:18:51 +00:00
|
|
|
: (loaded() ? 1. : 0.);
|
|
|
|
}
|
|
|
|
|
2021-11-05 18:32:26 +00:00
|
|
|
bool DocumentMedia::canBePlayed(HistoryItem *item) const {
|
2020-04-10 14:19:43 +00:00
|
|
|
return !_owner->inappPlaybackFailed()
|
|
|
|
&& _owner->useStreamingLoader()
|
2021-11-05 18:32:26 +00:00
|
|
|
&& (loaded() || _owner->canBeStreamed(item));
|
2020-04-09 14:02:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:06:34 +00:00
|
|
|
bool DocumentMedia::thumbnailEnoughForSticker() const {
|
|
|
|
const auto &location = owner()->thumbnailLocation();
|
|
|
|
const auto size = _thumbnail
|
|
|
|
? QSize(_thumbnail->width(), _thumbnail->height())
|
|
|
|
: location.valid()
|
|
|
|
? QSize(location.width(), location.height())
|
|
|
|
: QSize();
|
|
|
|
return (size.width() >= 128) || (size.height() >= 128);
|
|
|
|
}
|
|
|
|
|
2020-04-09 08:15:47 +00:00
|
|
|
void DocumentMedia::checkStickerSmall() {
|
|
|
|
const auto data = _owner->sticker();
|
2022-01-19 14:45:51 +00:00
|
|
|
if ((data && data->isAnimated()) || thumbnailEnoughForSticker()) {
|
2020-04-09 08:15:47 +00:00
|
|
|
_owner->loadThumbnail(_owner->stickerSetOrigin());
|
2022-01-19 14:45:51 +00:00
|
|
|
if (data && data->isAnimated()) {
|
2020-04-10 14:19:43 +00:00
|
|
|
automaticLoad(_owner->stickerSetOrigin(), nullptr);
|
2020-04-09 08:15:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
checkStickerLarge();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Image *DocumentMedia::getStickerLarge() {
|
|
|
|
checkStickerLarge();
|
|
|
|
return _sticker.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Image *DocumentMedia::getStickerSmall() {
|
|
|
|
const auto data = _owner->sticker();
|
2022-01-19 14:45:51 +00:00
|
|
|
if ((data && data->isAnimated()) || thumbnailEnoughForSticker()) {
|
2020-04-15 14:06:34 +00:00
|
|
|
return thumbnail();
|
2020-04-09 08:15:47 +00:00
|
|
|
}
|
|
|
|
return _sticker.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::checkStickerLarge(not_null<FileLoader*> loader) {
|
2020-05-30 18:37:03 +00:00
|
|
|
if (_sticker || !_owner->sticker()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (auto image = loader->imageData(); !image.isNull()) {
|
|
|
|
_sticker = std::make_unique<Image>(std::move(image));
|
2020-04-09 08:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 13:18:51 +00:00
|
|
|
void DocumentMedia::GenerateGoodThumbnail(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
QByteArray data) {
|
2021-08-11 16:56:12 +00:00
|
|
|
const auto type = document->isPatternWallPaperSVG()
|
|
|
|
? FileType::WallPatternSVG
|
|
|
|
: document->isPatternWallPaperPNG()
|
|
|
|
? FileType::WallPatternPNG
|
|
|
|
: document->isWallPaper()
|
2020-03-27 11:40:50 +00:00
|
|
|
? FileType::WallPaper
|
|
|
|
: document->isTheme()
|
|
|
|
? FileType::Theme
|
2022-01-21 14:30:12 +00:00
|
|
|
: !document->sticker()
|
|
|
|
? FileType::Video
|
|
|
|
: document->sticker()->isLottie()
|
2020-03-27 11:40:50 +00:00
|
|
|
? FileType::AnimatedSticker
|
2022-01-21 14:30:12 +00:00
|
|
|
: FileType::VideoSticker;
|
2020-03-27 11:40:50 +00:00
|
|
|
auto location = document->location().isEmpty()
|
|
|
|
? nullptr
|
2020-10-13 16:43:18 +00:00
|
|
|
: std::make_unique<Core::FileLocation>(document->location());
|
2020-03-27 11:40:50 +00:00
|
|
|
if (data.isEmpty() && !location) {
|
|
|
|
document->setGoodThumbnailChecked(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto guard = base::make_weak(&document->owner().session());
|
|
|
|
crl::async([=, location = std::move(location)] {
|
|
|
|
const auto filepath = (location && location->accessEnable())
|
|
|
|
? location->name()
|
|
|
|
: QString();
|
|
|
|
auto result = PrepareGoodThumbnail(filepath, data, type);
|
|
|
|
auto bytes = QByteArray();
|
|
|
|
if (!result.isNull()) {
|
|
|
|
auto buffer = QBuffer(&bytes);
|
2022-01-21 14:30:12 +00:00
|
|
|
const auto format = (type == FileType::AnimatedSticker
|
|
|
|
|| type == FileType::VideoSticker)
|
2020-03-27 11:40:50 +00:00
|
|
|
? "WEBP"
|
2021-08-11 16:56:12 +00:00
|
|
|
: (type == FileType::WallPatternPNG
|
|
|
|
|| type == FileType::WallPatternSVG)
|
2020-03-27 11:40:50 +00:00
|
|
|
? "PNG"
|
|
|
|
: "JPG";
|
|
|
|
result.save(&buffer, format, kGoodThumbQuality);
|
|
|
|
}
|
|
|
|
if (!filepath.isEmpty()) {
|
|
|
|
location->accessDisable();
|
|
|
|
}
|
|
|
|
const auto cache = bytes.isEmpty() ? QByteArray("(failed)") : bytes;
|
|
|
|
crl::on_main(guard, [=] {
|
|
|
|
document->setGoodThumbnailChecked(true);
|
|
|
|
if (const auto active = document->activeMediaView()) {
|
|
|
|
active->setGoodThumbnail(result);
|
|
|
|
}
|
|
|
|
document->owner().cache().put(
|
|
|
|
document->goodThumbnailCacheKey(),
|
|
|
|
Storage::Cache::Database::TaggedValue{
|
|
|
|
base::duplicate(cache),
|
|
|
|
kImageCacheTag });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::CheckGoodThumbnail(not_null<DocumentData*> document) {
|
|
|
|
if (!document->goodThumbnailChecked()) {
|
|
|
|
ReadOrGenerateThumbnail(document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentMedia::ReadOrGenerateThumbnail(
|
|
|
|
not_null<DocumentData*> document) {
|
|
|
|
if (document->goodThumbnailGenerating()
|
|
|
|
|| document->goodThumbnailNoData()
|
|
|
|
|| !MayHaveGoodThumbnail(document)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document->setGoodThumbnailGenerating();
|
|
|
|
|
|
|
|
const auto guard = base::make_weak(&document->session());
|
|
|
|
const auto active = document->activeMediaView();
|
|
|
|
const auto got = [=](QByteArray value) {
|
|
|
|
if (value.isEmpty()) {
|
2020-04-10 13:18:51 +00:00
|
|
|
const auto bytes = active ? active->bytes() : QByteArray();
|
2020-03-27 11:40:50 +00:00
|
|
|
crl::on_main(guard, [=] {
|
2020-04-10 13:18:51 +00:00
|
|
|
GenerateGoodThumbnail(document, bytes);
|
2020-03-27 11:40:50 +00:00
|
|
|
});
|
|
|
|
} else if (active) {
|
|
|
|
crl::async([=] {
|
2021-08-11 15:40:17 +00:00
|
|
|
auto image = Images::Read({ .content = value }).image;
|
|
|
|
crl::on_main(guard, [=, image = std::move(image)]() mutable {
|
2020-03-27 11:40:50 +00:00
|
|
|
document->setGoodThumbnailChecked(true);
|
|
|
|
if (const auto active = document->activeMediaView()) {
|
2021-08-11 15:40:17 +00:00
|
|
|
active->setGoodThumbnail(std::move(image));
|
2020-03-27 11:40:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
crl::on_main(guard, [=] {
|
|
|
|
document->setGoodThumbnailChecked(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
document->owner().cache().get(document->goodThumbnailCacheKey(), got);
|
|
|
|
}
|
|
|
|
|
2022-08-29 12:34:21 +00:00
|
|
|
auto DocumentIconFrameGenerator(not_null<DocumentMedia*> media)
|
|
|
|
-> FnMut<std::unique_ptr<Ui::FrameGenerator>()> {
|
|
|
|
if (!media->loaded()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
using Type = StickerType;
|
|
|
|
const auto document = media->owner();
|
|
|
|
const auto content = media->bytes();
|
|
|
|
const auto fromFile = content.isEmpty();
|
|
|
|
const auto type = document->sticker()
|
|
|
|
? document->sticker()->type
|
|
|
|
: (document->isVideoFile() || document->isAnimation())
|
|
|
|
? Type::Webm
|
|
|
|
: Type::Webp;
|
|
|
|
const auto &location = media->owner()->location(true);
|
|
|
|
if (fromFile && !location.accessEnable()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return [=]() -> std::unique_ptr<Ui::FrameGenerator> {
|
|
|
|
const auto bytes = Lottie::ReadContent(content, location.name());
|
|
|
|
if (fromFile) {
|
|
|
|
location.accessDisable();
|
|
|
|
}
|
|
|
|
if (bytes.isEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case Type::Tgs:
|
|
|
|
return std::make_unique<Lottie::FrameGenerator>(bytes);
|
|
|
|
case Type::Webm:
|
|
|
|
return std::make_unique<FFmpeg::FrameGenerator>(bytes);
|
|
|
|
case Type::Webp:
|
|
|
|
return std::make_unique<Ui::ImageFrameGenerator>(bytes);
|
|
|
|
}
|
|
|
|
Unexpected("Document type in DocumentIconFrameGenerator.");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto DocumentIconFrameGenerator(const std::shared_ptr<DocumentMedia> &media)
|
|
|
|
-> FnMut<std::unique_ptr<Ui::FrameGenerator>()> {
|
|
|
|
return DocumentIconFrameGenerator(media.get());
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:50 +00:00
|
|
|
} // namespace Data
|