Created
August 15, 2010 09:37
-
-
Save akiym/525295 to your computer and use it in GitHub Desktop.
Gyazo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
use Mojo::Asset::File; | |
use Digest::MD5 qw(md5_hex); | |
use File::Spec; | |
use IO::File; | |
my $dir = app->home->rel_dir('images'); | |
unless (-d $dir) { | |
warn "Creating $dir\n"; | |
mkdir $dir; | |
} | |
post '/upload' => sub { | |
my $self = shift; | |
my $imagedata = $self->param('imagedata'); | |
my $filename = md5_hex($imagedata) . '.png'; | |
my $path = File::Spec->catfile($dir, $filename); | |
my $file = Mojo::Asset::File->new( | |
path => $path, | |
handle => IO::File->new($path, 'w'), | |
); | |
$file->add_chunk($imagedata); | |
$self->render(text => $self->req->url->base . '/' . $filename); | |
}; | |
app->static( | |
MojoX::Dispatcher::Static->new( | |
root => $dir, | |
) | |
); | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment