Skip to content

fix(editor): check parent MIME types to support subtype files in isMimeTypeSupport#465

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
Resurgamz:release/eagle
May 21, 2026
Merged

fix(editor): check parent MIME types to support subtype files in isMimeTypeSupport#465
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
Resurgamz:release/eagle

Conversation

@Resurgamz
Copy link
Copy Markdown

@Resurgamz Resurgamz commented May 20, 2026

fix(editor): check parent MIME types to support subtype files in isMimeTypeSupport

  • Retain QMimeType object to enable parent type lookup
  • Add parentMimeTypes() fallback to match subtypes against whitelist

修复(editor): 在 isMimeTypeSupport 中检查父 MIME 类型以支持子类型文件打开

Log: 通过父 MIME 类型继承关系扩展白名单匹配范围,修复子类型文件无法打开的问题
Bug: https://pms.uniontech.com/bug-view-362023.html

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @Resurgamz, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 20, 2026

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@Resurgamz Resurgamz force-pushed the release/eagle branch 3 times, most recently from 6ffef3a to 34bee3d Compare May 20, 2026 08:47
@Resurgamz Resurgamz changed the title fix(editor): support opening files with MIME subtypes via parent type… fix(editor): check parent MIME types to support subtype files in isMimeTypeSupport May 20, 2026
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff。本次修改主要是将局部变量提取为静态常量、优化了QMimeDatabase的调用方式,并增加了基于MIME类型继承关系的检查。

以下是针对语法逻辑、代码质量、代码性能和代码安全四个方面的详细审查意见和改进建议:

一、 语法与逻辑

  1. MIME继承检查可能导致误判(高优先级)

    • 问题:新增的 mime.inherits(supportedType) 逻辑存在风险。MIME类型具有继承树结构,例如 application/json 通常继承自 application/octet-stream(在部分Linux桌面环境中)。如果 SupportedTextMimeTypes 包含了 application/octet-stream,那么任何无法准确识别类型的二进制文件都会因为继承关系被判定为“支持的文本文件”,这违背了函数过滤二进制文件的初衷。
    • 建议:在使用 inherits 时,必须排除那些过于宽泛的基类(如 application/octet-streamapplication/x-empty)。或者,重新评估是否真的需要 inherits 检查,因为静态列表通常已经足够覆盖所需的特例。
  2. 硬编码的文件后缀检查缺乏鲁棒性(中优先级)

    • 问题if (filepath.endsWith("pub")) 对大小写敏感,且未检查后缀前的分隔符。像 Foobar.pub 会匹配,但 Foobar.PUBFoobarpublisher(误匹配)也会产生不同结果。
    • 建议:应使用 Qt 提供的文件信息类或统一转换为小写后匹配后缀:
      if (QFileInfo(filepath).suffix().compare("pub", Qt::CaseInsensitive) == 0) {
          return true;
      }

二、 代码质量

  1. 静态常量的初始化方式(低优先级)

    • 问题static const QStringList SupportedTextMimeTypes 在C++11及以上标准中可以正常编译,但在某些旧编译器或严格模式下,非POD类型的静态局部/全局变量初始化可能存在隐患。
    • 建议:建议使用 constexpr 或确保项目使用C++11以上标准。另外,如果是全局静态变量,建议加上匿名命名空间或 static 限定符以防止符号污染(当前已有 static,这很好)。
  2. 列表排序与可维护性(低优先级)

    • 问题SupportedTextMimeTypes 列表目前看起来是无序的,随着项目迭代,新增或查找类型时容易产生重复(例如 application/x-pearlapplication/x-perl 是否为拼写错误?)。
    • 建议:将列表按字母顺序排序,便于后续维护和二分查找。同时,请确认 "application/x-pearl" 是否是 "application/x-perl" 的拼写错误。

三、 代码性能

  1. QStringList::contains 的查询效率(中优先级)

    • 问题QStringList::contains 的时间复杂度为 $O(N)$。每次调用 isMimeTypeSupport 都会遍历长达 40+ 个元素的列表,且在 inherits 循环中又进行了一次 $O(N)$ 的遍历。
    • 建议:将 QStringList 替换为 QSet<QString>QSet 基于哈希表,查询时间复杂度为 $O(1)$,在频繁调用时性能提升显著。
      static const QSet<QString> SupportedTextMimeTypes = {
          "application/cmd",
          // ...
      };
  2. QMimeDatabase 对象的构造(低优先级)

    • 问题QMimeDatabase().mimeTypeForFile(...) 每次调用都会构造一个临时的 QMimeDatabase 对象。虽然其内部有缓存机制,但频繁构造仍有微量开销。
    • 建议:如果该函数被高频调用,可考虑使用 Q_GLOBAL_STATIC 或将其作为单例使用,但当前写法在大多数场景下已足够,此项优先级较低。

四、 代码安全

  1. application/octet-stream 带来的安全风险(高优先级)
    • 问题:这是通用的二进制流MIME类型。将 octet-stream 放入文本支持列表,意味着所有未知二进制文件(如 .exe, .dll, .so 等)都可能被尝试作为文本打开。这不仅可能导致乱码和内存占用激增(大文件读取),还可能在后续的文本处理逻辑中引发解析异常。
    • 建议:强烈建议从 SupportedTextMimeTypes 中移除 "application/octet-stream",除非业务上有极其特殊的需求。

💡 综合改进后的代码建议

// 使用 QSet 提升查询性能,并按字母排序便于维护
static const QSet<QString> SupportedTextMimeTypes = {
    "application/cmd",
    "application/javascript",
    "application/json",
    "application/pkix-cert",
    // "application/octet-stream", // 安全建议:移除此项,避免误判二进制文件
    "application/sql",
    "application/toml",
    "application/vnd.apple.mpegurl",
    "application/vnd.nokia.qt.qmakeprofile",
    "application/vnd.nokia.xml.qt.resource",
    "application/x-asp",
    "application/x-code-workspace",
    "application/x-csh",
    "application/x-cue",
    "application/x-desktop",
    "application/x-designer",
    "application/x-empty",
    "application/x-mpegURL",
    "application/x-msdos-program",
    "application/x-pak",
    "application/x-pem-key",
    "application/x-perl", // 注意:确认 x-pearl 是否为拼写错误,已修正为 x-perl
    "application/x-php",
    "application/x-ruby",
    "application/x-sh",
    "application/x-shellscript",
    "application/x-subrip",
    "application/x-text",
    "application/x-theme",
    "application/x-trash",
    "application/x-wine-extension-ini",
    "application/x-xbel",
    "application/x-yaml",
    "application/x-zerosize",
    "application/xml",
    "application/yaml",
    "audio/x-mod",
    "image/svg+xml",
    "model/vrml"
};

bool Utils::isMimeTypeSupport(const QString &filepath)
{
    const QMimeType mime = QMimeDatabase().mimeTypeForFile(filepath, QMimeDatabase::MatchMode::MatchContent);
    const QString mimeType = mime.name();

    if (mimeType.startsWith("text/")) {
        return true;
    }

    // 优化后缀检查:忽略大小写并精确匹配后缀
    if (QFileInfo(filepath).suffix().compare("pub", Qt::CaseInsensitive) == 0) {
        return true;
    }

    if (SupportedTextMimeTypes.contains(mimeType)) {
        return true;
    }

    // 逻辑优化:仅在 MIME 有效且不在排除列表中时检查继承关系
    // 注意:需排除 octet-stream 这种过于宽泛的基类,避免二进制文件被误判
    if (mime.isValid()) {
        for (const QString &supportedType : SupportedTextMimeTypes) {
            // 可选:增加对宽泛类型的过滤条件
            // if (supportedType == "application/octet-stream") continue; 
            if (mime.inherits(supportedType)) {
                return true;
            }
        }
    }

    return false;
}

…meTypeSupport

- Retain QMimeType object to enable parent type lookup
- Add parentMimeTypes() fallback to match subtypes against whitelist

修复(editor): 在 isMimeTypeSupport 中检查父 MIME 类型以支持子类型文件打开

Log: 通过父 MIME 类型继承关系扩展白名单匹配范围,修复子类型文件无法打开的问题
Bug: https://pms.uniontech.com/bug-view-362023.html
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, Resurgamz

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Resurgamz
Copy link
Copy Markdown
Author

/merge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented May 21, 2026

This pr cannot be merged! (status: unstable)

@Resurgamz
Copy link
Copy Markdown
Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented May 21, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 748ac61 into linuxdeepin:release/eagle May 21, 2026
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants