feat: allow adding paste with predefined key
ci/woodpecker/push/woodpecker Pipeline was successful Details

trie-skips
Jef Roosens 2022-11-21 20:15:49 +01:00
parent 91bba2e9ff
commit b66c0f0e00
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 29 additions and 2 deletions

View File

@ -14,5 +14,5 @@ elif [ "$1" = get ]; then
curl -is "$URL/$2" | sed -En 's/^[lL]ocation: (.*)/\1/p'
elif [ "$1" = paste ]; then
curl --data-binary @"$2" -XPOST -H "X-Api-Key: $API_KEY" "$URL/p/"
curl --data-binary @"$2" -XPOST -H "X-Api-Key: $API_KEY" "$URL/p/$3"
fi

View File

@ -91,7 +91,7 @@ int main() {
return crow::response(res);
});
// Add a new Redirect with a given path
// Add a new Redirect with a given key
CROW_ROUTE(app, "/s/<string>")
.methods(crow::HTTPMethod::Post)(
[api_key, base_url, trie](const crow::request &req, std::string key) {
@ -139,5 +139,32 @@ int main() {
return crow::response(res);
});
// Add a paste with a given key
CROW_ROUTE(app, "/p/<string>")
.methods(crow::HTTPMethod::Post)(
[api_key, base_url, trie](const crow::request &req, std::string key) {
AUTH();
Entry *new_entry = entry_new(Paste, "");
bool added = ternarytrie_add(trie, key.c_str(), new_entry);
if (!added) {
return crow::response(crow::status::CONFLICT);
}
// Write paste contents to file
std::fstream file;
file.open(std::string("pastes/") + key, std::ios_base::out);
if (!file.is_open()) {
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
}
file << req.body;
file.close();
return crow::response(base_url + key);
});
app.port(18080).multithreaded().run();
}