aboutsummaryrefslogtreecommitdiffstats
path: root/src/upload_file.c
blob: 402ec6f4d4fdcbb293cedebd107688fd95a37bc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <json-c/json.h>
#include "asprintf.h"
#include "util.h"

int
upload_file(const char *path, const char *description, char **id_ptr)
{
	char instance[50];
	char client_id[50];
	char client_secret[50];
	char access_token[50];

	struct json_object *parsed_json;
	struct json_object *json_media_id;
	char buf[8192];
	
	get_tokens_from_file(".demiurgerc",instance,client_id,client_secret,access_token);
	CURL *curl = curl_easy_init();
	if(curl == NULL) {
		fprintf(stderr,"Error creating libcurl thing\n");
		return -1;
	}
	curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
	curl_easy_setopt(curl,CURLOPT_WRITEDATA,buf);
	
	char *url_to_post = NULL;
	asprintf(&url_to_post,"%s/api/v1/media",instance);

	/* Don't repeat yourself, so they say, it's the root of all evil
	 * today */
	
	char *header_fmt = "Authorization: Bearer %s";
	struct curl_slist *header_list = NULL;
	char *authorization_header = NULL;
	asprintf(&authorization_header,header_fmt,access_token);
	header_list = curl_slist_append(header_list, authorization_header);

	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
	
	curl_mime *mime;
	mime = curl_mime_init(curl);
	curl_mimepart *image_part;
	image_part = curl_mime_addpart(mime);
	curl_mimepart *description_part;
	description_part = curl_mime_addpart(mime);
	/* Upload file */
	
	curl_mime_filedata(image_part, path);
	curl_mime_name(image_part,"file");
	
	/* Description */
	
	curl_mime_data(description_part,description,CURL_ZERO_TERMINATED);
	curl_mime_name(description_part,"description");

	/* Post */
	
	curl_easy_setopt(curl,CURLOPT_MIMEPOST,mime);
	curl_easy_setopt(curl,CURLOPT_URL,url_to_post);	

	/* Free the things */
	curl_easy_perform(curl);
	free(url_to_post);
	curl_easy_cleanup(curl);
	curl_mime_free(mime);
	/* Get the media id */
	parsed_json = json_tokener_parse(buf);
	json_object_object_get_ex(parsed_json, "id", &json_media_id);
	const char *media_id = json_object_get_string(json_media_id);
	*id_ptr = media_id;
	return 0;
}