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 } // namespace Data

View file

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

View file

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

View file

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

View file

@ -1617,9 +1617,7 @@ void OverlayWidget::saveAs() {
return; return;
} }
const auto image = _photoMedia->image( const auto media = _photoMedia;
Data::PhotoSize::Large)->original();
const auto bytes = _photoMedia->imageBytes(Data::PhotoSize::Large);
const auto photo = _photo; const auto photo = _photo;
const auto filter = qsl("JPEG Image (*.jpg);;") const auto filter = qsl("JPEG Image (*.jpg);;")
+ FileDialog::AllFilesFilter(); + FileDialog::AllFilesFilter();
@ -1635,7 +1633,7 @@ void OverlayWidget::saveAs() {
_photo->date), _photo->date),
crl::guard(_widget, [=](const QString &result) { crl::guard(_widget, [=](const QString &result) {
if (!result.isEmpty() && _photo == photo) { if (!result.isEmpty() && _photo == photo) {
saveToFile(result, bytes, image); media->saveToFile(result);
} }
})); }));
} }
@ -1707,12 +1705,12 @@ void OverlayWidget::downloadMedia() {
updateOver(_lastMouseMovePos); updateOver(_lastMouseMovePos);
} }
} else if (_photo && _photo->hasVideo()) { } else if (_photo && _photo->hasVideo()) {
if (const auto bytes = _photoMedia->videoContent(); !bytes.isEmpty()) { if (!_photoMedia->videoContent().isEmpty()) {
if (!QDir().exists(path)) { if (!QDir().exists(path)) {
QDir().mkpath(path); QDir().mkpath(path);
} }
toName = filedialogDefaultName(qsl("photo"), qsl(".mp4"), path); toName = filedialogDefaultName(qsl("photo"), qsl(".mp4"), path);
if (!saveToFile(toName, bytes, QImage())) { if (!_photoMedia->saveToFile(toName)) {
toName = QString(); toName = QString();
} }
} else { } else {
@ -1728,10 +1726,7 @@ void OverlayWidget::downloadMedia() {
QDir().mkpath(path); QDir().mkpath(path);
} }
toName = filedialogDefaultName(qsl("photo"), qsl(".jpg"), path); toName = filedialogDefaultName(qsl("photo"), qsl(".jpg"), path);
const auto saved = saveToFile( const auto saved = _photoMedia->saveToFile(toName);
toName,
_photoMedia->imageBytes(Data::PhotoSize::Large),
_photoMedia->image(Data::PhotoSize::Large)->original());
if (!saved) { if (!saved) {
toName = QString(); 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() { void OverlayWidget::saveCancel() {
if (_document && _document->loading()) { if (_document && _document->loading()) {
_document->cancel(); _document->cancel();

View file

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