From 4a0862321efab8606280cc04544fe5c17f3adf14 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Mon, 11 Apr 2022 20:58:46 +0000 Subject: [PATCH] Form fixes and media id's FossilOrigin-Name: 8ebe26ce8da05e5f79339885842b1ac7a9010d1fa353c21618ffc13c80add968 --- include/mastodont.h | 1 + include/mastodont_args.h | 1 + include/mastodont_attachment.h | 4 ++-- include/mastodont_query.h | 4 +++- include/mastodont_types.h | 8 ++++++++ src/attachment.c | 9 ++++----- src/request.c | 17 +++++++++++++++-- src/status.c | 4 +++- 8 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/mastodont.h b/include/mastodont.h index d84f6a3..910f942 100644 --- a/include/mastodont.h +++ b/include/mastodont.h @@ -22,6 +22,7 @@ #include #include #include +#include /* Functions required form curl */ void mastodont_global_curl_init(); diff --git a/include/mastodont_args.h b/include/mastodont_args.h index 6f28492..6f5136e 100644 --- a/include/mastodont_args.h +++ b/include/mastodont_args.h @@ -59,6 +59,7 @@ struct mstdnt_args char* spoiler_text; char* status; char* visibility; + int media_ids_len; int remote; int local; char** exclude_types; diff --git a/include/mastodont_attachment.h b/include/mastodont_attachment.h index 44bac3b..8f7bbbf 100644 --- a/include/mastodont_attachment.h +++ b/include/mastodont_attachment.h @@ -41,8 +41,8 @@ struct mstdnt_attachment struct mstdnt_upload_media_args { - char* file; - char* thumbnail; + struct mstdnt_file file; + struct mstdnt_file* thumbnail; char* description; /* TODO focus */ }; diff --git a/include/mastodont_query.h b/include/mastodont_query.h index 7ad79c6..8783ed3 100644 --- a/include/mastodont_query.h +++ b/include/mastodont_query.h @@ -22,7 +22,8 @@ enum _mstdnt_query_type { _MSTDNT_QUERY_STRING, _MSTDNT_QUERY_INT, - _MSTDNT_QUERY_ARRAY + _MSTDNT_QUERY_ARRAY, + _MSTDNT_QUERY_FILE, }; struct _mstdnt_query_array @@ -39,6 +40,7 @@ struct _mstdnt_query_param char* s; int i; struct _mstdnt_query_array a; + struct mstdnt_file* f; } value; }; diff --git a/include/mastodont_types.h b/include/mastodont_types.h index 83a0c3a..bdda2c4 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -46,4 +46,12 @@ struct mstdnt_storage char* error_description; }; +struct mstdnt_file +{ + char* file; + char* filename; + size_t filesize; + char* filetype; +}; + #endif /* MASTODONT_TYPES_H */ diff --git a/src/attachment.c b/src/attachment.c index 01b3309..c92aff3 100644 --- a/src/attachment.c +++ b/src/attachment.c @@ -101,14 +101,13 @@ int mastodont_upload_media(mastodont_t* api, struct mstdnt_attachment* attachment) { union param_value u_file, u_thumbnail, u_description; - u_file.s = args->file; - u_thumbnail.s = args->thumbnail; + u_file.f = &(args->file); + u_thumbnail.f = args->thumbnail; u_description.s = args->description; struct _mstdnt_query_param params[] = { - { _MSTDNT_QUERY_STRING, "file", u_file }, - { _MSTDNT_QUERY_STRING, "thumbnail", u_thumbnail }, - { _MSTDNT_QUERY_STRING, "description", u_description }, + { _MSTDNT_QUERY_FILE, "file", u_file }, + { _MSTDNT_QUERY_FILE, "thumbnail", u_thumbnail } }; struct mastodont_request_args req_args = { diff --git a/src/request.c b/src/request.c index f2f97a0..c02ee12 100644 --- a/src/request.c +++ b/src/request.c @@ -31,10 +31,17 @@ static void mime_params_post(curl_mime* mime, char conv_val[CONV_SIZE]; char* val_ptr; char* escape_str; + struct mstdnt_file* file = NULL; for (i = 0; i < size; ++i) { part = curl_mime_addpart(mime); + + /* Skip if file is empty */ + if (params[i].type == _MSTDNT_QUERY_FILE && + params[i].value.f == NULL) + continue; + switch (params[i].type) { case _MSTDNT_QUERY_INT: @@ -44,12 +51,18 @@ static void mime_params_post(curl_mime* mime, case _MSTDNT_QUERY_STRING: val_ptr = params[i].value.s; break; + case _MSTDNT_QUERY_FILE: + file = params[i].value.f; + val_ptr = file->file; + curl_mime_type(part, file->filetype); + curl_mime_filename(part, file->filename); default: /* Any other types are not supported! */ break; } - - curl_mime_data(part, val_ptr, CURL_ZERO_TERMINATED); + + curl_mime_data(part, val_ptr, + file ? file->filesize : CURL_ZERO_TERMINATED); curl_mime_name(part, params[i].key); } diff --git a/src/status.c b/src/status.c index 90bb69e..d34f3d2 100644 --- a/src/status.c +++ b/src/status.c @@ -187,7 +187,8 @@ int mastodont_create_status(mastodont_t* data, u_in_reply_to_conversation_id.s = args->in_reply_to_conversation_id; u_in_reply_to_id.s = args->in_reply_to_id; u_language.s = args->language; - /*u_media_ids.s = args->media_ids;*/ + u_media_ids.a.arr = args->media_ids; + u_media_ids.a.arr_len = args->media_ids_len; /* poll */ u_preview.i = args->preview; u_scheduled_at.s = args->scheduled_at; @@ -201,6 +202,7 @@ int mastodont_create_status(mastodont_t* data, { _MSTDNT_QUERY_STRING, "content_type", u_content_type }, { _MSTDNT_QUERY_STRING, "status", u_status }, { _MSTDNT_QUERY_STRING, "visibility", u_visibility }, + { _MSTDNT_QUERY_ARRAY, "media_ids", u_media_ids }, }; struct mastodont_request_args req_args = {