| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\UserResource; |
| 6 |
use FluentCart\App\Hooks\Handlers\GlobalStorageHandler; |
| 7 |
use FluentCart\App\Http\Requests\UserRequest; |
| 8 |
use FluentCart\App\Services\FileSystem\FileManager; |
| 9 |
use FluentCart\Framework\Http\Request\File; |
| 10 |
use FluentCart\Framework\Http\Request\Request; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
use FluentCart\Framework\Support\Str; |
| 13 |
|
| 14 |
class FileUploadController extends Controller |
| 15 |
{ |
| 16 |
|
| 17 |
public function index(Request $request) |
| 18 |
{ |
| 19 |
$driver = sanitize_text_field($request->get('driver', 'local')); |
| 20 |
return [ |
| 21 |
'files' => (new FileManager($driver))->listFiles($request->all()) |
| 22 |
]; |
| 23 |
} |
| 24 |
|
| 25 |
public function getBucketList(Request $request) |
| 26 |
{ |
| 27 |
$driver = sanitize_text_field($request->get('driver', '')); |
| 28 |
$bucketList = (new FileManager($driver))->bucketLists(); |
| 29 |
$buckets = []; |
| 30 |
foreach ($bucketList as $bucket) { |
| 31 |
$buckets[] = array( |
| 32 |
"label" => $bucket, |
| 33 |
"value" => $bucket, |
| 34 |
); |
| 35 |
} |
| 36 |
$driverSettings = (new GlobalStorageHandler)->getSettings($driver); |
| 37 |
|
| 38 |
return [ |
| 39 |
// "default_bucket" => Arr::get($driverSettings, 'settings.bucket'), |
| 40 |
"default_bucket" => Arr::get($buckets, '0.value', ''), |
| 41 |
"buckets" => $buckets |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
public function upload(Request $request) |
| 46 |
{ |
| 47 |
/** |
| 48 |
* @var $file File |
| 49 |
*/ |
| 50 |
|
| 51 |
|
| 52 |
$request->validate([ |
| 53 |
'name' => 'required|sanitizeText|maxLength:160', |
| 54 |
]); |
| 55 |
|
| 56 |
$file = Arr::get($request->files(), 'file'); |
| 57 |
|
| 58 |
if (empty($file)) { |
| 59 |
return $this->sendError([ |
| 60 |
'message' => __('Failed To Upload File', 'fluent-cart'), |
| 61 |
'additional' => 'File is empty' |
| 62 |
]); |
| 63 |
} |
| 64 |
|
| 65 |
$fileName = sanitize_file_name($file->getClientOriginalName()); |
| 66 |
$driver = sanitize_text_field($request->get('driver', 'local')); |
| 67 |
|
| 68 |
if ($name = sanitize_file_name($request->get('name'))) { |
| 69 |
$fileName = $name.'.' . $file->getClientOriginalExtension(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
return (new FileManager($driver))->uploadFile( |
| 74 |
$file->getRealPath(), |
| 75 |
$fileName, |
| 76 |
$file, |
| 77 |
$request->all(), |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
public function uploadEditorFile(Request $request) |
| 82 |
{ |
| 83 |
$file = Arr::get($request->files(), 'file'); |
| 84 |
if ($file instanceof File) { |
| 85 |
$fileArray = $file->toArray(); |
| 86 |
|
| 87 |
$wp_filetype = wp_check_filetype_and_ext($fileArray['tmp_name'], $fileArray['name']); |
| 88 |
|
| 89 |
if (wp_match_mime_types('image', $wp_filetype['type'])) { |
| 90 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 91 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 92 |
require_once(ABSPATH . 'wp-admin/includes/media.php'); |
| 93 |
$attachment_id = media_handle_upload('file', 0, []); |
| 94 |
return wp_prepare_attachment_for_js($attachment_id); |
| 95 |
} |
| 96 |
return $this->sendError(__('Error Uploading File', 'fluent-cart')); |
| 97 |
} |
| 98 |
|
| 99 |
return $this->sendError(__('No File Attached', 'fluent-cart')); |
| 100 |
} |
| 101 |
|
| 102 |
public function deleteFile(Request $request) |
| 103 |
{ |
| 104 |
$request->validate([ |
| 105 |
'file_path' => 'required|string', |
| 106 |
'driver' => 'required|string' |
| 107 |
]); |
| 108 |
|
| 109 |
$filePath = sanitize_text_field($request->get('file_path')); |
| 110 |
$driver = sanitize_text_field($request->get('driver')); |
| 111 |
$bucket = sanitize_text_field($request->get('bucket')); |
| 112 |
|
| 113 |
$result = (new FileManager($driver))->deleteFile($filePath, $bucket); |
| 114 |
|
| 115 |
if (is_wp_error($result)) { |
| 116 |
return $this->sendError([ |
| 117 |
'message' => $result->get_error_message() |
| 118 |
]); |
| 119 |
} |
| 120 |
|
| 121 |
return $this->sendSuccess($result); |
| 122 |
} |
| 123 |
|
| 124 |
} |