lib/Jifty/Plugin/Media/Action/ManageFile.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Jifty/Plugin/Media/Widget.pm ↑ Summary blib/lib/Jifty/Plugin/Media.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Jifty::Plugin::Media::Action::ManageFile - upload file form
7  
8 =cut
9  
10 package Jifty::Plugin::Media::Action::ManageFile;
11  
12 1 ▶use base qw/Jifty::Plugin::Media::Action Jifty::Action/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶use Jifty::Param::Schema;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use Jifty::Action schema {
16 param 'selected'; # =>
17 # type is 'hidden';
18 param 'create_dir' =>
19 hints is _('ascii char, no white space'),
20 label is _('Create dir');
21 param file =>
22 label is _('File'),
23 render as 'Upload';
24 param action =>
25 is mandatory,
26 valid_values are qw(upload delete view create_dir);
27 };
28  
29 =head1 METHODS
30  
31 =head2 take_action
32  
33 =cut
34  
35 sub take_action {
36 my $self = shift;
37  
38 my ($plugin) = Jifty->find_plugin('Jifty::Plugin::Media');
39  
40 my $root = $plugin->real_root();
41  
42 my $action = $self->argument_value('action');
43  
44 my $selected = $self->argument_value('selected');
45 my $selected_dir = 1 if (-d $root.'/'.$selected);
46  
47 my $current_dir = ($selected_dir) ? $selected : File::Basename::dirname($selected);
48 $current_dir =~ s/^\///;
49 $current_dir =~ s/\/$//;
50  
51 if ( $action eq 'delete' ) {
52 my $delete = $self->argument_value('selected');
53 if ( ! -e $root.'/'.$selected ) {
54 $self->result->error(_('no more exist'));
55 }
56 else {
57 if (-f $root.'/'.$selected ) {
58 my $res = unlink $root.'/'.$selected;
59 $self->result->error(_('can not delete this file')) if !$res;
60 };
61 if (-d $root.'/'.$selected ) {
62 rmdir $root.'/'.$selected;
63 $self->result->error($!) if $!;
64 };
65  
66 };
67 };
68  
69  
70 if ( $action eq ('create_dir') ) {
71 my $dir = $self->argument_value('selected');
72 my $create_dir = $self->argument_value('create_dir');
73 $create_dir = Jifty::Plugin::Media->clean_dir_name($create_dir);
74 my $destdir = $root.'/'.$dir;
75 if ($create_dir) {
76 my $newdir = $destdir.'/'.$create_dir;
77 if ( ! -d $newdir) {
78 eval { File::Path::mkpath($newdir, 0, 0775); };
79 $self->result->error($@) if $@;
80 };
81 };
82 };
83  
84 if( $action eq ('upload') ) {
85 my $dir = $self->argument_value('selected');
86 my $destdir = $root.'/'.$dir;
87 $destdir .= '/' if $destdir !~ m/\/$/;
88  
89 my $fh = $self->argument_value('file');
90 if ($fh) {
91 my $filename = scalar($fh);
92 $filename = Jifty::Plugin::Media->clean_file_name($filename);
93  
94 local $/;
95 binmode $fh;
96 if (open FILE, '>', $destdir.$filename) {
97 print FILE <$fh>; close FILE ;
98 }
99 else {
100 $self->result->error($!);
101 };
102 };
103 };
104  
105 if ( $action eq 'view' ) {
106 my $url = $self->argument_value('selected');
107 my $redirect = '/static/'.$plugin->default_root().$url;
108 $self->result->content( viewfile => $redirect )
109 if (-f $root.'/'.$selected );
110 return 1;
111 };
112
113  
114 $self->result->content( dir => $current_dir );
115  
116  
117 $self->report_success if not $self->result->failure;
118  
119 return 1;
120 };
121  
122 =head2 report_success
123  
124 =cut
125  
126 sub report_success {
127 my $self = shift;
128 # Your success message here
129 $self->result->message(_('Success'));
130 };
131  
132 1;
133  
134  
135 1;
« lib/Jifty/Plugin/Media/Widget.pm ↑ Summary blib/lib/Jifty/Plugin/Media.pm »