From f3713072bafa49fad0085841d43e7fd301f43451 Mon Sep 17 00:00:00 2001 From: Josh Pollock Date: Thu, 25 Oct 2018 13:35:21 -0400 Subject: [PATCH] seperate file upload logic from rest api handler #2766 --- cf2/Fields/Handlers/FileUpload.php | 85 +++++++++++++++++++ cf2/RestApi/File/CreateFile.php | 54 ++++-------- cf2/Transients/Cf1TransientsApi.php | 37 ++++++++ cf2/Transients/TransientApiContract.php | 45 ++++++++++ classes/transient.php | 7 +- tests/Integration/RestApi/CreateFileTest.php | 3 +- .../Handlers/FileUpload/FileUploadTest.php | 28 ++++++ 7 files changed, 216 insertions(+), 43 deletions(-) create mode 100644 cf2/Fields/Handlers/FileUpload.php create mode 100644 cf2/Transients/Cf1TransientsApi.php create mode 100644 cf2/Transients/TransientApiContract.php create mode 100644 tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php diff --git a/cf2/Fields/Handlers/FileUpload.php b/cf2/Fields/Handlers/FileUpload.php new file mode 100644 index 000000000..6d275deb3 --- /dev/null +++ b/cf2/Fields/Handlers/FileUpload.php @@ -0,0 +1,85 @@ +field = $field; + $this->form = $form; + $this->transientApi = $transientApi; + } + + + /** + * @param array $files + * @param array $hashes + * @param $controlCode + * @return mixed + * @throws \Exception + */ + public function processFiles(array $files,array $hashes, $controlCode ){ + $transdata = call_user_func([$this->transientApi, 'getTransient'],$controlCode); + if( ! is_array( $transdata ) ){ + $transdata = []; + } + + $i = 0; + foreach ($files as $file) { + if (!\Caldera_Forms_Files::is_private($this->field)) { + $uploadArgs = array( + 'private' => false, + 'field_id' => $this->field['ID'], + 'form_id' => $this->form['ID'] + ); + } else { + $uploadArgs = array( + 'private' => true, + 'field_id' => $this->field['ID'], + 'form_id' => $this->form['ID'] + ); + } + + $expected = $hashes[$i]; + $actual = md5_file( $file['tmp_name'] ); + + if ( $expected !== $actual ) { + throw new \Exception(__( 'Content hash did not match expected.' ), 412 ); + } + + + $upload = wp_handle_upload($file, array( 'test_form' => false, 'action' => 'foo' ) ); + if( !empty( $field['config']['media_lib'] ) ){ + \Caldera_Forms_Files::add_to_media_library( $upload, $field ); + } + + + $uploads[] = $upload['url']; + $i++; + + } + + call_user_func([$this->transientApi, 'setTransient'],$controlCode, array_merge( $transdata, $uploads ), 24 * 60 * 60 ); + + + return $controlCode; + } +} \ No newline at end of file diff --git a/cf2/RestApi/File/CreateFile.php b/cf2/RestApi/File/CreateFile.php index 1c347ad77..6070e333f 100644 --- a/cf2/RestApi/File/CreateFile.php +++ b/cf2/RestApi/File/CreateFile.php @@ -5,6 +5,8 @@ use calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType; +use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload; +use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi; class CreateFile extends File { @@ -86,48 +88,21 @@ public function createItem(\WP_REST_Request $request) $uploader = $this->getUploader($field); $hashes = $request->get_param( 'hashes'); $controlCode = $request->get_param( 'control' ); - $transdata = \Caldera_Forms_Transient::get_transient($controlCode); - if( ! is_array( $transdata ) ){ - $transdata = []; - } - - $i = 0; - foreach ($files as $file) { - if (!\Caldera_Forms_Files::is_private($field)) { - $uploadArgs = array( - 'private' => false, - 'field_id' => $field['ID'], - 'form_id' => $this->getForm()['ID'] - ); - } else { - $uploadArgs = array( - 'private' => true, - 'field_id' => $field['ID'], - 'form_id' => $this->getForm()['ID'] - ); - } - - $expected = $hashes[$i]; - $actual = md5_file( $file['tmp_name'] ); - - if ( $expected !== $actual ) { - return new \WP_Error( 'rest_upload_hash_mismatch', __( 'Content hash did not match expected.' ), array( 'status' => 412 ) ); - } - - - $upload = wp_handle_upload($file, array( 'test_form' => false, 'action' => 'foo' ) ); - if( !empty( $field['config']['media_lib'] ) ){ - \Caldera_Forms_Files::add_to_media_library( $upload, $field ); - } - - - $uploads[] = $upload['url']; - $i++; + $handler = new FileUpload( + $field, + $this->getForm(), + new Cf1TransientsApi() + ); + try{ + $controlCode = $handler->processFiles($files,$hashes,$controlCode); + }catch( \Exception $e ){ + return new \WP_REST_Response(['message' => $e->getMessage() ],$e->getCode() ); } - \Caldera_Forms_Transient::set_transient($controlCode, array_merge( $transdata, $uploads ), DAY_IN_SECONDS); - + if( is_wp_error( $controlCode ) ){ + return $controlCode; + } $response = rest_ensure_response([ 'control' => $controlCode ]); @@ -151,4 +126,5 @@ protected function getUploader($field) } + } \ No newline at end of file diff --git a/cf2/Transients/Cf1TransientsApi.php b/cf2/Transients/Cf1TransientsApi.php new file mode 100644 index 000000000..018362f5b --- /dev/null +++ b/cf2/Transients/Cf1TransientsApi.php @@ -0,0 +1,37 @@ + 'fld1']; + $form = ['ID' => 'cd1' ]; + $transients = new Cf1TransientsApi(); + $handler = new FileUpload($field,$form, $transients); + $this->assertAttributeEquals( $field,'field', $handler ); + $this->assertAttributeEquals( $form,'form', $handler ); + $this->assertAttributeEquals( $transients,'transientApi', $handler ); + } +}