| 1 |
<?php |
| 2 |
namespace Depicter\Controllers\Ajax; |
| 3 |
|
| 4 |
use GuzzleHttp\Psr7\UploadedFile; |
| 5 |
use WPEmerge\Requests\RequestInterface; |
| 6 |
|
| 7 |
class ImportAjaxController |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* @param RequestInterface $request |
| 12 |
* @param $view |
| 13 |
* |
| 14 |
* @return \Psr\Http\Message\ResponseInterface |
| 15 |
*/ |
| 16 |
public function unpack( RequestInterface $request, $view ) { |
| 17 |
$zipFile = $request->files('file'); |
| 18 |
|
| 19 |
if ( empty( $zipFile ) || ! $zipFile instanceof UploadedFile ) { |
| 20 |
return \Depicter::json([ |
| 21 |
'errors' => [ 'No file provided to upload'] |
| 22 |
])->withStatus(400 ); |
| 23 |
} |
| 24 |
|
| 25 |
if ( $zipFile->getError() ) { |
| 26 |
return \Depicter::json([ |
| 27 |
'errors' => [ |
| 28 |
sprintf( __( 'Cannot upload the file, because max permitted file upload size is %s.', 'depicter' ), ini_get('upload_max_filesize') ) |
| 29 |
] |
| 30 |
]); |
| 31 |
} |
| 32 |
|
| 33 |
$zipMediaTypes = [ |
| 34 |
'application/zip', |
| 35 |
'application/x-zip-compressed' |
| 36 |
]; |
| 37 |
if( !in_array( $zipFile->getClientMediaType(), $zipMediaTypes ) ){ |
| 38 |
return \Depicter::json([ |
| 39 |
'errors' => [ 'Provided file must be zip file.'] |
| 40 |
])->withStatus(400 ); |
| 41 |
} |
| 42 |
|
| 43 |
if ( \Depicter::importService()->unpack($zipFile) ) { |
| 44 |
return \Depicter::json([ |
| 45 |
'success' => [ 'Slider imported successfully'] |
| 46 |
])->withStatus(200 ); |
| 47 |
} else { |
| 48 |
return \Depicter::json([ |
| 49 |
'errors' => [ 'Error occurred during import process.'] |
| 50 |
])->withStatus(400 ); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|