20#ifndef ORES_ASSETS_MESSAGING_IMAGE_HANDLER_HPP
21#define ORES_ASSETS_MESSAGING_IMAGE_HANDLER_HPP
25#include <boost/uuid/uuid_io.hpp>
26#include "ores.logging/make_logger.hpp"
27#include "ores.nats/domain/message.hpp"
28#include "ores.nats/service/client.hpp"
29#include "ores.database/domain/context.hpp"
30#include "ores.security/jwt/jwt_authenticator.hpp"
31#include "ores.assets.api/messaging/assets_protocol.hpp"
32#include "ores.assets.core/repository/image_repository.hpp"
33#include "ores.assets.core/service/assets_service.hpp"
34#include "ores.service/messaging/handler_helpers.hpp"
35#include "ores.service/service/request_context.hpp"
37namespace ores::assets::messaging {
40inline auto& image_handler_lg() {
41 static auto instance = ores::logging::make_logger(
42 "ores.assets.messaging.image_handler");
47using ores::service::messaging::reply;
48using ores::service::messaging::decode;
49using ores::service::messaging::stamp;
50using ores::service::messaging::error_reply;
51using ores::service::messaging::has_permission;
58 std::optional<ores::security::jwt::jwt_authenticator> verifier)
59 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
62 BOOST_LOG_SEV(image_handler_lg(), debug) <<
"Handling " << msg.
subject;
63 auto req = decode<get_images_request>(msg);
65 BOOST_LOG_SEV(image_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
68 auto ctx_expected = ores::service::service::make_request_context(
69 ctx_, msg, verifier_);
71 error_reply(nats_, msg, ctx_expected.error());
74 const auto& ctx = *ctx_expected;
76 service::assets_service svc(ctx);
77 const auto images = svc.get_images(req->image_ids);
78 BOOST_LOG_SEV(image_handler_lg(), debug) <<
"Completed " << msg.
subject;
79 reply(nats_, msg, get_images_response{
true, {}, images});
80 }
catch (
const std::exception& e) {
81 BOOST_LOG_SEV(image_handler_lg(), error) << msg.
subject <<
" failed: " << e.what();
82 reply(nats_, msg, get_images_response{
false, e.what()});
87 BOOST_LOG_SEV(image_handler_lg(), debug) <<
"Handling " << msg.
subject;
88 auto req = decode<list_images_request>(msg);
90 BOOST_LOG_SEV(image_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
93 auto ctx_expected = ores::service::service::make_request_context(
94 ctx_, msg, verifier_);
96 error_reply(nats_, msg, ctx_expected.error());
99 const auto& ctx = *ctx_expected;
101 repository::image_repository repo;
102 std::vector<domain::image> images;
103 if (req->modified_since.has_value())
104 images = repo.read_latest_since(ctx, *req->modified_since);
106 images = repo.read_latest(ctx);
107 list_images_response resp;
109 resp.images.reserve(images.size());
110 for (
const auto& img : images) {
112 info.image_id = boost::uuids::to_string(img.image_id);
114 info.description = img.description;
115 resp.images.push_back(std::move(info));
117 BOOST_LOG_SEV(image_handler_lg(), debug) <<
"Completed " << msg.
subject;
118 reply(nats_, msg, resp);
119 }
catch (
const std::exception& e) {
120 BOOST_LOG_SEV(image_handler_lg(), error) << msg.
subject <<
" failed: " << e.what();
121 reply(nats_, msg, list_images_response{
false, e.what()});
126 BOOST_LOG_SEV(image_handler_lg(), debug) <<
"Handling " << msg.
subject;
127 auto req = decode<save_image_request>(msg);
129 BOOST_LOG_SEV(image_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
132 auto ctx_expected = ores::service::service::make_request_context(
133 ctx_, msg, verifier_);
135 error_reply(nats_, msg, ctx_expected.error());
138 const auto& ctx = *ctx_expected;
139 if (!has_permission(ctx,
"assets::images:write")) {
144 repository::image_repository repo;
145 stamp(req->data, ctx);
146 repo.write(ctx, req->data);
147 BOOST_LOG_SEV(image_handler_lg(), debug) <<
"Completed " << msg.
subject;
148 reply(nats_, msg, save_image_response{
true, {}});
149 }
catch (
const std::exception& e) {
150 BOOST_LOG_SEV(image_handler_lg(), error) << msg.
subject <<
" failed: " << e.what();
151 reply(nats_, msg, save_image_response{
false, e.what()});
158 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73