/* 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 "storage/storage_media_prepare.h" #include "platform/platform_file_utilities.h" #include "storage/localimageloader.h" #include "core/mime_type.h" #include "ui/image/image_prepare.h" #include "ui/chat/attach/attach_extensions.h" #include "ui/chat/attach/attach_prepare.h" #include "app.h" #include #include namespace Storage { namespace { using Ui::PreparedFileInformation; using Ui::PreparedFile; using Ui::PreparedList; bool HasExtensionFrom(const QString &file, const QStringList &extensions) { for (const auto &extension : extensions) { const auto ext = file.right(extension.size()); if (ext.compare(extension, Qt::CaseInsensitive) == 0) { return true; } } return false; } bool ValidPhotoForAlbum( const PreparedFileInformation::Image &image, const QString &mime) { if (image.animated || Core::IsMimeSticker(mime)) { return false; } const auto width = image.data.width(); const auto height = image.data.height(); return Ui::ValidateThumbDimensions(width, height); } bool ValidVideoForAlbum(const PreparedFileInformation::Video &video) { const auto width = video.thumbnail.width(); const auto height = video.thumbnail.height(); return Ui::ValidateThumbDimensions(width, height); } QSize PrepareShownDimensions(const QImage &preview) { constexpr auto kMaxWidth = 1280; constexpr auto kMaxHeight = 1280; const auto result = preview.size(); return (result.width() > kMaxWidth || result.height() > kMaxHeight) ? result.scaled(kMaxWidth, kMaxHeight, Qt::KeepAspectRatio) : result; } bool PrepareDetailsIsWaiting( QSemaphore &semaphore, PreparedFile &file, int previewWidth) { crl::async([=, &semaphore, &file] { const auto guard = gsl::finally([&] { semaphore.release(); }); if (!file.path.isEmpty()) { file.mime = Core::MimeTypeForFile(QFileInfo(file.path)).name(); file.information = FileLoadTask::ReadMediaInformation( file.path, QByteArray(), file.mime); } else if (!file.content.isEmpty()) { file.mime = Core::MimeTypeForData(file.content).name(); file.information = FileLoadTask::ReadMediaInformation( QString(), file.content, file.mime); } else { Assert(file.information != nullptr); } using Image = PreparedFileInformation::Image; using Video = PreparedFileInformation::Video; if (const auto image = std::get_if( &file.information->media)) { if (ValidPhotoForAlbum(*image, file.mime)) { file.shownDimensions = PrepareShownDimensions(image->data); file.preview = Images::prepareOpaque(image->data.scaledToWidth( std::min(previewWidth, style::ConvertScale(image->data.width())) * cIntRetinaFactor(), Qt::SmoothTransformation)); Assert(!file.preview.isNull()); file.preview.setDevicePixelRatio(cRetinaFactor()); file.type = PreparedFile::AlbumType::Photo; } else if (Core::IsMimeSticker(file.mime)) { file.type = PreparedFile::AlbumType::None; } } else if (const auto video = std::get_if