Don't crash if language detection fails.
continuous-integration/drone/push Build is passing Details

If there is no container.xml or something unexpected happens, we just return an
empty string.
This commit is contained in:
tastytea 2021-08-20 17:51:44 +02:00
parent 1e0cde8a4b
commit 552df1a49e
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 25 additions and 14 deletions

View File

@ -58,23 +58,34 @@ book read(const fs::path filepath, const bool raw)
book current_book;
current_book.language = [&filepath]() -> string
{
pugi::xml_document xml;
auto opf_file_path{get_opf_file_path(filepath)};
const std::string opf_file{
zip::read_file(filepath, opf_file_path.string())};
const auto result{xml.load_buffer(&opf_file[0], opf_file.size())};
if (result)
try
{
auto lang{
xml.child("package").child("metadata").child("dc:language")};
if (lang == nullptr)
pugi::xml_document xml;
auto opf_file_path{get_opf_file_path(filepath)};
const std::string opf_file{
zip::read_file(filepath, opf_file_path.string())};
const auto result{xml.load_buffer(&opf_file[0], opf_file.size())};
if (result)
{
lang = xml.child("opf:package")
.child("opf:metadata")
.child("dc:language");
auto lang{xml.child("package")
.child("metadata")
.child("dc:language")};
if (lang == nullptr)
{
lang = xml.child("opf:package")
.child("opf:metadata")
.child("dc:language");
}
return lang.text().as_string();
}
}
catch (epubgrep::zip::exception &e)
{
if (e.code != 1) // 1 == container.xml not found.
{
LOG(log::sev::error) << e.what();
}
return lang.text().as_string();
}
return "";
}();