Committer: ssafronova
LJSUP-6426, LJSUP-6591 - improve cropping of pictures with bad urlU trunk/cgi-bin/ljuserpics.pl
Modified: trunk/cgi-bin/ljuserpics.pl =================================================================== --- trunk/cgi-bin/ljuserpics.pl 2010-08-25 07:05:28 UTC (rev 17194) +++ trunk/cgi-bin/ljuserpics.pl 2010-08-25 08:06:15 UTC (rev 17195) @@ -2,6 +2,7 @@ use strict; use Digest::MD5 qw(md5_hex); +use HTTP::Request::Common qw/GET/; # <LJFUNC> # name: LJ::fetch_userpic @@ -855,7 +856,7 @@ my %opts = @_; my $dataref = $opts{dataref}; - my $gals = $opts{gals}; + my $gals = $opts{gals} || []; my $username = $opts{username} || $LJ::FB_USER; my $password = $opts{password} || $LJ::FB_PASS; @@ -932,6 +933,7 @@ url => undef, status => 'error', errstr => ref $err eq 'HASH' ? $err->{content} : (ref $err eq 'ARRAY' ? $err->[0] : $err), + opts => \%opts, } } @@ -942,5 +944,65 @@ } } +# get picture from internet $opts{source} and crop it to $opts{size}, +# than save result into $opts{galleries} (arrayref) of scrapbook of $opts{username}, using $opts{password} +# returns result of &upload_to_fb +sub crop_picture_from_web { + my %opts = @_; + + my $source = LJ::trim($opts{source}); + + return { + url => '', + status => 'ok', + } unless $source; + + ## fetch a photo from Net + my $ua = LJ::get_useragent( role => 'crop_picture', + max_size => 10 * 1024 * 1024, + timeout => 10, + ); + my $result = $ua->request(GET($source)); + + unless ($result and $result->is_success) { + return { + picid => -1, + url => undef, + status => 'error', + errstr => $result ? $result->status_line : 'unknown error in downloading', + }; + } + + my $data = $result->content; + + my $res = LJ::_get_upf_scaled( + source => \$data, + size => $opts{size}, + save_to_FB => 1, + auto_crop => 1, + fb_username => $opts{username}, + fb_password => $opts{password}, + fb_gallery => $opts{galleries}, + ); + + unless ($res) { + return { + picid => -1, + url => undef, + status => 'error', + errstr => 'probably bad picture', + }; + } + + # need to repeat? (because of bad auth in CentOS-32 ScrapBook) + # DELETE THIS IN FUTURE!!! + if ($res->{picid} == -1) { + warn $res->{errstr} if $LJ::IS_DEV_SERVER; + return upload_to_fb(%{$res->{opts}}); + } + + return $res; +} + 1;