1 |
use warnings; |
2 |
use strict; |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
Perly::Action::Upload |
7 |
|
8 |
=cut |
9 |
|
10 |
package Perly::Action::Upload; |
11 |
use base qw/Perly::Action::CreateCode Jifty::Action/; |
12 |
|
13 |
use Perly::Model::Code; |
14 |
use Perly::Model::Input; |
15 |
use Data::UUID; |
16 |
|
17 |
=head2 arguments |
18 |
|
19 |
The fields for C<Upload> are: |
20 |
|
21 |
=over 4 |
22 |
|
23 |
=item file: The uploaded file |
24 |
|
25 |
=back |
26 |
|
27 |
=cut |
28 |
|
29 |
sub arguments { |
30 |
{ |
31 |
content => { label => 'The text version', render_as => 'Textarea' }, |
32 |
file => { |
33 |
label => '', |
34 |
render_as => 'Upload', |
35 |
}, |
36 |
format => { |
37 |
label => 'Textfile format', |
38 |
valid_values => [qw(input perl)], |
39 |
default_value => 'auto', |
40 |
render_as => 'select', |
41 |
} |
42 |
} |
43 |
} |
44 |
|
45 |
=head2 take_action |
46 |
|
47 |
Import the textfile dump |
48 |
|
49 |
=cut |
50 |
|
51 |
sub take_action { |
52 |
my $self = shift; |
53 |
unless( $self->argument_value('file') || $self->argument_value('content') ) { |
54 |
$self->result->failure('You must upload something!'); |
55 |
return 1; |
56 |
} |
57 |
|
58 |
my $content; |
59 |
my $filename; |
60 |
|
61 |
if ( my $fh = $self->argument_value('file') ) { |
62 |
local $/; |
63 |
$content = <$fh>; |
64 |
|
65 |
# This kills the filehandle from the action, so Apache::Session |
66 |
# doesn't try to save it away (and fail) when we do the redirect |
67 |
|
68 |
$self->argument_value( 'file', "" ); |
69 |
$filename = scalar( $fh ); |
70 |
} else { |
71 |
$content = $self->argument_value('content'); |
72 |
my $uuid = Data::UUID->new(); |
73 |
$filename = $uuid->create_str; |
74 |
} |
75 |
|
76 |
|
77 |
my $format = $self->argument_value( 'format' ); |
78 |
$format = 'perl' if ($content =~ m/perl/si); |
79 |
|
80 |
my $id; |
81 |
if ( $format eq 'input' ) { |
82 |
my $input = Perly::Model::Input->new(); |
83 |
eval { |
84 |
$id = $input->create( |
85 |
name => $filename, |
86 |
content => $content, |
87 |
); |
88 |
}; |
89 |
die "$@" if ($@); |
90 |
} else { |
91 |
my $code = Perly::Model::Code->new(); |
92 |
eval { |
93 |
$id = $code->create( |
94 |
name => $filename, |
95 |
source => $content, |
96 |
); |
97 |
}; |
98 |
die "$@" if ($@); |
99 |
} |
100 |
|
101 |
my $message = "Created $filename ($format) " . length( $content ) . " bytes [$id]"; |
102 |
|
103 |
# $self->result->content( $_ => $outcome->{$_} ) for keys %{$outcome}; |
104 |
$self->result->message($message); |
105 |
warn "## $message"; |
106 |
delete Jifty->handler->stash->{RECORD_ARGUMENTS}; |
107 |
} |
108 |
|
109 |
1; |