Skip to content

Commit

Permalink
Fix getExtension() and setExtension() for Twitter images (fix #2087)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Aug 21, 2020
1 parent 5d97f2c commit b311885
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/lib/src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,12 @@ QString getExtension(const QString &url)
{
const int lastDot = url.lastIndexOf('.');
if (lastDot != -1) {
return url.mid(lastDot + 1);
const int doubleDot = url.midRef(lastDot + 1).indexOf(':');
if (doubleDot != -1) {
return url.mid(lastDot + 1, doubleDot);
} else {
return url.mid(lastDot + 1);
}
}
return QString();
}
Expand All @@ -490,7 +495,8 @@ QUrl setExtension(QUrl url, const QString &extension)
const int lastSlash = path.lastIndexOf('/');
const int lastDot = path.midRef(lastSlash + 1).lastIndexOf('.');
if (lastDot != -1) {
url.setPath(path.left(lastDot + lastSlash + 1) + "." + extension);
const int doubleDot = path.midRef(lastDot + 1).indexOf(':');
url.setPath(path.left(lastDot + lastSlash + 1) + "." + extension + (doubleDot != -1 ? path.mid(lastDot + doubleDot + 1) : ""));
}

return url;
Expand Down
4 changes: 3 additions & 1 deletion src/tests/src/functions-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,16 @@ TEST_CASE("Functions")
REQUIRE(getExtension(QUrl("http://test.com/some.dir/file")) == QString(""));
REQUIRE(getExtension(QUrl("http://test.com/file.jpg")) == QString("jpg"));
REQUIRE(getExtension(QUrl("http://test.com/file.jpg?toto=1")) == QString("jpg"));
REQUIRE(getExtension(QUrl("http://test.com/file.jpg?toto=1")) == QString("jpg"));
REQUIRE(getExtension(QUrl("http://test.com/file.jpg:large")) == QString("jpg"));
REQUIRE(getExtension(QUrl("http://test.com/index.php?image=file.jpg")) == QString("jpg"));
}
SECTION("SetExtension")
{
REQUIRE(setExtension(QUrl(""), "png") == QUrl(""));
REQUIRE(setExtension(QUrl("http://test.com/file"), "png") == QUrl("http://test.com/file"));
REQUIRE(setExtension(QUrl("http://test.com/file.jpg"), "png") == QUrl("http://test.com/file.png"));
REQUIRE(setExtension(QUrl("http://test.com/file.jpg?toto=1"), "png") == QUrl("http://test.com/file.png?toto=1"));
REQUIRE(setExtension(QUrl("http://test.com/file.jpg:large"), "png") == QUrl("http://test.com/file.png:large"));
}

SECTION("Levenshtein")
Expand Down

0 comments on commit b311885

Please sign in to comment.