blib/lib/Livelink/DAV.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Livelink::DAV;
2  
3 1 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.0013';
8  
9 1 ▶use namespace::autoclean; #code hygene,
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use HTTP::DAV;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Moose;
12 use MooseX::NonMoose; #requires separate install from Moose
13 use Data::Dumper;
14 use Carp();
15 use File::Basename;
16  
17 extends 'CopyTree::VendorProof';
18  
19 has 'llddav' =>(
20 is=>'rw',
21 );
22 has 'lldsite' =>(
23 is=>'rw',
24 );
25  
26 has 'lldusern' =>(
27 is=>'rw',
28 );
29 has 'llduserp' =>(
30 is=>'rw',
31 );
32 has 'enodav' =>(
33 is =>'rw',
34 #isa =>'LWP::UserAgent',
35 lazy=>1,
36 required=>1,
37 #default => sub {LWP::UserAgent->new(cookie_jar =>{},timeout=>10,);},
38 builder =>'_init_davnocookies',
39  
40 );
41 sub BUILD{
42 my $self=shift;
43 my $args=shift;
44 #ugly tricky code. non-moose parent's constructor 'new' has these
45 #entries that we don't need here. the result of calling
46 #Livelink::DAV->new
47 #gives a Livelink::DAV obj, even though it uses CopyTree::VendorProof's new
48 #constructor. This is a Moosism, since the point here is to use methods from
49 #the parent, but not use the constructor.
50 #technically, there's nothing wrong with omitting this step;
51 #it just makes the object cleaner looking
52 delete $self->{'source'};
53 delete $self->{'destination'};
54  
55  
56 }
57 sub _init_davnocookies{
58 my $self =shift;
59 my $davobj = HTTP::DAV ->new ();
60 if ($self->lldsite =~m/[^\/]$/){ #just in case the site is provided without ending slash, we will add it
61 $self->lldsite($self->lldsite.'/');
62 }
63 if ($self->llddav =~m/[^\/]$/){ #add ending slash to llddav
64 $self->llddav($self->llddav.'/');
65 }
66 if ($self->llddav =~m/^\//){ #remove starting slash to llddav
67 my $newllddav=$self->llddav;
68 $newllddav=~s/^\///;
69 $self->llddav($newllddav);
70 }
71  
72 $davobj -> credentials(-url=>$self->lldsite.$self->llddav,-user=> $self-> lldusern , -pass => $self ->llduserp, );
73 $davobj-> open(-url => $self->lldsite.$self->llddav) or print STDERR $davobj ->message." dav open err $!\n";
74 #print Dumper $davobj ->get_user_agent;
75 $self ->enodav($davobj);
76 }
77 sub fdls{
78 my $self=shift;
79 my $lsoption=shift;
80 my $startpath=shift;
81 #removes trailing slashes to be consistant
82 $startpath='' if (!$startpath);
83 $startpath =~s/\/$//;
84  
85  
86 my ( @files, @dirs);
87 my $davobj = $self->enodav();
88 my $rstatus=0; #0 is bad status, 1 is success
89 my $r=$davobj -> propfind($startpath) and $rstatus=1 ;
90 if (!$rstatus){
91 Carp::cluck ( "propfind error [resource name is probably incorrect] err msg [".$davobj->message."]\n");
92 }
93 elsif ( $r->is_collection){
94 print "[you are probably looking at a populated dir]\n";
95 # print Dumper $r;
96 if ($r->get_resourcelist){
97 # print $r->get_resourcelist ."\n";
98 # print $r->get_resourcelist ->as_string ."\n";
99 #print Dumper $r->get_resourcelist ;
100 for my $resource ( @{$r->get_resourcelist->{'_resources'}}){
101  
102 if ($resource ->get_property("resourcetype") eq 'collection'){
103 push @dirs, $startpath.'/'.$resource->get_property('displayname');
104 }
105 elsif (! $resource ->get_property("resoucetype") ){
106 push @files, $startpath.'/'.$resource->get_property('displayname');
107 }
108 #print "- ".$resource->{'_properties'}->{'displayname'} . "\n" if ($resource->{'_properties'}->{'displayname'} );
109 #print "\tcreated ".$resource->{'_properties'}->{'creationdate'} . "\n" if ($resource->{'_properties'}->{'creationdate'});
110 #print "\tmodified ".$resource->{'_properties'}->{'getlastmodified'} . "\n" if ($resource->{'_properties'}->{'getlastmodified'} );
111 #print "\tbytes ".$resource->{'_properties'}->{'getcontentlength'} . "\n" if ($resource->{'_properties'}->{'getcontentlength'});
112 #print "\tcreater ".$resource->{'_properties'}->{'createdby'} . "\n" if ($resource->{'_properties'}->{'createdby'} );
113 #print "\trestype ".$resource->{'_properties'}->{'resourcetype'} . "\n" if ($resource->{'_properties'}->{'resourcetype'} );
114 #print "\tprop s ".$resource->{'_properties'}->{'short_props'} . "\n" if ($resource->{'_properties'}->{'short_props'} );
115 }
116 } #end if ($r->get_resourcelist)
117 else {
118 print "[no resource list, you're probably looking at an empty dir]\n";
119 }#end else of if $r->get_resourcelist
120 } #end if ($r ->is_collection)
121 else {
122 print "[propfind is not a collection. you're probably looking at a file]\n";
123 print $r->get_property("getcontentlength") . "\n";
124 }
125  
126 return $self->fdls_ret( $lsoption,\@files, \@dirs);
127  
128 }
129 sub is_fd{
130 my $self=shift;
131 my $query = shift;
132 my $queryparent;
133 my $davobj = $self->enodav();
134 if ($query =~m/\/$/){
135 Carp::cluck("no sense querying with an ending slash, removing it for you\n");
136 $query=~s/\/$//;
137 }
138 my $rstatus=0; #0 is bad status, 1 is success
139 my $r=$davobj -> propfind($query) and $rstatus=1 ;
140 if (!$rstatus){
141 #Carp::cluck ( "propfind error [resource name is probably incorrect] err msg [".$davobj->message."]\n");
142  
143 #in case the $query has a parent/child format:
144 if ($query =~m/\//){
145 #only do this if we're sure there's a slash in the middle,
146 #since dirname() returns '.' on a non-slash item
147 $queryparent=File::Basename::dirname($query);
148 if ($davobj ->propfind($queryparent)){
149 #Carp::cluck ( "dir or file does not exist, but has valid parent\n");
150 return ('pd');
151 }
152 else {
153 return (0);
154 #or no valid parent and file doesn't exist
155 }
156 }#end if (query =~m/\//) case where there's a dir slash
157 else {
158 #no slash in file name, must be right after webdav dir, which gets auto 'pd'
159 #status, though chances of anything working are small, unless admin
160 return ('pd');
161 }
162 }
163 elsif ( $r->is_collection){
164 print "[you are probably looking at a populated dir]\n";
165 return ('d');
166 # if ($r->get_resourcelist){
167 # print $r->get_resourcelist ."\n";
168 # print $r->get_resourcelist ->as_string ."\n";
169 # #print Dumper $r->get_resourcelist ;
170 # } #end if ($r->get_resourcelist)
171 # else {
172 # print "[no resource list, you're probably looking at an empty dir]\n";
173 # }#end else of if $r->get_resourcelist
174 } #end elsif ($r ->is_collection)
175 else {
176 print "[propfind is not a collection. you're probably looking at a file]\n";
177 print "size [".$r->get_property("getcontentlength") . "]bytes\n";
178 return ('f');
179 }
180  
181 }
182  
183 sub cust_mkdir{
184 my $self=shift;
185 my $newdir=shift;
186 #remove leading and training slashes of dir
187 $newdir =~s/^\///;
188 $newdir =~s/\/$//;
189 return unless $self->pathcheck($newdir);
190  
191 my $davobj = $self->enodav();
192 #creates a dav dir
193 my $fdstatus= $self->is_fd($newdir);
194 if ($newdir =~/\//){ #dir has a slash in it, test if parent exists, then create:
195 if ($fdstatus eq 'pd'){
196 $davobj -> mkcol(-url => $newdir) or Carp::cluck("cannot create $newdir\n".$davobj-> message );
197 }
198 elsif ($fdstatus eq 'd') {Carp::cluck("$newdir already exists, doing nothing\n")}
199 }
200 else {
201 if ($fdstatus eq 'd') {
202 Carp::cluck("$newdir already exists, doing nothing\n");
203 return;
204 }
205 Carp::cluck("no dir separator, assuming you are creating dir right under webdav folder, which usually doesn't work unless you have special privs\n");
206
207 $davobj -> mkcol(-url => $newdir) or Carp::cluck("cannot create $newdir\n".$davobj-> message );
208 }
209  
210 }
211 sub cust_rmdir{
212 my $self=shift;
213 my $deldir=shift;
214 #remove leading and training slashes of dir
215 $deldir =~s/^\///;
216 $deldir =~s/\/$//;
217 return unless $self->pathcheck($deldir);
218 my $davobj = $self->enodav();
219 #creates a dav dir
220 my $fdstatus= $self->is_fd($deldir);
221 if ($deldir =~/\//){ #dir has a slash in it, test if parent exists, then create:
222 if ($fdstatus eq 'd'){
223 $davobj -> delete(-url => $deldir) or Carp::cluck("cannot delete $deldir and its contents\n".$davobj-> message );
224 }
225 elsif ($fdstatus eq 'f') {Carp::cluck("$deldir is a file, not deleting.\n")}
226 }
227 else {
228 Carp::cluck("no dir separator, assuming you are deleting dir right under webdav folder, which usually doesn't work unless you have special privs\n");
229 if ($fdstatus eq 'd') {
230 $davobj -> delete(-url => $deldir) or (
231 Carp::cluck("cannot delete $deldir and its contents\n".$davobj-> message )
232 and return 0);
233 return 1;
234 }
235 elsif ($fdstatus eq 'f') {
236 Carp::cluck("$deldir is a file, not deleting.\n");
237 return 0;
238 }
239 }
240  
241 }
242 sub pathcheck{
243 my $self=shift;
244 my $path=shift;
245 #$site and $dav should all contain trailing '/', as set up by enodav init.
246 my $site=$self->lldsite;
247 my $dav=$self->llddav;
248 # use \Q \E because we don't want 'an additional level of interpretation as a regular expression' (perldoc perlop)
249 if ($path =~m/^\Q$site\E/){
250 Carp::cluck("it seems like you are using a full site path\nplease use path (starting from directly under your webdav dir)\n");
251 return 0;
252 }
253 elsif ( $path =~m/^\Q$dav\E/){
254 Carp::cluck ("it seems like you are starting your dir with your webdav folder.\nPlease start with the level under it.\n");
255 return 0;
256 }
257 else {return 1;}
258  
259 }
260  
261 sub write_from_memory{
262 my $self=shift;
263 my $binref=shift; #refernce to a scala already
264 my $dest=shift;
265 return unless $self->pathcheck($dest);
266 unless ($self->is_fd($dest) eq 'f' or 'pd'){
267 Carp::carp("write_from_memory should have 'f' or 'pd' destination\n");
268 return;
269 }
270 my $dav=$self->enodav;
271 $dav->put(-local=>$binref, -url=>$dest) or Carp::carp("HTTP::DAV put failed, [".$dav->message()."]\n");
272 }
273  
274 sub read_into_memory{
275 my $self=shift;
276 my $srcurl=shift;
277 return unless $self->pathcheck($srcurl);
278 my $binscalar;
279 my $dav=$self->enodav;
280 $dav->get(-url=>$srcurl, -to=>\$binscalar);
281 return (\$binscalar);
282  
283 }
284  
285 sub copy_local_files{
286 my $self=shift;
287 my $source=shift;
288 my $dest=shift;
289 my $dav=$self->enodav;
290 if ($self->pathcheck($source) + $self->pathcheck($dest) !=2){
291 Carp::cluck("please do double check your Livelink::DAV file names\n");
292 return;
293 }
294 my $sourcetest = $self->is_fd($source);
295 #do not handle dir copies
296 #need to explicitly state this since DAV module is powerful enough to handle this,
297 #but we are writing methods for the weakest system
298 return if ($sourcetest ne 'f');
299 my $test = $self->is_fd($dest);
300 if ($test ne 'pd') {
301 #contains case of '0', 'f' or 'd'
302 if ($test eq '0'){
303 if ($dest=~m/\//){
304 Carp::cluck("cannot copy to $dest whose parent does not exist, does nothing\n");
305 }
306 else{
307 Carp::cluck("chances are you are copying to right under the webdev folder, which has a small\nchance of success unless you are some kinda admin\n");
308 $dav->copy(-url=>$source, -dest=>$dest, -overwrite=>1) or Carp::cluck("cannot perform Livelink::DAV copy_local_files, ".$dav->message);
309 return;
310 }
311 }
312 elsif ($test eq 'd') {
313 Carp::cluck("dest is 'd', not handling this case. does nothing\n");
314 return;
315 }
316 elsif ($test eq 'f'){
317 Carp::carp("dest exists as file, overwriting\n");
318 $dav->copy(-url=>$source, -dest=>$dest, -depth=>0, -overwrite=>1) or Carp::cluck ("Livelink::DAV move local files failed\n".$dav->message);
319  
320 }
321 }
322 #$test is 'pd'
323 else {
324 $dav->copy(-url=>$source, -dest=>$dest, -depth=>0,-overwrite=>1) or Carp::cluck ("Livelink::DAV move local files failed\n".$dav->message);
325 }
326  
327
328 }
329  
330 sub cust_rmfile{
331 my $self=shift;
332 my $delfile=shift;
333 #remove leading and training slashes of dir
334 $delfile =~s/^\///;
335 $delfile =~s/\/$//;
336 return unless $self->pathcheck($delfile);
337 my $davobj = $self->enodav();
338 #creates a dav dir
339 my $fdstatus= $self->is_fd($delfile);
340 if ($delfile =~/\//){ #dir has a slash in it, test if parent exists, then create:
341 if ($fdstatus eq 'f'){
342 $davobj -> delete(-url => $delfile) or Carp::cluck("cannot delete $delfile\n".$davobj-> message );
343 }
344 elsif ($fdstatus eq 'd') {Carp::cluck("$delfile is a dir, not deleting.\n")}
345 }
346 else {
347 Carp::cluck("no dir separator, assuming you are deleting dir right under webdav folder, which usually doesn't work unless you have special privs\n");
348 if ($fdstatus eq 'f') {
349 $davobj -> delete(-url => $delfile) or (
350 Carp::cluck("cannot delete $delfile\n".$davobj-> message )
351 and return 0);
352 return 1;
353 }
354 elsif ($fdstatus eq 'd') {
355 Carp::cluck("$delfile is a dir, not deleting.\n");
356 return 0;
357 }
358 }
359  
360  
361 }
362  
363 no Moose; #from MooseX::NonMoose
364 Livelink::DAV -> meta->make_immutable;#speed up but no longer able to change class definition
365  
366 1;
367  
368 __END__
↑ Summary