App/Lib/IO.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 250 CC 23 SCAR 35.8
CC
23
Cov
0%
image_files
90.0
nodes
42.0
document_files
20.0
CRAP 35.7
1 package Web::App::Lib::IO;
2 # $Id: IO.pm,v 1.1 2009/03/29 10:12:26 apla Exp $
3  
4 use strict;
5  
6 use Imager;
7 use Image::Thumbnail;
8  
9 use IO::Easy;
10 use IO::Easy::Dir;
11 use IO::Easy::File;
12  
13 sub nodes {
14 my $class = shift;
15 my $app = shift;
16 my $params = shift;
17
18 my $place = $params->{place} || 'htdocs';
19
20 my $node = $app->request->params->param ('node') || '';
21
22 return # bwahaha
23 if $node =~ /\.\./;
24
25 my $images_root = $app->root->append ($place, $node)->as_dir;
26
27 my $list = [];
28
29 foreach my $dir_item ($images_root->items) {
30 my $name = $dir_item->name;
31 my $type = $dir_item->type;
32 $type = 'folder' if $type eq 'dir';
33
34 next
35 if $params->{'no-files'} and $type eq 'file';
36
37 push @$list, {
38 text => $name,
39 id => ($node eq '' ? '' : $node . '/') . $name,
40 cls => $type,
41 };
42
43 if ($type eq 'file') {
44 $list->[-1]->{leaf} = 1;
45 $list->[-1]->{extension} = $dir_item->extension;
46 }
47 }
48
49 return $list;
50 }
51  
52 sub file_contents {
53 my $class = shift;
54 my $app = shift;
55 my $params = shift;
56
57 my $place = $params->{place} || 'htdocs';
58
59 my $node = $app->request->path_info;
60
61 return # bwahaha
62 if $node =~ /\.\./;
63
64 my $contents = $app->root->append ($place, $node)->as_file->contents;
65
66 return {
67 contents => $contents,
68 path => $node
69 };
70 }
71  
72 sub store_file {
73 my $class = shift;
74 my $app = shift;
75 my $params = shift;
76
77 my $place = $params->{place} || 'htdocs';
78 my $entity = $params->{entity} || 'contents';
79 my $type = $params->{type} || 'xml';
80
81 my $node = $app->request->path_info;
82
83 my $cgi = $app->request->params;
84 my $path = $cgi->param ('doc-path');
85 my $name = $cgi->param ('doc-name');
86 my $cont = $cgi->param ('doc-contents');
87  
88 return # bwahaha
89 if $node =~ /\.\./;
90
91 my $file;
92 if ($name ne '') {
93 $file = $app->root->append ($place, $path, "$name.$type")->as_file;
94 } else {
95 $file = $app->root->append ($place, $path)->as_file;
96 }
97
98 $file->store ($cont);
99
100 $app->var->{result} = 'ok';
101
102 return;
103 }
104  
105  
106 sub document_files {
107 my $class = shift;
108 my $app = shift;
109 my $params = shift;
110
111 my $place = $params->{place} || 'htdocs';
112
113 my $node = $app->request->params->param ('node') || '';
114
115 return # bwahaha
116 if $node =~ /\.\./;
117
118 my $node_path = $app->root->append ($place, $node)->as_dir;
119
120 my $list = [];
121
122 foreach my $dir_item ($node_path->items ($params->{filter})) {
123 my $name = $dir_item->name;
124 my $type = $dir_item->type;
125
126 next
127 if $type eq 'dir';
128
129 push @$list, {
130 name => $name,
131 size => $dir_item->size,
132 mtime => $dir_item->mtime,
133 url => '/' . ($node eq '' ? '' : $node . '/') . $name,
134 };
135
136 }
137
138 return $list;
139 }
140  
141 sub image_files {
142 my $class = shift;
143 my $app = shift;
144 my $params = shift;
145
146 my $place = $params->{place} || 'htdocs';
147
148 my $node = $app->request->params->param ('node') || '';
149
150 return # bwahaha
151 if $node =~ /\.\./;
152
153 my $images_root = $app->root->append ($place, $node)->as_dir;
154
155 my $list = [];
156
157 foreach my $dir_item ($images_root->items ($params->{filter})) {
158 my $name = $dir_item->name;
159 my $type = $dir_item->type;
160
161 next
162 if $type eq 'dir';
163
164 next
165 if $name =~ /^--/;
166
167 my $thumb_file = $images_root->append ('--'.$name)->as_file;
168
169 my $im = Imager->new;
170 if (!$im->read (file => $dir_item->path)) {
171 warn $im->errstr;
172 next;
173 }
174
175 my ($w, $h) = ($im->getwidth, $im->getheight);
176
177 my $small_dims = 0;
178 $small_dims = 1
179 if $w < 351 and $h < 351;
180
181 if (! -f $thumb_file and ! $small_dims) {
182
183 my $t = Image::Thumbnail->new (
184 size => 300,
185 create => 1,
186 module => 'Imager',
187 quality => '90',
188 input => $dir_item->path,
189 outputpath => $thumb_file->path,
190 );
191 }
192
193 my $uri_dir = '/' . ($node eq '' ? '' : $node . '/');
194
195 push @$list, {
196 name => $name,
197 size => $dir_item->size,
198 mtime => $dir_item->mtime,
199 width => $w,
200 height => $h,
201 thumb_url => $uri_dir . ($small_dims ? '' : '--') . $name,
202 image_url => $uri_dir . $name
203 };
204
205 }
206
207 return $list;
208
209 }
210  
211 sub make_dir {
212 my $class = shift;
213 my $app = shift;
214 my $params = shift;
215
216 my $place = $params->{place} || 'htdocs';
217 my $entity = $params->{entity} || 'makeDir';
218
219 my $cgi = $app->request->params;
220 my $path = $cgi->param ('dir-path');
221 my $name = $cgi->param ('dir-name');
222
223 my $dir = $app->root->append ($place, $path, $name)->as_dir;
224
225 if (-e $dir) {
226 if (-d $dir) {
227 $app->var->{$entity} = {result => 'ok', reason => 'already-exists'};
228 } else {
229 $app->var->{$entity} = {result => 'error', reason => 'not-a-dir'};
230 }
231 } else {
232 $dir->create;
233 $app->var->{$entity} = {result => 'ok'};
234 }
235
236 return;
237
238 }
239  
240  
241 sub upload {
242 my $class = shift;
243 my $app = shift;
244 my $params = shift;
245
246 my $dir = $params->{dir} || 'htdocs';
247
248 my $req = $app->request;
249  
250 my $location = $req->param ('location');
251
252 return # bwahaha
253 if $location =~ /\.\./;
254
255 my $file_contents = $req->param ('file');
256 my $file_name = $req->params->param_filename ('file');
257  
258 my $file = $app->root->append ($dir, $location, $file_name)->as_file;
259  
260 $file->store ($file_contents);
261  
262 $app->var->{success} = 'true';
263  
264 return;
265
266 }
267  
268  
269 1;