| 1 | package Vee::Controller::Gallery; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use base 'Catalyst::Controller'; |
|---|
| 6 | |
|---|
| 7 | use Image::Size; |
|---|
| 8 | |
|---|
| 9 | =head1 NAME |
|---|
| 10 | |
|---|
| 11 | Vee::Controller::Gallery - Gallery Controller |
|---|
| 12 | |
|---|
| 13 | =head1 DESCRIPTION |
|---|
| 14 | |
|---|
| 15 | Catalyst Controller to handle the gallery. |
|---|
| 16 | |
|---|
| 17 | =head1 METHODS |
|---|
| 18 | |
|---|
| 19 | =head2 index |
|---|
| 20 | |
|---|
| 21 | Overview of recently uploaded works. |
|---|
| 22 | |
|---|
| 23 | =cut |
|---|
| 24 | |
|---|
| 25 | sub index : Path { |
|---|
| 26 | my ($self, $c) = @_; |
|---|
| 27 | my $s = $c->stash; |
|---|
| 28 | |
|---|
| 29 | $s->{recent_rs} = $c->model('DBIC::Gallery')->search(undef, { order_by => 'time DESC', rows => 40 }); |
|---|
| 30 | |
|---|
| 31 | $s->{page_title} = 'Gallery'; |
|---|
| 32 | $s->{extra_css} = 'gallery'; |
|---|
| 33 | $s->{breadcrumbs} = [ 'Gallery' ]; |
|---|
| 34 | |
|---|
| 35 | $s->{template} = 'gallery/index.tt'; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | =head2 view |
|---|
| 39 | |
|---|
| 40 | View of a single uploaded work. |
|---|
| 41 | |
|---|
| 42 | =cut |
|---|
| 43 | |
|---|
| 44 | sub view : Path : Args(1) { |
|---|
| 45 | my ($self, $c, $work_id) = @_; |
|---|
| 46 | my $s = $c->stash; |
|---|
| 47 | |
|---|
| 48 | my $row = $c->model('DBIC::Gallery')->find($work_id) |
|---|
| 49 | or $c->vee_abort('No work with id ', $work_id, ' exists.'); |
|---|
| 50 | |
|---|
| 51 | $s->{this} = $row; |
|---|
| 52 | |
|---|
| 53 | $s->{page_title} = 'Gallery Item'; |
|---|
| 54 | $s->{extra_css} = 'gallery'; |
|---|
| 55 | $s->{breadcrumbs} = [ |
|---|
| 56 | '<a href="' . $c->uri_for('/gallery') . '">Gallery</a>', |
|---|
| 57 | 'Gallery Item' |
|---|
| 58 | ]; |
|---|
| 59 | |
|---|
| 60 | $s->{template} = 'gallery/view.tt'; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | =head2 upload |
|---|
| 64 | |
|---|
| 65 | Upload a work. |
|---|
| 66 | |
|---|
| 67 | =cut |
|---|
| 68 | |
|---|
| 69 | sub upload : Local { |
|---|
| 70 | my ($self, $c) = @_; |
|---|
| 71 | my $s = $c->stash; |
|---|
| 72 | |
|---|
| 73 | if ($c->req->method ne 'POST') { |
|---|
| 74 | $s->{template} = 'gallery/upload.tt'; |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | # from here on down is actual uploady logic |
|---|
| 79 | my $upload = $c->req->upload('file'); |
|---|
| 80 | my ($width, $height, $type) = imgsize($upload->fh); |
|---|
| 81 | |
|---|
| 82 | # TODO: check that it's one of a few basic types; sanity-check physical size and filesize; etc |
|---|
| 83 | $c->vee_abort("Uploaded file ", $upload->filename, " is not a recognized image.") |
|---|
| 84 | if not $width or not $height; |
|---|
| 85 | |
|---|
| 86 | # TODO: errors |
|---|
| 87 | # TODO: filename |
|---|
| 88 | my $filename = $upload->filename; |
|---|
| 89 | $upload->copy_to( $c->path_to('root', 'uploads', $filename) ); |
|---|
| 90 | |
|---|
| 91 | system 'convert', qw(-resize 100x100), # TODO: config? |
|---|
| 92 | $c->path_to('root', 'uploads', $filename), |
|---|
| 93 | $c->path_to('root', 'uploads', 'thumbs', $filename); |
|---|
| 94 | |
|---|
| 95 | my $new_obj = $c->model('DBIC::Gallery')->create({ |
|---|
| 96 | creatorid => 3, |
|---|
| 97 | filename => $upload->filename, |
|---|
| 98 | uploaderid => $c->user->obj->id, |
|---|
| 99 | time => time, |
|---|
| 100 | hash => '', |
|---|
| 101 | title => $upload->filename, |
|---|
| 102 | description => $upload->filename, |
|---|
| 103 | height => $height, |
|---|
| 104 | width => $width, |
|---|
| 105 | type => 'colored', |
|---|
| 106 | }); |
|---|
| 107 | |
|---|
| 108 | $c->flash->{success_msg} = "Successfully uploaded " . $c->vee_cleanse($upload->filename) . "!"; |
|---|
| 109 | |
|---|
| 110 | # $c->res->redirect('/gallery/whatever/$new_obj'); |
|---|
| 111 | $c->res->redirect($c->uri_for('/gallery')); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | =head1 AUTHOR |
|---|
| 115 | |
|---|
| 116 | Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>) |
|---|
| 117 | |
|---|
| 118 | See the included F<AUTHORS> file for a full list of contributers. |
|---|
| 119 | |
|---|
| 120 | =head1 LICENSE |
|---|
| 121 | |
|---|
| 122 | See the included F<LICENSE> file. |
|---|
| 123 | |
|---|
| 124 | =cut |
|---|
| 125 | |
|---|
| 126 | 1; |
|---|
| 127 | |
|---|