Always try to save original photo bytes to disk.

This commit is contained in:
John Preston 2022-03-07 11:09:03 +04:00
parent 173fdf8056
commit 98f2f6d4c0
7 changed files with 27 additions and 34 deletions

View file

@ -190,4 +190,21 @@ void PhotoMedia::collectLocalData(not_null<PhotoMedia*> local) {
}
}
bool PhotoMedia::saveToFile(const QString &path) {
if (const auto video = videoContent(); !video.isEmpty()) {
QFile f(path);
return f.open(QIODevice::WriteOnly)
&& (f.write(video) == video.size());
} else if (const auto photo = imageBytes(Data::PhotoSize::Large)
; !photo.isEmpty()) {
QFile f(path);
return f.open(QIODevice::WriteOnly)
&& (f.write(photo) == photo.size());
} else if (const auto fallback = image(Data::PhotoSize::Large)->original()
; !fallback.isNull()) {
return fallback.save(path, "JPG");
}
return false;
}
} // namespace Data

View file

@ -47,6 +47,8 @@ public:
void collectLocalData(not_null<PhotoMedia*> local);
bool saveToFile(const QString &path);
private:
struct PhotoImage {
std::unique_ptr<Image> data;

View file

@ -1316,7 +1316,6 @@ void InnerWidget::savePhotoToFile(not_null<PhotoData*> photo) {
return;
}
const auto image = media->image(Data::PhotoSize::Large)->original();
auto filter = qsl("JPEG Image (*.jpg);;") + FileDialog::AllFilesFilter();
FileDialog::GetWritePath(
this,
@ -1325,7 +1324,7 @@ void InnerWidget::savePhotoToFile(not_null<PhotoData*> photo) {
filedialogDefaultName(qsl("photo"), qsl(".jpg")),
crl::guard(this, [=](const QString &result) {
if (!result.isEmpty()) {
image.save(result, "JPG");
media->saveToFile(result);
}
}));
}

View file

@ -2375,7 +2375,6 @@ void HistoryInner::savePhotoToFile(not_null<PhotoData*> photo) {
return;
}
const auto image = media->image(Data::PhotoSize::Large)->original();
auto filter = qsl("JPEG Image (*.jpg);;") + FileDialog::AllFilesFilter();
FileDialog::GetWritePath(
this,
@ -2386,7 +2385,7 @@ void HistoryInner::savePhotoToFile(not_null<PhotoData*> photo) {
qsl(".jpg")),
crl::guard(this, [=](const QString &result) {
if (!result.isEmpty()) {
image.save(result, "JPG");
media->saveToFile(result);
}
}));
}

View file

@ -105,7 +105,7 @@ void SavePhotoToFile(not_null<PhotoData*> photo) {
filedialogDefaultName(qsl("photo"), qsl(".jpg")),
crl::guard(&photo->session(), [=](const QString &result) {
if (!result.isEmpty()) {
image.save(result, "JPG");
media->saveToFile(result);
}
}));
}

View file

@ -1617,9 +1617,7 @@ void OverlayWidget::saveAs() {
return;
}
const auto image = _photoMedia->image(
Data::PhotoSize::Large)->original();
const auto bytes = _photoMedia->imageBytes(Data::PhotoSize::Large);
const auto media = _photoMedia;
const auto photo = _photo;
const auto filter = qsl("JPEG Image (*.jpg);;")
+ FileDialog::AllFilesFilter();
@ -1635,7 +1633,7 @@ void OverlayWidget::saveAs() {
_photo->date),
crl::guard(_widget, [=](const QString &result) {
if (!result.isEmpty() && _photo == photo) {
saveToFile(result, bytes, image);
media->saveToFile(result);
}
}));
}
@ -1707,12 +1705,12 @@ void OverlayWidget::downloadMedia() {
updateOver(_lastMouseMovePos);
}
} else if (_photo && _photo->hasVideo()) {
if (const auto bytes = _photoMedia->videoContent(); !bytes.isEmpty()) {
if (!_photoMedia->videoContent().isEmpty()) {
if (!QDir().exists(path)) {
QDir().mkpath(path);
}
toName = filedialogDefaultName(qsl("photo"), qsl(".mp4"), path);
if (!saveToFile(toName, bytes, QImage())) {
if (!_photoMedia->saveToFile(toName)) {
toName = QString();
}
} else {
@ -1728,10 +1726,7 @@ void OverlayWidget::downloadMedia() {
QDir().mkpath(path);
}
toName = filedialogDefaultName(qsl("photo"), qsl(".jpg"), path);
const auto saved = saveToFile(
toName,
_photoMedia->imageBytes(Data::PhotoSize::Large),
_photoMedia->image(Data::PhotoSize::Large)->original());
const auto saved = _photoMedia->saveToFile(toName);
if (!saved) {
toName = QString();
}
@ -1745,20 +1740,6 @@ void OverlayWidget::downloadMedia() {
}
}
bool OverlayWidget::saveToFile(
const QString &path,
const QByteArray &bytes,
const QImage &fallback) {
if (!bytes.isEmpty()) {
QFile f(path);
return f.open(QIODevice::WriteOnly)
&& (f.write(bytes) == bytes.size());
} else if (!fallback.isNull()) {
return fallback.save(path, "JPG");
}
return false;
}
void OverlayWidget::saveCancel() {
if (_document && _document->loading()) {
_document->cancel();

View file

@ -428,11 +428,6 @@ private:
void applyHideWindowWorkaround();
bool saveToFile(
const QString &path,
const QByteArray &bytes,
const QImage &fallback);
Window::SessionController *findWindow(bool switchTo = true) const;
bool _opengl = false;