ORE Studio 0.0.4
Loading...
Searching...
No Matches
image_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_ASSETS_MESSAGING_IMAGE_HANDLER_HPP
21#define ORES_ASSETS_MESSAGING_IMAGE_HANDLER_HPP
22
23#include <optional>
24#include <vector>
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"
36
37namespace ores::assets::messaging {
38
39namespace {
40inline auto& image_handler_lg() {
41 static auto instance = ores::logging::make_logger(
42 "ores.assets.messaging.image_handler");
43 return instance;
44}
45} // namespace
46
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;
52using namespace ores::logging;
53
54class image_handler {
55public:
56 image_handler(ores::nats::service::client& nats,
58 std::optional<ores::security::jwt::jwt_authenticator> verifier)
59 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
60
61 void get(ores::nats::message msg) {
62 BOOST_LOG_SEV(image_handler_lg(), debug) << "Handling " << msg.subject;
63 auto req = decode<get_images_request>(msg);
64 if (!req) {
65 BOOST_LOG_SEV(image_handler_lg(), warn) << "Failed to decode: " << msg.subject;
66 return;
67 }
68 auto ctx_expected = ores::service::service::make_request_context(
69 ctx_, msg, verifier_);
70 if (!ctx_expected) {
71 error_reply(nats_, msg, ctx_expected.error());
72 return;
73 }
74 const auto& ctx = *ctx_expected;
75 try {
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()});
83 }
84 }
85
86 void list(ores::nats::message msg) {
87 BOOST_LOG_SEV(image_handler_lg(), debug) << "Handling " << msg.subject;
88 auto req = decode<list_images_request>(msg);
89 if (!req) {
90 BOOST_LOG_SEV(image_handler_lg(), warn) << "Failed to decode: " << msg.subject;
91 return;
92 }
93 auto ctx_expected = ores::service::service::make_request_context(
94 ctx_, msg, verifier_);
95 if (!ctx_expected) {
96 error_reply(nats_, msg, ctx_expected.error());
97 return;
98 }
99 const auto& ctx = *ctx_expected;
100 try {
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);
105 else
106 images = repo.read_latest(ctx);
107 list_images_response resp;
108 resp.success = true;
109 resp.images.reserve(images.size());
110 for (const auto& img : images) {
111 image_info info;
112 info.image_id = boost::uuids::to_string(img.image_id);
113 info.key = img.key;
114 info.description = img.description;
115 resp.images.push_back(std::move(info));
116 }
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()});
122 }
123 }
124
125 void save(ores::nats::message msg) {
126 BOOST_LOG_SEV(image_handler_lg(), debug) << "Handling " << msg.subject;
127 auto req = decode<save_image_request>(msg);
128 if (!req) {
129 BOOST_LOG_SEV(image_handler_lg(), warn) << "Failed to decode: " << msg.subject;
130 return;
131 }
132 auto ctx_expected = ores::service::service::make_request_context(
133 ctx_, msg, verifier_);
134 if (!ctx_expected) {
135 error_reply(nats_, msg, ctx_expected.error());
136 return;
137 }
138 const auto& ctx = *ctx_expected;
139 if (!has_permission(ctx, "assets::images:write")) {
140 error_reply(nats_, msg, ores::service::error_code::forbidden);
141 return;
142 }
143 try {
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()});
152 }
153 }
154
155private:
158 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
159};
160
161} // namespace ores::assets::messaging
162
163#endif
STL namespace.
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