From 3a63eccb0e0b0d62b7a584d8c32f12610d982612 Mon Sep 17 00:00:00 2001 From: plegall Date: Tue, 31 May 2022 12:05:27 +0200 Subject: [PATCH] issue #389 partial back-end implementation of multiple formats web upload --- admin/include/functions_upload.inc.php | 68 ++++++++++++ .../default/template/photos_add_direct.tpl | 66 ++++++++++-- include/ws_functions/pwg.images.php | 101 ++++++++++++++++++ ws.php | 17 +++ 4 files changed, 243 insertions(+), 9 deletions(-) diff --git a/admin/include/functions_upload.inc.php b/admin/include/functions_upload.inc.php index c7695ee6c..8820c716f 100644 --- a/admin/include/functions_upload.inc.php +++ b/admin/include/functions_upload.inc.php @@ -427,6 +427,74 @@ SELECT return $image_id; } +function add_format($source_filepath, $format_ext, $format_of) +{ + // 1) find infos about the extended image + // + // 2) move uploaded file to upload/2022/05/16/pwg_format/20100122003814-449ada00.cr2 + // + // 3) register in database + + if (!conf_get_param('enable_formats', false)) + { + die('['.__FUNCTION__.'] formats are disabled'); + } + + if (!in_array($format_ext, conf_get_param('format_ext', array('cr2')))) + { + die('['.__FUNCTION__.'] unexpected format extension "'.$format_ext.'" (authorized extensions: '.implode(', ', conf_get_param('format_ext', array('cr2'))).')'); + } + + $query = ' +SELECT + path + FROM '.IMAGES_TABLE.' + WHERE id = '.$format_of.' +;'; + $images = query2array($query); + + if (!isset($images[0])) + { + die('['.__FUNCTION__.'] this photo does not exist in the database'); + } + + $format_path = dirname($images[0]['path']).'/pwg_format/'; + $format_path.= get_filename_wo_extension(basename($images[0]['path'])); + $format_path.= '.'.$format_ext; + + prepare_directory(dirname($format_path)); + + if (is_uploaded_file($source_filepath)) + { + move_uploaded_file($source_filepath, $format_path); + } + else + { + rename($source_filepath, $format_path); + } + @chmod($format_path, 0644); + + $file_infos = pwg_image_infos($format_path); + + $insert = array( + 'image_id' => $format_of, + 'ext' => $format_ext, + 'filesize' => $file_infos['filesize'], + ); + + single_insert(IMAGE_FORMAT_TABLE, $insert); + $format_id = pwg_db_insert_id(IMAGE_FORMAT_TABLE); + + pwg_activity('photo', $format_of, 'edit', array('action'=>'add format', 'format_ext'=>$format_ext, 'format_id'=>$format_id)); + + $format_infos = $insert; + $format_infos['format_id'] = $format_id; + + trigger_notify('loc_end_add_format', $format_infos); + + return $format_id; +} + add_event_handler('upload_file', 'upload_file_pdf'); function upload_file_pdf($representative_ext, $file_path) { diff --git a/admin/themes/default/template/photos_add_direct.tpl b/admin/themes/default/template/photos_add_direct.tpl index d0c20132e..a8a971ad6 100644 --- a/admin/themes/default/template/photos_add_direct.tpl +++ b/admin/themes/default/template/photos_add_direct.tpl @@ -69,6 +69,7 @@ var batch_Label = "{'Manage this set of %d photos'|translate}"; var albumSummary_label = "{'Album "%s" now contains %d photos'|translate|escape}"; var uploadedPhotos = []; var uploadCategory = null; +var uploadFormats = true; {literal} jQuery(document).ready(function(){ @@ -149,6 +150,50 @@ jQuery(document).ready(function(){ } }, + FilesAdded: function(up, files) { + console.log("FilesAdded"); + + // Création de la liste avec plupload_id : image_name + fileNames = {}; + files.forEach((file) => { + console.log(file) + fileNames[file.id] = file.name; + }); + + //ajax qui renvois les id des images dans la gallerie. + jQuery.ajax({ + url: "ws.php?format=json&method=pwg.images.formats.searchImage", + type: "POST", + data: { + category_id: jQuery("select[name=category] option:selected").val(), + filename_list: JSON.stringify(fileNames), + }, + success: function(data) { + + //les data qui sont renvoyées avec plupload_id : piwigo_id + var data = JSON.parse(data); + console.log(data.result); + // data = {o_1g369r2011io3v2k1hmn1fc11m7a : 42, o_1g36a8kgutjjajjpb9agvb16a : 69}; + + console.log("FilesInAjax"); + for (const [plupload_id, piwigo_data] of Object.entries(data.result)) { + console.log(plupload_id+': '+piwigo_data.image_id); + + files.forEach((file) => { + if (file.id == plupload_id) { + file.format_of = piwigo_data.image_id; + } + }) + } + } + }); + + + //vérifier qu'on a bien format_of ici + console.log("Files"); + console.log(files); + }, + UploadProgress: function(up, file) { jQuery('#uploadingActions .progressbar').width(up.total.percent+'%'); Piecon.setProgress(up.total.percent); @@ -173,15 +218,18 @@ jQuery(document).ready(function(){ jQuery("select[name=level]").attr("disabled", "disabled"); // You can override settings before the file is uploaded - up.setOption( - 'multipart_params', - { - category : jQuery("select[name=category] option:selected").val(), - level : jQuery("select[name=level] option:selected").val(), - pwg_token : pwg_token - // name : file.name - } - ); + var options = { + category : jQuery("select[name=category] option:selected").val(), + level : jQuery("select[name=level] option:selected").val(), + pwg_token : pwg_token + // name : file.name + }; + + if (uploadFormats) { + options.format_of = file.format_of; + } + + up.setOption('multipart_params', options); }, FileUploaded: function(up, file, info) { diff --git a/include/ws_functions/pwg.images.php b/include/ws_functions/pwg.images.php index 788b31a61..afa502798 100644 --- a/include/ws_functions/pwg.images.php +++ b/include/ws_functions/pwg.images.php @@ -1317,6 +1317,28 @@ function ws_images_upload($params, $service) return new PwgError(403, 'Invalid security token'); } + if (isset($params['format_of'])) + { + $format_ext = null; + + // are formats enabled? + if (!$conf['enable_formats']) + { + return new PwgError(401, 'formats are disabled'); + } + + // We must check if the extension is in the authorized list. + if (preg_match('/\.('.implode('|', $conf['format_ext']).')$/', $params['name'], $matches)) + { + $format_ext = $matches[1]; + } + + if (empty($format_ext)) + { + return new PwgError(401, 'unexpected format extension of file "'.$params['name'].'" (authorized extensions: '.implode(', ', $conf['format_ext']).')'); + } + } + // usleep(100000); // if (!isset($_FILES['image'])) @@ -1405,6 +1427,36 @@ function ws_images_upload($params, $service) include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); + if (isset($params['format_of'])) + { + $query=' +SELECT * + FROM '.IMAGES_TABLE.' + WHERE id = '. $params['format_of'] .' +;'; + $images = query2array($query); + if (count($images) == 0) + { + return new PwgError(404, __FUNCTION__.' : image_id not found'); + } + + $image = $images[0]; + + add_format($filePath, $format_ext, $image['id']); + + return array( + 'image_id' => $image['id'], + 'src' => DerivativeImage::thumb_url($image), + 'square_src' => DerivativeImage::url(ImageStdParams::get_by_type(IMG_SQUARE), $image), + 'name' => $image['name'], + 'category' => array( + 'id' => $params['category'][0], // not relevant + 'nb_photos' => 42, // not relevant, once again + 'label' => 'test', // not relevant + ) + ); + } + $image_id = add_uploaded_file( $filePath, stripslashes($params['name']), // function add_uploaded_file will secure before insert @@ -1806,6 +1858,55 @@ SELECT id, file return $result; } +/** + * API method + * Check if an image exists by it's name or md5 sum + * + * @since 13 + * @param mixed[] $params + * @option string category_id (optional) + * @option string filename_list + */ +function ws_images_formats_searchImage($params, $service) +{ + global $conf, $logger; + + $logger->debug(__FUNCTION__, 'WS', $params); + + $result = array(); + + $candidates = json_decode(stripslashes($params['filename_list']), true); + + // let's fake it for now: return an id without really testing the match + +/* + $unique_filenames_db = array(); + + $query = ' +SELECT + id, + file + FROM '.IMAGES_TABLE.' +;'; + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + $filename_wo_ext = get_filename_wo_extension($row['file']); + @$unique_filenames_db[ $filename_wo_ext ][] = $row['id']; + } +*/ + + foreach ($candidates as $format_external_id => $format_filename) + { + $result[$format_external_id] = array( + 'status' => 'found', // could be 'not found' or 'multiple' + 'image_id' => 84, + ); + } + + return $result; +} + /** * API method * Check is file has been update diff --git a/ws.php b/ws.php index 92c3812a6..785df0282 100644 --- a/ws.php +++ b/ws.php @@ -253,6 +253,18 @@ function ws_addDefaultMethods( $arr ) array('admin_only'=>true, 'post_only'=>true) ); + $service->addMethod( + 'pwg.images.formats.searchImage', + 'ws_images_formats_searchImage', + array( + 'category_id' => array('type'=>WS_TYPE_ID, 'default'=>null), + 'filename_list' => array(), + ), + 'Search for image ids matching the provided filenames. filename_list must be a JSON encoded associative array of unique_id:filename.

The method returns a list of unique_id:image_id.', + $ws_functions_root . 'pwg.images.php', + array('admin_only'=>true, 'post_only'=>true) + ); + $service->addMethod( 'pwg.images.setRank', 'ws_images_setRank', @@ -452,6 +464,11 @@ function ws_addDefaultMethods( $arr ) 'maxValue' => max($conf['available_permission_levels']), 'type' => WS_TYPE_INT|WS_TYPE_POSITIVE ), + 'format_of' => array( + 'default' => null, + 'type' => WS_TYPE_ID, + 'info' => 'id of the extended image (name/category/level are not used if format_of is provided)', + ), 'pwg_token' => array(), ), 'Add an image.