fix(QClipboardProxy): use more reliable clipboard/DND checks

- getFileSize: NIM version would crash on non-existing or remote files
- isValidImageUrl: properly detect file extensions when the URL contains
a query (eg "file.jpeg?width=1000&height=600")
This commit is contained in:
Lukáš Tinkl 2023-06-07 17:48:56 +02:00 committed by Lukáš Tinkl
parent 010640acd0
commit b5a7df685a
3 changed files with 22 additions and 10 deletions

View File

@ -76,16 +76,6 @@ QtObject:
proc generateAlias*(self: Utils, pk: string): string {.slot.} =
return generateAliasFromPk(pk)
proc getFileSize*(self: Utils, filename: string): string {.slot.} =
var f: File = nil
if f.open(self.formatImagePath(filename)):
try:
result = $(f.getFileSize())
finally:
close(f)
else:
raise newException(IOError, "cannot open: " & filename)
proc readTextFile*(self: Utils, filepath: string): string {.slot.} =
try:
return readFile(filepath)

View File

@ -52,6 +52,9 @@ public:
return new QClipboardProxy;
}
Q_INVOKABLE bool isValidImageUrl(const QUrl &url, const QStringList &acceptedExtensions) const;
Q_INVOKABLE qint64 getFileSize(const QUrl &url) const;
signals:
void contentChanged();
};

View File

@ -6,6 +6,9 @@
#include <QImage>
#include <QMimeData>
#include <QUrl>
#include <QFile>
#include <algorithm>
QClipboardProxy::QClipboardProxy()
: m_clipboard(QGuiApplication::clipboard())
@ -67,3 +70,19 @@ QList<QUrl> QClipboardProxy::urls() const
{
return m_clipboard->mimeData()->urls();
}
bool QClipboardProxy::isValidImageUrl(const QUrl& url, const QStringList& acceptedExtensions) const
{
const auto strippedUrl = url.url(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery);
return std::any_of(acceptedExtensions.constBegin(), acceptedExtensions.constEnd(), [strippedUrl](const auto & ext) {
return strippedUrl.endsWith(ext);
});
}
qint64 QClipboardProxy::getFileSize(const QUrl& url) const
{
if (url.isLocalFile()) {
return QFile(url.toLocalFile()).size();
}
return 0;
}