diff options
author | plegall <plg@piwigo.org> | 2011-02-11 22:57:23 +0000 |
---|---|---|
committer | plegall <plg@piwigo.org> | 2011-02-11 22:57:23 +0000 |
commit | e79538be5ce50fe552c0180c169cfb275aa4ec07 (patch) | |
tree | 6640d1748fb5450e21211a94c8904217ee2103e1 | |
parent | cc88f221110378648abd70f6141e2db07933ebfc (diff) |
feature 2189 added: ability to update a photo (the JPEG/PNG file) with
pwg.images.addSimple, simply by adding an $image_id as argument.
git-svn-id: http://piwigo.org/svn/trunk@9191 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to '')
-rw-r--r-- | admin/include/functions.php | 112 | ||||
-rw-r--r-- | admin/include/functions_upload.inc.php | 173 | ||||
-rw-r--r-- | include/ws_functions.inc.php | 47 | ||||
-rw-r--r-- | ws.php | 3 |
4 files changed, 228 insertions, 107 deletions
diff --git a/admin/include/functions.php b/admin/include/functions.php index f736902b0..1e1f1b454 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -166,26 +166,20 @@ DELETE FROM '.USER_CACHE_CATEGORIES_TABLE.' trigger_action('delete_categories', $ids); } -// The function delete_elements deletes the elements identified by the -// (numeric) values of the array $ids. It also deletes (in the database) : -// - all the comments related to elements -// - all the links between categories and elements -// - all the favorites associated to elements -// @return number of deleted elements -function delete_elements($ids, $physical_deletion=false) +// Deletes all files (on disk) related to given image ids +// @return image ids where files are deleted successfully +function delete_element_files($ids) { if (count($ids) == 0) { return 0; } - trigger_action('begin_delete_elements', $ids); - if ($physical_deletion) - { - include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); - $new_ids=array(); + include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); + + $new_ids = array(); - $query = ' + $query = ' SELECT id, path, @@ -195,40 +189,74 @@ SELECT FROM '.IMAGES_TABLE.' WHERE id IN ('.implode(',', $ids).') ;'; - $result = pwg_query($query); - while ($row = pwg_db_fetch_assoc($result)) + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + if (url_is_remote($row['path'])) { - if (url_is_remote($row['path'])) - continue; - $files = array(); - $files[] = get_element_path($row); - if (!empty($row['tn_ext'])) - $files[] = get_thumbnail_path($row); - if (!empty($row['has_high']) and get_boolean($row['has_high'])) - $files[] = get_high_path($row); - if (!empty($row['representative_ext'])) - { - $pi = pathinfo($row['path']); - $file_wo_ext = get_filename_wo_extension($pi['basename']); - $files[] = PHPWG_ROOT_PATH.$pi['dirname'].'/pwg_representative/'.$file_wo_ext.'.'.$element_info['representative_ext']; - } + continue; + } + + $files = array(); + $files[] = get_element_path($row); + + if (!empty($row['tn_ext'])) + { + $files[] = get_thumbnail_path($row); + } + + if (!empty($row['has_high']) and get_boolean($row['has_high'])) + { + $files[] = get_high_path($row); + } + + if (!empty($row['representative_ext'])) + { + $pi = pathinfo($row['path']); + $file_wo_ext = get_filename_wo_extension($pi['basename']); + $files[] = PHPWG_ROOT_PATH.$pi['dirname'].'/pwg_representative/'.$file_wo_ext.'.'.$row['representative_ext']; + } - $ok = true; - foreach ($files as $path) + $ok = true; + foreach ($files as $path) + { + if (is_file($path) and !unlink($path)) { - if (is_file($path) and !unlink($path)) - { - $ok = false; - trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING); - break; - } - } - if ($ok) - $new_ids[] += $row['id']; - else + $ok = false; + trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING); break; + } + } + + if ($ok) + { + $new_ids[] += $row['id']; + } + else + { + break; } - $ids = $new_ids; + } + return $new_ids; +} + +// The function delete_elements deletes the elements identified by the +// (numeric) values of the array $ids. It also deletes (in the database) : +// - all the comments related to elements +// - all the links between categories and elements +// - all the favorites associated to elements +// @return number of deleted elements +function delete_elements($ids, $physical_deletion=false) +{ + if (count($ids) == 0) + { + return 0; + } + trigger_action('begin_delete_elements', $ids); + + if ($physical_deletion) + { + $ids = delete_element_files($ids); if (count($ids)==0) { return 0; diff --git a/admin/include/functions_upload.inc.php b/admin/include/functions_upload.inc.php index c31047239..a0bcf34fe 100644 --- a/admin/include/functions_upload.inc.php +++ b/admin/include/functions_upload.inc.php @@ -168,7 +168,7 @@ function prepare_upload_configuration() } } -function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null) +function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null) { // Here is the plan // @@ -185,37 +185,68 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie // * check md5sum (already exists?) global $conf, $user; - - // current date - 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( - PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s', - $year, - $month, - $day - ); - // compute file path $md5sum = md5_file($source_filepath); - $date_string = preg_replace('/[^\d]/', '', $dbnow); - $random_string = substr($md5sum, 0, 8); - $filename_wo_ext = $date_string.'-'.$random_string; - $file_path = $upload_dir.'/'.$filename_wo_ext.'.'; + $file_path = null; + + if (isset($image_id)) + { + // we are performing an update + $query = ' +SELECT + path + FROM '.IMAGES_TABLE.' + WHERE id = '.$image_id.' +;'; + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + $file_path = $row['path']; + } + + if (!isset($file_path)) + { + die('['.__FUNCTION__.'] this photo does not exist in the database'); + } - list($width, $height, $type) = getimagesize($source_filepath); - if (IMAGETYPE_PNG == $type) - { - $file_path.= 'png'; + // delete all physical files related to the photo (thumbnail, web site, HD) + delete_element_files(array($image_id)); } else { - $file_path.= 'jpg'; + // this photo is new + + // current date + 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( + PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s', + $year, + $month, + $day + ); + + // compute file path + $date_string = preg_replace('/[^\d]/', '', $dbnow); + $random_string = substr($md5sum, 0, 8); + $filename_wo_ext = $date_string.'-'.$random_string; + $file_path = $upload_dir.'/'.$filename_wo_ext.'.'; + + list($width, $height, $type) = getimagesize($source_filepath); + if (IMAGETYPE_PNG == $type) + { + $file_path.= 'png'; + } + else + { + $file_path.= 'jpg'; + } + + prepare_directory($upload_dir); } - prepare_directory($upload_dir); if (is_uploaded_file($source_filepath)) { move_uploaded_file($source_filepath, $file_path); @@ -296,37 +327,77 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie $thumb_infos = pwg_image_infos($thumb_path); - // database registration - $insert = array( - 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), - 'date_available' => $dbnow, - 'tn_ext' => 'jpg', - 'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path), - 'filesize' => $file_infos['filesize'], - 'width' => $file_infos['width'], - 'height' => $file_infos['height'], - 'md5sum' => $md5sum, - 'added_by' => $user['id'], - ); - - if (isset($high_infos)) + if (isset($image_id)) { - $insert['has_high'] = 'true'; - $insert['high_filesize'] = $high_infos['filesize']; - } + $update = array( + 'id' => $image_id, + 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), + 'filesize' => $file_infos['filesize'], + 'width' => $file_infos['width'], + 'height' => $file_infos['height'], + 'md5sum' => $md5sum, + 'added_by' => $user['id'], + ); + + if (isset($high_infos)) + { + $update['has_high'] = 'true'; + $update['high_filesize'] = $high_infos['filesize']; + } + else + { + $update['has_high'] = 'false'; + $update['high_filesize'] = null; + } - if (isset($level)) - { - $insert['level'] = $level; + if (isset($level)) + { + $update['level'] = $level; + } + + mass_updates( + IMAGES_TABLE, + array( + 'primary' => array('id'), + 'update' => array_keys($update) + ), + array($update) + ); } + else + { + // database registration + $insert = array( + 'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)), + 'date_available' => $dbnow, + 'tn_ext' => 'jpg', + 'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path), + 'filesize' => $file_infos['filesize'], + 'width' => $file_infos['width'], + 'height' => $file_infos['height'], + 'md5sum' => $md5sum, + 'added_by' => $user['id'], + ); + + if (isset($high_infos)) + { + $insert['has_high'] = 'true'; + $insert['high_filesize'] = $high_infos['filesize']; + } + + if (isset($level)) + { + $insert['level'] = $level; + } - mass_inserts( - IMAGES_TABLE, - array_keys($insert), - array($insert) - ); + mass_inserts( + IMAGES_TABLE, + array_keys($insert), + array($insert) + ); - $image_id = pwg_db_insert_id(IMAGES_TABLE); + $image_id = pwg_db_insert_id(IMAGES_TABLE); + } if (isset($categories) and count($categories) > 0) { @@ -342,7 +413,7 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie $conf['use_exif'] = false; } update_metadata(array($image_id=>$file_path)); - + invalidate_user_cache(); return $image_id; diff --git a/include/ws_functions.inc.php b/include/ws_functions.inc.php index d0b550a2a..be453b6f7 100644 --- a/include/ws_functions.inc.php +++ b/include/ws_functions.inc.php @@ -1352,10 +1352,28 @@ function ws_images_addSimple($params, &$service) { return new PwgError(405, "This method requires HTTP POST"); } + + $params['image_id'] = (int)$params['image_id']; + if ($params['image_id'] > 0) + { + include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); + + $query=' +SELECT * + FROM '.IMAGES_TABLE.' + WHERE id = '.$params['image_id'].' +;'; + + $image_row = pwg_db_fetch_assoc(pwg_query($query)); + if ($image_row == null) + { + return new PwgError(404, "image_id not found"); + } + } // category $params['category'] = (int)$params['category']; - if ($params['category'] <= 0) + if ($params['category'] <= 0 and $params['image_id'] <= 0) { return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); } @@ -1366,8 +1384,9 @@ function ws_images_addSimple($params, &$service) $image_id = add_uploaded_file( $_FILES['image']['tmp_name'], $_FILES['image']['name'], - array($params['category']), - 8 + $params['category'] > 0 ? array($params['category']) : null, + 8, + $params['image_id'] > 0 ? $params['image_id'] : null ); $info_columns = array( @@ -1415,23 +1434,25 @@ function ws_images_addSimple($params, &$service) add_tags($tag_ids, array($image_id)); } - $query = ' + $url_params = array('image_id' => $image_id); + + if ($params['category'] > 0) + { + $query = ' SELECT id, name, permalink FROM '.CATEGORIES_TABLE.' WHERE id = '.$params['category'].' ;'; - $result = pwg_query($query); - $category = pwg_db_fetch_assoc($result); + $result = pwg_query($query); + $category = pwg_db_fetch_assoc($result); + + $url_params['section'] = 'categories'; + $url_params['category'] = $category; + } return array( 'image_id' => $image_id, - 'url' => make_picture_url( - array( - 'image_id' => $image_id, - 'section' => 'categories', - 'category' => $category - ) - ), + 'url' => make_picture_url($url_params), ); } @@ -233,8 +233,9 @@ function ws_addDefaultMethods( $arr ) 'maxValue' => $conf['available_permission_levels'] ), 'tags' => array('default' => null), + 'image_id' => array('default' => null), ), - 'POST method only.<br>Use the <b>image</b> field for uploading file.<br>Set the form encoding to "form-data"<br><b>category</b> is the numeric identifier of the destination category.' + 'POST method only.<br>Use the <b>image</b> field for uploading file.<br>Set the form encoding to "form-data"<br><b>category</b> is the numeric identifier of the destination category.<br>You can update an existing photo if you define an existing image_id.' ); $service->addMethod( |