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

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

View file

@ -49,6 +49,7 @@ namespace tawashi {
void PastieResponse::on_send (std::ostream& parStream) {
using opt_string = redis::IncRedis::opt_string;
using opt_string_list = redis::IncRedis::opt_string_list;
if (cgi_env().path_info().empty()) {
return;
@ -56,7 +57,9 @@ namespace tawashi {
auto token = boost::string_ref(cgi_env().path_info()).substr(1);
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) {
assert(false);
}

View file

@ -21,12 +21,44 @@
#include "num_to_token.hpp"
#include "settings_bag.hpp"
#include "curl_wrapper.hpp"
#include "duckhandy/compatibility.h"
#include "duckhandy/lexical_cast.hpp"
#include <ciso646>
#include <sstream>
#include <stdexcept>
#include <algorithm>
#include <boost/lexical_cast.hpp>
namespace tawashi {
namespace {
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
SubmitPasteResponse::SubmitPasteResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
@ -36,29 +68,44 @@ namespace tawashi {
void SubmitPasteResponse::on_process() {
auto post = cgi::read_post(cgi_env());
auto post_data_it = post.find(g_post_key);
if (post.end() == post_data_it) {
m_error_message = "can't find POST data";
boost::string_ref pastie;
boost::string_ref lang;
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;
}
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 auto max_sz = settings.as<uint32_t>("max_pastie_size");
boost::string_ref pastie(post_data_it->second);
if (post_data_it->second.size() < settings.as<uint32_t>("min_pastie_size"))
if (pastie.size() < settings.as<uint32_t>("min_pastie_size"))
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"))
pastie = pastie.substr(0, max_sz);
else
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;
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) {
std::ostringstream oss;
oss << base_uri() << '/' << *token;
if (not lang.empty())
oss << '?' << lang;
this->change_type(Response::Location, oss.str());
}
}
@ -69,7 +116,7 @@ namespace tawashi {
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();
if (not redis.is_connected())
return boost::optional<std::string>();
@ -77,11 +124,15 @@ namespace tawashi {
const auto next_id = redis.incr("paste_counter");
const std::string token = num_to_token(next_id);
assert(not token.empty());
if (redis.set(token, parText)) {
if (redis.hmset(token,
"pastie", parText,
"max_ttl", dhandy::lexical_cast<std::string>(parExpiry),
"lang", parLang)
) {
if (redis.expire(token, parExpiry))
return boost::make_optional(token);
}
else {
return boost::optional<std::string>();
}
}
} //namespace tawashi

View file

@ -30,7 +30,7 @@ namespace tawashi {
private:
virtual void on_process() 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;
};