Add image support

This commit is contained in:
Markus Koch 2019-07-26 20:38:33 +02:00
parent 0527614795
commit 51f7fc0394
4 changed files with 67 additions and 4 deletions

View File

@ -179,9 +179,47 @@ void yacos_cart_item_fetch_meta(YacosCartItem *self)
// TODO: The soup 's probably memory-leaking all over the place. Check this some time.. // TODO: The soup 's probably memory-leaking all over the place. Check this some time..
} }
static void yacos_cart_item_fetch_image_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
YacosCartItem *self = YACOS_CART_ITEM(user_data);
GdkPixbuf *pixbuf_raw, *pixbuf_scaled;
GMemoryInputStream *s;
if (msg->status_code != 200) {
g_warning("image: Received error code %d from server.", msg->status_code);
} else {
s = G_MEMORY_INPUT_STREAM(g_memory_input_stream_new());
g_memory_input_stream_add_data(s, msg->response_body->data, msg->response_body->length, NULL);
pixbuf_raw = gdk_pixbuf_new_from_stream(G_INPUT_STREAM(s), NULL, NULL);
g_object_unref(s);
pixbuf_scaled = gdk_pixbuf_scale_simple(pixbuf_raw, 100, 100, GDK_INTERP_BILINEAR);
g_object_unref(pixbuf_raw);
gtk_image_set_from_pixbuf(GTK_IMAGE(self->priv.image), pixbuf_scaled);
g_object_unref(pixbuf_scaled);
}
gtk_stack_set_visible_child(self->priv.stack_image_loader, self->priv.image);
}
void yacos_cart_item_fetch_image(YacosCartItem *self) void yacos_cart_item_fetch_image(YacosCartItem *self)
{ {
return; SoupMessage *msg;
gchar *url;
gchar *id_str;
id_str = yacos_tag_id_to_str(self->priv.tag_id);
url = g_strdup_printf("%s?image&id=%s", self->priv.api.url_base, id_str);
g_free(id_str);
msg = soup_message_new("GET", url);
soup_message_headers_append (msg->request_headers, "x-token", self->priv.api.token);
g_free(url);
soup_session_queue_message(self->priv.api.soup_session_meta,
msg,
yacos_cart_item_fetch_image_cb,
self);
/* TODO: More memory leaks? */
} }
yacos_tag_id yacos_cart_item_get_id(YacosCartItem *self) yacos_tag_id yacos_cart_item_get_id(YacosCartItem *self)

View File

@ -2,10 +2,22 @@
// This is just a dummy implementation during the development of the GTK+3 GUI // This is just a dummy implementation during the development of the GTK+3 GUI
if ($_SERVER['HTTP_X_TOKEN'] != "123456") die("Invalid token.\n"); if ($_SERVER['HTTP_X_TOKEN'] != "123456") {
http_response_code(403);
die("Invalid token.\n");
}
$id = $_GET['id'];
preg_match("/^[A-Za-z0-9]{6,10}$/", $id, $matches);
if (!$matches) {
http_response_code(400);
die ("Invalid tag ID.");
}
// $id is now safe to use
if (isset($_GET['meta'])) { if (isset($_GET['meta'])) {
if ($_GET['id'] == "BF09CE") { if ($id == "BF09CE") {
?> ?>
{ {
"title" : "Cute Wifi Cat", "title" : "Cute Wifi Cat",
@ -13,7 +25,7 @@ if (isset($_GET['meta'])) {
"comment" : "It's mewine!" "comment" : "It's mewine!"
} }
<?php <?php
} else if ($_GET['id'] == "BE6200") { } else if ($id == "BE6200") {
?> ?>
{ {
"title" : "Crazy Stuff", "title" : "Crazy Stuff",
@ -22,6 +34,7 @@ if (isset($_GET['meta'])) {
} }
<?php <?php
} else { } else {
http_response_code(400);
?> ?>
{ {
"title" : "INVALID", "title" : "INVALID",
@ -30,5 +43,17 @@ if (isset($_GET['meta'])) {
} }
<?php <?php
} }
} else if (isset($_GET['image'])) {
header('Content-Type: image/png');
$fn = "img/$id.png";
if (file_exists($fn)) {
readfile($fn);
} else {
http_response_code(404);
die ("Image not found.");
}
} else {
http_response_code(400);
die("Invalid request.");
} }
?> ?>

BIN
server/img/BE6200.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
server/img/BF09CE.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB