1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix(core): Clean illegal chars from filenames

Fixes: #1304
This commit is contained in:
Alice 2018-04-30 12:29:36 -07:00
parent 498c04ba50
commit ab85716f00
5 changed files with 31 additions and 1 deletions

View File

@ -184,6 +184,7 @@ signals:
void fileTransferInfo(ToxFile file);
void fileTransferRemotePausedUnpaused(ToxFile file, bool paused);
void fileTransferBrokenUnbroken(ToxFile file, bool broken);
void fileNameChanged();
void fileSendFailed(uint32_t friendId, const QString& fname);

View File

@ -21,12 +21,14 @@
#include "core.h"
#include "corefile.h"
#include "toxfile.h"
#include "toxstring.h"
#include "src/persistence/profile.h"
#include "src/persistence/settings.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QThread>
#include <QRegularExpression>
#include <memory>
/**
@ -273,11 +275,21 @@ void CoreFile::removeFile(uint32_t friendId, uint32_t fileId)
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,
uint64_t filesize, const uint8_t* fname, size_t fnameLen,
void* 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) {
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);
}
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.fileKind = kind;
file.resumeFileId.resize(TOX_FILE_ID_LENGTH);

View File

@ -77,6 +77,7 @@ private:
private:
static QMutex fileSendMutex;
static QHash<uint64_t, ToxFile> fileMap;
static QString getCleanFileName(QString filename);
};
#endif // COREFILE_H

View File

@ -166,6 +166,7 @@ ChatForm::ChatForm(Friend* chatFriend, History* history)
connect(core, &Core::friendMessageReceived, this, &ChatForm::onFriendMessageReceived);
connect(core, &Core::friendTypingChanged, this, &ChatForm::onFriendTypingChanged);
connect(core, &Core::friendStatusChanged, this, &ChatForm::onFriendStatusChanged);
connect(core, &Core::fileNameChanged, this, &ChatForm::onFileNameChanged);
const CoreAV* av = core->getAv();
@ -232,6 +233,12 @@ void ChatForm::onSendTriggered()
SendMessageStr(msgEdit->toPlainText());
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()
{

View File

@ -72,6 +72,7 @@ public slots:
void onAvEnd(uint32_t friendId, bool error);
void onAvatarChange(uint32_t friendId, const QPixmap& pic);
void onAvatarRemoved(uint32_t friendId);
void onFileNameChanged();
protected slots:
void onSearchUp(const QString& phrase) override;