From 44db5c68b57dbe10dc62117b338fd172ba6193b8 Mon Sep 17 00:00:00 2001 From: ralf Date: Wed, 27 Mar 2024 14:52:48 +0200 Subject: [PATCH] WIP allow to place custom-fields in tabs: fix automatic extension and use name not label name for filename type cf is a relative path, optionally ending in a / to create a directory with all files the user uploads preserving their name --- .../etemplate/et2_extension_customfields.ts | 6 ------ api/src/Etemplate/Widget/Customfields.php | 2 +- api/src/Etemplate/Widget/Vfs.php | 20 +++++++++---------- api/src/Storage/Customfields.php | 4 +++- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/api/js/etemplate/et2_extension_customfields.ts b/api/js/etemplate/et2_extension_customfields.ts index 0af7e26c06..d5e1b5acde 100644 --- a/api/js/etemplate/et2_extension_customfields.ts +++ b/api/js/etemplate/et2_extension_customfields.ts @@ -880,12 +880,6 @@ export class et2_customfields_list extends et2_valueWidget implements et2_IDetac attrs.type = 'vfs-upload'; delete(attrs.label); - // fix vfs-upload id="$app:$id:$rest" e.g. "infolog:$cont[info_id]:test.pdf" - const vfs_app_id_regexp = /^#([a-z_]+):([@$][^:]+):(.*)$/; - if (attrs.id.match(vfs_app_id_regexp)) - { - attrs.id = this.getArrayMgr('content').expandName(attrs.id); - } // allow to set/pass further et2_file attributes to the vfs-upload if (field.values && typeof field.values === 'object') { diff --git a/api/src/Etemplate/Widget/Customfields.php b/api/src/Etemplate/Widget/Customfields.php index 9cb44faa73..d75c2ddc1e 100644 --- a/api/src/Etemplate/Widget/Customfields.php +++ b/api/src/Etemplate/Widget/Customfields.php @@ -343,7 +343,7 @@ class Customfields extends Transformer case 'vfs-upload': $widget->attrs['path'] = $field['app'] . ':' . self::expand_name('$cont['.Api\Link::get_registry($field['app'],'view_id').']',0,0,0,0,self::$request->content). - ':'.$field['label']; + ':'.(preg_match('/^[a-z_]+:[^:]+:(.+)$/', $field['name'], $matches) ? $matches[1] : $field['name']); break; case 'link-to': diff --git a/api/src/Etemplate/Widget/Vfs.php b/api/src/Etemplate/Widget/Vfs.php index d07cad8e30..d9390fee9f 100644 --- a/api/src/Etemplate/Widget/Vfs.php +++ b/api/src/Etemplate/Widget/Vfs.php @@ -39,7 +39,7 @@ class Vfs extends File { if ($this->type === 'vfs-upload' || !empty($this->attrs['type']) && $this->attrs['type'] === 'vfs-upload') { - $form_name = self::form_name($cname, $this->id, $expand ? $expand : array('cont' => self::$request->content)); + $form_name = self::form_name($cname, $this->id, $expand ?: array('cont' => self::$request->content)); if (!empty($this->attrs['path'])) { $path = self::expand_name($this->attrs['path'], $expand['c'] ?? null, $expand['row'], $expand['c_'] ?? null, $expand['row_'] ?? null, $expand['cont']); @@ -286,21 +286,21 @@ class Vfs extends File { $name = $_REQUEST['widget_id']; - // Find real path - if($path[0] != '/') + // Find real path, could be "$app:$id:$path" + if($path[0] !== '/') { $path = self::get_vfs_path($path); } $filename = $file['name']; - if ($path && substr($path,-1) != '/') + if ($path && substr($path,-1) !== '/') { - // add extension to path - $parts = explode('.',$filename); - // check if path already contains a valid extension --> dont add an other one - $path_parts = explode('.', $path); - if (count($path_parts) > 2 && (!($extension = array_pop($path_parts)) || !Api\MimeMagic::ext2mime($extension)) && - ($extension = array_pop($parts)) && Api\MimeMagic::ext2mime($extension)) // really an extension --> add it to path + $parts = explode('.', $filename); + // check if path already contains a valid extension --> don't add another one + $path_parts = explode('.', Api\Vfs::basename($path)); + if ((!($path_ext = array_pop($path_parts)) || Api\MimeMagic::ext2mime($path_ext) === 'application/octet-stream') && + ($extension = array_pop($parts) ?: Api\MimeMagic::mime2ext($file['mime']))) { + // add extension to path $path .= '.'.$extension; } $file['name'] = Api\Vfs::basename($path); diff --git a/api/src/Storage/Customfields.php b/api/src/Storage/Customfields.php index 696eb2080c..a9721f87e1 100644 --- a/api/src/Storage/Customfields.php +++ b/api/src/Storage/Customfields.php @@ -637,7 +637,9 @@ class Customfields implements \IteratorAggregate protected static function handle_file($entry_id, $field, $value) { - $path = Api\Etemplate\Widget\Vfs::get_vfs_path($field['app'].":$entry_id:".$field['label']); + $path = Api\Etemplate\Widget\Vfs::get_vfs_path($field['app'].':'.$entry_id.':'. + (preg_match('/^[a-z_]+:[^:]+:(.+)$/', $field['name'], $matches) ? $matches[1] : $field['name'])); + if($path) { foreach($value as $file)