fix(draw): detect image format by content instead of suffix#199
fix(draw): detect image format by content instead of suffix#199max-lvs merged 1 commit intolinuxdeepin:release/eaglefrom
Conversation
Reviewer's GuideAdjusts 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 checkFileBeforeLoadsequenceDiagram
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
Class diagram for FileHander content-based image detection changesclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Comparing
reader.format()againstsupPictureSuffix()may reject valid images whose internal format name differs from the file suffix (e.g.jpegvsjpg,tiffvstif); consider normalizing or mapping between Qt image formats and your supported suffix list before checking. - For non-DDF images you now always instantiate a
QImageReaderand 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
QImageReadercalls is a bit redundant (e.g. settingfileNametwice 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| QString actualFormat = reader.format().toLower(); | ||
| QStringList supFormats = supPictureSuffix(); |
There was a problem hiding this comment.
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
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
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:
Enhancements: