constrain the length of the inserted image file

This commit is contained in:
Le Tan 2017-07-02 17:52:11 +08:00
parent c48d646a55
commit a802222b06

View File

@ -105,7 +105,13 @@ QString VUtils::generateImageFileName(const QString &path, const QString &title,
Q_ASSERT(!title.isEmpty()); Q_ASSERT(!title.isEmpty());
QRegExp regExp("\\W"); QRegExp regExp("\\W");
QString baseName(title.toLower()); QString baseName(title.toLower());
// Remove non-character chars.
baseName.remove(regExp); baseName.remove(regExp);
// Constrain the length of the name.
baseName.truncate(10);
baseName.prepend('_'); baseName.prepend('_');
// Add current time and random number to make the name be most likely unique // Add current time and random number to make the name be most likely unique
@ -116,11 +122,12 @@ QString VUtils::generateImageFileName(const QString &path, const QString &title,
QString filePath = QDir(path).filePath(imageName); QString filePath = QDir(path).filePath(imageName);
int index = 1; int index = 1;
while (QFile(filePath).exists()) { while (QFileInfo::exists(filePath)) {
imageName = QString("%1_%2.%3").arg(baseName).arg(index++) imageName = QString("%1_%2.%3").arg(baseName).arg(index++)
.arg(format.toLower()); .arg(format.toLower());
filePath = QDir(path).filePath(imageName); filePath = QDir(path).filePath(imageName);
} }
return imageName; return imageName;
} }