-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathneocities.cpp
More file actions
164 lines (133 loc) · 4.93 KB
/
neocities.cpp
File metadata and controls
164 lines (133 loc) · 4.93 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "neocities.h"
#include <curl/curl.h>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
// Callback function to write response data to a stringstream
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
((stringstream*)userp)->write((char*)contents, size * nmemb);
return size * nmemb;
}
// Function to perform cURL request
bool performCurlRequest(CURL *curl, const string& url, curl_mime *form, stringstream& responseStream, const string& userpwd) {
CURLcode res;
bool success = false;
// Set common cURL options
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if (form) {
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
}
curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseStream);
// Perform the request
res = curl_easy_perform(curl);
// Check for errors
if (res != CURLE_OK) {
cerr << "cURL Error: " << curl_easy_strerror(res) << endl;
} else {
// Check HTTP response code
long responseCode;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
// Check for authentication error in the response body
if (responseStream.str().find("invalid_auth") != string::npos) {
cerr << "Authentication error: " << responseStream.str() << endl;
} else {
success = true;
}
}
return success;
}
Neocities::Neocities(const std::string& user, const std::string& password)
: user_(user), password_(password) {}
bool Neocities::upload(const std::vector<std::pair<std::string, std::string>>& files) {
CURL *curl;
curl_mime *form = nullptr;
bool success = false;
stringstream responseStream;
curl = curl_easy_init();
if (curl) {
// Create a new mime form
form = curl_mime_init(curl);
// Add each file to the form
for (const auto& file : files) {
curl_mimepart *field = curl_mime_addpart(form);
curl_mime_name(field, file.first.c_str()); // Filename with extension
curl_mime_filedata(field, file.second.c_str()); // Path to the file
}
// Perform the cURL request
string url = "https://neocities.org/api/upload"; // The URL for the upload endpoint
success = performCurlRequest(curl, url, form, responseStream, user_ + ":" + password_);
// Clean up
curl_mime_free(form);
curl_easy_cleanup(curl);
}
return success;
}
bool Neocities::deleteFiles(const std::vector<std::string>& filenames) {
CURL *curl;
curl_httppost *post = nullptr;
curl_httppost *last = nullptr;
stringstream responseStream;
bool success = false;
curl = curl_easy_init();
if (curl) {
// Create the POST data
for (const auto& filename : filenames) {
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "filenames[]",
CURLFORM_COPYCONTENTS, filename.c_str(),
CURLFORM_END);
}
// Perform the cURL request
string url = "https://neocities.org/api/delete";
success = performCurlRequest(curl, url, nullptr, responseStream, user_ + ":" + password_);
// Clean up
curl_formfree(post);
curl_easy_cleanup(curl);
}
return success;
}
std::string Neocities::getInfo(const std::string& sitename) {
CURL *curl;
stringstream responseStream;
std::string response;
curl = curl_easy_init();
if (curl) {
// Construct the URL with sitename parameter
char* encodedSitename = curl_easy_escape(curl, sitename.c_str(), sitename.length());
string url = "https://neocities.org/api/info?sitename=" + string(encodedSitename);
// Perform the cURL request
if (performCurlRequest(curl, url, nullptr, responseStream, user_ + ":" + password_)) {
response = responseStream.str();
}
// Clean up
curl_free(encodedSitename);
curl_easy_cleanup(curl);
}
return response;
}
std::string Neocities::listFiles(const std::string& path) { // No default argument here
CURL *curl;
stringstream responseStream;
std::string response;
curl = curl_easy_init();
if (curl) {
// Construct the URL
string url = "https://neocities.org/api/list";
if (!path.empty()) {
// URL encode the path and append it as a query parameter
char* encodedPath = curl_easy_escape(curl, path.c_str(), path.length());
url += "?path=" + string(encodedPath);
curl_free(encodedPath);
}
// Perform the cURL request
if (performCurlRequest(curl, url, nullptr, responseStream, user_ + ":" + password_)) {
response = responseStream.str();
}
// Clean up
curl_easy_cleanup(curl);
}
return response;
}