Permalink
Browse files

merge r4344 from branch 2.0 to trunk

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/trunk@4347 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information...
plegall committed Nov 22, 2009
1 parent 7ee33e9 commit 858b43e87a9911269505c1fbc9036b374e89a565
Showing with 160 additions and 20 deletions.
  1. +85 −19 include/ws_functions.inc.php
  2. +63 −1 tools/piwigo_remote.pl
  3. +12 −0 ws.php
@@ -989,25 +989,7 @@ function merge_chunks($output_filepath, $original_sum, $type)
*/
function add_file($file_path, $type, $original_sum, $file_sum)
{
- // 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'
- )
- );
- }
+ $file_path = file_path_for_type($file_path, $type);
$upload_dir = dirname($file_path);
@@ -1501,6 +1483,90 @@ function ws_images_exist($params, &$service)
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)
{
global $conf;
View
@@ -17,6 +17,7 @@
use JSON;
use LWP::UserAgent;
+# LWP::Debug::level('+');
use Getopt::Long;
use Encode qw/is_utf8 decode/;
use POSIX qw(ceil floor);
@@ -40,6 +41,7 @@
);
our $ua = LWP::UserAgent->new;
+$ua->agent('Mozilla/piwigo_remote.pl 1.25');
$ua->cookie_jar({});
my %conf;
@@ -56,6 +58,11 @@
$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 $query = undef;
@@ -78,7 +85,6 @@
if ($opt{action} eq 'pwg.images.add') {
use MIME::Base64 qw(encode_base64);
use Digest::MD5::File qw/file_md5_hex/;
- use File::Slurp;
$form = {};
$form->{method} = 'pwg.images.add';
@@ -132,6 +138,7 @@
print "upload successful\n";
}
else {
+ print Dumper($response);
warn 'A problem has occured during upload', "\n";
warn $response->decoded_content, "\n";
die $response->status_line;
@@ -170,6 +177,7 @@
);
$result = $ua->get($query);
+ print Dumper($result);
my $tags = from_json($result->content)->{result}{tags};
foreach my $tag (@{$tags}) {
@@ -234,6 +242,37 @@
# 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') {
$form = {
method => $opt{action},
@@ -253,6 +292,27 @@
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(
method => 'pwg.session.logout'
);
@@ -273,6 +333,8 @@ sub pwg_ws_get_query {
sub send_chunks {
my %params = @_;
+ use File::Slurp;
+
my $content = read_file($params{filepath});
my $content_length = length($content);
my $nb_chunks = ceil($content_length / $conf{chunk_size});
View
12 ws.php
@@ -250,6 +250,18 @@ function ws_addDefaultMethods( $arr )
'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(
'pwg.images.setInfo',
'ws_images_setInfo',

0 comments on commit 858b43e

Please sign in to comment.