Announcement

#1 2022-01-16 20:07:48

SuperFlyGuy
Member
2022-01-16
3

Calling Piwigo_upload.pl through Ruby script

Hello,

I couldnt quite figure out how to call send a post request to upload images via ruby. However I found a Perl Script that seems to work just fine. So i am calling that Perl Script via Ruby, but although the upload succeeds i get this warning im unfamiliar with

Code:

Use of uninitialized value $v in concatenation (.) or string at /usr/share/perl5/Net/HTTP/Methods.pm line 161.

this is the script I am calling via ruby, I get the same warning if i call it directly through terminal

Code:

#!/usr/bin/perl
 
####
# Usage
#
# perl piwigo_upload.pl --url=http://piwigo.org/demo --user=admin --password=secret --file=photo.jpg --album_id=9
 
use strict;
use warnings;
 
use JSON;
use LWP::UserAgent;
use Getopt::Long;
use POSIX qw(ceil floor);
use Digest::MD5 qw/md5 md5_hex/;
use File::Slurp;
use File::Basename;
 
my %opt = ();
GetOptions(
    \%opt,
    qw/
          file=s
          album_id=i
          category=s
          url=s
          username=s
          password=s
      /
);
 
our %conf = (
    chunk_size => 500_000,
);
 
our $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/piwigo_upload.pl 1.56');
$ua->cookie_jar({});
 
my $result = undef;
 
my $form = {
    method => 'pwg.session.login',
    username => $opt{username},
    password => $opt{password},
};
 
$result = $ua->post(
    $opt{url}.'/ws.php?format=json',
    $form
);
 
my $response = $ua->post(
    $opt{url}.'/ws.php?format=json',
    {
        method => 'pwg.session.getStatus',
    }
);
 
my $pwg_token = from_json($response->content)->{result}->{pwg_token};
 
my $content = read_file($opt{file});
my $content_length = length($content);
my $nb_chunks = ceil($content_length / $conf{chunk_size});
 
my $chunk_pos = 0;
my $chunk_id = 0;
 
while ($chunk_pos < $content_length) {
    my $chunk = substr(
        $content,
        $chunk_pos,
        $conf{chunk_size}
    );
 
    # write the chunk as a temporary local file
    my $chunk_path = '/tmp/'.md5_hex($opt{file}).'.chunk';
 
    open(my $ofh, '>'.$chunk_path) or die "problem for writing temporary local chunk";
    print {$ofh} $chunk;
    close($ofh);
 
    $chunk_pos += $conf{chunk_size};
 
    my $response = $ua->post(
        $opt{url}.'/ws.php?format=json',
        {
            method => 'pwg.images.upload',
            chunk => $chunk_id,
            chunks => $nb_chunks,
            category => $opt{album_id},
            pwg_token => $pwg_token,
            file => [$chunk_path],
            name => basename($opt{file}),
        },
        'Content_Type' => 'form-data',
    );
 
    unlink($chunk_path);
 
    printf(
        'chunk %03u of %03u for "%s"'."\n",
        $chunk_id+1,
        $nb_chunks,
        $opt{file}
    );
 
    if ($response->code != 200) {
        printf("response code    : %u\n", $response->code);
        printf("response message : %s\n", $response->message);
    }
 
    $chunk_id++;
}
 
$result = $ua->get(
    $opt{url}.'/ws.php?format=json',
    {
        method => 'pwg.session.logout'
    }
);

Offline

 

#2 2022-01-18 16:16:43

SuperFlyGuy
Member
2022-01-16
3

Re: Calling Piwigo_upload.pl through Ruby script

Hello,

I've suppressed this warning by adding

Code:

 no warnings 'uninitialized';

in the Methods.pm file. Not such a good idea based on my research but the annoyance is gone at least until someone responds.

Offline

 

#3 2022-01-18 18:22:29

erAck
Only trying to help
2015-09-06
1494

Re: Calling Piwigo_upload.pl through Ruby script

That warning is from /usr/share/perl5/Net/HTTP/Methods.pm so the Perl Net module out of control of anything Piwigo.


Running Piwigo at https://erack.net/gallery/

Offline

 

#4 2022-01-19 00:20:12

SuperFlyGuy
Member
2022-01-16
3

Re: Calling Piwigo_upload.pl through Ruby script

i kinda figured, thanks for trying

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2022 · Contact