Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/common/vnoteitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include "vnoteitem.h"
#include "common/utils.h"

#include <DLog>

Check warning on line 9 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DLog> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 9 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DLog> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DGuiApplicationHelper>

Check warning on line 10 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DGuiApplicationHelper> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 10 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <DGuiApplicationHelper> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QDir>

Check warning on line 12 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 12 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFile>

Check warning on line 13 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 13 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFile> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 14 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 14 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QRegExp>

Check warning on line 15 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QRegExp> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 15 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QRegExp> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QStandardPaths>

Check warning on line 16 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QStandardPaths> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 16 in src/common/vnoteitem.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QStandardPaths> not found. Please note: Cppcheck does not need standard library headers to get proper results.

//导出为html文件时的头部部分
static const QString htmlHead =
Expand Down Expand Up @@ -334,9 +336,11 @@
//匹配图片块标签的正则表达式
QRegExp rx("<img.+src=.+>");
rx.setMinimal(true); //最小匹配
//匹配本地图片路径的正则表达式(图片位置限制在images文件夹,后缀限制为a-z长度为3到4位
QRegExp rxPath("(/\\S+)+/images/[\\w\\-]+\\.[a-z]{3,4}");
//匹配图片路径的正则表达式:兼容绝对路径和相对路径(images/xxx.png
QRegExp rxPath("(/[^/\\s]+)+/images/[\\w\\-]+\\.[a-z]{3,4}|images/[\\w\\-]+\\.[a-z]{3,4}");
rxPath.setMinimal(false); //最大匹配
//AppData基准路径,用于将相对路径还原为绝对路径
static const QString appDataBase = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
int pos = 0;
int last = 0;
//查找语音块
Expand All @@ -350,15 +354,25 @@
html.append(imgLabel);
} else {
//转换图片
QString base64 = "";
if (!Utils::pictureToBase64(rxPath.cap(0), base64)) {
//无效图片路径
QString matchedPath = rxPath.cap(0);
QString absolutePath = matchedPath.startsWith("/")
? matchedPath
: appDataBase + "/" + matchedPath;
absolutePath = QDir::cleanPath(absolutePath);
//路径遍历防护:规范化后路径必须在AppData目录下
if (!absolutePath.startsWith(appDataBase)) {
html.append(imgLabel);
} else {
//图片路径转换为base64编码
html.append(imgLabel.mid(0, last))
.append(base64)
.append(imgLabel.mid(last + rxPath.matchedLength(), imgLabel.size() - last - rxPath.matchedLength()));
QString base64 = "";
if (!Utils::pictureToBase64(absolutePath, base64)) {
//无效图片路径
html.append(imgLabel);
} else {
//图片路径转换为base64编码
html.append(imgLabel.mid(0, last))
.append(base64)
.append(imgLabel.mid(last + rxPath.matchedLength(), imgLabel.size() - last - rxPath.matchedLength()));
}
}
}
pos += rx.matchedLength();
Expand Down
7 changes: 4 additions & 3 deletions src/views/webrichtexteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ void WebRichTextEditor::onMenuActionClicked(QAction *action)
break;
case ActionManager::PictureView:
//查看图片
viewPicture(m_menuJson.toString().replace("file://", ""));
viewPicture(m_menuJson.toString());
break;
case ActionManager::PictureSaveAs:
//另存图片
Expand Down Expand Up @@ -528,8 +528,9 @@ void WebRichTextEditor::viewPicture(const QString &filePath)
if (imgView == nullptr) {
imgView = new ImageViewerDialog(this);
}
//加载图片并显示
imgView->open(filePath);
//加载图片并显示 兼容编辑区图片src为file://绝对URL的情况
QString path = filePath.startsWith("file://") ? filePath.mid(7) : filePath;
imgView->open(path);
}

void WebRichTextEditor::onPaste(bool isVoicePaste)
Expand Down
Loading