Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create fileExt function overload with MimeType enum as parameter #235

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions include/faker-cxx/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& mimeType = std::nullopt);
static std::string fileExtension(const std::optional<FileType>& mimeType = std::nullopt);

/**
* Returns a random file name with a given extension or a commonly used extension.
Expand All @@ -69,7 +68,7 @@ class System
* System::commonFileExt() // "gif"
* @endcode
*/
static std::string commonFileExt();
static std::string commonFileExtension();

/**
* Returns a mime-type.
Expand Down
67 changes: 67 additions & 0 deletions include/faker-cxx/types/MimeTypes.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -70,10 +71,76 @@ const std::vector<std::string> mimeTypes = {"application/atom+xml",
"video/x-msvideo",
"video/x-flv"};

// Only contains non obvious extensions.
const std::map<std::string, std::string> 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<std::string> commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav",
"image/png", "image/jpeg", "image/gif",
"video/mp4", "video/mpeg", "text/html"};

const std::vector<std::string> commonFileTypes = {"video", "audio", "image", "text", "application"};

enum class FileType
{
Video,
Audio,
Image,
Text,
Application
};
inline std::string toString(FileType type)
{
std::map<FileType, std::string> 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;
}
}
}
37 changes: 20 additions & 17 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".");
Expand All @@ -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);
}

Expand All @@ -51,32 +51,35 @@ std::string System::fileName(const FileOptions& options)
return baseName + extensionsStr;
}

std::string System::fileExt(const std::optional<std::string>& mimeType)
std::string System::fileExtension(const std::optional<FileType>& 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<std::string> 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<std::string>(extensions);
}
else
{
std::set<std::string> 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<std::string> extensions(extensionSet.begin(), extensionSet.end());
return Helper::arrayElement<std::string>(extensions);
}

return "";
}

std::string System::commonFileName(const std::optional<std::string>& ext)
Expand All @@ -90,14 +93,14 @@ std::string System::commonFileName(const std::optional<std::string>& ext)
}
else
{
return str + "." + commonFileExt();
return str + "." + commonFileExtension();
}
}

std::string System::commonFileExt()
std::string System::commonFileExtension()
{
std::optional<std::string> mimeType = Helper::arrayElement<std::string>(commonMimeTypes);
return fileExt(mimeType);
std::string mimeType = Helper::arrayElement<std::string>(commonMimeTypes);
return extension(mimeType);
}

std::string System::mimeType()
Expand Down
75 changes: 71 additions & 4 deletions src/modules/system/SystemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::string> 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<std::string> 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<std::string> 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<std::string> 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)
Expand Down