Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature "upload image from your computer" #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Galileo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ sub startup {
$r->any( '/page/:name' )->to('page#show');
$r->post( '/login' )->to('user#login');
$r->any( '/logout' )->to('user#logout');
$r->get( '/image/*name' )->to('image#show');

my $if_author = $r->under( sub {
my $self = shift;
Expand All @@ -178,6 +179,7 @@ sub startup {
$if_author->any( '/edit/:name' )->to('edit#edit_page');
$if_author->websocket( '/store/page' )->to('edit#store_page');
$if_author->websocket( '/store/menu' )->to('edit#store_menu');
$if_author->post( '/image_upload' )->to('image#upload');

my $if_admin = $r->under( sub {
my $self = shift;
Expand Down
14 changes: 12 additions & 2 deletions lib/Galileo/DB/Deploy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BEGIN{ extends 'DBIx::Class::DeploymentHandler' }
# A wrapper class for DBICDH for use with Galileo

use Mojo::JSON;
use Mojo::Util 'slurp';
my $json = Mojo::JSON->new();

use File::ShareDir qw/dist_dir/;
Expand Down Expand Up @@ -111,6 +112,15 @@ sub inject_sample_data {
is_admin => 1,
});

my $pub_dir = File::Spec->catdir( qw/ lib Galileo files public / );
$pub_dir = File::Spec->catdir( dist_dir('Galileo'), 'public' ) unless -d $pub_dir;
$schema->resultset('Image')->create({
name => 'galileo-portrait.jpg',
data => slurp(File::Spec->catfile($pub_dir, "portrait.jpg")),
format => 'jpg',
author_id => $admin->user_id,
});

$schema->resultset('Page')->create({
name => 'home',
title => 'Galileo CMS',
Expand All @@ -125,7 +135,7 @@ sub inject_sample_data {

<p>Like the great scientist it is named for, Galileo CMS is not afraid to be very modern. Learn more about it on the <a href="/page/about">about</a> page.</p>

<p><img src="/portrait.jpg" alt="Portrait of Galileo Galilei" title="" /></p>
<p><img src="/image/galileo-portrait.jpg" alt="Portrait of Galileo Galilei" title="" /></p>
HTML
md => <<'MARKDOWN',
##Welcome to your Galileo CMS site!
Expand All @@ -136,7 +146,7 @@ When he first turned the telescope to face Jupiter, he used modern technology to

Like the great scientist it is named for, Galileo CMS is not afraid to be very modern. Learn more about it on the [about](/page/about) page.

![Portrait of Galileo Galilei](/portrait.jpg)
![Portrait of Galileo Galilei](/image/galileo-portrait.jpg)
MARKDOWN
author_id => $admin->user_id,
});
Expand Down
32 changes: 32 additions & 0 deletions lib/Galileo/DB/Schema/Result/Image.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Galileo::DB::Schema::Result::Image;

use DBIx::Class::Candy
-autotable => v1;

primary_column image_id => {
data_type => 'INT',
is_auto_increment => 1,
};

column author_id => {
data_type => 'INT'
};

unique_column name => {
data_type => 'VARCHAR',
size => 255,
};

column format => {
data_type => 'VARCHAR',
size => 10,
};

column data => {
data_type => 'BLOB',
};

belongs_to author => 'Galileo::DB::Schema::Result::User', 'author_id';

1;

46 changes: 46 additions & 0 deletions lib/Galileo/Image.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package Galileo::Image;
use Mojo::Base 'Mojolicious::Controller';

use Mojo::JSON 'j';

my @gif_1x1px = (
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b
);

sub show {
my $self = shift;
my $name = $self->param('name');
if (my $image = $self->schema->resultset('Image')->single({name=>$name})) {
(my $format = lc($name)) =~ s/^.*\.([^\.]+)$/$1/;
$self->render_data($image->data, format=>$format);
}
else {
# image not found
$self->render_data(pack('C*', @gif_1x1px), format=>'gif');
}
}

sub upload {
my $self = shift;
my $file = $self->param('file');
my ($format) = ($file->headers->content_type =~ m!^image/(.+)$!);
my ($name) = ($file->headers->content_disposition =~ m!filename="([^"]+)"!);
if ($file->asset && $format && $name) {
my $author = $self->schema->resultset('User')->single({name=>$self->session->{username}});
$self->schema->resultset('Image')->update_or_create({
author_id => $author->id,
data => $file->asset->slurp,
name => $name,
format => $format,
});
$self->render_text(j {success=>1, imagePath=>"/image/$name"});
}
else {
$self->render_text(j {success=>0, message=>"Upload failed!"});
}
}

1;

Loading