replace empty fixture with one that works.
[sgn.git] / lib / SGN / Controller / Image.pm
blobe94fa10684ff170681c13d9041f6bf95568ebb95
1 package SGN::Controller::Image;
3 use Moose;
4 use namespace::autoclean;
5 use File::Basename;
6 use SGN::Image;
7 use CXGN::Login;
10 use URI::FromHash 'uri';
12 BEGIN { extends 'Catalyst::Controller'; }
14 sub view :Path('/image/view/') Args(1) {
15 my ( $self, $c, $image_id ) = @_;
17 my $dbh = $c->dbc->dbh;
19 my $image = $c->stash->{image} =
20 SGN::Image->new( $dbh, $image_id+0, $c );
22 $image->get_original_filename
23 or $c->throw_404('Image not found.');
25 $c->forward('get_user');
27 $c->stash(
28 template => '/image/index.mas',
30 object_id => $image_id,
31 dbh => $dbh,
32 size => $c->req->param("size")
36 sub add :Path('/image/add') Args(0) {
37 my ($self, $c) = @_;
39 $c->forward('require_logged_in');
41 $c->stash(
42 template => '/image/add_image.mas',
44 refering_page => $c->req->referer() || undef,
45 type => $c->req->param('type'),
46 type_id => $c->req->param('type_id'),
50 sub confirm :Path('/image/confirm') {
51 my ($self, $c) = @_;
53 $c->forward('require_logged_in');
55 my $upload = $c->req->upload('file')
56 or $c->throw( public_message => 'No image file uploaded.', is_client_error => 1 );
57 my $filename = $upload->filename();
58 my $tempfile = $upload->tempname();
59 #print STDERR "FILENAME: $filename TEMPNAME: $tempfile\n";
61 if (! -e $tempfile) {
62 die "No tempfile $tempfile\n";
65 my $filename_validation_msg = $self->validate_image_filename(basename($filename));
66 if ( $filename_validation_msg ) { #if non-blank, there is a problem with Filename, print messages
68 unlink $tempfile; # remove upload! prevents more errors on item we have rejected
70 $c->throw( public_message => <<EOM, is_client_error => 1 );
71 There is a problem with the image file you selected: $filename <br />
72 Error: $filename_validation_msg <br />
73 EOM
76 my $image_url = $c->tempfiles_subdir('image')."/".basename($tempfile);
77 my $confirm_filename = $c->get_conf('basepath')."/".$image_url;
78 if (! -e $tempfile) { die "Temp file does not exit $tempfile\n"; }
79 if (!$upload->copy_to( $confirm_filename )) {
80 die "Error copying $tempfile to $confirm_filename\n";
83 $c->stash(
84 type => $c->req->param('type'),
85 refering_page => $c->req->param('refering_page'),
86 type_id => $c->req->param('type_id'),
87 filename => $filename,
88 tempfile => basename($tempfile),
89 image_url => $image_url,
94 sub store :Path('/image/store') {
95 my $self = shift;
96 my $c = shift;
98 $c->forward('require_logged_in');
100 my $image = SGN::Image->new( $c->dbc->dbh(), undef, $c );
102 my $tempfile = $c->req()->param('tempfile');
103 my $filename = $c->req()->param('filename');
104 my $type = $c->req()->param('type');
105 my $type_id = $c->req()->param('type_id');
106 my $refering_page = $c->req()->param('refering_page');
109 my $temp_image_dir = $c->get_conf("basepath")."/".$c->tempfiles_subdir('image');
111 $image->set_sp_person_id( $c->stash->{person_id} );
113 if ((my $err = $image->process_image($temp_image_dir."/".$tempfile, $type, $type_id, 1))<=0) {
114 die "An error occurred during the upload. Is the file you are uploading an image file? [$err] ";
118 # set some image attributes...
119 # the image owner...
120 #print STDERR "Setting the submitter information in the image object...\n";
122 $image->set_name($filename);
124 $image->store();
126 # send_image_email($c, "store", $image, $sp_person_id, $refering_page, $type, $type_id);
127 #remove the temp_file
129 unlink $temp_image_dir."/".$tempfile;
131 my $image_id = $image->get_image_id();
133 # go to the image detail page
134 # open for editing.....
135 $c->res->redirect( $c->uri_for('view',$image_id )->relative() );
138 sub image_display_order :Path('/image/display_order') Args(0) {
139 my $self = shift;
140 my $c = shift;
142 $c->stash->{image_id} = $c->req->param("image_id");
143 $c->stash->{type} = $c->req->param("type");
144 $c->stash->{id} = $c->req->param("id");
145 $c->stash->{display_order} = $c->req->param("display_order");
147 print STDERR "image_id = ".$c->stash->{image_id}."\n";
149 $c->stash->{template} = '/image/display_order.mas';
152 sub validate_image_filename :Private {
153 my $self = shift;
154 my $fn = shift;
155 my %file_types = ( '.jpg' => 'JPEG file',
156 '.jpeg' => 'JPEG file',
157 '.gif' => 'GIF file',
158 '.pdf' => 'PDF file',
159 '.ps' => 'PS file',
160 '.eps' => 'EPS file',
161 '.png' => 'PNG file');
163 # first test is non-acceptable characters in filename
164 my $OK_CHARS='-a-zA-Z0-9_.@\ '; # as recommend by CERT, test for what you will allow
165 my $test_fn = $fn;
166 $test_fn =~ s/[^$OK_CHARS]/_/go;
167 if ( $fn ne $test_fn ) {
168 #print STDERR "Upload Attempt with bad shell characters: $fn \n";
169 return "Invalid characters found in filename, must not contain
170 characters <b>\& ; : \` \' \\ \| \* \? ~ ^ < > ( ) [ ] { } \$</b>" ;
173 my $ext;
174 if ($fn =~ m/^(.*)(\.\S{1,4})\r*$/) {
175 $ext = lc ($2);
176 #print STDERR "Upload Attempt with disallowed filename extension: $fn Extension: $ext\n";
177 return "File Type must be one of: .png, .jpg, .jpeg, .gif, .pdf, .ps, or .eps" unless exists $file_types{$ext};
178 } else {
179 #print STDERR "Upload Attempt with filename extension we could not parse: $fn \n";
180 return "File Type must be one of: .png, .jpg, .jpeg, .gif, .pdf, .ps, or .eps";
183 return 0; # FALSE, if passes all tests
186 sub send_image_email :Private {
187 my $self = shift;
188 my $c = shift;
189 my $action = shift;
190 my $image = shift;
191 my $sp_person_id = shift;
192 my $refering_page=shift;
193 my $type= shift; #locus or...?
194 my $type_id = shift; #the database id of the refering object (locus..)
196 my $image_id = $image->get_image_id();
198 my $person= CXGN::People::Person->new($c->dbc->dbh, $sp_person_id);
199 my $user=$person->get_first_name()." ".$person->get_last_name();
201 my $type_link;
204 my $user_link = qq | http://sgn.cornell.edu/solpeople/personal-info.pl?sp_person_id=$sp_person_id|;
205 my $usermail=$person->get_contact_email();
206 my $image_link = qq |http://sgn.cornell.edu/image/?image_id=$image_id|;
207 if ($type eq 'locus') {
208 $type_link = qq | http://sgn.cornell.edu/phenome/locus_display.pl?locus_id=$type_id|;
210 # elsif ($type eq 'allele') {
211 # $type_link = qq | http://sgn.cornell.edu/phenome/allele.pl?allele_id=$type_id|;
213 # elsif ($type eq 'population') {
214 # $type_link = qq | http://sgn.cornell.edu/phenome/population.pl?population_id=$type_id|;
217 my $fdbk_body;
218 my $subject;
220 if ($action eq 'store') {
222 $subject="[New image associated with $type: $type_id]";
223 $fdbk_body="$user ($user_link) has associated image $image_link \n with $type: $type_link";
225 elsif($action eq 'delete') {
228 $subject="[A image-$type association removed from $type: $type_id]";
229 $fdbk_body="$user ($user_link) has removed publication $image_link \n from $type: $type_link";
232 CXGN::Contact::send_email($subject,$fdbk_body, 'sgn-db-curation@sgn.cornell.edu');
236 sub get_user : Private{
237 my ( $self, $c ) = @_;
239 my $dbh = $c->dbc->dbh;
241 my $person_id =
242 $c->stash->{person_id} =
243 $c->stash->{sp_person_id} =
244 CXGN::Login->new( $c->dbc->dbh )->has_session();
246 if( $person_id ) {
247 $c->stash->{person} = CXGN::People::Person->new( $dbh, $person_id );
252 sub require_logged_in : Private {
253 my ( $self, $c ) = @_;
255 $c->forward('get_user');
257 unless( $c->stash->{person_id} ) {
258 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
261 return 1;