diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index f1ec5c2e..fb0a87d0 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -35,16 +35,15 @@ class System /** * @brief Returns a file extension. * - * @param mimeType The optional string to use. + * @param mimeType value of MimeType enum. * * @returns A file extension. * * @code - * System::fileExt() // "wav" - * System::fileExt("application/pdf") // "pdf" + * System::fileExt(MimeType::Image) // "png" * @endcode */ - static std::string fileExt(const std::optional& mimeType = std::nullopt); + static std::string fileExtension(const std::optional& mimeType = std::nullopt); /** * Returns a random file name with a given extension or a commonly used extension. @@ -69,7 +68,7 @@ class System * System::commonFileExt() // "gif" * @endcode */ - static std::string commonFileExt(); + static std::string commonFileExtension(); /** * Returns a mime-type. diff --git a/include/faker-cxx/types/MimeTypes.h b/include/faker-cxx/types/MimeTypes.h index 563be55b..f32540dd 100644 --- a/include/faker-cxx/types/MimeTypes.h +++ b/include/faker-cxx/types/MimeTypes.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -70,10 +71,76 @@ const std::vector mimeTypes = {"application/atom+xml", "video/x-msvideo", "video/x-flv"}; +// Only contains non obvious extensions. +const std::map mimeTypesExtensions{ + {"application/atom+xml", "xml"}, + {"application/font-woff", "woff"}, + {"application/gzip", "gz"}, + {"application/java-archive", "jar"}, + {"application/javascript", "js"}, + {"application/ld+json", "jsonld"}, + {"application/msword", "doc"}, + {"application/octet-stream", "bin"}, + {"application/ogg", "ogx"}, + {"application/vnd.ms-excel", "xls"}, + {"application/vnd.ms-fontobject", "eot"}, + {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"}, + {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"}, + {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"}, + {"application/x-7z-compressed", "7z"}, + {"application/x-tar", "tart"}, + {"application/xhtml+xml", "xhtml"}, + + {"audio/ogg", "oga"}, + {"audio/webm", "weba"}, + {"audio/mpeg", "mp3"}, + + {"image/svg+xml", "svg"}, + + {"text/calendar", "ics"}, + {"text/javascript", "js"}, + {"text/plain", "txt"}, + + {"video/3gpp", "3gp"}, + {"video/3gpp2", "3g2"}, + {"video/mp2t", "ts"}, + {"video/ogg", "ogv"}, + {"video/x-msvideo", "avi"}}; + const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav", "image/png", "image/jpeg", "image/gif", "video/mp4", "video/mpeg", "text/html"}; const std::vector commonFileTypes = {"video", "audio", "image", "text", "application"}; +enum class FileType +{ + Video, + Audio, + Image, + Text, + Application +}; +inline std::string toString(FileType type) +{ + std::map enumToStringMapping{{FileType::Video, "video"}, + {FileType::Audio, "audio"}, + {FileType::Image, "image"}, + {FileType::Text, "text"}, + {FileType::Application, "application"}}; + return enumToStringMapping.at(type); +} +inline std::string extension(const std::string& mimeType) +{ + const auto it = mimeTypesExtensions.find(mimeType); + if (it == mimeTypesExtensions.end()) + { + auto pos = mimeType.find_last_of('/'); + return mimeType.substr(pos + 1); + } + else + { + return it->second; + } +} } diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index dc786c9b..ab5c3e5e 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -28,7 +28,7 @@ std::string System::fileName(const FileOptions& options) { for (int i = 0; i < options.extensionCount; ++i) { - std::string randomExt = fileExt(); + std::string randomExt = fileExtension(); randomExtensions.push_back(randomExt); } extensionsStr = "." + StringHelper::join(randomExtensions, "."); @@ -41,7 +41,7 @@ std::string System::fileName(const FileOptions& options) for (int i = 0; i < numExtensions; ++i) { - std::string randomExt = fileExt(); + std::string randomExt = fileExtension(); randomExtensions.push_back(randomExt); } @@ -51,32 +51,35 @@ std::string System::fileName(const FileOptions& options) return baseName + extensionsStr; } -std::string System::fileExt(const std::optional& mimeType) +std::string System::fileExtension(const std::optional& mimeType) { - if (mimeType.has_value() && !mimeType->empty()) + if (mimeType.has_value()) { - if (const auto it = std::ranges::find(mimeTypes, *mimeType); it != mimeTypes.end()) + const auto mimeTypeName = toString(mimeType.value()); + std::vector extensions; + for (const auto& mime : mimeTypes) { - const std::string& extension = *it; - size_t pos = extension.find_last_of('/'); - return extension.substr(pos + 1); + size_t pos = mime.find_first_of('/'); + const auto mt = mime.substr(0, pos); + if (mimeTypeName == mt) + { + extensions.push_back(mime.substr(pos + 1)); + } } + return Helper::arrayElement(extensions); } else { std::set extensionSet; - for (const auto& extension : mimeTypes) + for (const auto& mimeTypeName : mimeTypes) { - size_t pos = extension.find_last_of('/'); - extensionSet.insert(extension.substr(pos + 1)); + extensionSet.insert(extension(mimeTypeName)); } std::vector extensions(extensionSet.begin(), extensionSet.end()); return Helper::arrayElement(extensions); } - - return ""; } std::string System::commonFileName(const std::optional& ext) @@ -90,14 +93,14 @@ std::string System::commonFileName(const std::optional& ext) } else { - return str + "." + commonFileExt(); + return str + "." + commonFileExtension(); } } -std::string System::commonFileExt() +std::string System::commonFileExtension() { - std::optional mimeType = Helper::arrayElement(commonMimeTypes); - return fileExt(mimeType); + std::string mimeType = Helper::arrayElement(commonMimeTypes); + return extension(mimeType); } std::string System::mimeType() diff --git a/src/modules/system/SystemTest.cpp b/src/modules/system/SystemTest.cpp index d6edc471..86a06312 100644 --- a/src/modules/system/SystemTest.cpp +++ b/src/modules/system/SystemTest.cpp @@ -40,13 +40,80 @@ TEST_F(SystemTest, FileNameTestWithExtensionCount) TEST_F(SystemTest, FileExtTestWithMimeType) { - std::string exampleFileExtension = System::fileExt(); + std::string exampleFileExtension = System::fileExtension(); EXPECT_FALSE(exampleFileExtension.empty()); +} - EXPECT_EQ(System::fileExt("image/png"), "png"); - EXPECT_EQ(System::fileExt("application/pdf"), "pdf"); - EXPECT_EQ(System::fileExt("text/html"), "html"); +TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) +{ + auto image = FileType::Image; + auto audio = FileType::Audio; + auto video = FileType::Video; + auto text = FileType::Text; + auto application = FileType::Application; + + std::vector imageExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(image)) + { + imageExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector audioExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(audio)) + { + audioExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector videoExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(video)) + { + videoExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector textExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(text)) + { + textExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector applicationExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(application)) + { + applicationExtensions.push_back(mimeType.substr(pos + 1)); + } + } + auto imageExt = System::fileExtension(image); + auto audioExt = System::fileExtension(audio); + auto videoExt = System::fileExtension(video); + auto textExt = System::fileExtension(text); + auto applicationExt = System::fileExtension(application); + + EXPECT_TRUE(std::ranges::find(imageExtensions, imageExt) != imageExtensions.end()); + EXPECT_TRUE(std::ranges::find(audioExtensions, audioExt) != audioExtensions.end()); + EXPECT_TRUE(std::ranges::find(videoExtensions, videoExt) != videoExtensions.end()); + EXPECT_TRUE(std::ranges::find(textExtensions, textExt) != textExtensions.end()); + EXPECT_TRUE(std::ranges::find(applicationExtensions, applicationExt) != applicationExtensions.end()); } TEST_F(SystemTest, CommonFileNameWithEmptyExtensionTest)