Browse Source

MdEditor: fix suffix of url with query parameters

Le Tan 7 years ago
parent
commit
b27da44c7a
4 changed files with 46 additions and 17 deletions
  1. 2 4
      src/dialog/vinsertimagedialog.cpp
  2. 27 0
      src/utils/vutils.cpp
  3. 6 0
      src/utils/vutils.h
  4. 11 13
      src/vmdeditor.cpp

+ 2 - 4
src/dialog/vinsertimagedialog.cpp

@@ -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;
     }

+ 27 - 0
src/utils/vutils.cpp

@@ -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);
+}

+ 6 - 0
src/utils/vutils.h

@@ -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.
     // ![image title]( http://github.com/tamlok/vnote.jpg "alt text" =200x100)
     // Captured texts (need to be trimmed):

+ 11 - 13
src/vmdeditor.cpp

@@ -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();
                 }