MdEditor: fix suffix of url with query parameters

This commit is contained in:
Le Tan 2018-10-09 19:47:29 +08:00
parent 33350e6bdf
commit b27da44c7a
4 changed files with 46 additions and 17 deletions

View File

@ -218,14 +218,12 @@ void VInsertImageDialog::imageDownloaded(const QByteArray &data)
goto image_data; goto image_data;
} }
QString format = QFileInfo(getPathInput()).suffix(); QString format = QFileInfo(VUtils::purifyUrl(getPathInput())).suffix();
if (format.isEmpty()) { if (format.isEmpty()) {
goto image_data; goto image_data;
} }
m_tempFile.reset(new QTemporaryFile(QDir::tempPath() m_tempFile.reset(VUtils::createTemporaryFile(format));
+ QDir::separator()
+ "XXXXXX." + format));
if (!m_tempFile->open()) { if (!m_tempFile->open()) {
goto image_data; goto image_data;
} }

View File

@ -31,6 +31,7 @@
#include <QFontDatabase> #include <QFontDatabase>
#include <QSvgRenderer> #include <QSvgRenderer>
#include <QPainter> #include <QPainter>
#include <QTemporaryFile>
#include "vorphanfile.h" #include "vorphanfile.h"
#include "vnote.h" #include "vnote.h"
@ -1799,3 +1800,29 @@ QString VUtils::parentDirName(const QString &p_path)
return QFileInfo(p_path).dir().dirName(); return QFileInfo(p_path).dir().dirName();
} }
QString VUtils::purifyUrl(const QString &p_url)
{
int idx = p_url.indexOf('?');
if (idx > -1) {
return p_url.left(idx);
}
return p_url;
}
// Suffix size for QTemporaryFile.
#define MAX_SIZE_SUFFIX_FOR_TEMP_FILE 10
QTemporaryFile *VUtils::createTemporaryFile(QString p_suffix)
{
if (p_suffix.size() > MAX_SIZE_SUFFIX_FOR_TEMP_FILE) {
p_suffix.clear();
}
QString xx = p_suffix.isEmpty() ? "XXXXXX" : "XXXXXX.";
return new QTemporaryFile(QDir::tempPath()
+ QDir::separator()
+ xx
+ p_suffix);
}

View File

@ -23,6 +23,7 @@ class QWebEngineView;
class QAction; class QAction;
class QTreeWidgetItem; class QTreeWidgetItem;
class QFormLayout; class QFormLayout;
class QTemporaryFile;
#if !defined(V_ASSERT) #if !defined(V_ASSERT)
#define V_ASSERT(cond) ((!(cond)) ? qt_assert(#cond, __FILE__, __LINE__) : qt_noop()) #define V_ASSERT(cond) ((!(cond)) ? qt_assert(#cond, __FILE__, __LINE__) : qt_noop())
@ -384,6 +385,11 @@ public:
// @p_path: file path of file or dir. // @p_path: file path of file or dir.
static QString parentDirName(const QString &p_path); static QString parentDirName(const QString &p_path);
// Remove query in the url (?xxx).
static QString purifyUrl(const QString &p_url);
static QTemporaryFile *createTemporaryFile(QString p_suffix);
// Regular expression for image link. // Regular expression for image link.
// ![image title]( http://github.com/tamlok/vnote.jpg "alt text" =200x100) // ![image title]( http://github.com/tamlok/vnote.jpg "alt text" =200x100)
// Captured texts (need to be trimmed): // Captured texts (need to be trimmed):

View File

@ -1689,9 +1689,7 @@ void VMdEditor::exportGraphAndCopy(const QString &p_lang,
const QString &p_text, const QString &p_text,
const QString &p_format) const QString &p_format)
{ {
m_exportTempFile.reset(new QTemporaryFile(QDir::tempPath() m_exportTempFile.reset(VUtils::createTemporaryFile(p_format));
+ QDir::separator()
+ "XXXXXX." + p_format));
if (!m_exportTempFile->open()) { if (!m_exportTempFile->open()) {
VUtils::showMessage(QMessageBox::Warning, VUtils::showMessage(QMessageBox::Warning,
tr("Warning"), tr("Warning"),
@ -2028,13 +2026,13 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text)
// Sort it in ascending order. // Sort it in ascending order.
std::sort(regs.begin(), regs.end()); std::sort(regs.begin(), regs.end());
QProgressDialog proDlg(tr("Fetching images to local..."), QProgressDialog proDlg(tr("Fetching images to local folder..."),
tr("Abort"), tr("Abort"),
0, 0,
regs.size(), regs.size(),
this); this);
proDlg.setWindowModality(Qt::WindowModal); proDlg.setWindowModality(Qt::WindowModal);
proDlg.setWindowTitle(tr("Fetching Images To Local")); proDlg.setWindowTitle(tr("Fetching Images To Local Folder"));
QRegExp regExp(VUtils::c_imageLinkRegExp); QRegExp regExp(VUtils::c_imageLinkRegExp);
for (int i = regs.size() - 1; i >= 0; --i) { for (int i = regs.size() - 1; i >= 0; --i) {
@ -2052,16 +2050,20 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text)
QString imageTitle = regExp.cap(1).trimmed(); QString imageTitle = regExp.cap(1).trimmed();
QString imageUrl = regExp.cap(2).trimmed(); QString imageUrl = regExp.cap(2).trimmed();
proDlg.setLabelText(tr("Fetching image: %1").arg(imageUrl)); const int maxUrlLength = 100;
QString urlToDisplay(imageUrl);
if (urlToDisplay.size() > maxUrlLength) {
urlToDisplay = urlToDisplay.left(maxUrlLength) + "...";
}
proDlg.setLabelText(tr("Fetching image: %1").arg(urlToDisplay));
QString destImagePath, urlInLink; QString destImagePath, urlInLink;
// Only handle absolute file path or network path. // Only handle absolute file path or network path.
QString srcImagePath; QString srcImagePath;
QFileInfo info(imageUrl); QFileInfo info(VUtils::purifyUrl(imageUrl));
// For network image. // For network image.
QString suffix = info.suffix();
QScopedPointer<QTemporaryFile> tmpFile; QScopedPointer<QTemporaryFile> tmpFile;
if (info.exists()) { if (info.exists()) {
@ -2073,11 +2075,7 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text)
// Network path. // Network path.
QByteArray data = VDownloader::downloadSync(QUrl(imageUrl)); QByteArray data = VDownloader::downloadSync(QUrl(imageUrl));
if (!data.isEmpty()) { if (!data.isEmpty()) {
QString xx = suffix.isEmpty() ? "XXXXXX" : "XXXXXX."; tmpFile.reset(VUtils::createTemporaryFile(info.suffix()));
tmpFile.reset(new QTemporaryFile(QDir::tempPath()
+ QDir::separator()
+ xx
+ suffix));
if (tmpFile->open() && tmpFile->write(data) > -1) { if (tmpFile->open() && tmpFile->write(data) > -1) {
srcImagePath = tmpFile->fileName(); srcImagePath = tmpFile->fileName();
} }