| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
// Libraries |
| 7 |
use Dudlewebs\WPMCS\Google\Cloud\Storage\StorageClient; |
| 8 |
use Dudlewebs\WPMCS\Google\Cloud\Core\Exception\ServiceException; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
|
| 12 |
class GCloud { |
| 13 |
private $assets_url; |
| 14 |
private $version; |
| 15 |
private $token; |
| 16 |
|
| 17 |
protected $config; |
| 18 |
protected $bucketConfig; |
| 19 |
protected $settings; |
| 20 |
protected $credentials; |
| 21 |
protected $bucket_name; |
| 22 |
protected $bucket; // Object |
| 23 |
protected $cdnConfig; |
| 24 |
|
| 25 |
public $service = 'gcloud'; |
| 26 |
public $gcloudClient = false; |
| 27 |
|
| 28 |
/** |
| 29 |
* Admin constructor. |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 34 |
$this->version = WPMCS_VERSION; |
| 35 |
$this->token = WPMCS_TOKEN; |
| 36 |
|
| 37 |
// Initialize setup |
| 38 |
$this->init(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initialise Client |
| 43 |
*/ |
| 44 |
public function init() { |
| 45 |
$this->settings = Utils::get_settings(); |
| 46 |
$this->credentials = Utils::get_credentials(); |
| 47 |
$this->config = isset($this->credentials['config']) && !empty($this->credentials['config']) |
| 48 |
? $this->credentials['config'] |
| 49 |
: []; |
| 50 |
$this->bucketConfig = isset($this->credentials['bucketConfig']) && !empty($this->credentials['bucketConfig']) |
| 51 |
? $this->credentials['bucketConfig'] |
| 52 |
: []; |
| 53 |
$this->bucket_name = isset($this->bucketConfig['bucket_name']) && !empty($this->bucketConfig['bucket_name']) |
| 54 |
? $this->bucketConfig['bucket_name'] |
| 55 |
: ''; |
| 56 |
$this->cdnConfig = isset($this->credentials['cdn']) && !empty($this->credentials['cdn']) |
| 57 |
? $this->credentials['cdn'] |
| 58 |
: []; |
| 59 |
|
| 60 |
if ( |
| 61 |
isset($this->config['config_json']) && !empty($this->config['config_json']) && |
| 62 |
isset($this->config['config_json']['path']) && !empty($this->config['config_json']['path']) && |
| 63 |
isset($this->bucket_name) && !empty($this->bucket_name) |
| 64 |
) { |
| 65 |
if(file_exists($this->config['config_json']['path'])){ |
| 66 |
// Set google client |
| 67 |
$this->gcloudClient = new StorageClient([ |
| 68 |
'keyFilePath' => $this->config['config_json']['path'], |
| 69 |
]); |
| 70 |
// Set bucket object |
| 71 |
$this->bucket = $this->gcloudClient->bucket($this->bucket_name); |
| 72 |
} else { |
| 73 |
add_action('admin_notices', function (){ |
| 74 |
echo wp_kses_post(sprintf( "<div class='error'><p><strong>%s: </strong><br>Google Cloud Storage configuration file missing from the directory. |
| 75 |
It may break the media url's as well as media uploads.<br> |
| 76 |
<a href='%s'>Re-configure</a> plugin to fix the issue. |
| 77 |
</p></div>", |
| 78 |
esc_html__('Media Cloud Sync', 'media-cloud-sync'), |
| 79 |
admin_url('admin.php?page='.$this->token . '-admin-ui#/configure') |
| 80 |
)); |
| 81 |
}); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Verify Credentials |
| 88 |
* @since 1.0.0 |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
public function verifyCredentials( $config_file ){ |
| 92 |
if ( |
| 93 |
isset($config_file) && !empty($config_file) && |
| 94 |
isset($config_file['path']) && !empty($config_file['path']) && |
| 95 |
file_exists($config_file['path']) |
| 96 |
) { |
| 97 |
try { |
| 98 |
$googleClient = new StorageClient([ |
| 99 |
'keyFilePath' => $config_file['path'], |
| 100 |
]); |
| 101 |
|
| 102 |
$result = [ |
| 103 |
'success' => false, |
| 104 |
'code' => 200, |
| 105 |
'message' => esc_html__('Please check the authorization details', 'media-cloud-sync'), |
| 106 |
]; |
| 107 |
|
| 108 |
try { |
| 109 |
$bucket = $googleClient->bucket($this->token . '_dummy-bucket-for-auth-check'); |
| 110 |
$exists = $bucket->exists(); // Triggers the API call |
| 111 |
|
| 112 |
// If we reach here, the credentials are valid |
| 113 |
$result = [ |
| 114 |
'success' => true, |
| 115 |
'code' => 200, |
| 116 |
'message' => esc_html__('Credentials are valid', 'media-cloud-sync'), |
| 117 |
]; |
| 118 |
} catch (ServiceException $e) { |
| 119 |
$statusCode = $e->getCode(); |
| 120 |
|
| 121 |
$validErrors = [200, 403, 404]; |
| 122 |
|
| 123 |
if (in_array($statusCode, $validErrors)) { |
| 124 |
// If we reach here, the credentials are valid |
| 125 |
$result = [ |
| 126 |
'success' => true, |
| 127 |
'code' => 200, |
| 128 |
'message' => esc_html__('Credentials are valid', 'media-cloud-sync'), |
| 129 |
]; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if($result['success'] == false) { |
| 134 |
return $result; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
try { |
| 139 |
$buckets = $googleClient->buckets(); |
| 140 |
$newBucketFormat = []; |
| 141 |
if(isset($buckets) && !empty($buckets)){ |
| 142 |
foreach($buckets as $bucket) { |
| 143 |
$name = $bucket->name(); |
| 144 |
if(!empty($name)) { |
| 145 |
// Fetch the bucket's metadata |
| 146 |
$bucketInfo = $bucket->info(); |
| 147 |
$newBucketFormat[] = ['Name' => $name, 'CreationDate' => $bucketInfo['timeCreated']]; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
$result['buckets_data']['buckets'] = $newBucketFormat; |
| 152 |
$result['buckets_data']['message'] = esc_html__('Buckets listed successfully', 'media-cloud-sync'); |
| 153 |
$result['buckets_data']['status'] = true; |
| 154 |
} catch (Exception $e) { |
| 155 |
$result ['buckets_data']['buckets'] = []; |
| 156 |
$result ['buckets_data']['message'] = esc_html__('Unable to list buckets, Please check the bucket listing permission', 'media-cloud-sync'); |
| 157 |
$result ['buckets_data']['status'] = false; |
| 158 |
} |
| 159 |
return $result; |
| 160 |
} catch (Exception $ex) { |
| 161 |
return array('message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 162 |
} |
| 163 |
} |
| 164 |
return array('message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* Verify Bucket Exists |
| 170 |
* @since 1.0.0 |
| 171 |
* @return boolean |
| 172 |
*/ |
| 173 |
public function verifyBucketExist( $config_file, $bucket_name ){ |
| 174 |
if ( !( isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && !empty($bucket_name) ) ) { |
| 175 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 176 |
} |
| 177 |
|
| 178 |
try { |
| 179 |
$googleClient = new StorageClient([ |
| 180 |
'keyFilePath' => $config_file['path'], |
| 181 |
]); |
| 182 |
|
| 183 |
try { |
| 184 |
$bucket = $googleClient->bucket($bucket_name); |
| 185 |
|
| 186 |
if ($bucket->exists()) { |
| 187 |
return [ |
| 188 |
'message' => esc_html__('Bucket exists', 'media-cloud-sync'), |
| 189 |
'code' => 200, |
| 190 |
'success' => true, |
| 191 |
]; |
| 192 |
} else { |
| 193 |
return [ |
| 194 |
'message' => esc_html__('Bucket does not exist', 'media-cloud-sync'), |
| 195 |
'code' => 200, |
| 196 |
'success' => false, |
| 197 |
]; |
| 198 |
} |
| 199 |
} catch (ServiceException $e) { |
| 200 |
return [ |
| 201 |
'message' => esc_html__('Bucket does not exist or credentials are invalid: ', 'media-cloud-sync') . $e->getMessage(), |
| 202 |
'code' => 200, |
| 203 |
'success' => false, |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
if($bucket_found) { |
| 209 |
return array('message' => esc_html__('Bucket exist', 'media-cloud-sync'), 'code' => 200, 'success' => true); |
| 210 |
} else { |
| 211 |
return array('message' => esc_html__("Bucket choosen does not exist / does not have read permission", 'media-cloud-sync'), 'code' => 200, 'success' => false); |
| 212 |
} |
| 213 |
} catch (Exception $ex) { |
| 214 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Create Bucket |
| 221 |
* @since 1.0.0 |
| 222 |
* @return boolean |
| 223 |
*/ |
| 224 |
public function createBucket( $config_file, $region, $bucket_name ){ |
| 225 |
if ( !( isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && !empty($region) && !empty($bucket_name) ) ) { |
| 226 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 227 |
} |
| 228 |
|
| 229 |
try { |
| 230 |
$googleClient = new StorageClient([ |
| 231 |
'keyFilePath' => $config_file['path'], |
| 232 |
]); |
| 233 |
|
| 234 |
// Create Bucket |
| 235 |
$bucket = $googleClient->createBucket($bucket_name, [ |
| 236 |
'location' => $region, |
| 237 |
'iamConfiguration' => [ |
| 238 |
'uniformBucketLevelAccess' => [ |
| 239 |
'enabled' => true |
| 240 |
] |
| 241 |
] |
| 242 |
]); |
| 243 |
|
| 244 |
$iam = $bucket->iam(); |
| 245 |
$policy = $iam->policy(); |
| 246 |
|
| 247 |
// Add allUsers as a Storage Object Viewer |
| 248 |
$policy['bindings'][] = [ |
| 249 |
'role' => 'roles/storage.objectViewer', |
| 250 |
'members' => ['allUsers'], |
| 251 |
]; |
| 252 |
|
| 253 |
// Set the updated policy |
| 254 |
$iam->setPolicy($policy); |
| 255 |
|
| 256 |
return [ |
| 257 |
'message' => esc_html__('Bucket created successfully. Choose bucket from list to select the bucket.', 'media-cloud-sync'), |
| 258 |
'data' => [ |
| 259 |
'Name' => $bucket_name, |
| 260 |
'CreationDate' => date('Y-m-d\TH:i:s\Z'), |
| 261 |
], |
| 262 |
'code' => 200, |
| 263 |
'success' => true, |
| 264 |
]; |
| 265 |
|
| 266 |
} catch (Exception $ex) { |
| 267 |
return ['message' => $e->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Check Bucket Write Permission |
| 273 |
* @since 1.0.0 |
| 274 |
*/ |
| 275 |
public function verifyObjectWritePermission($config_file, $bucket_name){ |
| 276 |
if ( !( isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && !empty($bucket_name) ) ) { |
| 277 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 278 |
} |
| 279 |
|
| 280 |
try { |
| 281 |
$googleClient = new StorageClient([ |
| 282 |
'keyFilePath' => $config_file['path'], |
| 283 |
]); |
| 284 |
|
| 285 |
$bucket = $googleClient->bucket($bucket_name); |
| 286 |
if ($bucket->exists()) { |
| 287 |
$bucket_found = true; |
| 288 |
} else { |
| 289 |
return ['message' => esc_html__('No Buckets found', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 290 |
} |
| 291 |
if ($bucket_found) { |
| 292 |
$object_key = Utils::generate_object_key($this->token . '_dummy-object-for-bucket-permission-check', ''); |
| 293 |
|
| 294 |
// Prepare a temporary file with content to check write permission |
| 295 |
$stream = fopen('php://temp', 'r+'); |
| 296 |
fwrite($stream, 'This is a test object to check write permission.'); |
| 297 |
rewind($stream); |
| 298 |
|
| 299 |
// Upload the object to the bucket |
| 300 |
$object = $bucket->upload( |
| 301 |
$stream, |
| 302 |
[ |
| 303 |
'name' => $object_key, |
| 304 |
] |
| 305 |
); |
| 306 |
if(is_resource($stream)) { |
| 307 |
fclose($stream); |
| 308 |
} |
| 309 |
|
| 310 |
if ($object->exists()) { |
| 311 |
return ['message' => esc_html__('Bucket write permission verified successfully', 'media-cloud-sync'), 'code' => 200, 'success' => true]; |
| 312 |
} else { |
| 313 |
return ['message' => esc_html__('Bucket write permission not verified', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 314 |
} |
| 315 |
} |
| 316 |
} catch (Exception $ex) { |
| 317 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Check Bucket Delete Permission |
| 323 |
* @since 1.0.0 |
| 324 |
*/ |
| 325 |
public function verifyObjectDeletePermission($config_file, $bucket_name){ |
| 326 |
if ( !( isset($config_file['path']) && !empty($config_file['path']) && file_exists($config_file['path']) && !empty($bucket_name) ) ) { |
| 327 |
return ['message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 328 |
} |
| 329 |
|
| 330 |
try { |
| 331 |
$googleClient = new StorageClient([ |
| 332 |
'keyFilePath' => $config_file['path'], |
| 333 |
]); |
| 334 |
|
| 335 |
$bucket = $googleClient->bucket($bucket_name); |
| 336 |
try { |
| 337 |
$object_key = Utils::generate_object_key($this->token . '_dummy-object-for-bucket-permission-check', ''); |
| 338 |
|
| 339 |
// Try fetching a dummy object to test access, like you did with S3 |
| 340 |
$object = $bucket->object($object_key); |
| 341 |
if ($object->exists()) { |
| 342 |
$object->delete(); |
| 343 |
if (!$object->exists()) { |
| 344 |
return [ |
| 345 |
'message' => esc_html__('Bucket exists', 'media-cloud-sync'), |
| 346 |
'code' => 200, |
| 347 |
'success' => true, |
| 348 |
]; |
| 349 |
} else { |
| 350 |
return [ |
| 351 |
'message' => esc_html__('You do not have permission to delete object', 'media-cloud-sync'), |
| 352 |
'code' => 200, |
| 353 |
'success' => false, |
| 354 |
]; |
| 355 |
} |
| 356 |
} else { |
| 357 |
return [ |
| 358 |
'message' => esc_html__('Object does not exist', 'media-cloud-sync'), |
| 359 |
'code' => 200, |
| 360 |
'success' => false, |
| 361 |
]; |
| 362 |
} |
| 363 |
} catch (Exception $ex) { |
| 364 |
return [ |
| 365 |
'message' => esc_html__('Object does not exist or credentials are invalid: ', 'media-cloud-sync') . $e->getMessage(), |
| 366 |
'code' => 200, |
| 367 |
'success' => false, |
| 368 |
]; |
| 369 |
} |
| 370 |
|
| 371 |
} catch (Exception $ex) { |
| 372 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Check Bucket Read Permission |
| 378 |
* @since 1.2.4 |
| 379 |
*/ |
| 380 |
public function verifyObjectReadPermission() { |
| 381 |
$result = [ |
| 382 |
'status' => false, |
| 383 |
'message' => '', |
| 384 |
'lastChecked' => time(), |
| 385 |
]; |
| 386 |
if (empty($this->gcloudClient) || empty($this->bucket_name)) { |
| 387 |
$result['message'] = esc_html__('Please check the authorization details', 'media-cloud-sync'); |
| 388 |
Utils::set_status('cdnRead', $result); |
| 389 |
return [ |
| 390 |
'message' => $result['message'], |
| 391 |
'code' => 200, |
| 392 |
'success' => false, |
| 393 |
'lastChecked' => $result['lastChecked'], |
| 394 |
]; |
| 395 |
} |
| 396 |
|
| 397 |
try { |
| 398 |
$object_key = Utils::generate_object_key($this->token . '_dummy-object-for-bucket-permission-check', ''); |
| 399 |
|
| 400 |
// Check if the object was created successfully |
| 401 |
if (!$this->bucket->object($object_key)->exists()) { |
| 402 |
// Create a dummy object to check write permission |
| 403 |
$stream = fopen('php://temp', 'r+'); |
| 404 |
fwrite($stream, 'This is a test object to check read permission.'); |
| 405 |
rewind($stream); |
| 406 |
$this->bucket->upload( |
| 407 |
$stream, |
| 408 |
[ |
| 409 |
'name' => $object_key, |
| 410 |
] |
| 411 |
); |
| 412 |
if (is_resource($stream)) { |
| 413 |
fclose($stream); |
| 414 |
} |
| 415 |
// Re-check if the object was created successfully |
| 416 |
if (!$this->bucket->object($object_key)->exists()) { |
| 417 |
$result['status'] = false; |
| 418 |
$result['message'] = esc_html__('Failed to create an object for read permission check, please check service configuration', 'media-cloud-sync'); |
| 419 |
Utils::set_status('cdnRead', $result); |
| 420 |
return [ |
| 421 |
'message' => $result['message'], |
| 422 |
'code' => 200, |
| 423 |
'success' => false, |
| 424 |
'lastChecked' => $result['lastChecked'], |
| 425 |
]; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
$url = $this->generate_file_url($object_key); |
| 430 |
$cdn_url = Cdn::may_generate_cdn_url($url, $object_key); |
| 431 |
|
| 432 |
$headers = @get_headers($cdn_url); |
| 433 |
if (strpos($headers[0], '200') !== false) { |
| 434 |
$result['status'] = true; |
| 435 |
$result['message'] = esc_html__('Objects are accessible to Read', 'media-cloud-sync'); |
| 436 |
} else if (strpos($headers[0], '403') !== false) { |
| 437 |
$result['status'] = false; |
| 438 |
if($this->cdnConfig['service'] == $this->service) { |
| 439 |
$result['message'] = esc_html__('Access Denied. Please check your bucket policy. Public Read Access is required.', 'media-cloud-sync'); |
| 440 |
} else { |
| 441 |
$result['message'] = esc_html__('Access Denied. Please check your bucket policy', 'media-cloud-sync'); |
| 442 |
} |
| 443 |
$result['message'] = esc_html__('Access Denied. Please check your bucket policy', 'media-cloud-sync'); |
| 444 |
} else if (strpos($headers[0], '404') !== false) { |
| 445 |
$result['status'] = false; |
| 446 |
$result['message'] = esc_html__('Object not found. Please check your bucket policy', 'media-cloud-sync'); |
| 447 |
} else if (strpos($headers[0], '500') !== false) { |
| 448 |
$result['status'] = false; |
| 449 |
$result['message'] = esc_html__('Internal Server error. Please check your bucket policy', 'media-cloud-sync'); |
| 450 |
} else { |
| 451 |
$result['status'] = false; |
| 452 |
$result['message'] = esc_html__('Objects are not accessible to read', 'media-cloud-sync'); |
| 453 |
} |
| 454 |
Utils::set_status('cdnRead', $result); |
| 455 |
$this->deleteSingle($object_key); |
| 456 |
return [ |
| 457 |
'message' => $result['message'], |
| 458 |
'code' => 200, |
| 459 |
'success' => $result['status'], |
| 460 |
'lastChecked' => $result['lastChecked'], |
| 461 |
]; |
| 462 |
} catch (ServiceException $ex) { |
| 463 |
return ['message' => $ex->getMessage(), 'code' => 200, 'success' => false]; |
| 464 |
} catch (Exception $ex) { |
| 465 |
return ['message' => $ex->getMessage() ?? esc_html__('Please check the authorization details', 'media-cloud-sync'), 'code' => 200, 'success' => false]; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* isConfigured Function To Identify the congfigurations are correct |
| 471 |
* @since 1.0.0 |
| 472 |
*/ |
| 473 |
public function isConfigured(){ |
| 474 |
if ($this->gcloudClient) { |
| 475 |
try { |
| 476 |
$bucket = $this->gcloudClient->bucket($this->token . '_dummy-bucket-for-auth-check'); |
| 477 |
$exists = $bucket->exists(); // Triggers the API call |
| 478 |
return false; |
| 479 |
} catch (ServiceException $e) { |
| 480 |
$statusCode = $e->getCode(); |
| 481 |
|
| 482 |
$validErrors = [200, 403, 404]; |
| 483 |
|
| 484 |
if (in_array($statusCode, $validErrors)) { |
| 485 |
// If we reach here, the credentials are valid |
| 486 |
return true; |
| 487 |
} else { |
| 488 |
// If we reach here, the credentials are not valid |
| 489 |
return false; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
return false; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Make Object Private |
| 498 |
* @since 1.0.0 |
| 499 |
* |
| 500 |
*/ |
| 501 |
public function toPrivate($key) { |
| 502 |
if(!$key) return false; |
| 503 |
|
| 504 |
try { |
| 505 |
$object = $this->bucket->object($key); |
| 506 |
if ($object->exists()) { |
| 507 |
$object->update(['acl' => []], ['predefinedAcl' => 'private']); |
| 508 |
return true; |
| 509 |
} |
| 510 |
} catch (ServiceException $e) { |
| 511 |
// Handle exception if needed |
| 512 |
return false; |
| 513 |
} catch (Exception $e) { |
| 514 |
// Handle other exceptions if needed |
| 515 |
return false; |
| 516 |
} |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* Make Object Public |
| 523 |
* @since 1.0.0 |
| 524 |
* |
| 525 |
*/ |
| 526 |
public function toPublic($key) { |
| 527 |
if(!$key) return false; |
| 528 |
|
| 529 |
try { |
| 530 |
$object = $this->bucket->object($key); |
| 531 |
if ($object->exists()) { |
| 532 |
$object->update(['acl' => []], ['predefinedAcl' => 'publicRead']); |
| 533 |
return true; |
| 534 |
} |
| 535 |
return false; |
| 536 |
} catch (ServiceException $e) { |
| 537 |
// Handle exception if needed |
| 538 |
return false; |
| 539 |
} catch (Exception $e) { |
| 540 |
// Handle other exceptions if needed |
| 541 |
return false; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
/** |
| 547 |
* Check the object exist |
| 548 |
* @since 1.1.8 |
| 549 |
*/ |
| 550 |
public function exists($key) { |
| 551 |
if(!$key) return false; |
| 552 |
|
| 553 |
try { |
| 554 |
$object = $this->bucket->object($key); |
| 555 |
if ($object->exists()) { |
| 556 |
return true; |
| 557 |
} else { |
| 558 |
return false; |
| 559 |
} |
| 560 |
} catch (ServiceException $e) { |
| 561 |
// Handle exception if needed |
| 562 |
return false; |
| 563 |
} catch (Exception $e) { |
| 564 |
// Handle other exceptions if needed |
| 565 |
return false; |
| 566 |
} |
| 567 |
|
| 568 |
return false; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
/** |
| 573 |
* Upload Single |
| 574 |
* @since 1.0.0 |
| 575 |
* @return boolean |
| 576 |
*/ |
| 577 |
public function uploadSingle($media_absolute_path, $media_path, $prefix=''){ |
| 578 |
$result = array(); |
| 579 |
if ( |
| 580 |
isset($media_absolute_path) && !empty($media_absolute_path) && |
| 581 |
isset($media_path) && !empty($media_path) |
| 582 |
) { |
| 583 |
$file_name = wp_basename( $media_path ); |
| 584 |
if ($file_name) { |
| 585 |
$upload_path = Utils::generate_object_key($media_path, $prefix); |
| 586 |
|
| 587 |
// Decide Multipart upload or normal put object |
| 588 |
if (filesize($media_absolute_path) <= Schema::getConstant('GCLOUD_MULTIPART_MIN_FILE_SIZE')) { |
| 589 |
// Upload a publicly accessible file. The file size and type are determined by the SDK. |
| 590 |
try { |
| 591 |
|
| 592 |
$upload = $this->bucket->upload( |
| 593 |
fopen($media_absolute_path, 'r'), |
| 594 |
[ |
| 595 |
'name' => $upload_path, |
| 596 |
] |
| 597 |
); |
| 598 |
|
| 599 |
$object = $this->bucket->object($upload_path); |
| 600 |
|
| 601 |
if ($object->exists()) { |
| 602 |
$result = array( |
| 603 |
'success' => true, |
| 604 |
'code' => 200, |
| 605 |
'file_url' => $this->generate_file_url($upload_path), |
| 606 |
'key' => $upload_path, |
| 607 |
'message' => esc_html__('File Uploaded Successfully', 'media-cloud-sync'), |
| 608 |
); |
| 609 |
} else { |
| 610 |
$result = array( |
| 611 |
'success' => false, |
| 612 |
'code' => 200, |
| 613 |
'message' => esc_html__('Object not found at server.', 'media-cloud-sync'), |
| 614 |
); |
| 615 |
} |
| 616 |
} catch (Exception $e) { |
| 617 |
$result = array( |
| 618 |
'success' => false, |
| 619 |
'code' => 200, |
| 620 |
'message' => $e->getMessage(), |
| 621 |
); |
| 622 |
} |
| 623 |
} else { |
| 624 |
try { |
| 625 |
$upload = $this->bucket->upload( |
| 626 |
fopen($media_absolute_path, 'r'), |
| 627 |
[ |
| 628 |
'name' => $upload_path, |
| 629 |
'chunkSize' => 262144 * 2, |
| 630 |
] |
| 631 |
); |
| 632 |
|
| 633 |
$object = $this->bucket->object($upload_path); |
| 634 |
|
| 635 |
if ($object->exists()) { |
| 636 |
$result = array( |
| 637 |
'success' => true, |
| 638 |
'code' => 200, |
| 639 |
'file_url' => $this->generate_file_url($upload_path), |
| 640 |
'key' => $upload_path, |
| 641 |
'message' => esc_html__('File Uploaded Successfully', 'media-cloud-sync'), |
| 642 |
); |
| 643 |
} else { |
| 644 |
$result = array( |
| 645 |
'success' => false, |
| 646 |
'code' => 200, |
| 647 |
'message' => esc_html__('Something happened while uploading to server', 'media-cloud-sync'), |
| 648 |
); |
| 649 |
} |
| 650 |
} catch (Exception $e) { |
| 651 |
$result = array( |
| 652 |
'success' => false, |
| 653 |
'code' => 200, |
| 654 |
'message' => $e->getMessage(), |
| 655 |
); |
| 656 |
} |
| 657 |
} |
| 658 |
} else { |
| 659 |
$result = array( |
| 660 |
'success' => false, |
| 661 |
'code' => 200, |
| 662 |
'message' => esc_html__('Check the file you are trying to upload. Please try again', 'media-cloud-sync'), |
| 663 |
); |
| 664 |
} |
| 665 |
} else { |
| 666 |
$result = array( |
| 667 |
'success' => false, |
| 668 |
'code' => 200, |
| 669 |
'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), |
| 670 |
); |
| 671 |
} |
| 672 |
return $result; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
/** |
| 677 |
* Save object to server |
| 678 |
* @since 1.0.0 |
| 679 |
*/ |
| 680 |
public function object_to_server($key, $save_path){ |
| 681 |
try { |
| 682 |
$object = $this->bucket->object($key); |
| 683 |
if ($object->exists()) { |
| 684 |
$object->downloadToFile($save_path); |
| 685 |
if (file_exists($save_path)) { |
| 686 |
return true; |
| 687 |
} |
| 688 |
} |
| 689 |
} catch (Exception $e) { |
| 690 |
return false; |
| 691 |
} |
| 692 |
return false; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
/** |
| 697 |
* Delete Single |
| 698 |
* @since 1.0.0 |
| 699 |
* @return boolean |
| 700 |
*/ |
| 701 |
public function deleteSingle($key){ |
| 702 |
$result = array(); |
| 703 |
if (isset($key) && !empty($key)) { |
| 704 |
try { |
| 705 |
$object = $this->bucket->object($key); |
| 706 |
$object->delete(); |
| 707 |
|
| 708 |
if (!$object->exists()) { |
| 709 |
$result = array( |
| 710 |
'success' => true, |
| 711 |
'code' => 200, |
| 712 |
'message' => esc_html__('Deleted Successfully', 'media-cloud-sync'), |
| 713 |
); |
| 714 |
} else { |
| 715 |
$result = array( |
| 716 |
'success' => false, |
| 717 |
'code' => 200, |
| 718 |
'message' => esc_html__('File not deleted', 'media-cloud-sync'), |
| 719 |
); |
| 720 |
} |
| 721 |
} catch (Exception $e) { |
| 722 |
$result = array( |
| 723 |
'success' => false, |
| 724 |
'code' => 200, |
| 725 |
'message' => $e->getMessage(), |
| 726 |
); |
| 727 |
} |
| 728 |
} else { |
| 729 |
$result = array( |
| 730 |
'success' => false, |
| 731 |
'code' => 200, |
| 732 |
'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), |
| 733 |
); |
| 734 |
} |
| 735 |
return $result; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
/** |
| 740 |
* get presigned URL |
| 741 |
* @since 1.0.0 |
| 742 |
* @return boolean |
| 743 |
*/ |
| 744 |
public function get_presigned_url($key) { |
| 745 |
$result = array(); |
| 746 |
if (isset($key) && !empty($key)) { |
| 747 |
try { |
| 748 |
$object = $this->bucket->object($key); |
| 749 |
|
| 750 |
$expires = isset($this->settings['presigned_expire']) ? $this->settings['presigned_expire'] : 20; |
| 751 |
|
| 752 |
$presignedUrl = $object->signedUrl(new \DateTime(sprintf('+%s minutes', $expires))); |
| 753 |
|
| 754 |
if ($presignedUrl) { |
| 755 |
$result = array( |
| 756 |
'success' => true, |
| 757 |
'code' => 200, |
| 758 |
'file_url' => $presignedUrl, |
| 759 |
'message' => esc_html__('Got Presigned URL Successfully', 'media-cloud-sync'), |
| 760 |
); |
| 761 |
} else { |
| 762 |
$result = array( |
| 763 |
'success' => false, |
| 764 |
'code' => 200, |
| 765 |
'message' => esc_html__('Error getting presigned URL', 'media-cloud-sync'), |
| 766 |
); |
| 767 |
} |
| 768 |
} catch (Exception $e) { |
| 769 |
$result = array( |
| 770 |
'success' => false, |
| 771 |
'code' => 200, |
| 772 |
'message' => $e->getMessage(), |
| 773 |
); |
| 774 |
} |
| 775 |
} else { |
| 776 |
$result = array( |
| 777 |
'success' => false, |
| 778 |
'code' => 200, |
| 779 |
'message' => esc_html__('Insufficient Data. Please try again', 'media-cloud-sync'), |
| 780 |
); |
| 781 |
} |
| 782 |
return $result; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
/** |
| 787 |
* Generate file URL |
| 788 |
*/ |
| 789 |
private function generate_file_url($key){ |
| 790 |
$domain = $this->get_domain(); |
| 791 |
|
| 792 |
return apply_filters('wpmcs_generate_google_file_url', |
| 793 |
$domain . '/' . $this->bucket_name . '/' . $key, |
| 794 |
$domain, $key, |
| 795 |
$this->bucket_name |
| 796 |
); |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
/** |
| 801 |
* Get domain URL |
| 802 |
*/ |
| 803 |
public function get_domain() { |
| 804 |
$url_base = 'https://storage.googleapis.com'; |
| 805 |
|
| 806 |
return $url_base; |
| 807 |
} |
| 808 |
} |