diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index eef5448..d51570a 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #ifdef _WIN32 # include @@ -44,6 +45,7 @@ #define LITERAL_AS_STR(A) #A #define AS_STR(A) LITERAL_AS_STR(A) +namespace fs = std::filesystem; static const char USAGE[] = R"(Deliver ZIM file(s) articles via HTTP @@ -191,6 +193,50 @@ bool reloadLibrary(kiwix::Manager& mgr, const std::vector& paths) } } +void addBookFromFilePath(kiwix::Manager& manager, const std::string& path, + bool skipInvalid, bool isVerboseFlag) +{ + if (!manager.addBookFromPath(path, path, "", false)) { + if (skipInvalid) { + std::cerr << "Skipping invalid '" << path << "' ...continuing" << std::endl; + } else { + std::cerr << "Unable to add the ZIM file '" << path + << "' to the internal library." << std::endl; + exit(1); + } + } + if (isVerboseFlag) + std::cout << "Added file: " << path << " to the library" << std::endl; +} + +void addBooksFromDirectory(kiwix::Manager& manager, const std::string& path, + bool skipInvalid, bool isVerboseFlag) +{ + if (isVerboseFlag) + std::cout << "Iterating over directory: " << path << std::endl; + for (const auto& dir_entry : fs::directory_iterator(path)) { + if (fs::is_directory(dir_entry)) { + addBooksFromDirectory(manager, dir_entry.path().u8string(), skipInvalid, isVerboseFlag); + } else { + addBookFromFilePath(manager, dir_entry.path().u8string(), skipInvalid, isVerboseFlag); + } + } + if (isVerboseFlag) + std::cout << "Completed iterating over directory: " << path << std::endl; +} + +void addPathsInManager(kiwix::Manager& manager, const std::vector& zimPaths, + bool skipInvalid, bool isVerboseFlag) +{ + for (auto it = zimPaths.begin(); it != zimPaths.end(); it++) { + if (fs::is_directory(*it)) { + addBooksFromDirectory(manager, *it, skipInvalid, isVerboseFlag); + } else { + addBookFromFilePath(manager, *it, skipInvalid, isVerboseFlag); + } + } +} + // docopt::value::isLong() is counting repeated values. // It doesn't check if the string can be parsed as long. // (Contrarly to `asLong` which will try to convert string to long) @@ -308,18 +354,7 @@ int main(int argc, char** argv) << "' is empty (or has only remote books)." << std::endl; } } else { - std::vector::iterator it; - for (it = zimPaths.begin(); it != zimPaths.end(); it++) { - if (!manager.addBookFromPath(*it, *it, "", false)) { - if (skipInvalid) { - std::cerr << "Skipping invalid '" << *it << "' ...continuing" << std::endl; - } else { - std::cerr << "Unable to add the ZIM file '" << *it - << "' to the internal library." << std::endl; - exit(1); - } - } - } + addPathsInManager(manager, zimPaths, skipInvalid, isVerboseFlag); } auto libraryFileTimestamp = newestFileTimestamp(libraryPaths); auto curLibraryFileTimestamp = libraryFileTimestamp;