feat: allow adding paste with predefined key
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
parent
91bba2e9ff
commit
b66c0f0e00
|
@ -14,5 +14,5 @@ elif [ "$1" = get ]; then
|
||||||
curl -is "$URL/$2" | sed -En 's/^[lL]ocation: (.*)/\1/p'
|
curl -is "$URL/$2" | sed -En 's/^[lL]ocation: (.*)/\1/p'
|
||||||
|
|
||||||
elif [ "$1" = paste ]; then
|
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
|
fi
|
||||||
|
|
29
src/main.cpp
29
src/main.cpp
|
@ -91,7 +91,7 @@ int main() {
|
||||||
return crow::response(res);
|
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>")
|
CROW_ROUTE(app, "/s/<string>")
|
||||||
.methods(crow::HTTPMethod::Post)(
|
.methods(crow::HTTPMethod::Post)(
|
||||||
[api_key, base_url, trie](const crow::request &req, std::string key) {
|
[api_key, base_url, trie](const crow::request &req, std::string key) {
|
||||||
|
@ -139,5 +139,32 @@ int main() {
|
||||||
return crow::response(res);
|
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();
|
app.port(18080).multithreaded().run();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue