in reply to a full-featured control for uploading files and a couple other tidbits
I realize I'm responding to my own original post. I want this to be the subthread to deal with the main control for uploads and have renamed it accordingly. I've got better results now, so I'd like to show newer code, output, and ask for code review to see if I am achieving an updated spec.
sub upload { use strict; use warnings; use 5.010; use Net::FTP; use Path::Class; my ($rvars, $rftp) = @_; my %vars = %$rvars; # prelims with server...see if we're able to do anything $rftp->cwd("/pages/eh5v.files") or warn "cwd failed in main $!\n"; my $rlist = $rftp->ls(); say "remote list is @$rlist"; $rftp->binary or warn "binary failed$!\n"; # filter for filetypes my @filetypes = qw/jpg jpeg png ogv mp4 m4v webm/; my $pattern = join '|', map "($_)", @filetypes; opendir my $eh, $vars{"to_vids"} or warn "can't open vids $!\n"; while (defined ($_ = readdir($eh))){ next if m/~$/; next if -d; next unless m/($pattern)/i; my $full_path = file($vars{"to_vids"}, $_); my $string_path = $full_path->stringify; my $size_local = -s $string_path; say "value for $_ is $size_local"; #find size on server my $size_remote = $rftp->size($_) or warn "size query failed $! fo +r $_\n"; say "size remote for $_ is $size_remote"; ## control for upload cases if (!defined($size_remote)) { say "Upload $_" }; if ($size_remote eq $size_local) { say "Sizes equal with $_. Overwrite? Default=n" } else { say "Sizes different with $_. Overwrite? Default=y" }; } closedir $eh; return $rftp; }
size query failed for stairs.webm Use of uninitialized value $size_remote in concatenation (.) or string + at template_stuff/html2.pm line 331. Use of uninitialized value $size_remote in string eq at template_stuff +/html2.pm line 336. loading vids remote list is . .. kitchen.jpg kitchen.m4v kitchen.webm stairs.jpg st +airs.m4v value for kitchen.jpg is 27261 size remote for kitchen.jpg is 27261 Sizes equal with kitchen.jpg. Overwrite? Default=n value for kitchen.m4v is 19297258 size remote for kitchen.m4v is 19297258 Sizes equal with kitchen.m4v. Overwrite? Default=n value for kitchen.webm is 12808479 size remote for kitchen.webm is 12808479 Sizes equal with kitchen.webm. Overwrite? Default=n value for stairs.jpg is 99199 size remote for stairs.jpg is 99199 Sizes equal with stairs.jpg. Overwrite? Default=n value for stairs.m4v is 41678290 size remote for stairs.m4v is 40860741 Sizes different with stairs.m4v. Overwrite? Default=y value for stairs.webm is 30127462 size remote for stairs.webm is Upload stairs.webm Sizes different with stairs.webm. Overwrite? Default=y
I'm really pleased with this early result. Instead of testing whether a file exists on the remote server, I simply query for the size and if I get undef, I take that to indicate non-existence, as the above output confirms. Also, just looking at these data makes me believe that size really tells the story of the revision history (how do you alter video without changing the size 99.9% of the time?), so I'll re-state the objective with this in mind:
I want a control to loop through binary files, and first see whether it exists on the remote server. If not, we upload it. If yes, then I'm interested in whether they are a differing size. If they are different, I want a prompt to overwrite with the default being yes. If they are not different, then I want a prompt to overwrite with the default being no. It would be cool if this could work on a five-second timer. Furthermore, I would like not to create race conditions. And finally, I'd like to have a portion of code to deal with error handling such as time-outs, or directory doesn't exist.I made a lot of mistakes with stat and lstat:
my (undef,undef,undef,undef,undef,undef,undef,$size, $atime,$mtime,$ctime,undef,undef) = lstat($string_path); say "File $string_path has $size bytes $mtime"; my $born_local = stat($string_path)->ctime ); my $size_local = stat($string_path)->size ); say "values for $_ are $born_local and $size_local";
I arrived at the opinion that using the native perl syntax was not only simpler, but for me, darn near necessary. This might be halfway there, so I'd appreciate any tips/criticisms. Otherwise, I'll just continue. Thanks for your attention.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: a full-featured control for uploading files
by Athanasius (Archbishop) on May 24, 2015 at 06:38 UTC | |
by Aldebaran (Curate) on May 26, 2015 at 07:59 UTC |