Offload some stuff to Helpers class

This commit is contained in:
swagg boi 2023-11-13 21:38:00 -05:00
parent 6e8a763929
commit 1dee00ace0
3 changed files with 25 additions and 16 deletions

View file

@ -1,12 +1,13 @@
{
"name": "Hyperlink-Redirect",
"depends": [
"Humming-Bird",
"Template::Mustache",
"Base64",
"Libarchive::Filter"
],
"provides": {
"Hyperlink-Redirect": "lib/Hyperlink-Redirect.rakumod"
}
"name": "Hyperlink-Redirect",
"depends": [
"Humming-Bird",
"Template::Mustache",
"Base64",
"Libarchive::Filter"
],
"provides": {
"Hyperlink-Redirect": "lib/Hyperlink-Redirect.rakumod",
"Hyperlink-Redirect::Helpers": "lib/Hyperlink-Redirect/Helpers.rakumod"
}
}

View file

@ -2,8 +2,6 @@ use Humming-Bird::Core;
use Humming-Bird::Middleware;
use Humming-Bird::Advice;
use Template::Mustache;
use Base64;
use Libarchive::Filter :gzip;
# Normally would 'use' local libs here for Controller and Model and
# what not but keeping it simple for now...
@ -36,7 +34,7 @@ $router.post(-> $request, $response {
$base-url = $url-scheme ~ '://' ~ $url-host ~
($meta-refresh ?? '/--meta-refresh/' !! '/');
$hyperlink = $base-url ~ encode-base64(gzip($return-url), :str);
$hyperlink = $base-url ~ hyperlink $return-url;
%stash = title => 'New hyperlink created', :$hyperlink;
@ -46,7 +44,7 @@ $router.post(-> $request, $response {
# Process the hyperlink
$router.get('/--meta-refresh/**', -> $request, $response {
my Str $return-url = $request.path.subst: /^ '/--meta-refresh/'/, Empty;
my Str $redirect-url = gunzip(decode-base64($return-url, :bin));
my Str $redirect-url = redirect $return-url;
my Str %stash = title => 'Hyperlinking...', :$redirect-url;
$response.html($template.render: 'index', %stash);
@ -54,8 +52,7 @@ $router.get('/--meta-refresh/**', -> $request, $response {
$router.get('/**', -> $request, $response {
my Str $return-url = $request.path.substr(1); # Omits the leading slash
my Str $redirect-url = gunzip(decode-base64($return-url, :bin));
my Str $redirect-url = redirect $return-url;
$response.redirect($redirect-url);
});

View file

@ -1,3 +1,6 @@
use Base64;
use Libarchive::Filter :gzip;
my $starts-with-protocol = rx:i/ ^https? '://'/;
sub fix-protocol($url) is export {
@ -17,3 +20,11 @@ sub dbug($message) {
# ERROR: Stringification of a Buf is not done with 'Stringy'. The 'decode'
# method should be used to convert a Buf to a Str.
# TYPE: X::Buf::AsStr
sub hyperlink(Str $return-url --> Str) is export {
encode-base64(gzip($return-url), :str)
}
sub redirect(Str $return-url --> Str) is export {
gunzip(decode-base64($return-url, :bin));
}