Skip to content
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set(ODR_SOURCE_FILES
"src/odr/global_params.cpp"
"src/odr/html.cpp"
"src/odr/http_server.cpp"
"src/odr/logger.cpp"
"src/odr/odr.cpp"
"src/odr/quantity.cpp"
"src/odr/style.cpp"
Expand Down
14 changes: 8 additions & 6 deletions cli/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using namespace odr;

int main(int argc, char **argv) {
auto logger = Logger::create_stdio("odr-server", LogLevel::verbose);

std::string input{argv[1]};

std::optional<std::string> password;
Expand All @@ -20,17 +22,17 @@ int main(int argc, char **argv) {
decode_preference.engine_priority = {
DecoderEngine::poppler, DecoderEngine::wvware, DecoderEngine::odr};

DecodedFile decoded_file{input, decode_preference};
DecodedFile decoded_file{input, decode_preference, *logger};

if (decoded_file.password_encrypted() && !password) {
std::cerr << "document encrypted but no password given" << std::endl;
ODR_FATAL(*logger, "document encrypted but no password given");
return 2;
}
if (decoded_file.password_encrypted()) {
try {
decoded_file = decoded_file.decrypt(*password);
} catch (const WrongPasswordError &) {
std::cerr << "wrong password" << std::endl;
ODR_FATAL(*logger, "wrong password");
return 1;
}
}
Expand All @@ -47,10 +49,10 @@ int main(int argc, char **argv) {
{
std::string prefix = "one_file";
HtmlViews views = server.serve_file(decoded_file, prefix, html_config);
std::cout << "hosted decoded file with id: " << prefix << std::endl;
ODR_INFO(*logger, "hosted decoded file with id: " << prefix);
for (const auto &view : views) {
std::cout << "http://localhost:8080/file/" << prefix << "/" << view.path()
<< std::endl;
ODR_INFO(*logger,
"http://localhost:8080/file/" << prefix << "/" << view.path());
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/odr/exceptions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <odr/exceptions.hpp>

#include <odr/file.hpp>
#include <odr/odr.hpp>

namespace odr {

Expand All @@ -18,11 +19,19 @@ FileNotFound::FileNotFound(const std::string &path)
UnknownFileType::UnknownFileType() : std::runtime_error("unknown file type") {}

UnsupportedFileType::UnsupportedFileType(const FileType file_type)
: std::runtime_error("unknown file type"), file_type{file_type} {}
: std::runtime_error("unknown file type: " +
file_type_to_string(file_type)),
file_type{file_type} {}

UnknownDecoderEngine::UnknownDecoderEngine()
: std::runtime_error("unknown decoder engine") {}

UnsupportedDecoderEngine::UnsupportedDecoderEngine(
const DecoderEngine decoder_engine)
: std::runtime_error("unsupported decoder engine: " +
decoder_engine_to_string(decoder_engine)),
decoder_engine{decoder_engine} {}

FileReadError::FileReadError() : std::runtime_error("file read error") {}

FileWriteError::FileWriteError(const std::string &path)
Expand Down
8 changes: 8 additions & 0 deletions src/odr/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace odr {
enum class FileType;
enum class DecoderEngine;

/// @brief Unsupported operation exception
struct UnsupportedOperation final : public std::runtime_error {
Expand Down Expand Up @@ -34,6 +35,13 @@ struct UnknownDecoderEngine final : public std::runtime_error {
UnknownDecoderEngine();
};

/// @brief Unsupported decoder engine exception
struct UnsupportedDecoderEngine final : public std::runtime_error {
DecoderEngine decoder_engine;

explicit UnsupportedDecoderEngine(DecoderEngine decoder_engine);
};

/// @brief File read error
struct FileReadError final : public std::runtime_error {
FileReadError();
Expand Down
37 changes: 19 additions & 18 deletions src/odr/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ void File::copy(const std::string &path) const {

std::shared_ptr<internal::abstract::File> File::impl() const { return m_impl; }

std::vector<FileType> DecodedFile::list_file_types(const std::string &path) {
std::vector<FileType> DecodedFile::list_file_types(const std::string &path,
Logger &logger) {
return internal::open_strategy::list_file_types(
std::make_shared<internal::common::DiskFile>(path));
std::make_shared<internal::common::DiskFile>(path), logger);
}

std::vector<DecoderEngine>
DecodedFile::list_decoder_engines(const std::string &path, FileType as) {
return internal::open_strategy::list_decoder_engines(
std::make_shared<internal::common::DiskFile>(path), as);
std::vector<DecoderEngine> DecodedFile::list_decoder_engines(FileType as) {
return internal::open_strategy::list_decoder_engines(as);
}

DecodedFile::DecodedFile(std::shared_ptr<internal::abstract::DecodedFile> impl)
Expand All @@ -78,24 +77,26 @@ DecodedFile::DecodedFile(std::shared_ptr<internal::abstract::DecodedFile> impl)
}
}

DecodedFile::DecodedFile(const File &file)
: DecodedFile(internal::open_strategy::open_file(file.impl())) {}
DecodedFile::DecodedFile(const File &file, Logger &logger)
: DecodedFile(internal::open_strategy::open_file(file.impl(), logger)) {}

DecodedFile::DecodedFile(const File &file, FileType as)
: DecodedFile(internal::open_strategy::open_file(file.impl(), as)) {}
DecodedFile::DecodedFile(const File &file, FileType as, Logger &logger)
: DecodedFile(internal::open_strategy::open_file(file.impl(), as, logger)) {
}

DecodedFile::DecodedFile(const std::string &path)
DecodedFile::DecodedFile(const std::string &path, Logger &logger)
: DecodedFile(internal::open_strategy::open_file(
std::make_shared<internal::common::DiskFile>(path))) {}
std::make_shared<internal::common::DiskFile>(path), logger)) {}

DecodedFile::DecodedFile(const std::string &path, FileType as)
DecodedFile::DecodedFile(const std::string &path, FileType as, Logger &logger)
: DecodedFile(internal::open_strategy::open_file(
std::make_shared<internal::common::DiskFile>(path), as)) {}
std::make_shared<internal::common::DiskFile>(path), as, logger)) {}

DecodedFile::DecodedFile(const std::string &path,
const DecodePreference &preference)
const DecodePreference &preference, Logger &logger)
: DecodedFile(internal::open_strategy::open_file(
std::make_shared<internal::common::DiskFile>(path), preference)) {}
std::make_shared<internal::common::DiskFile>(path), preference,
logger)) {}

File DecodedFile::file() const { return File(m_impl->file()); }

Expand Down Expand Up @@ -227,9 +228,9 @@ DocumentFile::DocumentFile(
std::shared_ptr<internal::abstract::DocumentFile> impl)
: DecodedFile(impl), m_impl{std::move(impl)} {}

DocumentFile::DocumentFile(const std::string &path)
DocumentFile::DocumentFile(const std::string &path, Logger &logger)
: DocumentFile(internal::open_strategy::open_document_file(
std::make_shared<internal::common::DiskFile>(path))) {}
std::make_shared<internal::common::DiskFile>(path), logger)) {}

DocumentType DocumentFile::document_type() const {
return m_impl->document_type();
Expand Down
24 changes: 15 additions & 9 deletions src/odr/file.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <odr/logger.hpp>

#include <cstdint>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -174,16 +176,19 @@ class File final {
/// @brief Represents a decoded file.
class DecodedFile {
public:
static std::vector<FileType> list_file_types(const std::string &path);
static std::vector<DecoderEngine>
list_decoder_engines(const std::string &path, FileType as);
static std::vector<FileType> list_file_types(const std::string &path,
Logger &logger = Logger::null());
static std::vector<DecoderEngine> list_decoder_engines(FileType as);

explicit DecodedFile(std::shared_ptr<internal::abstract::DecodedFile> impl);
explicit DecodedFile(const File &file);
DecodedFile(const File &file, FileType as);
explicit DecodedFile(const std::string &path);
DecodedFile(const std::string &path, FileType as);
DecodedFile(const std::string &path, const DecodePreference &preference);
explicit DecodedFile(const File &file, Logger &logger = Logger::null());
DecodedFile(const File &file, FileType as, Logger &logger = Logger::null());
explicit DecodedFile(const std::string &path,
Logger &logger = Logger::null());
DecodedFile(const std::string &path, FileType as,
Logger &logger = Logger::null());
DecodedFile(const std::string &path, const DecodePreference &preference,
Logger &logger = Logger::null());

[[nodiscard]] File file() const;

Expand Down Expand Up @@ -255,7 +260,8 @@ class DocumentFile final : public DecodedFile {
static FileMeta meta(const std::string &path);

explicit DocumentFile(std::shared_ptr<internal::abstract::DocumentFile>);
explicit DocumentFile(const std::string &path);
explicit DocumentFile(const std::string &path,
Logger &logger = Logger::null());

[[nodiscard]] DocumentType document_type() const;
[[nodiscard]] DocumentMeta document_meta() const;
Expand Down
72 changes: 46 additions & 26 deletions src/odr/html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,23 @@ void HtmlResource::write_resource(std::ostream &os) const {

HtmlService html::translate(const DecodedFile &decoded_file,
const std::string &output_path,
const HtmlConfig &config) {
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
if (decoded_file.is_text_file()) {
return translate(decoded_file.as_text_file(), output_path, config);
return translate(decoded_file.as_text_file(), output_path, config,
std::move(logger));
} else if (decoded_file.is_image_file()) {
return translate(decoded_file.as_image_file(), output_path, config);
return translate(decoded_file.as_image_file(), output_path, config,
std::move(logger));
} else if (decoded_file.is_archive_file()) {
return translate(decoded_file.as_archive_file(), output_path, config);
return translate(decoded_file.as_archive_file(), output_path, config,
std::move(logger));
} else if (decoded_file.is_document_file()) {
return translate(decoded_file.as_document_file(), output_path, config);
return translate(decoded_file.as_document_file(), output_path, config,
std::move(logger));
} else if (decoded_file.is_pdf_file()) {
return translate(decoded_file.as_pdf_file(), output_path, config);
return translate(decoded_file.as_pdf_file(), output_path, config,
std::move(logger));
}

throw UnsupportedFileType(decoded_file.file_type());
Expand Down Expand Up @@ -257,75 +263,89 @@ HtmlResourceLocator html::standard_resource_locator() {

HtmlService html::translate(const TextFile &text_file,
const std::string &output_path,
const HtmlConfig &config) {
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
std::filesystem::create_directories(output_path);
return internal::html::create_text_service(text_file, output_path, config);
return internal::html::create_text_service(text_file, output_path, config,
std::move(logger));
}

HtmlService html::translate(const ImageFile &image_file,
const std::string &output_path,
const HtmlConfig &config) {
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
std::filesystem::create_directories(output_path);
return internal::html::create_image_service(image_file, output_path, config);
return internal::html::create_image_service(image_file, output_path, config,
std::move(logger));
}

HtmlService html::translate(const ArchiveFile &archive_file,
const std::string &output_path,
const HtmlConfig &config) {
return translate(archive_file.archive(), output_path, config);
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
return translate(archive_file.archive(), output_path, config,
std::move(logger));
}

HtmlService html::translate(const DocumentFile &document_file,
const std::string &output_path,
const HtmlConfig &config) {
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
auto document_file_impl = document_file.impl();

#ifdef ODR_WITH_WVWARE
if (auto wv_document_file =
std::dynamic_pointer_cast<internal::WvWareLegacyMicrosoftFile>(
document_file_impl)) {
std::filesystem::create_directories(output_path);
return internal::html::create_wvware_oldms_service(*wv_document_file,
output_path, config);
return internal::html::create_wvware_oldms_service(
*wv_document_file, output_path, config, std::move(logger));
}
#endif

return translate(document_file.document(), output_path, config);
return translate(document_file.document(), output_path, config,
std::move(logger));
}

HtmlService html::translate(const PdfFile &pdf_file,
const std::string &output_path,
const HtmlConfig &config) {
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
auto pdf_file_impl = pdf_file.impl();

#ifdef ODR_WITH_PDF2HTMLEX
if (auto poppler_pdf_file =
std::dynamic_pointer_cast<internal::PopplerPdfFile>(pdf_file_impl)) {
std::filesystem::create_directories(output_path);
return internal::html::create_poppler_pdf_service(*poppler_pdf_file,
output_path, config);
return internal::html::create_poppler_pdf_service(
*poppler_pdf_file, output_path, config, std::move(logger));
}
#endif

return internal::html::create_pdf_service(pdf_file, output_path, config);
return internal::html::create_pdf_service(pdf_file, output_path, config,
std::move(logger));
}

HtmlService html::translate(const Archive &archive,
const std::string &output_path,
const HtmlConfig &config) {
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
std::filesystem::create_directories(output_path);
return internal::html::create_filesystem_service(archive.as_filesystem(),
output_path, config);
return internal::html::create_filesystem_service(
archive.as_filesystem(), output_path, config, std::move(logger));
}

HtmlService html::translate(const Document &document,
const std::string &output_path,
const HtmlConfig &config) {
const HtmlConfig &config,
std::shared_ptr<Logger> logger) {
std::filesystem::create_directories(output_path);
return internal::html::create_document_service(document, output_path, config);
return internal::html::create_document_service(document, output_path, config,
std::move(logger));
}

void html::edit(const Document &document, const char *diff) {
void html::edit(const Document &document, const char *diff,
Logger & /*logger*/) {
auto json = nlohmann::json::parse(diff);
for (const auto &[key, value] : json["modifiedText"].items()) {
auto element =
Expand Down
Loading