Skip to content

Commit

Permalink
remove caching
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Jan 8, 2025
1 parent 95c0790 commit f2bc58e
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 deletions src/odr/http_server.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#include <odr/http_server.hpp>

#include <odr/document.hpp>
#include <odr/file.hpp>
#include <odr/filesystem.hpp>
#include <odr/html.hpp>
#include <odr/html_service.hpp>
#include <odr/internal/html/document.hpp>
#include <odr/internal/html/pdf2htmlex_wrapper.hpp>
#include <odr/internal/pdf_poppler/poppler_pdf_file.hpp>
#include <odr/internal/util/file_util.hpp>
#include <odr/internal/util/stream_util.hpp>

#include <random>

Expand Down Expand Up @@ -65,10 +62,8 @@ class HttpServer::Impl {
HtmlConfig config;
config.embed_resources = false;
std::string output_path = "/tmp/" + id;
std::string cache_path = output_path + "/cache";

std::filesystem::create_directories(output_path);
std::filesystem::create_directories(cache_path);

HtmlDocumentService html_service;

Expand All @@ -92,14 +87,8 @@ class HttpServer::Impl {
throw std::runtime_error("Unsupported file type");
}

std::string document_html_path = cache_path + "/document.html";
HtmlResources resources;
{
std::ofstream ostream(document_html_path);
resources = html_service.write_document(ostream);
}
std::size_t document_html_size =
internal::util::file::size(document_html_path);
std::ofstream null;
HtmlResources resources = html_service.write_document(null);

m_server.Get("/" + id,
[id](const httplib::Request &req, httplib::Response &res) {
Expand All @@ -108,25 +97,16 @@ class HttpServer::Impl {

m_server.Get("/" + id + "/document.html", [=](const httplib::Request &req,
httplib::Response &res) {
httplib::ContentProvider content_provider =
[document_html_path,
document_html_size](std::size_t offset, std::size_t length,
httplib::DataSink &sink) -> bool {
httplib::ContentProviderWithoutLength content_provider =
[html_service](std::size_t offset, httplib::DataSink &sink) -> bool {
if (offset != 0) {
throw std::runtime_error("Invalid offset: " + std::to_string(offset) +
". Must be 0.");
}
if (length != document_html_size) {
throw std::runtime_error("Invalid length: " + std::to_string(length) +
". Must be " +
std::to_string(document_html_size) + ".");
}
std::ifstream in(document_html_path);
internal::util::stream::pipe(in, sink.os);
html_service.write_document(sink.os);
return false;
};
res.set_content_provider(document_html_size, "text/html",
content_provider);
res.set_content_provider("text/html", content_provider);
});

for (const auto &[resource, location] : resources) {
Expand Down

0 comments on commit f2bc58e

Please sign in to comment.