fix(webrichtexteditor): add image drag-and-drop validation#344
Merged
Conversation
Contributor
pppanghu77
commented
Apr 28, 2026
- Implement MIME type checking to only accept image files (jpg, png, bmp)
- Add URL validation logic to filter valid image formats during drag operations
- Update dragEnterEvent and dragMoveEvent handlers to properly handle image file validation before accepting proposed actions
- Prevent non-image files from being processed in the rich text editor Log: fix(webrichtexteditor): add image drag-and-drop validation Bug: https://pms.uniontech.com/bug-view-354083.html
deepin pr auto review这份代码的修改意图是限制 以下是对这段代码的审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面的改进建议: 1. 代码逻辑与功能
2. 代码质量
3. 代码性能
4. 代码安全
改进建议与重构代码建议将验证逻辑提取为一个私有函数 修改后的代码示例:// 在头文件 WebRichTextEditor.h 的 private 部分声明
private:
bool isAcceptableDrop(const QMimeData *mimeData) const;
static const QStringList s_supportedImageFormats; // 声明静态常量
// 在源文件 WebRichTextEditor.cpp 中定义和实现
const QStringList WebRichTextEditor::s_supportedImageFormats = {"jpg", "jpeg", "png", "bmp", "gif"};
bool WebRichTextEditor::isAcceptableDrop(const QMimeData *mimeData) const
{
if (mimeData->hasImage()) {
return true;
}
if (mimeData->hasUrls()) {
const QList<QUrl> urls = mimeData->urls();
for (const QUrl &url : urls) {
// 使用 isLocalFile 检查更安全,path() 在本地文件上表现更好
if (url.isLocalFile()) {
const QString suffix = QFileInfo(url.toLocalFile()).suffix().toLower();
if (s_supportedImageFormats.contains(suffix)) {
return true; // 只要有一个有效文件即接受
}
}
}
}
return false;
}
// 修改后的 dragEnterEvent
void WebRichTextEditor::dragEnterEvent(QDragEnterEvent *event)
{
if (isAcceptableDrop(event->mimeData())) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
// 修改后的 dragMoveEvent
void WebRichTextEditor::dragMoveEvent(QDragMoveEvent *e)
{
if (isAcceptableDrop(e->mimeData())) {
e->acceptProposedAction();
} else {
e->ignore();
}
}详细改进点说明:
|
- Implement MIME type checking to only accept image files (jpg, png, bmp) - Add URL validation logic to filter valid image formats during drag operations - Update dragEnterEvent and dragMoveEvent handlers to properly handle image file validation before accepting proposed actions - Prevent non-image files from being processed in the rich text editor Log: fix(webrichtexteditor): add image drag-and-drop validation Bug: https://pms.uniontech.com/bug-view-354083.html
max-lvs
approved these changes
Apr 28, 2026
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: max-lvs, pppanghu77 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Contributor
Author
|
/forcemerge |
Contributor
|
This pr force merged! (status: unstable) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.