Ignore:
Timestamp:
Jan 17, 2012, 1:11:14 AM (12 years ago)
Author:
plg
Message:

feature 2548 (multiple sizes): adapt the web API method pwg.images.add (used
by pLoader, Digikam, Lightroom, iPhoto), pwg.images.checkFiles (pLoader only).

The "resize" parameter was removed for pwg.images.add since this behavior
becomes the default behavior in Piwigo 2.4.

Just like pwg.images.addSimple, pwg.images.add now uses the add_uploaded_file
function (next step is to merge pwg.images.add and pwg.images.addSimple)

File:
1 edited

Legend:

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

    r12865 r12906  
    14321432  global $conf;
    14331433
    1434   ws_logfile('[ws_images_add_chunk] welcome');
    14351434  // data
    14361435  // original_sum
     
    15701569}
    15711570
     1571/**
     1572 * Function introduced for Piwigo 2.4 and the new "multiple size"
     1573 * (derivatives) feature. As we only need the biggest sent photo as
     1574 * "original", we remove chunks for smaller sizes. We can't make it earlier
     1575 * in ws_images_add_chunk because at this moment we don't know which $type
     1576 * will be the biggest (we could remove the thumb, but let's use the same
     1577 * algorithm)
     1578 */
     1579function remove_chunks($original_sum, $type)
     1580{
     1581  global $conf;
     1582
     1583  $upload_dir = $conf['upload_dir'].'/buffer';
     1584  $pattern = '/'.$original_sum.'-'.$type.'/';
     1585  $chunks = array();
     1586
     1587  if ($handle = opendir($upload_dir))
     1588  {
     1589    while (false !== ($file = readdir($handle)))
     1590    {
     1591      if (preg_match($pattern, $file))
     1592      {
     1593        array_push($chunks, $upload_dir.'/'.$file);
     1594      }
     1595    }
     1596    closedir($handle);
     1597  }
     1598
     1599  foreach ($chunks as $chunk)
     1600  {
     1601    unlink($chunk);
     1602  }
     1603}
     1604
    15721605/*
    15731606 * The $file_path must be the path of the basic "web sized" photo
     
    15781611  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
    15791612
    1580   $file_path = file_path_for_type($file_path, $type);
    1581 
    15821613  $upload_dir = dirname($file_path);
    15831614  if (substr(PHP_OS, 0, 3) == 'WIN')
     
    16111642  secure_directory($upload_dir);
    16121643
    1613   // merge the thumbnail
     1644  // merge the file
    16141645  merge_chunks($file_path, $original_sum, $type);
    16151646  chmod($file_path, 0644);
     
    16341665function ws_images_addFile($params, &$service)
    16351666{
     1667  ws_logfile(__FUNCTION__.', input :  '.var_export($params, true));
    16361668  // image_id
    16371669  // type {thumb, file, high}
    1638   // sum
     1670  // sum -> not used currently (Piwigo 2.4)
    16391671
    16401672  global $conf;
     
    16511683
    16521684  //
    1653   // what is the path?
     1685  // what is the path and other infos about the photo?
    16541686  //
    16551687  $query = '
    16561688SELECT
    16571689    path,
    1658     md5sum
     1690    file,
     1691    md5sum,
     1692    width,
     1693    height,
     1694    filesize
    16591695  FROM '.IMAGES_TABLE.'
    16601696  WHERE id = '.$params['image_id'].'
    16611697;';
    1662   list($file_path, $original_sum) = pwg_db_fetch_row(pwg_query($query));
    1663 
    1664   // TODO only files added with web API can be updated with web API
    1665 
    1666   //
    1667   // makes sure directories are there and call the merge_chunks
    1668   //
    1669   $infos = add_file($file_path, $params['type'], $original_sum, $params['sum']);
    1670 
    1671   //
    1672   // update basic metadata from file
    1673   //
    1674   $update = array();
    1675 
     1698  $image = pwg_db_fetch_assoc(pwg_query($query));
     1699
     1700  if ($image == null)
     1701  {
     1702    return new PwgError(404, "image_id not found");
     1703  }
     1704
     1705  // since Piwigo 2.4 and derivatives, we do not take the imported "thumb"
     1706  // into account
     1707  if ('thumb' == $params['type'])
     1708  {
     1709    remove_chunks($image['md5sum'], $type);
     1710    return true;
     1711  }
     1712
     1713  // since Piwigo 2.4 and derivatives, we only care about the "original"
     1714  $original_type = 'file';
    16761715  if ('high' == $params['type'])
    16771716  {
    1678     $update['high_filesize'] = $infos['filesize'];
    1679     $update['high_width'] = $infos['width'];
    1680     $update['high_height'] = $infos['height'];
    1681     $update['has_high'] = 'true';
    1682   }
    1683 
     1717    $original_type = 'high';
     1718  }
     1719
     1720  $file_path = $conf['upload_dir'].'/buffer/'.$image['md5sum'].'-original';
     1721
     1722  merge_chunks($file_path, $image['md5sum'], $original_type);
     1723  chmod($file_path, 0644);
     1724
     1725  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
     1726
     1727  // if we receive the "file", we only update the original if the "file" is
     1728  // bigger than current original
    16841729  if ('file' == $params['type'])
    16851730  {
    1686     $update['filesize'] = $infos['filesize'];
    1687     $update['width'] = $infos['width'];
    1688     $update['height'] = $infos['height'];
    1689   }
    1690 
    1691   // we may have nothing to update at database level, for example with a
    1692   // thumbnail update
    1693   if (count($update) > 0)
    1694   {
    1695     $update['id'] = $params['image_id'];
    1696 
    1697     include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    1698     mass_updates(
    1699       IMAGES_TABLE,
    1700       array(
    1701         'primary' => array('id'),
    1702         'update'  => array_diff(array_keys($update), array('id'))
    1703         ),
    1704       array($update)
    1705       );
    1706   }
     1731    $do_update = false;
     1732   
     1733    $infos = pwg_image_infos($file_path);
     1734   
     1735    foreach (array('width', 'height', 'filesize') as $image_info)
     1736    {
     1737      if ($infos[$image_info] > $image[$image_info])
     1738      {
     1739        $do_update = true;
     1740      }
     1741    }
     1742
     1743    if (!$do_update)
     1744    {
     1745      unlink($file_path);
     1746      return true;
     1747    }
     1748  }
     1749
     1750  $image_id = add_uploaded_file(
     1751    $file_path,
     1752    $image['file'],
     1753    null,
     1754    null,
     1755    $params['image_id'],
     1756    $image['md5sum'] // we force the md5sum to remain the same
     1757    );
     1758
     1759  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     1760  delete_element_derivatives($params['image_id']);
    17071761}
    17081762
     
    17491803  }
    17501804
    1751   if ($params['resize'])
    1752   {
    1753     ws_logfile('[pwg.images.add] resize activated');
    1754 
    1755     // temporary file path
    1756     $type = 'file';
    1757     $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-'.$type;
    1758 
    1759     merge_chunks($file_path, $params['original_sum'], $type);
    1760     chmod($file_path, 0644);
    1761 
    1762     include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
    1763 
    1764     $image_id = add_uploaded_file(
    1765       $file_path,
    1766       $params['original_filename']
    1767       );
    1768 
    1769     // add_uploaded_file doesn't remove the original file in the buffer
    1770     // directory if it was not uploaded as $_FILES
    1771     unlink($file_path);
     1805  // due to the new feature "derivatives" (multiple sizes) introduced for
     1806  // Piwigo 2.4, we only take the biggest photos sent on
     1807  // pwg.images.addChunk. If "high" is available we use it as "original"
     1808  // else we use "file".
     1809  remove_chunks($params['original_sum'], 'thumb');
     1810 
     1811  if (isset($params['high_sum']))
     1812  {
     1813    $original_type = 'high';
     1814    remove_chunks($params['original_sum'], 'file');
    17721815  }
    17731816  else
    17741817  {
    1775     // current date
    1776     list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
    1777     list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
    1778 
    1779     // upload directory hierarchy
    1780     $upload_dir = sprintf(
    1781       $conf['upload_dir'].'/%s/%s/%s',
    1782       $year,
    1783       $month,
    1784       $day
    1785       );
    1786 
    1787     // compute file path
    1788     $date_string = preg_replace('/[^\d]/', '', $dbnow);
    1789     $random_string = substr($params['file_sum'], 0, 8);
    1790     $filename_wo_ext = $date_string.'-'.$random_string;
    1791     $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg';
    1792 
    1793     // add files
    1794     $file_infos  = add_file($file_path, 'file',  $params['original_sum'], $params['file_sum']);
    1795     $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']);
    1796 
    1797     if (isset($params['high_sum']))
    1798     {
    1799       $high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']);
    1800     }
    1801 
    1802     // database registration
    1803     $insert = array(
    1804       'file' => !empty($params['original_filename']) ? $params['original_filename'] : $filename_wo_ext.'.jpg',
    1805       'date_available' => $dbnow,
    1806       'tn_ext' => 'jpg',
    1807       'name' => $params['name'],
    1808       'path' => $file_path,
    1809       'filesize' => $file_infos['filesize'],
    1810       'width' => $file_infos['width'],
    1811       'height' => $file_infos['height'],
    1812       'md5sum' => $params['original_sum'],
    1813       'added_by' => $user['id'],
    1814       );
    1815 
    1816     if (isset($params['high_sum']))
    1817     {
    1818       $insert['has_high'] = 'true';
    1819       $insert['high_filesize'] = $high_infos['filesize'];
    1820       $insert['high_width'] = $high_infos['width'];
    1821       $insert['high_height'] = $high_infos['height'];
    1822     }
    1823 
    1824     single_insert(
    1825       IMAGES_TABLE,
    1826       $insert
    1827       );
    1828 
    1829     $image_id = pwg_db_insert_id(IMAGES_TABLE);
    1830 
    1831     // update metadata from the uploaded file (exif/iptc)
    1832     require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
    1833     sync_metadata(array($image_id));
    1834   }
     1818    $original_type = 'file';
     1819  }
     1820
     1821  $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-original';
     1822
     1823  merge_chunks($file_path, $params['original_sum'], $original_type);
     1824  chmod($file_path, 0644);
     1825
     1826  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
     1827
     1828  $image_id = add_uploaded_file(
     1829    $file_path,
     1830    $params['original_filename'],
     1831    null, // categories
     1832    isset($params['level']) ? $params['level'] : null,
     1833    null, // image_id
     1834    $params['original_sum']
     1835    );
    18351836
    18361837  $info_columns = array(
     
    18381839    'author',
    18391840    'comment',
    1840     'level',
    18411841    'date_creation',
    18421842    );
     1843
     1844  $update = array();
    18431845
    18441846  foreach ($info_columns as $key)
     
    23392341function ws_images_exist($params, &$service)
    23402342{
     2343  ws_logfile(__FUNCTION__.' '.var_export($params, true));
     2344
    23412345  global $conf;
    23422346
     
    24172421function ws_images_checkFiles($params, &$service)
    24182422{
     2423  ws_logfile(__FUNCTION__.', input :  '.var_export($params, true));
     2424
    24192425  if (!is_admin())
    24202426  {
     
    24422448;';
    24432449  $result = pwg_query($query);
    2444   if (pwg_db_num_rows($result) == 0) {
     2450  if (pwg_db_num_rows($result) == 0)
     2451  {
    24452452    return new PwgError(404, "image_id not found");
    24462453  }
     
    24492456  $ret = array();
    24502457
    2451   foreach (array('thumb', 'file', 'high') as $type) {
    2452     $param_name = $type;
    2453     if ('thumb' == $type) {
    2454       $param_name = 'thumbnail';
    2455     }
    2456 
    2457     if (isset($params[$param_name.'_sum'])) {
    2458       include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
    2459       $type_path = file_path_for_type($path, $type);
    2460       if (!is_file($type_path)) {
    2461         $ret[$param_name] = 'missing';
    2462       }
    2463       else {
    2464         if (md5_file($type_path) != $params[$param_name.'_sum']) {
    2465           $ret[$param_name] = 'differs';
    2466         }
    2467         else {
    2468           $ret[$param_name] = 'equals';
    2469         }
    2470       }
    2471     }
    2472   }
     2458  if (isset($params['thumbnail_sum']))
     2459  {
     2460    // We always say the thumbnail is equal to create no reaction on the
     2461    // other side. Since Piwigo 2.4 and derivatives, the thumbnails and web
     2462    // sizes are always generated by Piwigo
     2463    $ret['thumbnail'] = 'equals';
     2464  }
     2465
     2466  if (isset($params['high_sum']))
     2467  {
     2468    $ret['file'] = 'equals';
     2469    $compare_type = 'high';
     2470  }
     2471  elseif (isset($params['file_sum']))
     2472  {
     2473    $compare_type = 'file';
     2474  }
     2475
     2476  if (isset($compare_type))
     2477  {
     2478    ws_logfile(__FUNCTION__.', md5_file($path) = '.md5_file($path));
     2479    if (md5_file($path) != $params[$compare_type.'_sum'])
     2480    {
     2481      $ret[$compare_type] = 'differs';
     2482    }
     2483    else
     2484    {
     2485      $ret[$compare_type] = 'equals';
     2486    }
     2487  }
     2488
     2489  ws_logfile(__FUNCTION__.', output :  '.var_export($ret, true));
    24732490
    24742491  return $ret;
Note: See TracChangeset for help on using the changeset viewer.