-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud.c
108 lines (91 loc) · 3.5 KB
/
cloud.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "prototype.h"
#include <unistd.h> // For access()
#include <json-c/json.h> // JSON parsing library (install libjson-c-dev)
#define URL "https://api.pdfrest.com/upload"
#define API_KEY "dce9867b-1889-4465-8569-b7b21231dde7" // Your API Key
// Callback function to capture response data
size_t writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
strncat((char *)userdata, ptr, size * nmemb);
return size * nmemb;
}
// Function to handle the cloud upload
int uploadFileToCloud(const char *filePath, char *response)
{
if (access(filePath, F_OK) == -1)
{
perror("File not found or inaccessible");
return 1;
}
CURL *curl;
CURLcode res;
struct curl_slist *headers = NULL;
curl_mime *mime;
curl_mimepart *part;
// Initialize libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
{
// Set the URL for the POST request
curl_easy_setopt(curl, CURLOPT_URL, URL);
// Set the headers
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Api-Key: " API_KEY);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Create the MIME object for file upload
mime = curl_mime_init(curl);
// Create the file part of the multipart form-data
part = curl_mime_addpart(mime);
curl_mime_name(part, "file");
curl_mime_filedata(part, filePath);
curl_mime_type(part, "application/octet-stream");
// Set the POST data to the mime object
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
// Set the write callback for capturing response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
// Perform the file upload
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "File upload failed: %s\n", curl_easy_strerror(res));
}
else
{
printf("File uploaded successfully.\n");
printf("Response: %s\n", response);
// Parse the JSON response to extract the file ID
struct json_object *parsed_json, *files, *file, *id;
parsed_json = json_tokener_parse(response);
if (json_object_object_get_ex(parsed_json, "files", &files))
{
file = json_object_array_get_idx(files, 0);
if (json_object_object_get_ex(file, "id", &id))
{
const char *fileId = json_object_get_string(id);
// printf("File ID: %s\n", fileId);
// Construct the URL
char fileUrl[512];
snprintf(fileUrl, sizeof(fileUrl), "https://api.pdfrest.com/resource/%s?format=file", fileId);
// printf("Download URL: %s\n", fileUrl);
}
}
else
{
fprintf(stderr, "Failed to parse response JSON.\n");
}
}
// Cleanup
curl_mime_free(mime);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}