Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU Affero General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
12 :
13 : You should have received a copy of the GNU Affero General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file src/backend/taler-merchant-httpd_get-products-IMAGE_HASH-image.c
18 : * @brief implement GET /products/$HASH/image
19 : * @author Bohdan Potuzhnyi
20 : */
21 : #include "platform.h"
22 : #include "taler-merchant-httpd_get-products-IMAGE_HASH-image.h"
23 : #include "taler-merchant-httpd_helper.h"
24 : #include <taler/taler_error_codes.h>
25 : #include "merchant-database/get_product_image_by_hash.h"
26 :
27 :
28 : enum MHD_Result
29 8 : TMH_get_products_image (const struct TMH_RequestHandler *rh,
30 : struct MHD_Connection *connection,
31 : struct TMH_HandlerContext *hc)
32 : {
33 8 : struct TMH_MerchantInstance *mi = hc->instance;
34 8 : const char *image_hash = hc->infix;
35 8 : char *image = NULL;
36 : enum GNUNET_DB_QueryStatus qs;
37 : (void) rh;
38 :
39 : {
40 : /* Just simple check if the string is what we really expect */
41 8 : size_t ih_len = strlen (image_hash);
42 :
43 8 : if ( (sizeof (struct GNUNET_ShortHashCode) * 2) != ih_len)
44 4 : return TALER_MHD_reply_with_error (connection,
45 : MHD_HTTP_BAD_REQUEST,
46 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
47 : "image_hash");
48 :
49 260 : for (size_t i = 0; i < ih_len; i++)
50 256 : if (! isxdigit ((unsigned char) image_hash[i]))
51 0 : return TALER_MHD_reply_with_error (connection,
52 : MHD_HTTP_BAD_REQUEST,
53 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
54 : "image_hash");
55 : }
56 :
57 4 : qs = TALER_MERCHANTDB_get_product_image_by_hash (TMH_db,
58 4 : mi->settings.id,
59 : image_hash,
60 : &image);
61 4 : if (0 > qs)
62 : {
63 0 : GNUNET_break (0);
64 0 : return TALER_MHD_reply_with_error (connection,
65 : MHD_HTTP_INTERNAL_SERVER_ERROR,
66 : TALER_EC_GENERIC_DB_FETCH_FAILED,
67 : "get_product_image_by_hash");
68 : }
69 4 : if ( (0 == qs) ||
70 2 : (NULL == image) )
71 : {
72 2 : return TALER_MHD_reply_with_error (connection,
73 : MHD_HTTP_NOT_FOUND,
74 : TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
75 : image_hash);
76 : }
77 :
78 : {
79 : enum MHD_Result ret;
80 :
81 2 : ret = TALER_MHD_REPLY_JSON_PACK (connection,
82 : MHD_HTTP_OK,
83 : GNUNET_JSON_pack_string ("image",
84 : image));
85 2 : GNUNET_free (image);
86 2 : return ret;
87 : }
88 : }
|