root/veekun/trunk/lib/Vee/Controller/Gallery.pm

Revision 403, 2.8 KB (checked in by eevee, 2 years ago)

Very simple view controller for a gallery item. Been sitting around for a while and could use committing.

Line 
1package Vee::Controller::Gallery;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7use Image::Size;
8
9=head1 NAME
10
11Vee::Controller::Gallery - Gallery Controller
12
13=head1 DESCRIPTION
14
15Catalyst Controller to handle the gallery.
16
17=head1 METHODS
18
19=head2 index
20
21Overview of recently uploaded works.
22
23=cut
24
25sub 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
40View of a single uploaded work.
41
42=cut
43
44sub 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
65Upload a work.
66
67=cut
68
69sub 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
116Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>)
117
118See the included F<AUTHORS> file for a full list of contributers.
119
120=head1 LICENSE
121
122See the included F<LICENSE> file.
123
124=cut
125
1261;
127
Note: See TracBrowser for help on using the browser.