Upload attachment API call

FossilOrigin-Name: f8e1aa6aac057394f9fbaef40d6733226671b4331c51c1653c6a08ba5ce0fb6c
This commit is contained in:
nekobit 2022-07-13 22:13:56 +00:00
parent 170d651650
commit 04e48fc2e7
3 changed files with 36 additions and 1 deletions

View file

@ -18,6 +18,7 @@
#include <string.h>
#include <stdlib.h>
#include "base_page.h"
#include "helpers.h"
#include "easprintf.h"
#include "attachments.h"
@ -196,3 +197,35 @@ char* construct_attachments(struct session* ssn,
free(elements);
return att_view;
}
void api_attachment_create(struct session* ssn, mastodont_t* api, char** data)
{
struct mstdnt_storage *att_storage = NULL;
struct mstdnt_attachment* attachments = NULL;
char* string;
char** media_ids = NULL;
cJSON* root = cJSON_CreateObject();
// TODO If multiple attachments are submitted, this uploads all of them.
// I don't think we want that, but it's just a minor issue and won't happen on TreebirdFE.
try_upload_media(&att_storage, ssn, api, &attachments, &media_ids);
if (media_ids)
cJSON_AddStringToObject(root, "id", media_ids[0]);
if (media_ids)
{
string = cJSON_Print(root);
send_result(NULL, "application/json", string, 0);
free(string);
}
else
send_result(NULL, "application/json", "{\"status\":\"Couldn't\"}", 0);
// Cleanup media stuff
cleanup_media_storages(ssn, att_storage);
cleanup_media_ids(ssn, media_ids);
free(attachments);
cJSON_Delete(root);
}

View file

@ -34,5 +34,6 @@ void cleanup_media_storages(struct session* ssn, struct mstdnt_storage* storage)
void cleanup_media_ids(struct session* ssn, char** media_ids);
char* construct_attachment(struct session* ssn, mstdnt_bool sensitive, struct mstdnt_attachment* att, size_t* str_size);
char* construct_attachments(struct session* ssn, mstdnt_bool sensitive, struct mstdnt_attachment* atts, size_t atts_len, size_t* str_size);
void api_attachment_create(struct session* ssn, mastodont_t* api, char** data);
#endif // ATTACHMENTS_H

View file

@ -111,7 +111,8 @@ int main(void)
{ "/chats_embed/:", content_chat_embed },
// API
{ "/treebird_api/v1/notifications", api_notifications },
{ "/treebird_api/v1/interact", api_status_interact }
{ "/treebird_api/v1/interact", api_status_interact },
{ "/treebird_api/v1/attachment", api_attachment_create },
};
while (FCGI_Accept() >= 0)