mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Merge pull request #5111
Alice (1): fix(core): Clean illegal chars from filenames
This commit is contained in:
commit
3d7d791e5f
|
@ -184,6 +184,7 @@ signals:
|
||||||
void fileTransferInfo(ToxFile file);
|
void fileTransferInfo(ToxFile file);
|
||||||
void fileTransferRemotePausedUnpaused(ToxFile file, bool paused);
|
void fileTransferRemotePausedUnpaused(ToxFile file, bool paused);
|
||||||
void fileTransferBrokenUnbroken(ToxFile file, bool broken);
|
void fileTransferBrokenUnbroken(ToxFile file, bool broken);
|
||||||
|
void fileNameChanged();
|
||||||
|
|
||||||
void fileSendFailed(uint32_t friendId, const QString& fname);
|
void fileSendFailed(uint32_t friendId, const QString& fname);
|
||||||
|
|
||||||
|
|
|
@ -21,12 +21,14 @@
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include "corefile.h"
|
#include "corefile.h"
|
||||||
#include "toxfile.h"
|
#include "toxfile.h"
|
||||||
|
#include "toxstring.h"
|
||||||
#include "src/persistence/profile.h"
|
#include "src/persistence/profile.h"
|
||||||
#include "src/persistence/settings.h"
|
#include "src/persistence/settings.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QRegularExpression>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -273,11 +275,21 @@ void CoreFile::removeFile(uint32_t friendId, uint32_t fileId)
|
||||||
fileMap.remove(key);
|
fileMap.remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString CoreFile::getCleanFileName(QString filename)
|
||||||
|
{
|
||||||
|
QRegularExpression regex("[<>:\"/\\|?*]");
|
||||||
|
filename.replace(regex, "_");
|
||||||
|
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, uint32_t kind,
|
void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, uint32_t kind,
|
||||||
uint64_t filesize, const uint8_t* fname, size_t fnameLen,
|
uint64_t filesize, const uint8_t* fname, size_t fnameLen,
|
||||||
void* vCore)
|
void* vCore)
|
||||||
{
|
{
|
||||||
Core* core = static_cast<Core*>(vCore);
|
Core* core = static_cast<Core*>(vCore);
|
||||||
|
auto filename = ToxString(fname, fnameLen);
|
||||||
|
const auto cleanFileName = CoreFile::getCleanFileName(filename.getQString());
|
||||||
|
|
||||||
if (kind == TOX_FILE_KIND_AVATAR) {
|
if (kind == TOX_FILE_KIND_AVATAR) {
|
||||||
const ToxPk friendPk = core->getFriendPublicKey(friendId);
|
const ToxPk friendPk = core->getFriendPublicKey(friendId);
|
||||||
|
@ -314,7 +326,15 @@ void CoreFile::onFileReceiveCallback(Tox*, uint32_t friendId, uint32_t fileId, u
|
||||||
qDebug() << QString("Received file request %1:%2 kind %3").arg(friendId).arg(fileId).arg(kind);
|
qDebug() << QString("Received file request %1:%2 kind %3").arg(friendId).arg(fileId).arg(kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
ToxFile file{fileId, friendId, QByteArray((char*)fname, fnameLen), "", ToxFile::RECEIVING};
|
if (cleanFileName != filename.getQString()) {
|
||||||
|
qDebug() << QStringLiteral("Cleaned filename from %1 to %2").arg(filename.getQString()).arg(cleanFileName);
|
||||||
|
filename = ToxString(cleanFileName);
|
||||||
|
emit core->fileNameChanged();
|
||||||
|
} else {
|
||||||
|
qDebug() << QStringLiteral("cleanFileName: filename already clean");
|
||||||
|
}
|
||||||
|
|
||||||
|
ToxFile file{fileId, friendId, filename.getBytes(), "", ToxFile::RECEIVING};
|
||||||
file.filesize = filesize;
|
file.filesize = filesize;
|
||||||
file.fileKind = kind;
|
file.fileKind = kind;
|
||||||
file.resumeFileId.resize(TOX_FILE_ID_LENGTH);
|
file.resumeFileId.resize(TOX_FILE_ID_LENGTH);
|
||||||
|
|
|
@ -77,6 +77,7 @@ private:
|
||||||
private:
|
private:
|
||||||
static QMutex fileSendMutex;
|
static QMutex fileSendMutex;
|
||||||
static QHash<uint64_t, ToxFile> fileMap;
|
static QHash<uint64_t, ToxFile> fileMap;
|
||||||
|
static QString getCleanFileName(QString filename);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // COREFILE_H
|
#endif // COREFILE_H
|
||||||
|
|
|
@ -166,6 +166,7 @@ ChatForm::ChatForm(Friend* chatFriend, History* history)
|
||||||
connect(core, &Core::friendMessageReceived, this, &ChatForm::onFriendMessageReceived);
|
connect(core, &Core::friendMessageReceived, this, &ChatForm::onFriendMessageReceived);
|
||||||
connect(core, &Core::friendTypingChanged, this, &ChatForm::onFriendTypingChanged);
|
connect(core, &Core::friendTypingChanged, this, &ChatForm::onFriendTypingChanged);
|
||||||
connect(core, &Core::friendStatusChanged, this, &ChatForm::onFriendStatusChanged);
|
connect(core, &Core::friendStatusChanged, this, &ChatForm::onFriendStatusChanged);
|
||||||
|
connect(core, &Core::fileNameChanged, this, &ChatForm::onFileNameChanged);
|
||||||
|
|
||||||
|
|
||||||
const CoreAV* av = core->getAv();
|
const CoreAV* av = core->getAv();
|
||||||
|
@ -232,6 +233,12 @@ void ChatForm::onSendTriggered()
|
||||||
SendMessageStr(msgEdit->toPlainText());
|
SendMessageStr(msgEdit->toPlainText());
|
||||||
msgEdit->clear();
|
msgEdit->clear();
|
||||||
}
|
}
|
||||||
|
void ChatForm::onFileNameChanged()
|
||||||
|
{
|
||||||
|
QMessageBox::warning(this, tr("Filename contained illegal characters"),
|
||||||
|
tr("Illegal characters have been changed to _ \n"
|
||||||
|
"so you can save the file on windows."));
|
||||||
|
}
|
||||||
|
|
||||||
void ChatForm::onTextEditChanged()
|
void ChatForm::onTextEditChanged()
|
||||||
{
|
{
|
||||||
|
|
|
@ -74,6 +74,7 @@ public slots:
|
||||||
void onAvEnd(uint32_t friendId, bool error);
|
void onAvEnd(uint32_t friendId, bool error);
|
||||||
void onAvatarChange(uint32_t friendId, const QPixmap& pic);
|
void onAvatarChange(uint32_t friendId, const QPixmap& pic);
|
||||||
void onAvatarRemoved(uint32_t friendId);
|
void onAvatarRemoved(uint32_t friendId);
|
||||||
|
void onFileNameChanged();
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void onSearchUp(const QString& phrase) override;
|
void onSearchUp(const QString& phrase) override;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user