Use base::FileNameFromUserString.

This commit is contained in:
John Preston 2019-10-20 14:22:46 +04:00
parent 3ae7f9f93d
commit 27a83a7a09
7 changed files with 9 additions and 107 deletions

View file

@ -160,53 +160,6 @@ QString DefaultDownloadPath() {
+ '/';
}
QString NameFromUserString(QString name) {
static const auto Bad = { '/', '\\', '<', '>', ':', '"', '|', '?', '*' };
for (auto &ch : name) {
if (ch < 32 || ranges::find(Bad, ch.unicode()) != end(Bad)) {
ch = '_';
}
}
if (name.isEmpty() || name.endsWith(' ') || name.endsWith('.')) {
name.append('_');
}
#ifdef Q_OS_WIN
static const auto BadNames = {
qstr("CON"),
qstr("PRN"),
qstr("AUX"),
qstr("NUL"),
qstr("COM1"),
qstr("COM2"),
qstr("COM3"),
qstr("COM4"),
qstr("COM5"),
qstr("COM6"),
qstr("COM7"),
qstr("COM8"),
qstr("COM9"),
qstr("LPT1"),
qstr("LPT2"),
qstr("LPT3"),
qstr("LPT4"),
qstr("LPT5"),
qstr("LPT6"),
qstr("LPT7"),
qstr("LPT8"),
qstr("LPT9")
};
for (const auto bad : BadNames) {
if (name.startsWith(bad, Qt::CaseInsensitive)) {
if (name.size() == bad.size() || name[bad.size()] == '.') {
name = '_' + name;
break;
}
}
}
#endif // Q_OS_WIN
return name;
}
namespace internal {
void UnsafeOpenEmailLinkDefault(const QString &email) {

View file

@ -37,8 +37,6 @@ void ShowInFolder(const QString &filepath);
[[nodiscard]] QString DefaultDownloadPath();
[[nodiscard]] QString NameFromUserString(QString name);
namespace internal {
inline QString UrlToLocalDefault(const QUrl &url) {

View file

@ -30,6 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/image/image.h"
#include "ui/image/image_source.h"
#include "ui/text/text_utilities.h"
#include "base/base_file_utilities.h"
#include "mainwindow.h"
#include "core/application.h"
#include "lottie/lottie_animation.h"
@ -136,13 +137,7 @@ QString FileNameUnsafe(
QString name,
bool savingAs,
const QDir &dir) {
#ifdef Q_OS_WIN
name = name.replace(QRegularExpression(qsl("[\\\\\\/\\:\\*\\?\\\"\\<\\>\\|]")), qsl("_"));
#elif defined Q_OS_MAC
name = name.replace(QRegularExpression(qsl("[\\:]")), qsl("_"));
#elif defined Q_OS_LINUX
name = name.replace(QRegularExpression(qsl("[\\/]")), qsl("_"));
#endif
name = base::FileNameFromUserString(name);
if (Global::AskDownloadPath() || savingAs) {
if (!name.isEmpty() && name.at(0) == QChar::fromLatin1('.')) {
name = filedialogDefaultName(prefix, name);

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "export/export_settings.h"
#include "export/output/export_output_file.h"
#include "base/base_file_utilities.h"
#include "core/mime_type.h"
#include "core/utils.h"
#include <QtCore/QDateTime>
@ -337,53 +338,6 @@ QString ComputeDocumentName(
}
}
QString CleanDocumentName(QString name) {
// We don't want LTR/RTL mark/embedding/override/isolate chars
// in filenames, because they introduce a security issue, when
// an executable "Fil[x]gepj.exe" may look like "Filexe.jpeg".
QChar controls[] = {
0x200E, // LTR Mark
0x200F, // RTL Mark
0x202A, // LTR Embedding
0x202B, // RTL Embedding
0x202D, // LTR Override
0x202E, // RTL Override
0x2066, // LTR Isolate
0x2067, // RTL Isolate
#ifdef Q_OS_WIN
'\\',
'/',
':',
'*',
'?',
'"',
'<',
'>',
'|',
#elif defined Q_OS_MAC // Q_OS_WIN
':',
#elif defined Q_OS_LINUX // Q_OS_WIN || Q_OS_MAC
'/',
#endif // Q_OS_WIN || Q_OS_MAC || Q_OS_LINUX
};
for (const auto ch : controls) {
name = std::move(name).replace(ch, '_');
}
#ifdef Q_OS_WIN
const auto lower = name.trimmed().toLower();
const auto kBadExtensions = { qstr(".lnk"), qstr(".scf") };
const auto kMaskExtension = qsl(".download");
for (const auto extension : kBadExtensions) {
if (lower.endsWith(extension)) {
return name + kMaskExtension;
}
}
#endif // Q_OS_WIN
return name;
}
QString DocumentFolder(const Document &data) {
if (data.isVideoFile) {
return "video_files";
@ -469,7 +423,8 @@ Document ParseDocument(
MTP_string());
result.file.suggestedPath = suggestedFolder
+ DocumentFolder(result) + '/'
+ CleanDocumentName(ComputeDocumentName(context, result, date));
+ base::FileNameFromUserString(
ComputeDocumentName(context, result, date));
result.thumb = ParseDocumentThumb(
data,

View file

@ -28,6 +28,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/application.h"
#include "lang/lang_keys.h"
#include "base/event_filter.h"
#include "base/base_file_utilities.h"
#include "base/zlib_help.h"
#include "base/unixtime.h"
#include "data/data_session.h"
@ -435,7 +436,7 @@ SendMediaReady PrepareThemeMedia(
};
push("s", std::move(thumbnail));
const auto filename = File::NameFromUserString(name)
const auto filename = base::FileNameFromUserString(name)
+ qsl(".tdesktop-theme");
auto attributes = QVector<MTPDocumentAttribute>(
1,

@ -1 +1 @@
Subproject commit dd5ca832f9b212e553ea5afee9a2dab2c3acd982
Subproject commit 2169055d740dc069924e609fcc473006e5511134

@ -1 +1 @@
Subproject commit bc62f87f0ec7a0ef21ad4f676ef65cbb3d3631a4
Subproject commit 37f777e230215aeba0ee6eba149cff7b64d7ed0d