Skip to content

Instantly share code, notes, and snippets.

@mfontani
Created September 2, 2010 22:35
Show Gist options
  • Save mfontani/563091 to your computer and use it in GitHub Desktop.
Save mfontani/563091 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite;
my $use_auth = 1; # with auth enabled..
my ( $user, $pass ) = qw/user password/;
if ($use_auth) { plugin 'basic_auth'; }
get '/' => sub { my $self = shift; $self->redirect_to('/upload'); };
get '/upload' => sub {
my $self = shift;
# give "upload_message" as flash if it was in the session
if ( my $flash = $self->session('upload_message') ) {
$self->flash( 'upload_message' => $flash );
$self->session('upload_message' => ''); # and remove it from the session...
}
return if ( $use_auth && !$self->helper( basic_auth => realm => $user => $pass ) );
$self->render('upload_form');
};
post '/upload' => sub {
my $self = shift;
return if ( $use_auth && !$self->helper( basic_auth => realm => $user => $pass ) );
if ( my $u = $self->req->upload('file') ) {
# pretend the file is uploaded and put somewhere
# rather than ->flash, use ->session as the flash is eaten by the 401 redirect
$self->session( upload_message => "Uploaded " . $u->filename . " (size: " . $u->size . ")", );
}
$self->redirect_to('/upload');
};
app->start;
__DATA__
@@ upload_form.html.ep
% layout 'html5';
Upload a file:
<form action="/upload" enctype="multipart/form-data" method="post">
<label for="file">File to upload:</label>
<input type="file" name="file"><br />
<input type="submit" value="Upload it!">
</form>
<% if ( my $message = flash 'upload_message' ) { %>
<pre>Flash: <%= $message =></pre>
<% } %>
@@ layouts/html5.html.ep
<!doctype html><html>
<head><title>Upload issue</title></head>
<body><%== content %></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment