diff --git a/landerctl b/landerctl index b22393c..c9a2c12 100755 --- a/landerctl +++ b/landerctl @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 8f2d3ad..35c1c82 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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/") .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/") + .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(); }