diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.cpp b/Telegram/SourceFiles/boxes/edit_caption_box.cpp index 306213f255..97adb77177 100644 --- a/Telegram/SourceFiles/boxes/edit_caption_box.cpp +++ b/Telegram/SourceFiles/boxes/edit_caption_box.cpp @@ -488,11 +488,8 @@ void EditCaptionBox::updateEditMediaButton() { void EditCaptionBox::createEditMediaButton() { const auto callback = [=](FileDialog::OpenResult &&result) { - if (result.paths.isEmpty() && result.remoteContent.isEmpty()) { - return; - } - const auto isValidFile = [](QString mimeType) { + auto isValidFile = [](QString mimeType) { if (mimeType == qstr("image/webp")) { Ui::show( Box(tr::lng_edit_media_invalid_file(tr::now)), @@ -502,73 +499,21 @@ void EditCaptionBox::createEditMediaButton() { return true; }; - if (!result.remoteContent.isEmpty()) { + auto list = Storage::PreparedList::EditedPreparedFile( + std::move(result), + _isAlbum, + [] { + Ui::show( + Box(tr::lng_edit_media_album_error(tr::now)), + Ui::LayerOption::KeepOther); + }, + std::move(isValidFile), + st::sendMediaPreviewSize); - auto list = Storage::PrepareMediaFromImage( - QImage(), - std::move(result.remoteContent), - st::sendMediaPreviewSize); - - if (!isValidFile(list.files.front().mime)) { - return; - } - - if (_isAlbum) { - const auto albumMimes = { - "image/jpeg", - "image/png", - "video/mp4", - }; - const auto file = &list.files.front(); - if (ranges::find(albumMimes, file->mime) == end(albumMimes) - || file->type == Storage::PreparedFile::AlbumType::None) { - Ui::show( - Box(tr::lng_edit_media_album_error(tr::now)), - Ui::LayerOption::KeepOther); - return; - } - } - - _preparedList = std::move(list); - } else if (!result.paths.isEmpty()) { - auto list = Storage::PrepareMediaList( - QStringList(result.paths.front()), - st::sendMediaPreviewSize); - - // Don't rewrite _preparedList if new list is not valid for album. - if (_isAlbum) { - using Info = FileMediaInformation; - - const auto media = &list.files.front().information->media; - const auto valid = media->match([&](const Info::Image &data) { - return Storage::ValidateThumbDimensions( - data.data.width(), - data.data.height()) - && !data.animated; - }, [&](Info::Video &data) { - data.isGifv = false; - return true; - }, [](auto &&other) { - return false; - }); - if (!valid) { - Ui::show( - Box(tr::lng_edit_media_album_error(tr::now)), - Ui::LayerOption::KeepOther); - return; - } - } - const auto info = QFileInfo(result.paths.front()); - if (!isValidFile(Core::MimeTypeForFile(info).name())) { - return; - } - - _preparedList = std::move(list); - } else { - return; + if (list) { + _preparedList = std::move(*list); + updateEditPreview(); } - - updateEditPreview(); }; const auto buttonCallback = [=] { diff --git a/Telegram/SourceFiles/storage/storage_media_prepare.cpp b/Telegram/SourceFiles/storage/storage_media_prepare.cpp index 45a81fdc82..450c9102e4 100644 --- a/Telegram/SourceFiles/storage/storage_media_prepare.cpp +++ b/Telegram/SourceFiles/storage/storage_media_prepare.cpp @@ -278,6 +278,79 @@ PreparedList PrepareMediaFromImage( return result; } +std::optional PreparedList::EditedPreparedFile( + FileDialog::OpenResult &&result, + bool isAlbum, + Fn errorCallback, + Fn isValidFileCallback, + int previewWidth) { + if (result.paths.isEmpty() && result.remoteContent.isEmpty()) { + return std::nullopt; + } + + if (!result.remoteContent.isEmpty()) { + + auto list = Storage::PrepareMediaFromImage( + QImage(), + std::move(result.remoteContent), + previewWidth); + + if (!isValidFileCallback(list.files.front().mime)) { + return std::nullopt; + } + + if (isAlbum) { + const auto albumMimes = { + "image/jpeg", + "image/png", + "video/mp4", + }; + const auto file = &list.files.front(); + if (ranges::find(albumMimes, file->mime) == end(albumMimes) + || file->type == Storage::PreparedFile::AlbumType::None) { + errorCallback(); + return std::nullopt; + } + } + Expects(list.files.size() == 1); + return std::move(list); + } else if (!result.paths.isEmpty()) { + auto list = Storage::PrepareMediaList( + QStringList(result.paths.front()), + previewWidth); + + // Don't rewrite _preparedList if a new list is not valid for album. + if (isAlbum) { + using Info = FileMediaInformation; + + const auto media = &list.files.front().information->media; + const auto valid = media->match([&](const Info::Image &data) { + return Storage::ValidateThumbDimensions( + data.data.width(), + data.data.height()) + && !data.animated; + }, [&](Info::Video &data) { + data.isGifv = false; + return true; + }, [](auto &&other) { + return false; + }); + if (!valid) { + errorCallback(); + return std::nullopt; + } + } + const auto info = QFileInfo(result.paths.front()); + if (!isValidFileCallback(Core::MimeTypeForFile(info).name())) { + return std::nullopt; + } + + Expects(list.files.size() == 1); + return std::move(list); + } + return std::nullopt; +} + PreparedList PreparedList::Reordered( PreparedList &&list, std::vector order) { diff --git a/Telegram/SourceFiles/storage/storage_media_prepare.h b/Telegram/SourceFiles/storage/storage_media_prepare.h index b81f7aad38..3d02fbc6a8 100644 --- a/Telegram/SourceFiles/storage/storage_media_prepare.h +++ b/Telegram/SourceFiles/storage/storage_media_prepare.h @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "core/file_utilities.h" + struct FileMediaInformation; namespace Storage { @@ -59,6 +61,12 @@ struct PreparedList { static PreparedList Reordered( PreparedList &&list, std::vector order); + static std::optional EditedPreparedFile( + FileDialog::OpenResult &&result, + bool isAlbum, + Fn errorCallback, + Fn isValidFileCallback, + int previewWidth); void mergeToEnd(PreparedList &&other); bool canAddCaption(bool isAlbum, bool compressImages) const;