2017-12-19 16:57:42 +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.
|
2017-12-19 16:57:42 +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
|
2017-12-19 16:57:42 +00:00
|
|
|
*/
|
|
|
|
#include "storage/storage_media_prepare.h"
|
|
|
|
|
|
|
|
#include "platform/platform_file_utilities.h"
|
|
|
|
#include "storage/localimageloader.h"
|
2018-06-13 22:06:21 +00:00
|
|
|
#include "core/mime_type.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image_prepare.h"
|
2020-10-13 10:00:35 +00:00
|
|
|
#include "ui/chat/attach/attach_extensions.h"
|
2020-10-13 12:07:53 +00:00
|
|
|
#include "ui/chat/attach/attach_prepare.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2017-12-19 16:57:42 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QSemaphore>
|
|
|
|
#include <QtCore/QMimeData>
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
namespace Storage {
|
|
|
|
namespace {
|
|
|
|
|
2020-10-13 12:07:53 +00:00
|
|
|
using Ui::PreparedFileInformation;
|
|
|
|
using Ui::PreparedFile;
|
|
|
|
using Ui::PreparedList;
|
2017-12-19 16:57:42 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-15 12:08:35 +00:00
|
|
|
bool ValidPhotoForAlbum(
|
2020-10-13 12:07:53 +00:00
|
|
|
const PreparedFileInformation::Image &image,
|
2019-07-15 12:08:35 +00:00
|
|
|
const QString &mime) {
|
2020-12-24 09:52:38 +00:00
|
|
|
if (image.animated
|
|
|
|
|| Core::IsMimeSticker(mime)
|
|
|
|
|| (mime == u"application/pdf"_q)) {
|
2017-12-19 16:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto width = image.data.width();
|
|
|
|
const auto height = image.data.height();
|
2020-10-13 17:15:52 +00:00
|
|
|
return Ui::ValidateThumbDimensions(width, height);
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 12:07:53 +00:00
|
|
|
bool ValidVideoForAlbum(const PreparedFileInformation::Video &video) {
|
2017-12-19 16:57:42 +00:00
|
|
|
const auto width = video.thumbnail.width();
|
|
|
|
const auto height = video.thumbnail.height();
|
2020-10-13 17:15:52 +00:00
|
|
|
return Ui::ValidateThumbDimensions(width, height);
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 13:48:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-16 07:52:55 +00:00
|
|
|
void PrepareDetailsInParallel(PreparedList &result, int previewWidth) {
|
|
|
|
Expects(result.files.size() <= Ui::MaxAlbumItems());
|
2017-12-19 16:57:42 +00:00
|
|
|
|
2020-10-16 14:11:23 +00:00
|
|
|
if (result.files.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
QSemaphore semaphore;
|
|
|
|
for (auto &file : result.files) {
|
2020-10-16 14:11:23 +00:00
|
|
|
crl::async([=, &semaphore, &file] {
|
|
|
|
PrepareDetails(file, previewWidth);
|
|
|
|
semaphore.release();
|
|
|
|
});
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2020-10-16 14:11:23 +00:00
|
|
|
semaphore.acquire(result.files.size());
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-10-20 08:19:48 +00:00
|
|
|
bool ValidateEditMediaDragData(
|
|
|
|
not_null<const QMimeData*> data,
|
|
|
|
Ui::AlbumType albumType) {
|
2020-07-30 07:50:12 +00:00
|
|
|
if (data->urls().size() > 1) {
|
|
|
|
return false;
|
|
|
|
} else if (data->hasImage()) {
|
2020-10-20 08:19:48 +00:00
|
|
|
return (albumType != Ui::AlbumType::Music);
|
2020-07-30 07:50:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 08:19:48 +00:00
|
|
|
if (albumType == Ui::AlbumType::PhotoVideo && data->hasUrls()) {
|
2020-07-30 07:50:12 +00:00
|
|
|
const auto url = data->urls().front();
|
|
|
|
if (url.isLocalFile()) {
|
|
|
|
using namespace Core;
|
|
|
|
const auto info = QFileInfo(Platform::File::UrlToLocal(url));
|
2020-10-20 08:19:48 +00:00
|
|
|
return IsMimeAcceptedForPhotoVideoAlbum(MimeTypeForFile(info).name());
|
2020-07-30 07:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
MimeDataState ComputeMimeDataState(const QMimeData *data) {
|
2018-01-11 15:51:59 +00:00
|
|
|
if (!data || data->hasFormat(qsl("application/x-td-forward"))) {
|
2017-12-19 16:57:42 +00:00
|
|
|
return MimeDataState::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->hasImage()) {
|
|
|
|
return MimeDataState::Image;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto uriListFormat = qsl("text/uri-list");
|
|
|
|
if (!data->hasFormat(uriListFormat)) {
|
|
|
|
return MimeDataState::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &urls = data->urls();
|
|
|
|
if (urls.isEmpty()) {
|
|
|
|
return MimeDataState::None;
|
|
|
|
}
|
|
|
|
|
2020-10-13 10:00:35 +00:00
|
|
|
const auto imageExtensions = Ui::ImageExtensions();
|
2017-12-19 16:57:42 +00:00
|
|
|
auto files = QStringList();
|
|
|
|
auto allAreSmallImages = true;
|
|
|
|
for (const auto &url : urls) {
|
|
|
|
if (!url.isLocalFile()) {
|
|
|
|
return MimeDataState::None;
|
|
|
|
}
|
|
|
|
const auto file = Platform::File::UrlToLocal(url);
|
|
|
|
|
|
|
|
const auto info = QFileInfo(file);
|
|
|
|
if (info.isDir()) {
|
|
|
|
return MimeDataState::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto filesize = info.size();
|
2020-07-06 08:18:11 +00:00
|
|
|
if (filesize > kFileSizeLimit) {
|
2017-12-19 16:57:42 +00:00
|
|
|
return MimeDataState::None;
|
|
|
|
} else if (allAreSmallImages) {
|
|
|
|
if (filesize > App::kImageSizeLimit) {
|
|
|
|
allAreSmallImages = false;
|
|
|
|
} else if (!HasExtensionFrom(file, imageExtensions)) {
|
|
|
|
allAreSmallImages = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allAreSmallImages
|
|
|
|
? MimeDataState::PhotoFiles
|
|
|
|
: MimeDataState::Files;
|
|
|
|
}
|
|
|
|
|
|
|
|
PreparedList PrepareMediaList(const QList<QUrl> &files, int previewWidth) {
|
|
|
|
auto locals = QStringList();
|
|
|
|
locals.reserve(files.size());
|
|
|
|
for (const auto &url : files) {
|
|
|
|
if (!url.isLocalFile()) {
|
|
|
|
return {
|
|
|
|
PreparedList::Error::NonLocalUrl,
|
|
|
|
url.toDisplayString()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
locals.push_back(Platform::File::UrlToLocal(url));
|
|
|
|
}
|
|
|
|
return PrepareMediaList(locals, previewWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreparedList PrepareMediaList(const QStringList &files, int previewWidth) {
|
|
|
|
auto result = PreparedList();
|
|
|
|
result.files.reserve(files.size());
|
2020-10-13 10:00:35 +00:00
|
|
|
const auto extensionsToCompress = Ui::ExtensionsForCompression();
|
2017-12-19 16:57:42 +00:00
|
|
|
for (const auto &file : files) {
|
|
|
|
const auto fileinfo = QFileInfo(file);
|
|
|
|
const auto filesize = fileinfo.size();
|
|
|
|
if (fileinfo.isDir()) {
|
|
|
|
return {
|
|
|
|
PreparedList::Error::Directory,
|
|
|
|
file
|
|
|
|
};
|
|
|
|
} else if (filesize <= 0) {
|
|
|
|
return {
|
|
|
|
PreparedList::Error::EmptyFile,
|
|
|
|
file
|
|
|
|
};
|
2020-07-06 08:18:11 +00:00
|
|
|
} else if (filesize > kFileSizeLimit) {
|
2017-12-19 16:57:42 +00:00
|
|
|
return {
|
|
|
|
PreparedList::Error::TooLargeFile,
|
|
|
|
file
|
|
|
|
};
|
|
|
|
}
|
2020-10-16 07:52:55 +00:00
|
|
|
if (result.files.size() < Ui::MaxAlbumItems()) {
|
|
|
|
result.files.emplace_back(file);
|
|
|
|
result.files.back().size = filesize;
|
|
|
|
} else {
|
|
|
|
result.filesToProcess.emplace_back(file);
|
|
|
|
result.files.back().size = filesize;
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2017-12-21 16:15:37 +00:00
|
|
|
}
|
2020-10-16 07:52:55 +00:00
|
|
|
PrepareDetailsInParallel(result, previewWidth);
|
2017-12-21 16:15:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PreparedList PrepareMediaFromImage(
|
|
|
|
QImage &&image,
|
|
|
|
QByteArray &&content,
|
|
|
|
int previewWidth) {
|
2020-10-16 10:38:12 +00:00
|
|
|
auto result = PreparedList();
|
2017-12-21 16:15:37 +00:00
|
|
|
auto file = PreparedFile(QString());
|
|
|
|
file.content = content;
|
|
|
|
if (file.content.isEmpty()) {
|
2020-10-13 12:07:53 +00:00
|
|
|
file.information = std::make_unique<PreparedFileInformation>();
|
2017-12-21 16:15:37 +00:00
|
|
|
const auto animated = false;
|
|
|
|
FileLoadTask::FillImageInformation(
|
|
|
|
std::move(image),
|
|
|
|
animated,
|
|
|
|
file.information);
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
2017-12-21 16:15:37 +00:00
|
|
|
result.files.push_back(std::move(file));
|
2020-10-16 07:52:55 +00:00
|
|
|
PrepareDetailsInParallel(result, previewWidth);
|
2017-12-19 16:57:42 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-13 12:07:53 +00:00
|
|
|
std::optional<PreparedList> PreparedFileFromFilesDialog(
|
2020-01-03 21:01:09 +00:00
|
|
|
FileDialog::OpenResult &&result,
|
2020-10-16 14:11:23 +00:00
|
|
|
Fn<bool(const Ui::PreparedList&)> checkResult,
|
2020-01-21 20:31:34 +00:00
|
|
|
Fn<void(tr::phrase<>)> errorCallback,
|
2020-01-03 21:01:09 +00:00
|
|
|
int previewWidth) {
|
|
|
|
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:11:23 +00:00
|
|
|
auto list = result.remoteContent.isEmpty()
|
|
|
|
? PrepareMediaList(result.paths, previewWidth)
|
|
|
|
: PrepareMediaFromImage(
|
2020-01-03 21:01:09 +00:00
|
|
|
QImage(),
|
|
|
|
std::move(result.remoteContent),
|
|
|
|
previewWidth);
|
2020-10-16 14:11:23 +00:00
|
|
|
if (list.error != PreparedList::Error::None) {
|
|
|
|
errorCallback(tr::lng_send_media_invalid_files);
|
|
|
|
return std::nullopt;
|
|
|
|
} else if (!checkResult(list)) {
|
|
|
|
return std::nullopt;
|
|
|
|
} else {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 21:01:09 +00:00
|
|
|
|
2020-10-16 14:11:23 +00:00
|
|
|
void PrepareDetails(PreparedFile &file, int previewWidth) {
|
|
|
|
if (!file.path.isEmpty()) {
|
|
|
|
file.information = FileLoadTask::ReadMediaInformation(
|
|
|
|
file.path,
|
|
|
|
QByteArray(),
|
|
|
|
Core::MimeTypeForFile(QFileInfo(file.path)).name());
|
|
|
|
} else if (!file.content.isEmpty()) {
|
|
|
|
file.information = FileLoadTask::ReadMediaInformation(
|
|
|
|
QString(),
|
|
|
|
file.content,
|
|
|
|
Core::MimeTypeForData(file.content).name());
|
|
|
|
} else {
|
|
|
|
Assert(file.information != nullptr);
|
|
|
|
}
|
2020-01-03 21:01:09 +00:00
|
|
|
|
2020-10-16 14:11:23 +00:00
|
|
|
using Image = PreparedFileInformation::Image;
|
|
|
|
using Video = PreparedFileInformation::Video;
|
2020-10-19 10:25:28 +00:00
|
|
|
using Song = PreparedFileInformation::Song;
|
2020-10-16 14:11:23 +00:00
|
|
|
if (const auto image = std::get_if<Image>(
|
|
|
|
&file.information->media)) {
|
|
|
|
if (ValidPhotoForAlbum(*image, file.information->filemime)) {
|
|
|
|
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());
|
2020-10-20 08:19:48 +00:00
|
|
|
file.type = PreparedFile::Type::Photo;
|
2020-10-16 14:11:23 +00:00
|
|
|
} else if (Core::IsMimeSticker(file.information->filemime)) {
|
2020-10-20 08:19:48 +00:00
|
|
|
file.type = PreparedFile::Type::None;
|
2020-01-03 21:01:09 +00:00
|
|
|
}
|
2020-10-16 14:11:23 +00:00
|
|
|
} else if (const auto video = std::get_if<Video>(
|
|
|
|
&file.information->media)) {
|
|
|
|
if (ValidVideoForAlbum(*video)) {
|
|
|
|
auto blurred = Images::prepareBlur(Images::prepareOpaque(video->thumbnail));
|
|
|
|
file.shownDimensions = PrepareShownDimensions(video->thumbnail);
|
|
|
|
file.preview = std::move(blurred).scaledToWidth(
|
|
|
|
previewWidth * cIntRetinaFactor(),
|
|
|
|
Qt::SmoothTransformation);
|
|
|
|
Assert(!file.preview.isNull());
|
|
|
|
file.preview.setDevicePixelRatio(cRetinaFactor());
|
2020-10-20 08:19:48 +00:00
|
|
|
file.type = PreparedFile::Type::Video;
|
2020-01-21 20:31:34 +00:00
|
|
|
}
|
2020-10-19 10:25:28 +00:00
|
|
|
} else if (const auto song = std::get_if<Song>(&file.information->media)) {
|
2020-10-20 08:19:48 +00:00
|
|
|
file.type = PreparedFile::Type::Music;
|
2020-01-03 21:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
} // namespace Storage
|
|
|
|
|