Recursively scan directories for ZIM files

Added the ability to provide a directory path along with files.
If the path provided is a directory, we recursively scan it and either add the files in it or go through the same operation for sub directories.
This commit is contained in:
Nikhil Tanwar 2025-04-26 18:54:41 +05:30
parent 63ddddfa07
commit 48dd794ed6

View File

@ -23,6 +23,7 @@
#include <kiwix/server.h>
#include <kiwix/name_mapper.h>
#include <kiwix/tools.h>
#include <filesystem>
#ifdef _WIN32
# include <windows.h>
@ -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<std::string>& 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<std::string>& 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<std::string>::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;