Skip to content

Commit defebdb

Browse files
committed
fix(draw): detect image format by content instead of suffix
Prioritize content-based format detection to support opening images with modified file extensions. 优先通过文件内容检测格式,支持打开修改后缀的图片文件。 Log: 修复修改后缀后图片无法打开的问题 bug: https://pms.uniontech.com/bug-view-355941.html
1 parent 2abfcab commit defebdb

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/service/filehander.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2020 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -760,10 +760,39 @@ bool FileHander::checkFileBeforeLoad(const QString &file, bool isDdf)
760760
//4 check if file be supported.
761761
QFileInfo info(legalPath);
762762
auto stuff = info.suffix().toLower();
763-
QStringList list = isDdf ? supDdfStuffix() : supPictureSuffix();
764-
if (!list.contains(stuff)) {
765-
d_pri()->setError(EUnSupportFile, tr("Unable to open \"%1\", unsupported file format").arg(info.fileName()));
766-
return false;
763+
764+
if (isDdf) {
765+
// DDF文件仍然通过后缀名检查
766+
QStringList list = supDdfStuffix();
767+
if (!list.contains(stuff)) {
768+
d_pri()->setError(EUnSupportFile, tr("Unable to open \"%1\", unsupported file format").arg(info.fileName()));
769+
return false;
770+
}
771+
} else {
772+
// 图片文件通过文件内容判断格式,而不是仅依赖后缀名
773+
QImageReader reader;
774+
reader.setFileName(legalPath);
775+
776+
// 如果不能读取,尝试通过内容自动检测格式
777+
if (!reader.canRead()) {
778+
reader.setAutoDetectImageFormat(true);
779+
reader.setDecideFormatFromContent(true);
780+
reader.setFileName(legalPath);
781+
}
782+
783+
// 检查文件格式是否支持
784+
if (!reader.canRead()) {
785+
d_pri()->setError(EUnSupportFile, tr("Unable to open \"%1\", unsupported file format").arg(info.fileName()));
786+
return false;
787+
}
788+
789+
// 获取实际格式并检查是否在支持列表中
790+
QString actualFormat = reader.format().toLower();
791+
QStringList supFormats = supPictureSuffix();
792+
if (!supFormats.contains(actualFormat)) {
793+
d_pri()->setError(EUnSupportFile, tr("Unable to open \"%1\", unsupported file format").arg(info.fileName()));
794+
return false;
795+
}
767796
}
768797

769798
if (isDdf) {

0 commit comments

Comments
 (0)