mirror of
https://github.com/Piwigo/Piwigo.git
synced 2026-06-08 23:35:05 +02:00
Page:
pwg.images.upload
Pages
Branches and releases
Coding guidelines
Compilation of personal plugins
Database upgrade
Git workflow and best pratices
Home
How to create a theme
Logging
Migrate a plugin from Piwigo.org SVN to Github
Multiple Site (Multisite)
Piwigo Web API
Plugin Tutorial: Hello world!
Plugin Tutorial: Several tricks from the Copyrights plugin
Plugin developpement
Publishing an extension
Security and coding
Send emails with Piwigo
Technical changes in Piwigo 11
Technical changes in Piwigo 12
Technical changes in Piwigo 13
Technical changes in Piwigo 14
Technical changes in Piwigo 15
Technical changes in Piwigo 16
Technical changes in Piwigo 2.10
Technical changes in Piwigo 2.2
Technical changes in Piwigo 2.3
Technical changes in Piwigo 2.4
Technical changes in Piwigo 2.5
Technical changes in Piwigo 2.6
Technical changes in Piwigo 2.7
Technical changes in Piwigo 2.8
Use a source code manager for your extension
pwg.images.add and pwg.images.addChunk
pwg.images.addSimple
pwg.images.upload
Clone
1
pwg.images.upload
Pierrick Le Gall edited this page 2023-05-04 18:19:20 +02:00
Table of Contents
Add a photo.
Similar to pwg.images.addChunk + pwg.images.add, but simpler and better suited for Piwigo 2.7+
Authentication
This method requires authentication with an administrator user.
This method requires an HTTP POST request.
This method also requires a pwg_token (session valid key).
Parameters
Important note: we are uploading a file, so the HTTP request encoding must be set to "form-data".
| Key | Example | Mandatory? | Description |
|---|---|---|---|
| image | /path/to/file.jpg | mandatory | the uploaded file. JPG or PNG files only. |
| category | 123 | mandatory | the identifier of the destination category, use pwg.categories.getList to find existing categories |
| pwg_token | verylongstring | mandatory | this is a session key, once identified you can get it with method pwg.session.getStatus |
| chunks | 12 | optional | number of expected chunks |
| chunk | 0 | optional | current chunk number, from 0 to chunks-1 |
| name | A nice title | optional | the title of your photo, don't make it too long (use the comment to go into details |
| name | A nice title | optional | the title of your photo, don't make it too long (use the comment to go into details |
| level | 2 | optional | (default = 0) privacy setting. See an example in your Piwigo on screen Administration > Photos > Add. 0 is for everybody, 8 is for admins only |
Example
Here comes a full example in Perl language (see the most updated version on piwigo_upload.pl on source code repository):
#!/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'
}
);