feature 1051: new API method pwg.images.checkFiles. This method will be useful

before asking for an update on photo files.

Enhancement in code factorization.

git-svn-id: http://piwigo.org/svn/branches/2.0@4344 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
plegall 2009-11-22 22:09:41 +00:00
parent 2bba9bc48a
commit 6768482bcf
3 changed files with 160 additions and 20 deletions

View file

@ -989,25 +989,7 @@ function merge_chunks($output_filepath, $original_sum, $type)
*/ */
function add_file($file_path, $type, $original_sum, $file_sum) function add_file($file_path, $type, $original_sum, $file_sum)
{ {
// resolve the $file_path depending on the $type $file_path = file_path_for_type($file_path, $type);
if ('thumb' == $type) {
$file_path = get_thumbnail_location(
array(
'path' => $file_path,
'tn_ext' => 'jpg',
)
);
}
if ('high' == $type) {
@include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
$file_path = get_high_location(
array(
'path' => $file_path,
'has_high' => 'true'
)
);
}
$upload_dir = dirname($file_path); $upload_dir = dirname($file_path);
@ -1501,6 +1483,90 @@ SELECT
return $result; return $result;
} }
function ws_images_checkFiles($params, &$service)
{
if (!is_admin() or is_adviser())
{
return new PwgError(401, 'Access denied');
}
// input parameters
//
// image_id
// thumbnail_sum
// file_sum
// high_sum
$params['image_id'] = (int)$params['image_id'];
if ($params['image_id'] <= 0)
{
return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id");
}
$query = '
SELECT
path
FROM '.IMAGES_TABLE.'
WHERE id = '.$params['image_id'].'
;';
$result = pwg_query($query);
if (mysql_num_rows($result) == 0) {
return new PwgError(404, "image_id not found");
}
list($path) = mysql_fetch_row($result);
$ret = array();
foreach (array('thumb', 'file', 'high') as $type) {
$param_name = $type;
if ('thumb' == $type) {
$param_name = 'thumbnail';
}
if (isset($params[$param_name.'_sum'])) {
$type_path = file_path_for_type($path, $type);
if (!is_file($type_path)) {
$ret[$param_name] = 'missing';
}
else {
if (md5_file($type_path) != $params[$param_name.'_sum']) {
$ret[$param_name] = 'differs';
}
else {
$ret[$param_name] = 'equals';
}
}
}
}
return $ret;
}
function file_path_for_type($file_path, $type='thumb')
{
// resolve the $file_path depending on the $type
if ('thumb' == $type) {
$file_path = get_thumbnail_location(
array(
'path' => $file_path,
'tn_ext' => 'jpg',
)
);
}
if ('high' == $type) {
@include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
$file_path = get_high_location(
array(
'path' => $file_path,
'has_high' => 'true'
)
);
}
return $file_path;
}
function ws_images_setInfo($params, &$service) function ws_images_setInfo($params, &$service)
{ {
global $conf; global $conf;

View file

@ -17,6 +17,7 @@ use warnings;
use JSON; use JSON;
use LWP::UserAgent; use LWP::UserAgent;
# LWP::Debug::level('+');
use Getopt::Long; use Getopt::Long;
use Encode qw/is_utf8 decode/; use Encode qw/is_utf8 decode/;
use POSIX qw(ceil floor); use POSIX qw(ceil floor);
@ -40,6 +41,7 @@ GetOptions(
); );
our $ua = LWP::UserAgent->new; our $ua = LWP::UserAgent->new;
$ua->agent('Mozilla/piwigo_remote.pl 1.25');
$ua->cookie_jar({}); $ua->cookie_jar({});
my %conf; my %conf;
@ -56,6 +58,11 @@ foreach my $conf_key (keys %conf_default) {
$conf{$conf_key} = defined $opt{$conf_key} ? $opt{$conf_key} : $conf_default{$conf_key} $conf{$conf_key} = defined $opt{$conf_key} ? $opt{$conf_key} : $conf_default{$conf_key}
} }
$ua->default_headers->authorization_basic(
$conf{username},
$conf{password}
);
my $result = undef; my $result = undef;
my $query = undef; my $query = undef;
@ -78,7 +85,6 @@ $result = $ua->post(
if ($opt{action} eq 'pwg.images.add') { if ($opt{action} eq 'pwg.images.add') {
use MIME::Base64 qw(encode_base64); use MIME::Base64 qw(encode_base64);
use Digest::MD5::File qw/file_md5_hex/; use Digest::MD5::File qw/file_md5_hex/;
use File::Slurp;
$form = {}; $form = {};
$form->{method} = 'pwg.images.add'; $form->{method} = 'pwg.images.add';
@ -132,6 +138,7 @@ if ($opt{action} eq 'pwg.images.add') {
print "upload successful\n"; print "upload successful\n";
} }
else { else {
print Dumper($response);
warn 'A problem has occured during upload', "\n"; warn 'A problem has occured during upload', "\n";
warn $response->decoded_content, "\n"; warn $response->decoded_content, "\n";
die $response->status_line; die $response->status_line;
@ -170,6 +177,7 @@ if ($opt{action} eq 'pwg.tags.getAdminList') {
); );
$result = $ua->get($query); $result = $ua->get($query);
print Dumper($result);
my $tags = from_json($result->content)->{result}{tags}; my $tags = from_json($result->content)->{result}{tags};
foreach my $tag (@{$tags}) { foreach my $tag (@{$tags}) {
@ -234,6 +242,37 @@ if ($opt{action} eq 'pwg.images.exist') {
# print Dumper($response); # print Dumper($response);
} }
if ($opt{action} eq 'pwg.images.checkFiles') {
use Digest::MD5::File qw/file_md5_hex/;
$form = {};
$form->{method} = $opt{action};
foreach my $type (qw/thumbnail file high/) {
if (defined $opt{$type}) {
$form->{$type.'_sum'} = file_md5_hex($opt{$type});
}
}
foreach my $key (keys %{ $opt{define} }) {
$form->{$key} = $opt{define}{$key};
}
my $response = $ua->post(
$conf{base_url}.'/ws.php?format=json',
$form
);
print "-" x 50, "\n";
printf("response code : %u\n", $response->code);
printf("response message : %s\n", $response->message);
print "-" x 50, "\n";
print "\n";
use Data::Dumper;
print Dumper(from_json($response->content));
}
if ($opt{action} eq 'pwg.images.setInfo' or $opt{action} eq 'pwg.categories.setInfo') { if ($opt{action} eq 'pwg.images.setInfo' or $opt{action} eq 'pwg.categories.setInfo') {
$form = { $form = {
method => $opt{action}, method => $opt{action},
@ -253,6 +292,27 @@ if ($opt{action} eq 'pwg.images.setInfo' or $opt{action} eq 'pwg.categories.setI
print Dumper($response); print Dumper($response);
} }
if ($opt{action} eq 'pwg.categories.getList') {
$form = {
method => $opt{action},
};
foreach my $key (keys %{ $opt{define} }) {
$form->{$key} = $opt{define}{$key};
}
my $response = $ua->post(
$conf{base_url}.'/ws.php?format=json',
$form
);
use Data::Dumper;
print Dumper($response->content);
print Dumper(from_json($response->content)->{result});
print Dumper($response);
}
$query = pwg_ws_get_query( $query = pwg_ws_get_query(
method => 'pwg.session.logout' method => 'pwg.session.logout'
); );
@ -273,6 +333,8 @@ sub pwg_ws_get_query {
sub send_chunks { sub send_chunks {
my %params = @_; my %params = @_;
use File::Slurp;
my $content = read_file($params{filepath}); my $content = read_file($params{filepath});
my $content_length = length($content); my $content_length = length($content);
my $nb_chunks = ceil($content_length / $conf{chunk_size}); my $nb_chunks = ceil($content_length / $conf{chunk_size});

12
ws.php
View file

@ -250,6 +250,18 @@ function ws_addDefaultMethods( $arr )
'check existence of a photo list' 'check existence of a photo list'
); );
$service->addMethod(
'pwg.images.checkFiles',
'ws_images_checkFiles',
array(
'image_id' => array(),
'thumbnail_sum' => array('default' => null),
'file_sum' => array('default' => null),
'high_sum' => array('default' => null),
),
'check if you have updated version of your files for a given photo, for each requested file type, the answer can be "missing", "equals" or "differs"'
);
$service->addMethod( $service->addMethod(
'pwg.images.setInfo', 'pwg.images.setInfo',
'ws_images_setInfo', 'ws_images_setInfo',