|
1 | | -// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd. |
| 1 | +// SPDX-FileCopyrightText: 2020 - 2026 UnionTech Software Technology Co., Ltd. |
2 | 2 | // |
3 | 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | 4 |
|
@@ -760,10 +760,39 @@ bool FileHander::checkFileBeforeLoad(const QString &file, bool isDdf) |
760 | 760 | //4 check if file be supported. |
761 | 761 | QFileInfo info(legalPath); |
762 | 762 | 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 | + } |
767 | 796 | } |
768 | 797 |
|
769 | 798 | if (isDdf) { |
|
0 commit comments