From de5437643580173452eb0ab838e2033579604efc Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 9 Mar 2020 23:50:25 +0100 Subject: switch to proper base64 encoding of preview images, add mime type logic --- src/main.c | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index ff46a7e..ba1b6dc 100644 --- a/src/main.c +++ b/src/main.c @@ -1557,9 +1557,8 @@ launch_payment (struct Product *product) /* create the json object for the order request */ if (NULL != product->preview) { - orderReq = json_pack ("{ s:{s:s}, s:s, s:o, s:s, s:o }", + orderReq = json_pack ("{ s:s, s:s, s:o, s:s, s:o }", "summary", - "_", product->description, "preview", product->preview, "amount", TALER_JSON_from_amount (&product->price), @@ -1570,9 +1569,8 @@ launch_payment (struct Product *product) } else { - orderReq = json_pack ("{ s:{s:s}, s:o, s:s, s:o }", + orderReq = json_pack ("{ s:s}, s:o, s:s, s:o }", "summary", - "_", product->description, "amount", TALER_JSON_from_amount (&product->price), "fulfillment_url", fulflmntUrl @@ -2036,7 +2034,7 @@ write_mdb_command (void *cls) chkSum += mdb.txBuffer[idx] = mdb.cmd->cmd.bin[idx]; for (size_t idx = 0; idx < mdb.cmd->data.bin_size; idx++) chkSum += mdb.txBuffer[idx + mdb.cmd->cmd.bin_size] = - mdb.cmd->data.bin[idx]; + mdb.cmd->data.bin[idx]; mdb.txBuffer[mdb.cmd->cmd.bin_size + mdb.cmd->data.bin_size] = (uint8_t) (chkSum & 0xFF); } @@ -2715,9 +2713,42 @@ read_products (void *cls, } else { - tmpProduct.preview - = GNUNET_STRINGS_data_to_string_alloc (thumb, - len); + const char *mime_type = ""; + const char *ext; + size_t preview_len; + char *base64; + + ext = strrchr (thumbnail_fn, '.'); + if (NULL != ext) + { + static const struct + { + const char *ext; + const char *mime; + } mimes[] = { + { ".png", "image/png" }, + { ".jpg", "image/jpeg" }, + { ".jpeg", "image/jpeg" }, + { ".svg", "image/svg" }, + { NULL, NULL } + }; + + for (unsigned int i = 0; NULL != mimes[i].ext; i++) + if (0 == strcasecmp (mimes[i].ext, + ext)) + { + mime_type = mimes[i].mime; + break; + } + } + (void) GNUNET_STRINGS_base64_encode (thumb, + len, + &base64); + GNUNET_asprintf (&tmpProduct.preview, + "data:%s;base64,%s", + mime_type, + base64); + GNUNET_free (base64); } GNUNET_free (thumb); } -- cgit v1.2.3