| 1 |
<?php |
| 2 |
namespace Depicter\Controllers\Ajax; |
| 3 |
|
| 4 |
use WPEmerge\Requests\RequestInterface; |
| 5 |
|
| 6 |
class FileUploaderController |
| 7 |
{ |
| 8 |
public function uploadFile(RequestInterface $request, $view) { |
| 9 |
|
| 10 |
try{ |
| 11 |
$files = $request->files(); |
| 12 |
|
| 13 |
if ( empty( $files ) ) { |
| 14 |
return \Depicter::json([ |
| 15 |
'errors' => [ 'No file provided to upload'] |
| 16 |
])->withStatus(400 ); |
| 17 |
} |
| 18 |
|
| 19 |
$results = \Depicter::fileUploader()->upload( $files ); |
| 20 |
|
| 21 |
if ( ! empty( $results ) ) { |
| 22 |
return \Depicter::json([ |
| 23 |
'hits' => $results |
| 24 |
])->withStatus(200 ); |
| 25 |
} else { |
| 26 |
return \Depicter::json([ |
| 27 |
'errors' => [ 'Failed to upload media files.'] |
| 28 |
])->withStatus(400 ); |
| 29 |
} |
| 30 |
|
| 31 |
} catch( \Exception $exception ){ |
| 32 |
return \Depicter::json([ |
| 33 |
'errors' => [ $exception->getMessage() ] |
| 34 |
])->withStatus(400 ); |
| 35 |
} |
| 36 |
|
| 37 |
} |
| 38 |
} |
| 39 |
|