From 187aa40e245f007e307356a4d2afcf8cbb15df89 Mon Sep 17 00:00:00 2001 From: "Dr. David Alan Gilbert" Date: Sun, 13 Nov 2016 20:29:12 +0000 Subject: Add matrix_api_download_thumb Allow downloading of a thumbnail for a file. Note: Synapse really doesn't like it if you don't specify the height (I get a 500); so the caller has to give some guess of dimensions, but the server gives something reasonable. Signed-off-by: Dr. David Alan Gilbert --- matrix-api.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'matrix-api.c') diff --git a/matrix-api.c b/matrix-api.c index a8528ce..18b365c 100644 --- a/matrix-api.c +++ b/matrix-api.c @@ -846,6 +846,48 @@ MatrixApiRequestData *matrix_api_download_file(MatrixConnectionData *conn, return fetch_data; } +/** + * Download a thumbnail for a file + * @param uri URI string in the form mxc://example.com/unique + */ +MatrixApiRequestData *matrix_api_download_thumb(MatrixConnectionData *conn, + const gchar *uri, + gsize max_size, + unsigned int width, unsigned int height, gboolean scale, + MatrixApiCallback callback, + MatrixApiErrorCallback error_callback, + MatrixApiBadResponseCallback bad_response_callback, + gpointer user_data) +{ + GString *url; + MatrixApiRequestData *fetch_data; + char tmp[64]; + + /* Sanity check the uri - TODO: Add more sanity */ + if (strncmp(uri, "mxc://", 6)) { + error_callback(conn, user_data, "bad media uri"); + return NULL; + } + url = g_string_new(conn->homeserver); + g_string_append(url, "_matrix/media/r0/thumbnail/"); + g_string_append(url, uri + 6); /* i.e. after the mxc:// */ + sprintf(tmp, "?width=%u", width); + g_string_append(url, tmp); + sprintf(tmp, "&height=%u", height); + g_string_append(url, tmp); + g_string_append(url, scale ? "&method=scale": "&method=crop"); + + /* I'd like to validate the headers etc a bit before downloading the + * data (maybe using _handle_header_completed), also I'm not convinced + * purple always does sane things on over-size. + */ + fetch_data = matrix_api_start(url->str, "GET", NULL, conn, callback, + error_callback, bad_response_callback, user_data, max_size); + g_string_free(url, TRUE); + + return fetch_data; +} + #if 0 MatrixApiRequestData *matrix_api_get_room_state(MatrixConnectionData *conn, const gchar *room_id, -- cgit