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)


git-svn-id: http://piwigo.org/svn/trunk@12906 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
plegall 2012-01-17 00:11:14 +00:00
parent a7f39070f4
commit f7421e5789
4 changed files with 182 additions and 154 deletions

View file

@ -2307,4 +2307,17 @@ function clear_derivative_cache_rec($path, $pattern)
return $rmdir; return $rmdir;
} }
} }
function delete_element_derivatives($ids)
{
// todo
if (!is_array($ids))
{
$ids = array($ids);
}
// for now I do a massive clear, to be removed once the function is
// properly implemented
clear_derivative_cache();
}
?> ?>

View file

@ -150,7 +150,7 @@ function save_upload_form_config($data, &$errors=array())
return false; return false;
} }
function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null) function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null, $original_md5sum=null)
{ {
// 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg // 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg
// //
@ -163,7 +163,15 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
global $conf, $user; global $conf, $user;
$md5sum = md5_file($source_filepath); if (isset($original_md5sum))
{
$md5sum = $original_md5sum;
}
else
{
$md5sum = md5_file($source_filepath);
}
$file_path = null; $file_path = null;
if (isset($image_id)) if (isset($image_id))
@ -234,7 +242,7 @@ SELECT
} }
else else
{ {
copy($source_filepath, $file_path); rename($source_filepath, $file_path);
} }
if (pwg_image::get_library() != 'gd') if (pwg_image::get_library() != 'gd')
@ -291,7 +299,6 @@ SELECT
$insert = array( $insert = array(
'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
'date_available' => $dbnow, 'date_available' => $dbnow,
'tn_ext' => 'jpg',
'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path), 'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
'filesize' => $file_infos['filesize'], 'filesize' => $file_infos['filesize'],
'width' => $file_infos['width'], 'width' => $file_infos['width'],
@ -300,14 +307,6 @@ SELECT
'added_by' => $user['id'], 'added_by' => $user['id'],
); );
if (isset($high_infos))
{
$insert['has_high'] = 'true';
$insert['high_filesize'] = $high_infos['filesize'];
$insert['high_width'] = $high_infos['width'];
$insert['high_height'] = $high_infos['height'];
}
if (isset($level)) if (isset($level))
{ {
$insert['level'] = $level; $insert['level'] = $level;

View file

@ -1431,7 +1431,6 @@ function ws_images_add_chunk($params, &$service)
{ {
global $conf; global $conf;
ws_logfile('[ws_images_add_chunk] welcome');
// data // data
// original_sum // original_sum
// type {thumb, file, high} // type {thumb, file, high}
@ -1569,6 +1568,40 @@ function merge_chunks($output_filepath, $original_sum, $type)
} }
} }
/**
* Function introduced for Piwigo 2.4 and the new "multiple size"
* (derivatives) feature. As we only need the biggest sent photo as
* "original", we remove chunks for smaller sizes. We can't make it earlier
* in ws_images_add_chunk because at this moment we don't know which $type
* will be the biggest (we could remove the thumb, but let's use the same
* algorithm)
*/
function remove_chunks($original_sum, $type)
{
global $conf;
$upload_dir = $conf['upload_dir'].'/buffer';
$pattern = '/'.$original_sum.'-'.$type.'/';
$chunks = array();
if ($handle = opendir($upload_dir))
{
while (false !== ($file = readdir($handle)))
{
if (preg_match($pattern, $file))
{
array_push($chunks, $upload_dir.'/'.$file);
}
}
closedir($handle);
}
foreach ($chunks as $chunk)
{
unlink($chunk);
}
}
/* /*
* The $file_path must be the path of the basic "web sized" photo * The $file_path must be the path of the basic "web sized" photo
* The $type value will automatically modify the $file_path to the corresponding file * The $type value will automatically modify the $file_path to the corresponding file
@ -1577,8 +1610,6 @@ function add_file($file_path, $type, $original_sum, $file_sum)
{ {
include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
$file_path = file_path_for_type($file_path, $type);
$upload_dir = dirname($file_path); $upload_dir = dirname($file_path);
if (substr(PHP_OS, 0, 3) == 'WIN') if (substr(PHP_OS, 0, 3) == 'WIN')
{ {
@ -1610,7 +1641,7 @@ function add_file($file_path, $type, $original_sum, $file_sum)
secure_directory($upload_dir); secure_directory($upload_dir);
// merge the thumbnail // merge the file
merge_chunks($file_path, $original_sum, $type); merge_chunks($file_path, $original_sum, $type);
chmod($file_path, 0644); chmod($file_path, 0644);
@ -1633,9 +1664,10 @@ function add_file($file_path, $type, $original_sum, $file_sum)
function ws_images_addFile($params, &$service) function ws_images_addFile($params, &$service)
{ {
ws_logfile(__FUNCTION__.', input : '.var_export($params, true));
// image_id // image_id
// type {thumb, file, high} // type {thumb, file, high}
// sum // sum -> not used currently (Piwigo 2.4)
global $conf; global $conf;
if (!is_admin()) if (!is_admin())
@ -1650,60 +1682,82 @@ function ws_images_addFile($params, &$service)
} }
// //
// what is the path? // what is the path and other infos about the photo?
// //
$query = ' $query = '
SELECT SELECT
path, path,
md5sum file,
md5sum,
width,
height,
filesize
FROM '.IMAGES_TABLE.' FROM '.IMAGES_TABLE.'
WHERE id = '.$params['image_id'].' WHERE id = '.$params['image_id'].'
;'; ;';
list($file_path, $original_sum) = pwg_db_fetch_row(pwg_query($query)); $image = pwg_db_fetch_assoc(pwg_query($query));
// TODO only files added with web API can be updated with web API if ($image == null)
{
return new PwgError(404, "image_id not found");
}
// // since Piwigo 2.4 and derivatives, we do not take the imported "thumb"
// makes sure directories are there and call the merge_chunks // into account
// if ('thumb' == $params['type'])
$infos = add_file($file_path, $params['type'], $original_sum, $params['sum']); {
remove_chunks($image['md5sum'], $type);
// return true;
// update basic metadata from file }
//
$update = array();
// since Piwigo 2.4 and derivatives, we only care about the "original"
$original_type = 'file';
if ('high' == $params['type']) if ('high' == $params['type'])
{ {
$update['high_filesize'] = $infos['filesize']; $original_type = 'high';
$update['high_width'] = $infos['width'];
$update['high_height'] = $infos['height'];
$update['has_high'] = 'true';
} }
$file_path = $conf['upload_dir'].'/buffer/'.$image['md5sum'].'-original';
merge_chunks($file_path, $image['md5sum'], $original_type);
chmod($file_path, 0644);
include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
// if we receive the "file", we only update the original if the "file" is
// bigger than current original
if ('file' == $params['type']) if ('file' == $params['type'])
{ {
$update['filesize'] = $infos['filesize']; $do_update = false;
$update['width'] = $infos['width'];
$update['height'] = $infos['height']; $infos = pwg_image_infos($file_path);
foreach (array('width', 'height', 'filesize') as $image_info)
{
if ($infos[$image_info] > $image[$image_info])
{
$do_update = true;
}
}
if (!$do_update)
{
unlink($file_path);
return true;
}
} }
// we may have nothing to update at database level, for example with a $image_id = add_uploaded_file(
// thumbnail update $file_path,
if (count($update) > 0) $image['file'],
{ null,
$update['id'] = $params['image_id']; null,
$params['image_id'],
$image['md5sum'] // we force the md5sum to remain the same
);
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
mass_updates( delete_element_derivatives($params['image_id']);
IMAGES_TABLE,
array(
'primary' => array('id'),
'update' => array_diff(array_keys($update), array('id'))
),
array($update)
);
}
} }
function ws_images_add($params, &$service) function ws_images_add($params, &$service)
@ -1748,99 +1802,47 @@ SELECT
} }
} }
if ($params['resize']) // due to the new feature "derivatives" (multiple sizes) introduced for
// Piwigo 2.4, we only take the biggest photos sent on
// pwg.images.addChunk. If "high" is available we use it as "original"
// else we use "file".
remove_chunks($params['original_sum'], 'thumb');
if (isset($params['high_sum']))
{ {
ws_logfile('[pwg.images.add] resize activated'); $original_type = 'high';
remove_chunks($params['original_sum'], 'file');
// temporary file path
$type = 'file';
$file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-'.$type;
merge_chunks($file_path, $params['original_sum'], $type);
chmod($file_path, 0644);
include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
$image_id = add_uploaded_file(
$file_path,
$params['original_filename']
);
// add_uploaded_file doesn't remove the original file in the buffer
// directory if it was not uploaded as $_FILES
unlink($file_path);
} }
else else
{ {
// current date $original_type = 'file';
list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
// upload directory hierarchy
$upload_dir = sprintf(
$conf['upload_dir'].'/%s/%s/%s',
$year,
$month,
$day
);
// compute file path
$date_string = preg_replace('/[^\d]/', '', $dbnow);
$random_string = substr($params['file_sum'], 0, 8);
$filename_wo_ext = $date_string.'-'.$random_string;
$file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg';
// add files
$file_infos = add_file($file_path, 'file', $params['original_sum'], $params['file_sum']);
$thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']);
if (isset($params['high_sum']))
{
$high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']);
}
// database registration
$insert = array(
'file' => !empty($params['original_filename']) ? $params['original_filename'] : $filename_wo_ext.'.jpg',
'date_available' => $dbnow,
'tn_ext' => 'jpg',
'name' => $params['name'],
'path' => $file_path,
'filesize' => $file_infos['filesize'],
'width' => $file_infos['width'],
'height' => $file_infos['height'],
'md5sum' => $params['original_sum'],
'added_by' => $user['id'],
);
if (isset($params['high_sum']))
{
$insert['has_high'] = 'true';
$insert['high_filesize'] = $high_infos['filesize'];
$insert['high_width'] = $high_infos['width'];
$insert['high_height'] = $high_infos['height'];
}
single_insert(
IMAGES_TABLE,
$insert
);
$image_id = pwg_db_insert_id(IMAGES_TABLE);
// update metadata from the uploaded file (exif/iptc)
require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
sync_metadata(array($image_id));
} }
$file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-original';
merge_chunks($file_path, $params['original_sum'], $original_type);
chmod($file_path, 0644);
include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
$image_id = add_uploaded_file(
$file_path,
$params['original_filename'],
null, // categories
isset($params['level']) ? $params['level'] : null,
null, // image_id
$params['original_sum']
);
$info_columns = array( $info_columns = array(
'name', 'name',
'author', 'author',
'comment', 'comment',
'level',
'date_creation', 'date_creation',
); );
$update = array();
foreach ($info_columns as $key) foreach ($info_columns as $key)
{ {
if (isset($params[$key])) if (isset($params[$key]))
@ -2338,6 +2340,8 @@ function ws_tags_add($params, &$service)
function ws_images_exist($params, &$service) function ws_images_exist($params, &$service)
{ {
ws_logfile(__FUNCTION__.' '.var_export($params, true));
global $conf; global $conf;
if (!is_admin()) if (!is_admin())
@ -2416,6 +2420,8 @@ SELECT
function ws_images_checkFiles($params, &$service) function ws_images_checkFiles($params, &$service)
{ {
ws_logfile(__FUNCTION__.', input : '.var_export($params, true));
if (!is_admin()) if (!is_admin())
{ {
return new PwgError(401, 'Access denied'); return new PwgError(401, 'Access denied');
@ -2441,36 +2447,47 @@ SELECT
WHERE id = '.$params['image_id'].' WHERE id = '.$params['image_id'].'
;'; ;';
$result = pwg_query($query); $result = pwg_query($query);
if (pwg_db_num_rows($result) == 0) { if (pwg_db_num_rows($result) == 0)
{
return new PwgError(404, "image_id not found"); return new PwgError(404, "image_id not found");
} }
list($path) = pwg_db_fetch_row($result); list($path) = pwg_db_fetch_row($result);
$ret = array(); $ret = array();
foreach (array('thumb', 'file', 'high') as $type) { if (isset($params['thumbnail_sum']))
$param_name = $type; {
if ('thumb' == $type) { // We always say the thumbnail is equal to create no reaction on the
$param_name = 'thumbnail'; // other side. Since Piwigo 2.4 and derivatives, the thumbnails and web
} // sizes are always generated by Piwigo
$ret['thumbnail'] = 'equals';
}
if (isset($params[$param_name.'_sum'])) { if (isset($params['high_sum']))
include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); {
$type_path = file_path_for_type($path, $type); $ret['file'] = 'equals';
if (!is_file($type_path)) { $compare_type = 'high';
$ret[$param_name] = 'missing'; }
} elseif (isset($params['file_sum']))
else { {
if (md5_file($type_path) != $params[$param_name.'_sum']) { $compare_type = 'file';
$ret[$param_name] = 'differs'; }
}
else { if (isset($compare_type))
$ret[$param_name] = 'equals'; {
} ws_logfile(__FUNCTION__.', md5_file($path) = '.md5_file($path));
} if (md5_file($path) != $params[$compare_type.'_sum'])
{
$ret[$compare_type] = 'differs';
}
else
{
$ret[$compare_type] = 'equals';
} }
} }
ws_logfile(__FUNCTION__.', output : '.var_export($ret, true));
return $ret; return $ret;
} }

1
ws.php
View file

@ -259,7 +259,6 @@ function ws_addDefaultMethods( $arr )
'default' => 0, 'default' => 0,
'maxValue' => $conf['available_permission_levels'] 'maxValue' => $conf['available_permission_levels']
), ),
'resize' => array('default' => false),
'check_uniqueness' => array('default' => true), 'check_uniqueness' => array('default' => true),
), ),
'POST method only. 'POST method only.