1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-23 00:33:44 +00:00

Enable language and expiry in pasties.

This commit is contained in:
King_DuckZ 2017-04-25 00:21:44 +01:00
parent 9232644cbb
commit b423522af8
5 changed files with 82 additions and 28 deletions

View file

@ -10,21 +10,21 @@
<div> <div>
<p class="title">Syntax Highlighting:</p> <p class="title">Syntax Highlighting:</p>
<select name="Language" class="selectBox"> <select name="lang" class="selectBox">
<option value="None" selected="selected">None </option> <option value="" selected="selected">None </option>
{{#languages}} {{#languages}}
<option value="{{language_name}}">{{language_name}}</option> <option value="{{language_name}}">{{language_name}}</option>
{{/languages}} {{/languages}}
</select> </select>
<p class="title">Paste Expiration:</p> <p class="title">Paste Expiration:</p>
<select name="Expiration" class="selectBox"> <select name="ttl" class="selectBox">
<option value="10 minutes" >10 minutes</option> <option value="600">10 minutes</option>
<option value="1 hour">1 hour</option> <option value="3600">1 hour</option>
<option value="1 day" selected="selected">1 day</option> <option value="86400" selected="selected">1 day</option>
<option value="1 week">1 week</option> <option value="604800">1 week</option>
<option value="2 weeks">2 weeks</option> <option value="1209600">2 weeks</option>
<option value="1 month">1 month</option> <option value="2628000">1 month</option>
</select> </select>
</div> </div>

@ -1 +1 @@
Subproject commit 1d7a89fee3a8d56db805e7515cee24aaa1fe3036 Subproject commit ac26d244a1d4d464e869b931c42712878b85b143

View file

@ -49,6 +49,7 @@ namespace tawashi {
void PastieResponse::on_send (std::ostream& parStream) { void PastieResponse::on_send (std::ostream& parStream) {
using opt_string = redis::IncRedis::opt_string; using opt_string = redis::IncRedis::opt_string;
using opt_string_list = redis::IncRedis::opt_string_list;
if (cgi_env().path_info().empty()) { if (cgi_env().path_info().empty()) {
return; return;
@ -56,7 +57,9 @@ namespace tawashi {
auto token = boost::string_ref(cgi_env().path_info()).substr(1); auto token = boost::string_ref(cgi_env().path_info()).substr(1);
auto& redis = this->redis(); auto& redis = this->redis();
opt_string pastie = redis.get(token); opt_string_list pastie_reply = redis.hmget(token, "pastie");
opt_string pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string());
if (not pastie) { if (not pastie) {
assert(false); assert(false);
} }

View file

@ -21,12 +21,44 @@
#include "num_to_token.hpp" #include "num_to_token.hpp"
#include "settings_bag.hpp" #include "settings_bag.hpp"
#include "curl_wrapper.hpp" #include "curl_wrapper.hpp"
#include "duckhandy/compatibility.h"
#include "duckhandy/lexical_cast.hpp"
#include <ciso646> #include <ciso646>
#include <sstream> #include <sstream>
#include <stdexcept>
#include <algorithm>
#include <boost/lexical_cast.hpp>
namespace tawashi { namespace tawashi {
namespace { namespace {
const char g_post_key[] = "pastie"; const char g_post_key[] = "pastie";
const char g_language_key[] = "lang";
const char g_duration_key[] = "ttl";
class MissingPostVarError : public std::runtime_error {
public:
MissingPostVarError (const std::string& parMsg) : std::runtime_error(parMsg) {}
};
template <std::size_t N>
inline boost::string_ref make_string_ref (const char (&parStr)[N]) a_always_inline;
template <std::size_t N>
boost::string_ref make_string_ref (const char (&parStr)[N]) {
static_assert(N > 0, "wat?");
return boost::string_ref(parStr, N - 1);
}
boost::string_ref get_value_from_post (const cgi::PostMapType& parPost, boost::string_ref parKey) {
std::string key(parKey.data(), parKey.size());
auto post_data_it = parPost.find(key);
if (parPost.end() == post_data_it) {
std::ostringstream oss;
oss << "can't find POST data field \"" << parKey << '"';
throw MissingPostVarError(oss.str());
}
return post_data_it->second;
}
} //unnamed namespace } //unnamed namespace
SubmitPasteResponse::SubmitPasteResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) : SubmitPasteResponse::SubmitPasteResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
@ -36,29 +68,44 @@ namespace tawashi {
void SubmitPasteResponse::on_process() { void SubmitPasteResponse::on_process() {
auto post = cgi::read_post(cgi_env()); auto post = cgi::read_post(cgi_env());
auto post_data_it = post.find(g_post_key); boost::string_ref pastie;
if (post.end() == post_data_it) { boost::string_ref lang;
m_error_message = "can't find POST data"; boost::string_ref duration;
try {
pastie = get_value_from_post(post, make_string_ref(g_post_key));
}
catch (const MissingPostVarError& e) {
m_error_message = e.what();
return; return;
} }
try {
lang = get_value_from_post(post, make_string_ref(g_language_key));
duration = get_value_from_post(post, make_string_ref(g_duration_key));
}
catch (const MissingPostVarError&) {
}
const SettingsBag& settings = this->settings(); const SettingsBag& settings = this->settings();
const auto max_sz = settings.as<uint32_t>("max_pastie_size"); const auto max_sz = settings.as<uint32_t>("max_pastie_size");
boost::string_ref pastie(post_data_it->second); if (pastie.size() < settings.as<uint32_t>("min_pastie_size"))
if (post_data_it->second.size() < settings.as<uint32_t>("min_pastie_size"))
return; return;
if (max_sz and post_data_it->second.size() > max_sz) { if (max_sz and pastie.size() > max_sz) {
if (settings.as<bool>("truncate_long_pasties")) if (settings.as<bool>("truncate_long_pasties"))
pastie = pastie.substr(0, max_sz); pastie = pastie.substr(0, max_sz);
else else
return; return;
} }
//TODO: replace boost's lexical_cast with mine when I have some checks
//oven invalid inputs
const uint32_t duration_int = std::max(std::min((duration.empty() ? 86400U : boost::lexical_cast<uint32_t>(duration)), 2628000U), 1U);
CurlWrapper curl; CurlWrapper curl;
boost::optional<std::string> token = submit_to_redis(curl.escape(pastie)); boost::optional<std::string> token = submit_to_redis(curl.escape(pastie), duration_int, lang);
if (token) { if (token) {
std::ostringstream oss; std::ostringstream oss;
oss << base_uri() << '/' << *token; oss << base_uri() << '/' << *token;
if (not lang.empty())
oss << '?' << lang;
this->change_type(Response::Location, oss.str()); this->change_type(Response::Location, oss.str());
} }
} }
@ -69,7 +116,7 @@ namespace tawashi {
m_error_message << '\n'; m_error_message << '\n';
} }
boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const std::string& parText) const { boost::optional<std::string> SubmitPasteResponse::submit_to_redis (const std::string& parText, uint32_t parExpiry, const boost::string_ref& parLang) const {
auto& redis = this->redis(); auto& redis = this->redis();
if (not redis.is_connected()) if (not redis.is_connected())
return boost::optional<std::string>(); return boost::optional<std::string>();
@ -77,11 +124,15 @@ namespace tawashi {
const auto next_id = redis.incr("paste_counter"); const auto next_id = redis.incr("paste_counter");
const std::string token = num_to_token(next_id); const std::string token = num_to_token(next_id);
assert(not token.empty()); assert(not token.empty());
if (redis.set(token, parText)) { if (redis.hmset(token,
return boost::make_optional(token); "pastie", parText,
} "max_ttl", dhandy::lexical_cast<std::string>(parExpiry),
else { "lang", parLang)
return boost::optional<std::string>(); ) {
if (redis.expire(token, parExpiry))
return boost::make_optional(token);
} }
return boost::optional<std::string>();
} }
} //namespace tawashi } //namespace tawashi

View file

@ -30,7 +30,7 @@ namespace tawashi {
private: private:
virtual void on_process() override; virtual void on_process() override;
virtual void on_send (std::ostream& parStream) override; virtual void on_send (std::ostream& parStream) override;
boost::optional<std::string> submit_to_redis (const std::string& parText) const; boost::optional<std::string> submit_to_redis (const std::string& parText, uint32_t parExpiry, const boost::string_ref& parLang) const;
std::string m_error_message; std::string m_error_message;
}; };