mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
MdEditor: fix suffix of url with query parameters
This commit is contained in:
parent
33350e6bdf
commit
b27da44c7a
@ -218,14 +218,12 @@ void VInsertImageDialog::imageDownloaded(const QByteArray &data)
|
||||
goto image_data;
|
||||
}
|
||||
|
||||
QString format = QFileInfo(getPathInput()).suffix();
|
||||
QString format = QFileInfo(VUtils::purifyUrl(getPathInput())).suffix();
|
||||
if (format.isEmpty()) {
|
||||
goto image_data;
|
||||
}
|
||||
|
||||
m_tempFile.reset(new QTemporaryFile(QDir::tempPath()
|
||||
+ QDir::separator()
|
||||
+ "XXXXXX." + format));
|
||||
m_tempFile.reset(VUtils::createTemporaryFile(format));
|
||||
if (!m_tempFile->open()) {
|
||||
goto image_data;
|
||||
}
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include <QFontDatabase>
|
||||
#include <QSvgRenderer>
|
||||
#include <QPainter>
|
||||
#include <QTemporaryFile>
|
||||
|
||||
#include "vorphanfile.h"
|
||||
#include "vnote.h"
|
||||
@ -1799,3 +1800,29 @@ QString VUtils::parentDirName(const QString &p_path)
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ class QWebEngineView;
|
||||
class QAction;
|
||||
class QTreeWidgetItem;
|
||||
class QFormLayout;
|
||||
class QTemporaryFile;
|
||||
|
||||
#if !defined(V_ASSERT)
|
||||
#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.
|
||||
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.
|
||||
// 
|
||||
// Captured texts (need to be trimmed):
|
||||
|
@ -1689,9 +1689,7 @@ void VMdEditor::exportGraphAndCopy(const QString &p_lang,
|
||||
const QString &p_text,
|
||||
const QString &p_format)
|
||||
{
|
||||
m_exportTempFile.reset(new QTemporaryFile(QDir::tempPath()
|
||||
+ QDir::separator()
|
||||
+ "XXXXXX." + p_format));
|
||||
m_exportTempFile.reset(VUtils::createTemporaryFile(p_format));
|
||||
if (!m_exportTempFile->open()) {
|
||||
VUtils::showMessage(QMessageBox::Warning,
|
||||
tr("Warning"),
|
||||
@ -2028,13 +2026,13 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text)
|
||||
// Sort it in ascending order.
|
||||
std::sort(regs.begin(), regs.end());
|
||||
|
||||
QProgressDialog proDlg(tr("Fetching images to local..."),
|
||||
QProgressDialog proDlg(tr("Fetching images to local folder..."),
|
||||
tr("Abort"),
|
||||
0,
|
||||
regs.size(),
|
||||
this);
|
||||
proDlg.setWindowModality(Qt::WindowModal);
|
||||
proDlg.setWindowTitle(tr("Fetching Images To Local"));
|
||||
proDlg.setWindowTitle(tr("Fetching Images To Local Folder"));
|
||||
|
||||
QRegExp regExp(VUtils::c_imageLinkRegExp);
|
||||
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 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;
|
||||
|
||||
// Only handle absolute file path or network path.
|
||||
QString srcImagePath;
|
||||
QFileInfo info(imageUrl);
|
||||
QFileInfo info(VUtils::purifyUrl(imageUrl));
|
||||
|
||||
// For network image.
|
||||
QString suffix = info.suffix();
|
||||
QScopedPointer<QTemporaryFile> tmpFile;
|
||||
|
||||
if (info.exists()) {
|
||||
@ -2073,11 +2075,7 @@ void VMdEditor::replaceTextWithLocalImages(QString &p_text)
|
||||
// Network path.
|
||||
QByteArray data = VDownloader::downloadSync(QUrl(imageUrl));
|
||||
if (!data.isEmpty()) {
|
||||
QString xx = suffix.isEmpty() ? "XXXXXX" : "XXXXXX.";
|
||||
tmpFile.reset(new QTemporaryFile(QDir::tempPath()
|
||||
+ QDir::separator()
|
||||
+ xx
|
||||
+ suffix));
|
||||
tmpFile.reset(VUtils::createTemporaryFile(info.suffix()));
|
||||
if (tmpFile->open() && tmpFile->write(data) > -1) {
|
||||
srcImagePath = tmpFile->fileName();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user