From be464a5986a3e44c953fe30bc63f0b58543cd2f9 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Wed, 14 Aug 2024 09:47:07 -0400 Subject: [PATCH] Introduce getSlugifiedFileName in tools.h The function sanitizes file names depending on OS. --- include/tools.h | 9 +++++++++ src/tools/stringTools.cpp | 11 +++++++++++ test/stringTools.cpp | 14 ++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/include/tools.h b/include/tools.h index 13931e876..ddc0b4c6a 100644 --- a/include/tools.h +++ b/include/tools.h @@ -282,5 +282,14 @@ FeedCategories readCategoriesFromFeed(const std::string& content); * @return full language name. */ std::string getLanguageSelfName(const std::string& lang); + +/** + * Slugifies the filename by converting any characters reserved by the operating + * system to '_'. Note filename is only the file name and not a path. + * + * @param filename Valid UTF-8 encoded file name string. + * @return slugified string. + */ +std::string getSlugifiedFileName(const std::string& filename); } #endif // KIWIX_TOOLS_H diff --git a/src/tools/stringTools.cpp b/src/tools/stringTools.cpp index 7ec66deec..690fcd65c 100644 --- a/src/tools/stringTools.cpp +++ b/src/tools/stringTools.cpp @@ -32,6 +32,7 @@ #include #include +#include /* tell ICU where to find its dat file (tables) */ void kiwix::loadICUExternalTables() @@ -439,3 +440,13 @@ template<> std::string kiwix::extractFromString(const std::string& str) { return str; } + +std::string kiwix::getSlugifiedFileName(const std::string& filename) +{ +#ifdef _WIN32 + const std::regex reservedCharsReg(R"([<>:"/\\|?*])"); +#else + const std::regex reservedCharsReg("/"); +#endif + return std::regex_replace(filename, reservedCharsReg, "_"); +} diff --git a/test/stringTools.cpp b/test/stringTools.cpp index d866cda81..19aba3b6a 100644 --- a/test/stringTools.cpp +++ b/test/stringTools.cpp @@ -19,6 +19,7 @@ #include "gtest/gtest.h" #include "../src/tools/stringTools.h" +#include "../include/tools.h" #include #include @@ -170,4 +171,17 @@ TEST(stringTools, stripSuffix) EXPECT_EQ(stripSuffix("abc123", "987"), "abc123"); } +TEST(stringTools, getSlugifiedFileName) +{ + EXPECT_EQ(getSlugifiedFileName("abc123.png"), "abc123.png"); + EXPECT_EQ(getSlugifiedFileName("/"), "_"); + EXPECT_EQ(getSlugifiedFileName("abc/123.pdf"), "abc_123.pdf"); + EXPECT_EQ(getSlugifiedFileName("abc//123.yaml"), "abc__123.yaml"); + EXPECT_EQ(getSlugifiedFileName("//abc//123//"), "__abc__123__"); +#ifdef _WIN32 + EXPECT_EQ(getSlugifiedFileName(R"(<>:"/\\|?*)"), "__________"); + EXPECT_EQ(getSlugifiedFileName(R"(:"/123\\|?*<.txt>)"), "_abc____123______.txt_"); +#endif +} + };