| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\UserResource; |
| 6 |
use FluentCart\Api\StorageDrivers; |
| 7 |
use FluentCart\App\Hooks\Handlers\GlobalStorageHandler; |
| 8 |
use FluentCart\App\Http\Requests\UserRequest; |
| 9 |
use FluentCart\App\Services\FileSystem\FileManager; |
| 10 |
use FluentCart\App\Services\FileSystem\StoragePath; |
| 11 |
use FluentCart\Framework\Http\Request\File; |
| 12 |
use FluentCart\Framework\Http\Request\Request; |
| 13 |
use FluentCart\Framework\Support\Arr; |
| 14 |
use FluentCart\Framework\Support\Str; |
| 15 |
|
| 16 |
class FileUploadController extends Controller |
| 17 |
{ |
| 18 |
|
| 19 |
public function index(Request $request) |
| 20 |
{ |
| 21 |
$driver = sanitize_text_field($request->get('driver', 'local')) ?: 'local'; |
| 22 |
|
| 23 |
if ($error = $this->invalidDriverResponse($driver)) { |
| 24 |
return $error; |
| 25 |
} |
| 26 |
|
| 27 |
return [ |
| 28 |
'files' => (new FileManager($driver))->listFiles($request->all()) |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
public function getBucketList(Request $request) |
| 33 |
{ |
| 34 |
$driver = sanitize_text_field($request->get('driver', 'local')) ?: 'local'; |
| 35 |
|
| 36 |
if ($error = $this->invalidDriverResponse($driver)) { |
| 37 |
return $error; |
| 38 |
} |
| 39 |
|
| 40 |
$bucketList = (new FileManager($driver))->bucketLists(); |
| 41 |
$buckets = []; |
| 42 |
foreach ($bucketList as $bucket) { |
| 43 |
$buckets[] = array( |
| 44 |
"label" => $bucket, |
| 45 |
"value" => $bucket, |
| 46 |
); |
| 47 |
} |
| 48 |
$driverSettings = (new GlobalStorageHandler)->getSettings($driver); |
| 49 |
|
| 50 |
return [ |
| 51 |
// "default_bucket" => Arr::get($driverSettings, 'settings.bucket'), |
| 52 |
"default_bucket" => Arr::get($buckets, '0.value', ''), |
| 53 |
"buckets" => $buckets |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
private function invalidDriverResponse($driver) |
| 58 |
{ |
| 59 |
$availableDrivers = array_keys((new StorageDrivers())->getActive()); |
| 60 |
|
| 61 |
if (in_array($driver, $availableDrivers, true)) { |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
if (empty($availableDrivers)) { |
| 66 |
$message = __('No storage drivers are enabled. Please enable a storage driver from the storage settings.', 'fluent-cart'); |
| 67 |
} else { |
| 68 |
/* translators: %1$s: comma separated list of available storage drivers */ |
| 69 |
$message = sprintf(__('Invalid driver. Available drivers: %1$s', 'fluent-cart'), implode(', ', $availableDrivers)); |
| 70 |
} |
| 71 |
|
| 72 |
return $this->sendError([ |
| 73 |
'message' => $message, |
| 74 |
'available_drivers' => $availableDrivers |
| 75 |
], 422); |
| 76 |
} |
| 77 |
|
| 78 |
public function upload(Request $request) |
| 79 |
{ |
| 80 |
/** |
| 81 |
* @var $file File |
| 82 |
*/ |
| 83 |
|
| 84 |
|
| 85 |
$request->validate([ |
| 86 |
'name' => 'required|sanitizeText|maxLength:160', |
| 87 |
]); |
| 88 |
|
| 89 |
$file = Arr::get($request->files(), 'file'); |
| 90 |
|
| 91 |
if (empty($file)) { |
| 92 |
return $this->sendError([ |
| 93 |
'message' => __('Failed To Upload File', 'fluent-cart'), |
| 94 |
'additional' => 'File is empty' |
| 95 |
]); |
| 96 |
} |
| 97 |
|
| 98 |
$fileName = sanitize_file_name($file->getClientOriginalName()); |
| 99 |
$driver = sanitize_text_field($request->get('driver', 'local')); |
| 100 |
|
| 101 |
if ($name = sanitize_file_name($request->get('name'))) { |
| 102 |
$fileName = $name.'.' . $file->getClientOriginalExtension(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
return (new FileManager($driver))->uploadFile( |
| 107 |
$file->getRealPath(), |
| 108 |
$fileName, |
| 109 |
$file, |
| 110 |
$request->all(), |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
public function uploadEditorFile(Request $request) |
| 115 |
{ |
| 116 |
$file = Arr::get($request->files(), 'file'); |
| 117 |
if ($file instanceof File) { |
| 118 |
$fileArray = $file->toArray(); |
| 119 |
|
| 120 |
$wp_filetype = wp_check_filetype_and_ext($fileArray['tmp_name'], $fileArray['name']); |
| 121 |
|
| 122 |
if (wp_match_mime_types('image', $wp_filetype['type'])) { |
| 123 |
require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 124 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 125 |
require_once(ABSPATH . 'wp-admin/includes/media.php'); |
| 126 |
$attachment_id = media_handle_upload('file', 0, []); |
| 127 |
return wp_prepare_attachment_for_js($attachment_id); |
| 128 |
} |
| 129 |
return $this->sendError(__('Error Uploading File', 'fluent-cart')); |
| 130 |
} |
| 131 |
|
| 132 |
return $this->sendError(__('No File Attached', 'fluent-cart')); |
| 133 |
} |
| 134 |
|
| 135 |
public function deleteFile(Request $request) |
| 136 |
{ |
| 137 |
$request->validate([ |
| 138 |
'file_path' => 'required|string', |
| 139 |
'driver' => 'required|string' |
| 140 |
]); |
| 141 |
|
| 142 |
$filePath = sanitize_text_field($request->get('file_path')); |
| 143 |
$driver = sanitize_text_field($request->get('driver')); |
| 144 |
$bucket = sanitize_text_field($request->get('bucket')); |
| 145 |
|
| 146 |
// `..` survives sanitize_text_field(). The local driver contains the |
| 147 |
// path itself, but no driver has a use for a relative segment, so it is |
| 148 |
// refused here for every driver. |
| 149 |
if (!StoragePath::isSafe($filePath)) { |
| 150 |
return $this->sendError([ |
| 151 |
'message' => __('Invalid file path', 'fluent-cart') |
| 152 |
], 422); |
| 153 |
} |
| 154 |
|
| 155 |
$result = (new FileManager($driver))->deleteFile($filePath, $bucket); |
| 156 |
|
| 157 |
if (is_wp_error($result)) { |
| 158 |
return $this->sendError([ |
| 159 |
'message' => $result->get_error_message() |
| 160 |
]); |
| 161 |
} |
| 162 |
|
| 163 |
return $this->sendSuccess($result); |
| 164 |
} |
| 165 |
|
| 166 |
} |