/* This file is part of Telegram Desktop, the official desktop version of Telegram messaging app, see https://telegram.org Telegram Desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. In addition, as a special exception, the copyright holders give permission to link the code of portions of this program with the OpenSSL library. Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org */ #include "storage/storage_media_prepare.h" #include "platform/platform_file_utilities.h" #include "base/task_queue.h" #include "storage/localimageloader.h" namespace Storage { namespace { constexpr auto kMaxAlbumCount = 10; 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 FileMediaInformation::Image &image) { if (image.animated) { return false; } const auto width = image.data.width(); const auto height = image.data.height(); return ValidateThumbDimensions(width, height); } bool ValidVideoForAlbum(const FileMediaInformation::Video &video) { const auto width = video.thumbnail.width(); const auto height = video.thumbnail.height(); return ValidateThumbDimensions(width, height); } bool PrepareAlbumMediaIsWaiting( QSemaphore &semaphore, PreparedFile &file, int previewWidth) { // Use some special thread queue, like a separate QThreadPool. base::TaskQueue::Normal().Put([&, previewWidth] { const auto guard = gsl::finally([&] { semaphore.release(); }); if (!file.path.isEmpty()) { file.mime = mimeTypeForFile(QFileInfo(file.path)).name(); file.information = FileLoadTask::ReadMediaInformation( file.path, QByteArray(), file.mime); } else if (!file.content.isEmpty()) { file.mime = mimeTypeForData(file.content).name(); file.information = FileLoadTask::ReadMediaInformation( QString(), file.content, file.mime); } else { Assert(file.information != nullptr); } using Image = FileMediaInformation::Image; using Video = FileMediaInformation::Video; if (const auto image = base::get_if( &file.information->media)) { if (ValidPhotoForAlbum(*image)) { file.preview = image->data.scaledToWidth( previewWidth * cIntRetinaFactor(), Qt::SmoothTransformation); file.preview.setDevicePixelRatio(cRetinaFactor()); file.type = PreparedFile::AlbumType::Photo; } } else if (const auto video = base::get_if