Skip to content

Commit

Permalink
first lazy pdf loading version
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Jan 8, 2025
1 parent 1ce6579 commit 826110f
Show file tree
Hide file tree
Showing 18 changed files with 399 additions and 215 deletions.
19 changes: 16 additions & 3 deletions cli/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ int main(int argc, char **argv) {
password = argv[2];
}

DecodedFile decoded_file{input};
DecodePreference decode_preference;
decode_preference.engine_priority = {
DecoderEngine::poppler, DecoderEngine::wvware, DecoderEngine::odr};

DecodedFile decoded_file{input, decode_preference};

if (decoded_file.is_document_file()) {
DocumentFile document_file = decoded_file.document_file();
Expand All @@ -32,8 +36,17 @@ int main(int argc, char **argv) {
HttpServer::Config config;
HttpServer server(config);

std::string id = server.host_file(File(input));
std::cout << "hosted file with id: " << id << std::endl;
{
std::string id = server.host_file(File(input));
std::cout << "hosted file with id: " << id << std::endl;
std::cout << "http://localhost:8080/" << id << std::endl;
}

{
std::string id = server.host_file(decoded_file);
std::cout << "hosted decoded file with id: " << id << std::endl;
std::cout << "http://localhost:8080/" << id << std::endl;
}

server.listen("localhost", 8080);

Expand Down
13 changes: 6 additions & 7 deletions src/odr/html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <nlohmann/json.hpp>

using namespace odr::internal;
namespace fs = std::filesystem;

namespace odr {

Expand Down Expand Up @@ -96,13 +95,13 @@ Html html::translate(const DecodedFile &decoded_file,

Html html::translate(const TextFile &text_file, const std::string &output_path,
const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_text_file(text_file, output_path, config);
}

Html html::translate(const ImageFile &image_file,
const std::string &output_path, const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_image_file(image_file, output_path, config);
}

Expand All @@ -119,7 +118,7 @@ Html html::translate(const DocumentFile &document_file,
if (auto wv_document_file =
std::dynamic_pointer_cast<internal::WvWareLegacyMicrosoftFile>(
document_file_impl)) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_wvware_oldms_file(*wv_document_file,
output_path, config);
}
Expand All @@ -135,7 +134,7 @@ Html html::translate(const PdfFile &pdf_file, const std::string &output_path,
#ifdef ODR_WITH_PDF2HTMLEX
if (auto poppler_pdf_file =
std::dynamic_pointer_cast<internal::PopplerPdfFile>(pdf_file_impl)) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_poppler_pdf_file(*poppler_pdf_file,
output_path, config);
}
Expand All @@ -146,14 +145,14 @@ Html html::translate(const PdfFile &pdf_file, const std::string &output_path,

Html html::translate(const Archive &archive, const std::string &output_path,
const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_filesystem(
FileType::unknown, archive.filesystem(), output_path, config);
}

Html html::translate(const Document &document, const std::string &output_path,
const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_document(document, output_path, config);
}

Expand Down
61 changes: 31 additions & 30 deletions src/odr/html_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@

namespace odr {

HtmlService::HtmlService(std::shared_ptr<internal::abstract::HtmlService> impl)
: m_impl{std::move(impl)} {}
HtmlDocumentService::HtmlDocumentService() = default;

const HtmlConfig &HtmlService::config() const { return m_impl->config(); }
HtmlDocumentService::HtmlDocumentService(
std::shared_ptr<internal::abstract::HtmlDocumentService> impl)
: m_impl{std::move(impl)} {}

const HtmlResourceLocator &HtmlService::resource_locator() const {
return m_impl->resource_locator();
const HtmlConfig &HtmlDocumentService::config() const {
return m_impl->config();
}

std::vector<HtmlFragment> HtmlService::fragments() const {
std::vector<HtmlFragment> result;
for (const auto &fragment : m_impl->fragments()) {
result.emplace_back(fragment);
}
return result;
const HtmlResourceLocator &HtmlDocumentService::resource_locator() const {
return m_impl->resource_locator();
}

HtmlResources HtmlService::write_document(std::ostream &os) const {
HtmlResources HtmlDocumentService::write_document(std::ostream &os) const {
internal::html::HtmlWriter out(os, config());

auto internal_resources = m_impl->write_document(out);
Expand All @@ -36,35 +33,33 @@ HtmlResources HtmlService::write_document(std::ostream &os) const {
return resources;
}

HtmlFragment::HtmlFragment(
std::shared_ptr<internal::abstract::HtmlFragment> impl)
: m_impl{std::move(impl)} {}
const std::shared_ptr<internal::abstract::HtmlDocumentService> &
HtmlDocumentService::impl() const {
return m_impl;
}

std::string HtmlFragment::name() const { return m_impl->name(); }
HtmlFragmentService::HtmlFragmentService(
std::shared_ptr<internal::abstract::HtmlFragmentService> impl)
: m_impl{std::move(impl)} {}

const HtmlConfig &HtmlFragment::config() const { return m_impl->config(); }
const HtmlConfig &HtmlFragmentService::config() const {
return m_impl->config();
}

const HtmlResourceLocator &HtmlFragment::resource_locator() const {
const HtmlResourceLocator &HtmlFragmentService::resource_locator() const {
return m_impl->resource_locator();
}

void HtmlFragment::write_fragment(std::ostream &os,
HtmlResources &resources) const {
void HtmlFragmentService::write_fragment(std::ostream &os,
HtmlResources &resources) const {
internal::html::HtmlWriter out(os, config());

m_impl->write_fragment(out, resources);
}

HtmlResources HtmlFragment::write_document(std::ostream &os) const {
internal::html::HtmlWriter out(os, config());

auto internal_resources = m_impl->write_document(out);

HtmlResources resources;
for (const auto &[resource, location] : internal_resources) {
resources.emplace_back(HtmlResource(resource), location);
}
return resources;
const std::shared_ptr<internal::abstract::HtmlFragmentService> &
HtmlFragmentService::impl() const {
return m_impl;
}

HtmlResource::HtmlResource() = default;
Expand All @@ -75,6 +70,10 @@ HtmlResource::HtmlResource(

HtmlResourceType HtmlResource::type() const { return m_impl->type(); }

const std::string &HtmlResource::mime_type() const {
return m_impl->mime_type();
}

const std::string &HtmlResource::name() const { return m_impl->name(); }

const std::string &HtmlResource::path() const { return m_impl->path(); }
Expand All @@ -85,6 +84,8 @@ bool HtmlResource::is_shipped() const { return m_impl->is_shipped(); }

bool HtmlResource::is_relocatable() const { return m_impl->is_relocatable(); }

bool HtmlResource::is_external() const { return m_impl->is_external(); }

void HtmlResource::write_resource(std::ostream &os) const {
m_impl->write_resource(os);
}
Expand Down
32 changes: 18 additions & 14 deletions src/odr/html_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <vector>

namespace odr::internal::abstract {
class HtmlService;
class HtmlFragment;
class HtmlDocumentService;
class HtmlFragmentService;
class HtmlResource;
} // namespace odr::internal::abstract

Expand All @@ -18,7 +18,6 @@ enum class FileType;
class File;
struct HtmlConfig;

class HtmlFragment;
class HtmlResource;

enum class HtmlResourceType {
Expand All @@ -35,36 +34,39 @@ using HtmlResourceLocator =
using HtmlResources =
std::vector<std::pair<HtmlResource, HtmlResourceLocation>>;

class HtmlService final {
class HtmlDocumentService final {
public:
explicit HtmlService(std::shared_ptr<internal::abstract::HtmlService> impl);
HtmlDocumentService();
explicit HtmlDocumentService(
std::shared_ptr<internal::abstract::HtmlDocumentService> impl);

[[nodiscard]] const HtmlConfig &config() const;
[[nodiscard]] const HtmlResourceLocator &resource_locator() const;

[[nodiscard]] std::vector<HtmlFragment> fragments() const;

HtmlResources write_document(std::ostream &os) const;

[[nodiscard]] const std::shared_ptr<internal::abstract::HtmlDocumentService> &
impl() const;

private:
std::shared_ptr<internal::abstract::HtmlService> m_impl;
std::shared_ptr<internal::abstract::HtmlDocumentService> m_impl;
};

class HtmlFragment final {
class HtmlFragmentService final {
public:
explicit HtmlFragment(std::shared_ptr<internal::abstract::HtmlFragment> impl);

[[nodiscard]] std::string name() const;
explicit HtmlFragmentService(
std::shared_ptr<internal::abstract::HtmlFragmentService> impl);

[[nodiscard]] const HtmlConfig &config() const;
[[nodiscard]] const HtmlResourceLocator &resource_locator() const;

void write_fragment(std::ostream &os, HtmlResources &resources) const;

HtmlResources write_document(std::ostream &os) const;
[[nodiscard]] const std::shared_ptr<internal::abstract::HtmlFragmentService> &
impl() const;

private:
std::shared_ptr<internal::abstract::HtmlFragment> m_impl;
std::shared_ptr<internal::abstract::HtmlFragmentService> m_impl;
};

class HtmlResource final {
Expand All @@ -73,11 +75,13 @@ class HtmlResource final {
explicit HtmlResource(std::shared_ptr<internal::abstract::HtmlResource> impl);

[[nodiscard]] HtmlResourceType type() const;
[[nodiscard]] const std::string &mime_type() const;
[[nodiscard]] const std::string &name() const;
[[nodiscard]] const std::string &path() const;
[[nodiscard]] const File &file() const;
[[nodiscard]] bool is_shipped() const;
[[nodiscard]] bool is_relocatable() const;
[[nodiscard]] bool is_external() const;

void write_resource(std::ostream &os) const;

Expand Down
Loading

0 comments on commit 826110f

Please sign in to comment.