Skip to content

fix(draw): detect image format by content instead of suffix#199

Merged
max-lvs merged 1 commit intolinuxdeepin:release/eaglefrom
LiHua000:release/eagle
Apr 10, 2026
Merged

fix(draw): detect image format by content instead of suffix#199
max-lvs merged 1 commit intolinuxdeepin:release/eaglefrom
LiHua000:release/eagle

Conversation

@LiHua000
Copy link
Copy Markdown
Contributor

@LiHua000 LiHua000 commented Apr 9, 2026

Prioritize content-based format detection to support opening images with modified file extensions.

优先通过文件内容检测格式,支持打开修改后缀的图片文件。

Log: 修复修改后缀后图片无法打开的问题

bug: https://pms.uniontech.com/bug-view-355941.html

Summary by Sourcery

Detect image file formats based on their actual content instead of relying solely on file extensions while preserving extension-based checks for DDF files.

Bug Fixes:

  • Allow opening image files whose extensions do not match their actual formats by validating supported formats via content-based detection.

Enhancements:

  • Update file handler copyright years to include 2026.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 9, 2026

Reviewer's Guide

Adjusts file loading validation so that image formats are determined primarily from file content via QImageReader instead of file suffix, while keeping suffix-based checks for DDF files, and updates the copyright year range.

Sequence diagram for content-based image format detection in checkFileBeforeLoad

sequenceDiagram
    actor User
    participant FileHander
    participant QFileInfo
    participant QImageReader

    User->>FileHander: checkFileBeforeLoad(file, false)
    FileHander->>QFileInfo: QFileInfo(legalPath)
    QFileInfo-->>FileHander: suffix()
    FileHander->>QImageReader: QImageReader()
    FileHander->>QImageReader: setFileName(legalPath)
    FileHander->>QImageReader: canRead()
    alt cannot read initially
        FileHander->>QImageReader: setAutoDetectImageFormat(true)
        FileHander->>QImageReader: setDecideFormatFromContent(true)
        FileHander->>QImageReader: setFileName(legalPath)
        FileHander->>QImageReader: canRead()
        alt still cannot read
            FileHander-->>User: error EUnSupportFile
        else can read after content detection
            FileHander->>QImageReader: format()
            FileHander->>FileHander: supPictureSuffix()
            alt format not supported
                FileHander-->>User: error EUnSupportFile
            else format supported
                FileHander-->>User: validation success
            end
        end
    else can read with initial settings
        FileHander->>QImageReader: format()
        FileHander->>FileHander: supPictureSuffix()
        alt format not supported
            FileHander-->>User: error EUnSupportFile
        else format supported
            FileHander-->>User: validation success
        end
    end
Loading

Class diagram for FileHander content-based image detection changes

classDiagram
    class FileHander {
        +bool checkFileBeforeLoad(QString file, bool isDdf)
        +QStringList supDdfStuffix()
        +QStringList supPictureSuffix()
    }

    class QImageReader {
        +void setFileName(QString fileName)
        +bool canRead()
        +void setAutoDetectImageFormat(bool enabled)
        +void setDecideFormatFromContent(bool enabled)
        +QByteArray format()
    }

    FileHander ..> QImageReader : uses
Loading

File-Level Changes

Change Details Files
Change image file validation to use QImageReader content-based format detection instead of relying solely on filename suffixes.
  • Split validation path between DDF documents and image files so that only DDF still uses suffix-based checks
  • Instantiate a QImageReader for image files and set the target file name for reading
  • Enable auto-detection and content-based format decision on the reader when initial canRead() fails
  • Reject files when QImageReader still cannot read them and set the unsupported format error
  • Compare the detected image format against the existing supported picture suffix list and reject unsupported formats
src/service/filehander.cpp
Update the source file copyright header year range.
  • Extend SPDX-FileCopyrightText year range from 2022 to 2026
src/service/filehander.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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.

Hey - I've found 1 issue, and left some high level feedback:

  • Comparing reader.format() against supPictureSuffix() may reject valid images whose internal format name differs from the file suffix (e.g. jpeg vs jpg, tiff vs tif); consider normalizing or mapping between Qt image formats and your supported suffix list before checking.
  • For non-DDF images you now always instantiate a QImageReader and attempt content detection, which may be more expensive than the previous suffix-based fast path; consider first validating known-good suffixes and only falling back to content-based detection when the suffix is unknown or missing.
  • The sequence of QImageReader calls is a bit redundant (e.g. setting fileName twice and toggling auto-detection flags that are enabled by default in many Qt versions); simplifying this setup would make the intent clearer and reduce potential confusion about when format detection actually happens.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Comparing `reader.format()` against `supPictureSuffix()` may reject valid images whose internal format name differs from the file suffix (e.g. `jpeg` vs `jpg`, `tiff` vs `tif`); consider normalizing or mapping between Qt image formats and your supported suffix list before checking.
- For non-DDF images you now always instantiate a `QImageReader` and attempt content detection, which may be more expensive than the previous suffix-based fast path; consider first validating known-good suffixes and only falling back to content-based detection when the suffix is unknown or missing.
- The sequence of `QImageReader` calls is a bit redundant (e.g. setting `fileName` twice and toggling auto-detection flags that are enabled by default in many Qt versions); simplifying this setup would make the intent clearer and reduce potential confusion about when format detection actually happens.

## Individual Comments

### Comment 1
<location path="src/service/filehander.cpp" line_range="790-791" />
<code_context>
+        }
+
+        // 获取实际格式并检查是否在支持列表中
+        QString actualFormat = reader.format().toLower();
+        QStringList supFormats = supPictureSuffix();
+        if (!supFormats.contains(actualFormat)) {
+            d_pri()->setError(EUnSupportFile, tr("Unable to open \"%1\", unsupported file format").arg(info.fileName()));
</code_context>
<issue_to_address>
**issue (bug_risk):** Potential mismatch between `QImageReader::format()` and `supPictureSuffix()` contents.

`QImageReader::format()` returns names like "png" / "jpeg" without a dot, while suffix lists often use leading dots or alternate names (e.g. ".jpg" vs "jpeg"). Please ensure `supPictureSuffix()` uses the same normalization (case, dot, and jpg/jpeg mapping), or normalize both sides before comparison, otherwise some supported images may be rejected as unsupported.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +790 to +791
QString actualFormat = reader.format().toLower();
QStringList supFormats = supPictureSuffix();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Potential mismatch between QImageReader::format() and supPictureSuffix() contents.

QImageReader::format() returns names like "png" / "jpeg" without a dot, while suffix lists often use leading dots or alternate names (e.g. ".jpg" vs "jpeg"). Please ensure supPictureSuffix() uses the same normalization (case, dot, and jpg/jpeg mapping), or normalize both sides before comparison, otherwise some supported images may be rejected as unsupported.

Prioritize content-based format detection to support opening images with modified file extensions.

优先通过文件内容检测格式,支持打开修改后缀的图片文件。

Log: 修复修改后缀后图片无法打开的问题

bug: https://pms.uniontech.com/bug-view-355941.html
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LiHua000, max-lvs

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

@LiHua000
Copy link
Copy Markdown
Contributor Author

LiHua000 commented Apr 9, 2026

/merge

@max-lvs max-lvs merged commit 1b7e347 into linuxdeepin:release/eagle Apr 10, 2026
16 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