Moved media editing via FileDialog to separate static method.

This commit is contained in:
23rd 2020-01-04 00:01:09 +03:00 committed by John Preston
parent 7e00930319
commit efa4deef6a
3 changed files with 95 additions and 69 deletions

View File

@ -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<InformBox>(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<InformBox>(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<InformBox>(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<InformBox>(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 = [=] {

View File

@ -278,6 +278,79 @@ PreparedList PrepareMediaFromImage(
return result;
}
std::optional<PreparedList> PreparedList::EditedPreparedFile(
FileDialog::OpenResult &&result,
bool isAlbum,
Fn<void()> errorCallback,
Fn<bool(QString)> 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<int> order) {

View File

@ -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<int> order);
static std::optional<PreparedList> EditedPreparedFile(
FileDialog::OpenResult &&result,
bool isAlbum,
Fn<void()> errorCallback,
Fn<bool(QString)> isValidFileCallback,
int previewWidth);
void mergeToEnd(PreparedList &&other);
bool canAddCaption(bool isAlbum, bool compressImages) const;