Skip to content

Commit

Permalink
Implement attachment query
Browse files Browse the repository at this point in the history
  • Loading branch information
MetroWind committed Sep 25, 2024
1 parent 1cbcd38 commit b39032e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include <ctime>
#include <exception>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <memory>
#include <regex>
#include <sstream>
Expand Down Expand Up @@ -572,6 +574,34 @@ void App::handleAttachments(const httplib::Request& req, httplib::Response& res)
res.set_content(result, "text/html");
}

void App::handleAttachment(const httplib::Request& req, httplib::Response& res)
{
ASSIGN_OR_RESPOND_ERROR(
std::optional<Attachment> att,
data->getAttachment(req.path_params.at("hash")),
res);
if(!att.has_value())
{
res.status = 404;
res.set_content("Attachment not found", "text/plain");
return;
}

namespace fs = std::filesystem;
auto path = fs::path(config.attachment_dir) / attachment_manager.path(*att);
if(!fs::exists(path))
{
res.status = 404;
res.set_content("File not found", "text/plain");
return;
}

std::ifstream file(path);
std::string data(std::istreambuf_iterator<char>{file}, {});
file.close();
res.set_content(data, att->content_type);
}

nlohmann::json App::postToJson(const Post& p) const
{
nlohmann::json result;
Expand Down Expand Up @@ -737,6 +767,11 @@ void App::start()
{
handleAttachments(req, res);
});
server.Get(getPath("attachment", "hash/:_"),
[&](const httplib::Request& req, httplib::Response& res)
{
handleAttachment(req, res);
});

spdlog::info("Listening at http://{}:{}/...", config.listen_address,
config.listen_port);
Expand Down
1 change: 1 addition & 0 deletions src/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class App
void handlePublishFromNewDraft(const httplib::Request& req,
httplib::Response& res) const;
void handleAttachments(const httplib::Request& req, httplib::Response& res);
void handleAttachment(const httplib::Request& req, httplib::Response& res);

void start();
void stop();
Expand Down
23 changes: 23 additions & 0 deletions src/app_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class UserAppTest : public testing::Test
config.listen_address = "localhost";
config.listen_port = 8080;
config.data_dir = ".";
config.attachment_dir = "test-data";
config.blog_title = "Test Blog";

auto auth = std::make_unique<AuthMock>();
Expand Down Expand Up @@ -339,3 +340,25 @@ TEST_F(UserAppTest, CanHandleAttachments)
app->stop();
app->wait();
}

TEST_F(UserAppTest, CanHandleAttachment)
{
Attachment att;
att.content_type = "image/png";
att.hash = "xyz";
att.original_name = "test.png";
att.upload_time = Clock::now();

EXPECT_CALL(*data_source, getAttachment("xyz")).WillOnce(Return(att));
app->start();
{
HTTPSession client;
ASSIGN_OR_FAIL(const HTTPResponse* res,
client.get("http://localhost:8080/blog/attachment/xyz/test.png"));

EXPECT_EQ(res->status, 200) << "Response body: " << res->payloadAsStr();
EXPECT_EQ(res->payloadAsStr().size(), 313);
}
app->stop();
app->wait();
}
4 changes: 4 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ E<Configuration> Configuration::fromYaml(const std::filesystem::path& path)
{
tree["data-dir"] >> config.data_dir;
}
if(tree["attachment-dir"].has_key())
{
tree["attachment-dir"] >> config.attachment_dir;
}
if(tree["listen-address"].has_key())
{
tree["listen-address"] >> config.listen_address;
Expand Down
1 change: 1 addition & 0 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Configuration
using StringMap = std::unordered_map<std::string, std::string>;

std::string data_dir;
std::string attachment_dir;
std::string listen_address;
int listen_port;
std::string client_id;
Expand Down
Binary file added test-data/x/xyz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b39032e

Please sign in to comment.