From 1aa410fc407213b0d7c7fa3787e133e757803e2e Mon Sep 17 00:00:00 2001 From: Victor Manuel Agudelo Date: Fri, 16 Aug 2024 14:58:56 -0500 Subject: [PATCH 1/2] Added compatibility with providers other than amazon such as wasabi or that handle a standard s3 connection. --- storage-s3/config.php | 59 ++++++++++++++++++++++++++++++++++++++++-- storage-s3/plugin.php | 4 +-- storage-s3/storage.php | 13 +++++++--- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/storage-s3/config.php b/storage-s3/config.php index e655240d..015eff9c 100644 --- a/storage-s3/config.php +++ b/storage-s3/config.php @@ -27,6 +27,34 @@ function getOptions() { 'label' => $__('S3 Folder Path'), 'configuration' => array('size'=>40), )), + 'storage_type' => new ChoiceField(array( + 'label' => $__('Storage Type'), + 'configuration' => array('data-name'=>'storage_type'), + 'onChange' => 'javascript:toggleStorageType();', + 'choices' => array( + 'amazon_s3' => $__('Amazon S3 Storage'), + 's3_compatible' => $__('S3 Compatible Storage'), + ), + 'default' => 'amazon_s3', + )), + 'rest-endpoint' => new TextboxField(array( + 'label' => $__('REST Endpoint'), + 'hint' => $__('Specify S3-compatible API endpoint (ex: https://s3.wasabisys.com)'), + 'configuration' => array('size'=> 40, 'length'=> 80), + 'visibility' => new VisibilityConstraint( + new Q(array('storage_type__eq'=> 's3_compatible')), + VisibilityConstraint::HIDDEN + ), + )), + 'rest-region' => new TextboxField(array( + 'label' => $__('REST Region'), + 'hint' => $__('Specify S3-compatible API Region (ex:us-east-1)'), + 'configuration' => array('size'=>30), + 'visibility' => new VisibilityConstraint( + new Q(array('storage_type__eq'=> 's3_compatible')), + VisibilityConstraint::HIDDEN + ), + )), 'aws-region' => new ChoiceField(array( 'label' => $__('AWS Region'), 'choices' => array( @@ -58,6 +86,11 @@ function getOptions() { 'us-gov-west-1' => 'AWS GovCloud (US-West)', ), 'default' => '', + //'visibility' => 'amazon_s3', // Visibilidad condicional + 'visibility' => new VisibilityConstraint( + new Q(array('storage_type__eq'=> 'amazon_s3')), + VisibilityConstraint::HIDDEN + ), )), 'acl' => new ChoiceField(array( 'label' => $__('Default ACL for Attachments'), @@ -98,8 +131,30 @@ function pre_save(&$config, &$errors) { ?: Crypto::decrypt($this->get('secret-access-key'), SECRET_SALT, $this->getNamespace()), ); - if ($config['aws-region']) - $credentials['region'] = $config['aws-region']; + + if ($config['storage_type'] === 's3_compatible') { + // Si el campo rest-endpoint está vacío, añade un error + if (empty($config['rest-endpoint'])) { + $this->getForm()->getField('rest-endpoint')->addError( + __('REST Endpoint is required for S3 Compatible Storage')); + $errors['err'] = __('Please complete the required fields.'); + } elseif (!filter_var($config['rest-endpoint'], FILTER_VALIDATE_URL)) { + $this->getForm()->getField('rest-endpoint')->addError( + __('Please enter a valid URL for the REST Endpoint')); + $errors['err'] = __('Please enter a valid URL.'); + } + + if (empty($config['rest-region'])) { + $this->getForm()->getField('rest-region')->addError( + __('REST Region is required for S3 Compatible Storage')); + $errors['err'] = __('Please complete the required fields.'); + } + $credentials['endpoint'] = $config['rest-endpoint']; + $credentials['region'] = $config['rest-region']; + }else{ + if ($config['aws-region']) + $credentials['region'] = $config['aws-region']; + } if (!$credentials['credentials']['secret']) $this->getForm()->getField('secret-access-key')->addError( diff --git a/storage-s3/plugin.php b/storage-s3/plugin.php index eb33fa73..a97c1f27 100644 --- a/storage-s3/plugin.php +++ b/storage-s3/plugin.php @@ -2,10 +2,10 @@ return array( 'id' => 'storage:s3', - 'version' => '0.5', + 'version' => '0.6', 'ost_version' => '1.17', # Require osTicket v1.17+ 'name' => /* trans */ 'Attachments hosted in Amazon S3', - 'author' => 'Jared Hancock, Kevin Thorne', + 'author' => 'Jared Hancock, Kevin Thorne, Victor Manuel Agudelo', 'description' => /* trans */ 'Enables storing attachments in Amazon S3', 'url' => 'http://www.osticket.com/plugins/storage-s3', 'requires' => array( diff --git a/storage-s3/storage.php b/storage-s3/storage.php index 3a717bbf..aa44382b 100644 --- a/storage-s3/storage.php +++ b/storage-s3/storage.php @@ -36,11 +36,18 @@ function __construct($meta) { 'secret' => Crypto::decrypt(static::$config['secret-access-key'], SECRET_SALT, $this->getConfig()->getNamespace()) ); - if (static::$config['aws-region']) - $credentials['region'] = static::$config['aws-region']; + + if (static::$config['storage_type'] == 's3_compatible'){ + $credentials['endpoint'] = static::$config['rest-endpoint']; + $credentials['region'] = static::$config['rest-region']; + }else{ + if (static::$config['aws-region']) + $credentials['region'] = static::$config['aws-region']; + } + $credentials['version'] = self::$version; - $credentials['signature_version'] = self::$sig_vers; + $credentials['signature_version'] = self::$sig_vers; $this->client = new S3Client($credentials); } From 823e458c6897b28e154bd7d18730941b7bbc6dd4 Mon Sep 17 00:00:00 2001 From: Victor Manuel Agudelo Date: Sun, 18 Aug 2024 11:12:03 -0500 Subject: [PATCH 2/2] eliminated unnecessary onchange method --- storage-s3/config.php | 1 - 1 file changed, 1 deletion(-) diff --git a/storage-s3/config.php b/storage-s3/config.php index 015eff9c..e7b1eaf7 100644 --- a/storage-s3/config.php +++ b/storage-s3/config.php @@ -30,7 +30,6 @@ function getOptions() { 'storage_type' => new ChoiceField(array( 'label' => $__('Storage Type'), 'configuration' => array('data-name'=>'storage_type'), - 'onChange' => 'javascript:toggleStorageType();', 'choices' => array( 'amazon_s3' => $__('Amazon S3 Storage'), 's3_compatible' => $__('S3 Compatible Storage'),