From e1f9083c81b3de7d52ebfe9171dee238cffef604 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 21 Feb 2021 17:06:30 +0800 Subject: [PATCH] Improve detection of filename extension of audio/video files --- src/base/utils/misc.cpp | 16 +++++++++++++--- src/base/utils/misc.h | 2 +- src/gui/previewselectdialog.cpp | 5 ++--- src/gui/transferlistwidget.cpp | 6 +++++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index aad684195..e48b1db91 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -51,6 +51,7 @@ #include #include +#include #include #include #include @@ -62,6 +63,7 @@ #include "base/types.h" #include "base/unicodestrings.h" +#include "base/utils/fs.h" #include "base/utils/string.h" namespace @@ -290,9 +292,17 @@ qlonglong Utils::Misc::sizeInBytes(qreal size, const Utils::Misc::SizeUnit unit) return size; } -bool Utils::Misc::isPreviewable(const QString &extension) +bool Utils::Misc::isPreviewable(const QString &filename) { - static const QSet multimediaExtensions = + const QString mime = QMimeDatabase().mimeTypeForFile(filename, QMimeDatabase::MatchExtension).name(); + + if (mime.startsWith(QLatin1String("audio"), Qt::CaseInsensitive) + || mime.startsWith(QLatin1String("video"), Qt::CaseInsensitive)) + { + return true; + } + + const QSet multimediaExtensions = { "3GP", "AAC", @@ -337,7 +347,7 @@ bool Utils::Misc::isPreviewable(const QString &extension) "WMA", "WMV" }; - return multimediaExtensions.contains(extension.toUpper()); + return multimediaExtensions.contains(Utils::Fs::fileExtension(filename).toUpper()); } QString Utils::Misc::userFriendlyDuration(const qlonglong seconds, const qlonglong maxCap) diff --git a/src/base/utils/misc.h b/src/base/utils/misc.h index e6d4d2e29..be0583cdc 100644 --- a/src/base/utils/misc.h +++ b/src/base/utils/misc.h @@ -77,7 +77,7 @@ namespace Utils::Misc int friendlyUnitPrecision(SizeUnit unit); qint64 sizeInBytes(qreal size, SizeUnit unit); - bool isPreviewable(const QString &extension); + bool isPreviewable(const QString &filename); // Take a number of seconds and return a user-friendly // time duration like "1d 2h 10m". diff --git a/src/gui/previewselectdialog.cpp b/src/gui/previewselectdialog.cpp index cb7d9a611..73f326edb 100644 --- a/src/gui/previewselectdialog.cpp +++ b/src/gui/previewselectdialog.cpp @@ -90,9 +90,8 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr { QString fileName = torrent->fileName(i); if (fileName.endsWith(QB_EXT)) - fileName.chop(4); - QString extension = Utils::Fs::fileExtension(fileName).toUpper(); - if (Utils::Misc::isPreviewable(extension)) + fileName.chop(QB_EXT.length()); + if (Utils::Misc::isPreviewable(fileName)) { int row = m_previewListModel->rowCount(); m_previewListModel->insertRow(row); diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index 56450b9be..c5662fb44 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -44,6 +44,7 @@ #include #include +#include "base/bittorrent/common.h" #include "base/bittorrent/infohash.h" #include "base/bittorrent/session.h" #include "base/bittorrent/torrent.h" @@ -93,7 +94,10 @@ namespace for (int i = 0; i < torrent->filesCount(); ++i) { - if (Utils::Misc::isPreviewable(Utils::Fs::fileExtension(torrent->fileName(i)))) + QString fileName = torrent->fileName(i); + if (fileName.endsWith(QB_EXT)) + fileName.chop(QB_EXT.length()); + if (Utils::Misc::isPreviewable(fileName)) return true; }