forked from net-angels/webrados
/
meta.c
96 lines (85 loc) · 4.83 KB
/
meta.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
#include "webrados.h"
#include "protos.h"
#ifdef ELASTIC_SEARCH
void add_meta(int worker_no) {
long http_code = 0;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");
CURLcode curl_res;
char request_url[255] = {0};
char data[1024] = {0};
snprintf(request_url, 255, "%s%s/%s", globals.elastic_path, current_data[worker_no].poolname, current_data[worker_no].filename);
snprintf(data, 1024, "{\"Created\":\"%ld\",\"Bytes\":\"%ld\",\"Extension\":\"%s\",\"Mime\":\"%s\",\"Path\":\"%s\"}",
current_data[worker_no].metadata.c_time,
current_data[worker_no].metadata.f_size,
current_data[worker_no].metadata.extension,
current_data[worker_no].metadata.mime,
current_data[worker_no].metadata.path
);
current_data[worker_no].elastic = curl_easy_init();
if (NULL == current_data[worker_no].elastic) {
return;
}
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_USERAGENT, "WebRados");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FORBID_REUSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FRESH_CONNECT, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_NOSIGNAL, true);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEDATA, fopen("/dev/null", "w"));
#if LIBCURL_VERSION_MINOR >= 25
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_TCP_KEEPALIVE, true);
#endif
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_VERBOSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_URL, request_url);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) - 1);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_HTTPHEADER, headers);
curl_res = curl_easy_perform(current_data[worker_no].elastic);
curl_easy_getinfo(current_data[worker_no].elastic, CURLINFO_RESPONSE_CODE, &http_code);
if (curl_res != CURLE_OK || http_code >= 400) {
logger("Failed to open '%s' on remote server for indexing - %s [HTTP code: %ld]", globals.elastic_path, curl_easy_strerror(curl_res), http_code);
}
if (headers != NULL) {
curl_slist_free_all(headers);
}
}
void delete_meta(int worker_no) {
long http_code = 0;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");
CURLcode curl_res;
char request_url[255] = {0};
snprintf(request_url, 255, "%s%s/%s", globals.elastic_path, current_data[worker_no].poolname, current_data[worker_no].filename);
current_data[worker_no].elastic = curl_easy_init();
if (NULL == current_data[worker_no].elastic) {
return;
}
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_USERAGENT, "WebRados");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FORBID_REUSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_FRESH_CONNECT, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_NOSIGNAL, true);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_WRITEDATA, fopen("/dev/null", "w"));
#if LIBCURL_VERSION_MINOR >= 25
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_TCP_KEEPALIVE, true);
#endif
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_VERBOSE, false);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_URL, request_url);
curl_easy_setopt(current_data[worker_no].elastic, CURLOPT_HTTPHEADER, headers);
curl_res = curl_easy_perform(current_data[worker_no].elastic);
curl_easy_getinfo(current_data[worker_no].elastic, CURLINFO_RESPONSE_CODE, &http_code);
if (curl_res != CURLE_OK || http_code >= 400) {
logger("Failed to open '%s' on remote server for delete - %s [HTTP code: %ld]", globals.elastic_path, curl_easy_strerror(curl_res), http_code);
}
if (headers != NULL) {
curl_slist_free_all(headers);
}
}
#endif