blib/lib/Jifty/Plugin/Media.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Jifty/Plugin/Media/Action/ManageFile.pm ↑ Summary lib/Jifty/Plugin/Media/View.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Jifty::Plugin::Media;
6 3 ▶use base qw/Jifty::Plugin Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.01';
9  
10 =head1 NAME
11  
12 Jifty::Plugin::Media - Provides upload file and select url for Jifty
13  
14 =head1 DESCRIPTION
15  
16 Jifty::Plugin::Media is a Jifty plugin to allow managing static files, upload
17 create directory, delete and select url for any media file for your application.
18  
19 =head1 SYNOPSIS
20  
21 In your B<Model> class schema description, add the following:
22  
23 column color1 => is Media;
24  
25 In your jifty B<config.yml> under the framework section:
26  
27 Plugins:
28 - Media:
29 default_root: files
30  
31 C<default_root> will be added to C<Your_app_root/share/web/static/> path
32 Your web process need to have write rights in this directory.
33  
34 In your B<Dispatcher> manage allowed uploaders :
35  
36 use strict;
37 use warnings;
38 package TestApp::Dispatcher;
39 use Jifty::Dispatcher -base;
40 before '*' => run {
41 Jifty->api->allow('Jifty::Plugin::Media::Action::ManageFile')
42 if Jifty->web-current_user->is_supersuser;
43 };
44 before '/media_*' => run {
45 tangent '/access_denied'
46 if ( ! Jifty->web-current_user->is_superuser );
47 };
48 1;
49  
50 In your B<View> you can access to a manager page C<'/media_manage_page'> or
51 a fragment C<'/media_manage'> usable in a popout link:
52  
53 hyperlink (label => 'Manage files',
54 onclick => { popout => '/media_manage' });
55  
56 you can open a repository on load with C<mediadir> argument
57  
58 hyperlink (label => 'Manage files',
59 onclick => {
60 popout => '/media_manage',
61 args => { mediadir => '/images/'}
62 });
63  
64 or you can change default_root to a sub directory with C<rootdir> argument
65  
66 hyperlink (label => 'Manage files',
67 onclick => {
68 popout => '/media_manage',
69 args => { rootdir => '/images/'}
70 });
71  
72 =cut
73  
74 __PACKAGE__->mk_accessors(qw(real_root default_root));
75  
76 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 0 ▶use Text::Unaccent;
Subroutine BEGIN: called CC=1 SCAR=2.6
79  
80 =head2 init
81  
82 load config values, javascript and css
83  
84 =cut
85  
86 sub init {
87 my $self = shift;
88 my %opt = @_;
89  
90 my $default_root = $opt{default_root} || 'files';
91 $self->default_root( $default_root );
92 my $root = Jifty::Util->app_root().'/share/web/static/'.$default_root.'/file';
93 my $dir = File::Basename::dirname($root);
94  
95 if ( ! -d $dir) {
96 eval { File::Path::mkpath($dir, 0, 0775); };
97 die if $@;
98 };
99 $self->real_root( $dir );
100  
101 Jifty->web->add_javascript(qw( jqueryFileTree.js ) );
102 Jifty->web->add_css('jqueryFileTree.css');
103 };
104  
105  
106 use Jifty::DBI::Schema;
107  
108 sub _media {
109 my ($column, $from) = @_;
110 my $name = $column->name;
111 $column->type('text');
112 }
113  
114 Jifty::DBI::Schema->register_types(
115 Media =>
116 sub { _init_handler is \&_media, render_as 'Jifty::Plugin::Media::Widget' },
117 );
118  
119 =head2 read_dir
120  
121 =cut
122  
123 sub read_dir {
124 my $self = shift;
125 my $dir = shift;
126  
127 # don't allow relative path
128 $dir =~ s/\.\.//g;
129 # don't allow cached path
130 $dir =~ s/^\.//g;
131  
132 my ($plugin) = Jifty->find_plugin($self);
133  
134 my $root = $plugin->real_root();
135 my $fullDir = $root . $dir;
136  
137 return if ! -e $fullDir;
138  
139 opendir(BIN, $fullDir) or die "Can't open $dir: $!";
140 my (@folders, @files);
141 my $total = 0;
142 while( defined (my $file = readdir BIN) ) {
143 next if $file eq '.' or $file eq '..';
144 $total++;
145 if (-d "$fullDir/$file") {
146 push (@folders, $file);
147 } else {
148 push (@files, $file);
149 }
150 };
151 closedir(BIN);
152  
153 return ({ Folders => \@folders, Files => \@files });
154 };
155  
156 sub _get_filesize_str
157 {
158 my $size = shift;
159  
160 if ($size > 1099511627776) # TiB: 1024 GiB
161 {
162 return sprintf("%.2f T", $size / 1099511627776);
163 }
164 elsif ($size > 1073741824) # GiB: 1024 MiB
165 {
166 return sprintf("%.2f G", $size / 1073741824);
167 }
168 elsif ($size > 1048576) # MiB: 1024 KiB
169 {
170 return sprintf("%.2f M", $size / 1048576);
171 }
172 elsif ($size > 1024) # KiB: 1024 B
173 {
174 return sprintf("%.2f K", $size / 1024);
175 }
176 else # bytes
177 {
178 return sprintf("%.2f", $size);
179 }
180 };
181  
182  
183 =head2 file_info
184  
185 =cut
186  
187 sub file_info {
188 my $self = shift;
189 my $file = shift;
190  
191 my $root = Jifty::Util->app_root().'/share/web';
192 my $fullName = $root . $file;
193 return if (! -e $fullName);
194  
195 my $size = _get_filesize_str((stat($fullName))[7]);
196 DateTime->DefaultLocale(Jifty::I18N->get_current_language);
197 my $dt = Jifty::DateTime->from_epoch(time_zone => 'local',epoch => (stat($fullName))[9]);
198 my $date = $dt->strftime("%a %d %b %H:%M:%S");
199 my $sname = File::Basename::basename($fullName);
200 return ($sname,$size,$date);
201 };
202  
203 =head2 conv2ascii
204  
205 convert accent character to ascii
206  
207 =cut
208  
209 sub conv2ascii {
210 my ($self,$string) = @_;
211 my $res = unac_string('utf8',$string); # if( !$res);
212 return $res;
213 };
214  
215  
216 =head2 clean_dir_name
217  
218 convert dir name in ascii
219  
220 =cut
221  
222 sub clean_dir_name {
223 my $self = shift;
224 my $string = shift;
225 return if !$string;
226 $string=~s/[ '"\.\/\\()%&~{}|`,;:!*\$]/-/g;
227 $string=~s/#/Sharp/g;
228 $string=~s/--/-/g; $string=~s/--/-/g; $string=~s/--/-/g;
229 $string=$self->conv2ascii($string);
230 return $string;
231 };
232  
233 =head2 clean_file_name
234  
235 convert file name in ascii
236  
237 =cut
238  
239  
240 sub clean_file_name {
241 my $self = shift;
242 my $name = shift;
243 my $string=''; my $ext='';
244 ($string,$ext) = $name =~m/^(.*?)(\.\w+)?$/;
245 $ext =~ s/^\.// if $ext;
246 $string = $self->clean_dir_name($string);
247 $ext = $self->clean_dir_name($ext);
248 return ($ext)?$string.'.'.$ext:$string;
249 };
250  
251  
252 =head1 AUTHOR
253  
254 Yves Agostini, <yvesago@cpan.org>
255  
256 =head1 LICENSE
257  
258 Copyright 2010, Yves Agostini.
259  
260 This program is free software and may be modified and distributed under the same
261 terms as Perl itself.
262  
263 Embeded C<jqueryFileTree.js> is based on B<jQuery File Tree>
264 from http://abeautifulsite.net/2008/03/jquery-file-tree/
265  
266 Which is dual-licensed under the GNU General Public License and the MIT License
267 and is copyright 2008 A Beautiful Site, LLC.
268  
269 =cut
270  
271 1;
« lib/Jifty/Plugin/Media/Action/ManageFile.pm ↑ Summary lib/Jifty/Plugin/Media/View.pm »