Changeset 4347


Ignore:
Timestamp:
Nov 23, 2009, 12:58:44 AM (14 years ago)
Author:
plg
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/ws_functions.inc.php

    r4346 r4347  
    990990function add_file($file_path, $type, $original_sum, $file_sum)
    991991{
    992   // resolve the $file_path depending on the $type
    993   if ('thumb' == $type) {
    994     $file_path = get_thumbnail_location(
    995       array(
    996         'path' => $file_path,
    997         'tn_ext' => 'jpg',
    998         )
    999       );
    1000   }
    1001 
    1002   if ('high' == $type) {
    1003     @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
    1004     $file_path = get_high_location(
    1005       array(
    1006         'path' => $file_path,
    1007         'has_high' => 'true'
    1008         )
    1009       );
    1010   }
     992  $file_path = file_path_for_type($file_path, $type);
    1011993
    1012994  $upload_dir = dirname($file_path);
     
    15021484}
    15031485
     1486function ws_images_checkFiles($params, &$service)
     1487{
     1488  if (!is_admin() or is_adviser())
     1489  {
     1490    return new PwgError(401, 'Access denied');
     1491  }
     1492
     1493  // input parameters
     1494  //
     1495  // image_id
     1496  // thumbnail_sum
     1497  // file_sum
     1498  // high_sum
     1499
     1500  $params['image_id'] = (int)$params['image_id'];
     1501  if ($params['image_id'] <= 0)
     1502  {
     1503    return new PwgError(WS_ERR_INVALID_PARAM, "Invalid image_id");
     1504  }
     1505
     1506  $query = '
     1507SELECT
     1508    path
     1509  FROM '.IMAGES_TABLE.'
     1510  WHERE id = '.$params['image_id'].'
     1511;';
     1512  $result = pwg_query($query);
     1513  if (mysql_num_rows($result) == 0) {
     1514    return new PwgError(404, "image_id not found");
     1515  }
     1516  list($path) = mysql_fetch_row($result);
     1517
     1518  $ret = array();
     1519
     1520  foreach (array('thumb', 'file', 'high') as $type) {
     1521    $param_name = $type;
     1522    if ('thumb' == $type) {
     1523      $param_name = 'thumbnail';
     1524    }
     1525
     1526    if (isset($params[$param_name.'_sum'])) {
     1527      $type_path = file_path_for_type($path, $type);
     1528      if (!is_file($type_path)) {
     1529        $ret[$param_name] = 'missing';
     1530      }
     1531      else {
     1532        if (md5_file($type_path) != $params[$param_name.'_sum']) {
     1533          $ret[$param_name] = 'differs';
     1534        }
     1535        else {
     1536          $ret[$param_name] = 'equals';
     1537        }
     1538      }
     1539    }
     1540  }
     1541
     1542  return $ret;
     1543}
     1544
     1545function file_path_for_type($file_path, $type='thumb')
     1546{
     1547  // resolve the $file_path depending on the $type
     1548  if ('thumb' == $type) {
     1549    $file_path = get_thumbnail_location(
     1550      array(
     1551        'path' => $file_path,
     1552        'tn_ext' => 'jpg',
     1553        )
     1554      );
     1555  }
     1556
     1557  if ('high' == $type) {
     1558    @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
     1559    $file_path = get_high_location(
     1560      array(
     1561        'path' => $file_path,
     1562        'has_high' => 'true'
     1563        )
     1564      );
     1565  }
     1566
     1567  return $file_path;
     1568}
     1569
    15041570function ws_images_setInfo($params, &$service)
    15051571{
  • trunk/tools/piwigo_remote.pl

    r3454 r4347  
    1818use JSON;
    1919use LWP::UserAgent;
     20# LWP::Debug::level('+');
    2021use Getopt::Long;
    2122use Encode qw/is_utf8 decode/;
     
    4142
    4243our $ua = LWP::UserAgent->new;
     44$ua->agent('Mozilla/piwigo_remote.pl 1.25');
    4345$ua->cookie_jar({});
    4446
     
    5658    $conf{$conf_key} = defined $opt{$conf_key} ? $opt{$conf_key} : $conf_default{$conf_key}
    5759}
     60
     61$ua->default_headers->authorization_basic(
     62    $conf{username},
     63    $conf{password}
     64);
    5865
    5966my $result = undef;
     
    7986    use MIME::Base64 qw(encode_base64);
    8087    use Digest::MD5::File qw/file_md5_hex/;
    81     use File::Slurp;
    8288
    8389    $form = {};
     
    133139    }
    134140    else {
     141        print Dumper($response);
    135142        warn 'A problem has occured during upload', "\n";
    136143        warn $response->decoded_content, "\n";
     
    171178
    172179    $result = $ua->get($query);
     180    print Dumper($result);
    173181    my $tags = from_json($result->content)->{result}{tags};
    174182
     
    235243}
    236244
     245if ($opt{action} eq 'pwg.images.checkFiles') {
     246    use Digest::MD5::File qw/file_md5_hex/;
     247
     248    $form = {};
     249    $form->{method} = $opt{action};
     250
     251    foreach my $type (qw/thumbnail file high/) {
     252        if (defined $opt{$type}) {
     253            $form->{$type.'_sum'} = file_md5_hex($opt{$type});
     254        }
     255    }
     256
     257    foreach my $key (keys %{ $opt{define} }) {
     258        $form->{$key} = $opt{define}{$key};
     259    }
     260
     261    my $response = $ua->post(
     262        $conf{base_url}.'/ws.php?format=json',
     263        $form
     264    );
     265
     266    print "-" x 50, "\n";
     267    printf("response code    : %u\n", $response->code);
     268    printf("response message : %s\n", $response->message);
     269    print "-" x 50, "\n";
     270    print "\n";
     271
     272    use Data::Dumper;
     273    print Dumper(from_json($response->content));
     274}
     275
    237276if ($opt{action} eq 'pwg.images.setInfo' or $opt{action} eq 'pwg.categories.setInfo') {
    238277    $form = {
     
    254293}
    255294
     295if ($opt{action} eq 'pwg.categories.getList') {
     296    $form = {
     297        method => $opt{action},
     298    };
     299
     300    foreach my $key (keys %{ $opt{define} }) {
     301        $form->{$key} = $opt{define}{$key};
     302    }
     303
     304    my $response = $ua->post(
     305        $conf{base_url}.'/ws.php?format=json',
     306        $form
     307    );
     308
     309    use Data::Dumper;
     310    print Dumper($response->content);
     311    print Dumper(from_json($response->content)->{result});
     312    print Dumper($response);
     313}
     314
     315
    256316$query = pwg_ws_get_query(
    257317    method => 'pwg.session.logout'
     
    273333sub send_chunks {
    274334    my %params = @_;
     335
     336    use File::Slurp;
    275337
    276338    my $content = read_file($params{filepath});
  • trunk/ws.php

    r3454 r4347  
    252252
    253253  $service->addMethod(
     254    'pwg.images.checkFiles',
     255    'ws_images_checkFiles',
     256    array(
     257      'image_id' => array(),
     258      'thumbnail_sum' => array('default' => null),
     259      'file_sum' => array('default' => null),
     260      'high_sum' => array('default' => null),
     261      ),
     262    '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"'
     263    );
     264
     265  $service->addMethod(
    254266    'pwg.images.setInfo',
    255267    'ws_images_setInfo',
Note: See TracChangeset for help on using the changeset viewer.