Allow uploading many scans at once in passport.

This commit is contained in:
John Preston 2018-07-10 23:51:07 +03:00
parent 7be1f16313
commit da60739893
3 changed files with 53 additions and 23 deletions

View File

@ -945,20 +945,24 @@ void PanelController::editWithUpload(int index, int documentIndex) {
Expects(documentIndex >= 0
&& documentIndex < _scopes[index].documents.size());
const auto &document = _scopes[index].documents[documentIndex];
const auto requiresSpecialScan = document->requiresSpecialScan(
SpecialFile::FrontSide,
false);
const auto allowMany = !requiresSpecialScan;
const auto widget = _panel->widget();
EditScans::ChooseScan(widget.get(), [=](QByteArray &&content) {
base::take(_scopeDocumentTypeBox);
editScope(index, documentIndex);
if (_scopes[index].documents[documentIndex]->requiresSpecialScan(
SpecialFile::FrontSide,
false)) {
if (!_editScope || !_editDocument) {
editScope(index, documentIndex);
}
if (requiresSpecialScan) {
uploadSpecialScan(SpecialFile::FrontSide, std::move(content));
} else {
uploadScan(std::move(content));
}
}, [=](ReadScanError error) {
readScanError(error);
});
}, allowMany);
}
void PanelController::readScanError(ReadScanError error) {

View File

@ -659,7 +659,7 @@ void EditScans::chooseScan() {
_controller->uploadScan(std::move(content));
}, [=](ReadScanError error) {
_controller->readScanError(error);
});
}, true);
}
void EditScans::chooseSpecialScan(SpecialFile type) {
@ -667,24 +667,33 @@ void EditScans::chooseSpecialScan(SpecialFile type) {
_controller->uploadSpecialScan(type, std::move(content));
}, [=](ReadScanError error) {
_controller->readScanError(error);
});
}, false);
}
void EditScans::ChooseScan(
QPointer<QWidget> parent,
Fn<void(QByteArray&&)> doneCallback,
Fn<void(ReadScanError)> errorCallback) {
Fn<void(ReadScanError)> errorCallback,
bool allowMany) {
Expects(parent != nullptr);
const auto processFiles = std::make_shared<Fn<void(QStringList&&)>>();
const auto filter = FileDialog::AllFilesFilter()
+ qsl(";;Image files (*")
+ cImgExtensions().join(qsl(" *"))
+ qsl(")");
const auto guardedCallback = crl::guard(parent, doneCallback);
const auto guardedError = crl::guard(parent, errorCallback);
const auto onMainCallback = [=](QByteArray content) {
crl::on_main([=, bytes = std::move(content)]() mutable {
const auto onMainCallback = [=](
QByteArray &&content,
QStringList &&remainingFiles) {
crl::on_main([
=,
bytes = std::move(content),
remainingFiles = std::move(remainingFiles)
]() mutable {
guardedCallback(std::move(bytes));
(*processFiles)(std::move(remainingFiles));
});
};
const auto onMainError = [=](ReadScanError error) {
@ -692,22 +701,38 @@ void EditScans::ChooseScan(
guardedError(error);
});
};
const auto processImage = [=](QByteArray &&content) {
crl::async([=, bytes = std::move(content)]() mutable {
const auto processImage = [=](
QByteArray &&content,
QStringList &&remainingFiles) {
crl::async([
=,
bytes = std::move(content),
remainingFiles = std::move(remainingFiles)
]() mutable {
auto result = ProcessImage(std::move(bytes));
if (const auto error = base::get_if<ReadScanError>(&result)) {
onMainError(*error);
} else {
auto content = base::get_if<QByteArray>(&result);
Assert(content != nullptr);
onMainCallback(std::move(*content));
onMainCallback(std::move(*content), std::move(remainingFiles));
}
});
};
const auto processFile = [=](FileDialog::OpenResult &&result) {
if (result.paths.size() == 1) {
const auto processOpened = [=](FileDialog::OpenResult &&result) {
if (result.paths.size() > 0) {
(*processFiles)(std::move(result.paths));
} else if (!result.remoteContent.isEmpty()) {
processImage(std::move(result.remoteContent), {});
}
};
*processFiles = [=](QStringList &&files) {
while (!files.isEmpty()) {
auto file = files.front();
files.removeAt(0);
auto content = [&] {
QFile f(result.paths.front());
QFile f(file);
if (f.size() > App::kImageSizeLimit) {
guardedError(ReadScanError::FileTooLarge);
return QByteArray();
@ -718,17 +743,17 @@ void EditScans::ChooseScan(
return f.readAll();
}();
if (!content.isEmpty()) {
processImage(std::move(content));
processImage(std::move(content), std::move(files));
return;
}
} else if (!result.remoteContent.isEmpty()) {
processImage(std::move(result.remoteContent));
}
};
FileDialog::GetOpenPath(
(allowMany ? FileDialog::GetOpenPaths : FileDialog::GetOpenPath)(
parent,
lang(lng_passport_choose_image),
filter,
processFile);
processOpened,
nullptr);
}
rpl::producer<QString> EditScans::uploadButtonText() const {

View File

@ -56,7 +56,8 @@ public:
static void ChooseScan(
QPointer<QWidget> parent,
Fn<void(QByteArray&&)> doneCallback,
Fn<void(ReadScanError)> errorCallback);
Fn<void(ReadScanError)> errorCallback,
bool allowMany);
~EditScans();