From dc9c9f6bbdc3ea61e8c142f58469c2ec92f20014 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 8 Oct 2018 20:40:45 +0800 Subject: [PATCH] MdEditor: close the temporary file explicitly after writing --- src/vdownloader.cpp | 3 ++- src/vdownloader.h | 1 + src/vmdeditor.cpp | 9 +++++++-- src/vpreviewmanager.cpp | 1 - 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/vdownloader.cpp b/src/vdownloader.cpp index 5a2ab2e9..e413af22 100644 --- a/src/vdownloader.cpp +++ b/src/vdownloader.cpp @@ -17,7 +17,8 @@ void VDownloader::handleDownloadFinished(QNetworkReply *reply) data = reply->readAll(); reply->deleteLater(); - emit downloadFinished(data, reply->url().toString()); + // The url() of the reply may be redirected and different from that of the request. + emit downloadFinished(data, reply->request().url().toString()); } static QNetworkRequest networkRequest(const QUrl &p_url) diff --git a/src/vdownloader.h b/src/vdownloader.h index 3a48c388..fb0cfce7 100644 --- a/src/vdownloader.h +++ b/src/vdownloader.h @@ -18,6 +18,7 @@ public: static QByteArray downloadSync(const QUrl &p_url); signals: + // Url is the original url of the request. void downloadFinished(const QByteArray &data, const QString &url); private slots: diff --git a/src/vmdeditor.cpp b/src/vmdeditor.cpp index 6488f803..f5c41c1d 100644 --- a/src/vmdeditor.cpp +++ b/src/vmdeditor.cpp @@ -1719,6 +1719,7 @@ void VMdEditor::exportGraphAndCopy(const QString &p_lang, } if (out.isEmpty() || m_exportTempFile->write(out) == -1) { + m_exportTempFile->close(); VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to export graph."), @@ -1727,8 +1728,11 @@ void VMdEditor::exportGraphAndCopy(const QString &p_lang, QMessageBox::Ok, this); } else { + m_exportTempFile->close(); + QClipboard *clipboard = QApplication::clipboard(); clipboard->clear(); + QImage img; img.loadFromData(out, p_format.toLocal8Bit().data()); if (!img.isNull()) { @@ -1741,8 +1745,6 @@ void VMdEditor::exportGraphAndCopy(const QString &p_lang, emit m_object->statusMessage(tr("Fail to read exported image: %1").arg(filePath)); } } - - m_exportTempFile->close(); } void VMdEditor::parseAndPaste() @@ -2064,6 +2066,9 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text) if (tmpFile->open() && tmpFile->write(data) > -1) { srcImagePath = tmpFile->fileName(); } + + // Need to close it explicitly to flush cache of small file. + tmpFile->close(); } } diff --git a/src/vpreviewmanager.cpp b/src/vpreviewmanager.cpp index 7e78eafa..413636a0 100644 --- a/src/vpreviewmanager.cpp +++ b/src/vpreviewmanager.cpp @@ -59,7 +59,6 @@ void VPreviewManager::imageDownloaded(const QByteArray &p_data, const QString &p QPixmap image; image.loadFromData(p_data); - if (!image.isNull()) { m_editor->addImage(name, image); emit requestUpdateImageLinks();