merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-httpd_get-products-image.c (3280B)


      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 taler-merchant-httpd_get-products-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.h"
     23 #include "taler-merchant-httpd_helper.h"
     24 #include <taler/taler_error_codes.h>
     25 
     26 
     27 MHD_RESULT
     28 TMH_get_products_image (const struct TMH_RequestHandler *rh,
     29                         struct MHD_Connection *connection,
     30                         struct TMH_HandlerContext *hc)
     31 {
     32   struct TMH_MerchantInstance *mi = hc->instance;
     33   const char *image_hash = hc->infix;
     34   char *image = NULL;
     35   enum GNUNET_DB_QueryStatus qs;
     36   (void) rh;
     37 
     38   {
     39     /* Just simple check if the string is what we really expect */
     40     size_t ih_len = strlen (image_hash);
     41 
     42     if ( (sizeof (struct GNUNET_ShortHashCode) * 2) != ih_len)
     43       return TALER_MHD_reply_with_error (connection,
     44                                          MHD_HTTP_BAD_REQUEST,
     45                                          TALER_EC_GENERIC_PARAMETER_MALFORMED,
     46                                          "image_hash");
     47 
     48     for (size_t i = 0; i < ih_len; i++)
     49       if (! isxdigit ((unsigned char) image_hash[i]))
     50         return TALER_MHD_reply_with_error (connection,
     51                                            MHD_HTTP_BAD_REQUEST,
     52                                            TALER_EC_GENERIC_PARAMETER_MALFORMED,
     53                                            "image_hash");
     54   }
     55 
     56   qs = TMH_db->lookup_product_image_by_hash (TMH_db->cls,
     57                                              mi->settings.id,
     58                                              image_hash,
     59                                              &image);
     60   if (0 > qs)
     61   {
     62     GNUNET_break (0);
     63     return TALER_MHD_reply_with_error (connection,
     64                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     65                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
     66                                        "lookup_product_image_by_hash");
     67   }
     68   if ( (0 == qs) ||
     69        (NULL == image) )
     70   {
     71     return TALER_MHD_reply_with_error (connection,
     72                                        MHD_HTTP_NOT_FOUND,
     73                                        TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
     74                                        image_hash);
     75   }
     76 
     77   {
     78     MHD_RESULT ret;
     79 
     80     ret = TALER_MHD_REPLY_JSON_PACK (connection,
     81                                      MHD_HTTP_OK,
     82                                      GNUNET_JSON_pack_string ("image",
     83                                                               image));
     84     GNUNET_free (image);
     85     return ret;
     86   }
     87 }