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 |
|
79 |
my $id; |
80 |
if ( $format eq 'input' ) { |
81 |
my $input = Perly::Model::Input->new(); |
82 |
eval { |
83 |
$id = $input->create( |
84 |
name => $filename, |
85 |
content => $content, |
86 |
); |
87 |
}; |
88 |
die "$@" if ($@); |
89 |
} else { |
90 |
my $code = Perly::Model::Code->new(); |
91 |
eval { |
92 |
$id = $code->create( |
93 |
name => $filename, |
94 |
source => $content, |
95 |
); |
96 |
}; |
97 |
die "$@" if ($@); |
98 |
} |
99 |
|
100 |
my $message = "Created $filename ($format) " . length( $content ) . " bytes [$id]"; |
101 |
|
102 |
# $self->result->content( $_ => $outcome->{$_} ) for keys %{$outcome}; |
103 |
$self->result->message($message); |
104 |
warn "## $message"; |
105 |
delete Jifty->handler->stash->{RECORD_ARGUMENTS}; |
106 |
} |
107 |
|
108 |
1; |