/** * Create new Openstack Object */ function __construct() { // Set opencloud version to use $this->opencloud_version = version_compare(phpversion(), '5.3.3') >= 0 ? '1.9.2' : '1.5.10'; // Get settings, if they exist $custom_settings = (object) get_option(RS_CDN_OPTIONS); // Set settings $settings = new stdClass(); $settings->username = isset($custom_settings->username) ? $custom_settings->username : '******'; $settings->apiKey = isset($custom_settings->apiKey) ? $custom_settings->apiKey : 'API Key'; $settings->use_ssl = isset($custom_settings->use_ssl) ? $custom_settings->use_ssl : false; $settings->container = isset($custom_settings->container) ? $custom_settings->container : 'default'; $settings->cdn_url = null; $settings->files_to_ignore = null; $settings->verified = false; $settings->custom_cname = null; $settings->region = isset($custom_settings->region) ? $custom_settings->region : 'ORD'; $settings->url = isset($custom_settings->url) ? $custom_settings->url : 'https://identity.api.rackspacecloud.com/v2.0/'; // Set API settings $this->api_settings = (object) $settings; // Return client OR set settings if ($this->opencloud_version == '1.9.2') { // Set new Cloud Files client $cloud_files_client = new \OpenCloud\Rackspace($settings->url, (array) $settings); // Set Rackspace CDN settings $this->oc_service = $cloud_files_client->objectStoreService('cloudFiles', $settings->region); // Set container object $this->oc_container = $this->container_object(); } }
function rs_file_sync() { echo "\n###### Uploading Files To Rackspace ######\n"; // Build a hash of all the files currently at rackspace. $connection = new \OpenCloud\Rackspace(RACKSPACE_US, array('username' => $this->cdn_user, 'apiKey' => $this->cdn_key)); $ostore = $connection->objectStoreService(); $cont = $ostore->getContainer($this->cdn_container); $list = $cont->objectList(array('prefix' => 'assets')); $mdhash = array(); while ($o = $list->next()) { $file = $cont->getObject($o->getName()); $mdhash[$o->name] = $o->hash; } // Loop through the different image directories. And see if we should upload the files to RS. foreach ($this->css_file_dirs as $key => $row) { echo "\n###### Reviewing - {$row} ######\n"; $files = glob("{$row}/*.*"); foreach ($files as $key2 => $row2) { $name = basename($row2); // See if the file is already uploaded at Rackspace if (isset($mdhash["assets/{$key}/" . $name]) && $mdhash["assets/{$key}/" . $name] == md5_file($row2)) { continue; } $this->rs_upload($row2, "assets/{$key}/{$name}", mime_content_type($row2)); } } }
/** * @param string $args */ public function edit_ajax($args = '') { $error = ''; if (is_array($args)) { $ajax = FALSE; } else { if (!current_user_can('backwpup_jobs_edit')) { wp_die(-1); } check_ajax_referer('backwpup_ajax_nonce'); $args['rscusername'] = $_POST['rscusername']; $args['rscapikey'] = $_POST['rscapikey']; $args['rscselected'] = $_POST['rscselected']; $args['rscregion'] = $_POST['rscregion']; $ajax = TRUE; } echo '<span id="rsccontainererror" style="color:red;">'; $container_list = array(); if (!empty($args['rscusername']) && !empty($args['rscapikey']) && !empty($args['rscregion'])) { try { $conn = new OpenCloud\Rackspace(self::get_auth_url_by_region($args['rscregion']), array('username' => $args['rscusername'], 'apiKey' => BackWPup_Encryption::decrypt($args['rscapikey']))); $ostore = $conn->objectStoreService('cloudFiles', $args['rscregion'], 'publicURL'); $containerlist = $ostore->listContainers(); while ($container = $containerlist->next()) { $container_list[] = $container->name; } } catch (Exception $e) { $error = $e->getMessage(); } } if (empty($args['rscusername'])) { _e('Missing username!', 'backwpup'); } elseif (empty($args['rscapikey'])) { _e('Missing API Key!', 'backwpup'); } elseif (!empty($error)) { echo esc_html($error); } elseif (empty($container_list)) { _e("A container could not be found!", 'backwpup'); } echo '</span>'; if (!empty($container_list)) { echo '<select name="rsccontainer" id="rsccontainer">'; foreach ($container_list as $container_name) { echo "<option " . selected(strtolower($args['rscselected']), strtolower($container_name), FALSE) . ">" . $container_name . "</option>"; } echo '</select>'; } if ($ajax) { die; } else { return; } }
/** * Execute the console command. * * @return mixed */ public function fire() { $this->info('Starting Image CDN Publish'); $client = new \OpenCloud\Rackspace('https://identity.api.rackspacecloud.com/v2.0/', ['username' => env('RACKSPACE_USERNAME'), 'apiKey' => env('RACKSPACE_API_KEY')]); $objectStoreService = $client->objectStoreService(null, 'ORD'); $container = $objectStoreService->getContainer('CAAS_Assets'); // There's more than 10,000 items // http://docs.php-opencloud.com/en/latest/services/object-store/objects.html?highlight=objectlist#list-over-10-000-objects $existing_files = []; $marker = ''; $this->info('Pulling existing images. Each dot represents 100 objects.'); while ($marker !== null) { echo '.'; $objects = $container->objectList(['marker' => $marker]); $total = $objects->count(); $count = 0; if ($total == 0) { break; } foreach ($objects as $object) { $existing_files[] = $object->getName(); if (++$count == $total) { $marker = $object->getName(); break; } else { $marker = null; } } } echo "\n"; // Only show this if we're publishing $this->info(count($existing_files) . ' files found'); // Recursively go through Images. // Get the md5 of the contents. // If it's already in the deleted block, remove it from that array, no action // Otherwise, prepare the new filename // and upload that file as the new filename into the cdn $files_to_upload = array(); $tally = 0; foreach (array('items/nq', 'items/hq') as $ext) { foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator('resources/assets/images/' . $ext)) as $filename) { // Ignore '..' or '.' directories. if (in_array(substr($filename, -2), array('..', '\\.', '/.'))) { // \. for windows, /. for linux continue; } $original_file = $filename->getPathname(); $save_as = $ext . '/' . $filename->getFilename(); // Upload this file if (!in_array($save_as, $existing_files)) { $files_to_upload[] = array('name' => $save_as, 'path' => $original_file); } } } $this->info('Uploading ' . count($files_to_upload) . ' files in batches of 100.'); // Upload in batches foreach (array_chunk($files_to_upload, 100) as $files) { $this->info('Uploading ' . count($files) . ' files.'); $container->uploadObjects($files); } $this->info('cdn images publish finished'); }
/** * Execute the console command. * * @return mixed */ public function fire() { $action = $this->argument('action'); $publish = $action == 'publish'; $delete = !$publish; $this->info('Starting cdn ' . $action); // Get all files from the CDN, store their names. We'll be deleting those at the end. // Get our secret key // File kept out of repository. Contents: <?php return 'whatever_the_key_is'; $secret_key = (include 'secret_api_key.php'); $client = new \OpenCloud\Rackspace('https://identity.api.rackspacecloud.com/v2.0/', array('username' => 'nicholas.wright', 'apiKey' => $secret_key)); $objectStoreService = $client->objectStoreService(null, 'ORD'); $container = $objectStoreService->getContainer('CAAS'); $objects = $container->objectList(); $existing_files = []; foreach ($objects as $object) { $existing_files[] = $object->getName(); } // Only show this if we're publishing if ($publish) { $this->info(count($existing_files) . ' files found'); } // Recursively go through JS and CSS. // Get the md5 of the contents. // If it's already in the deleted block, remove it from that array, no action // Otherwise, prepare the new filename // and upload that file as the new filename into the cdn $files_to_upload = array(); foreach (array('css', 'js') as $ext) { foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('public/' . $ext)) as $filename) { // Ignore '..' or '.' directories. if (in_array(substr($filename, -2), array('..', '\\.', '/.'))) { // \. for windows, /. for linux continue; } // Change \'s to /'s, save this for later too $original_filename = $filename = preg_replace('/\\\\/', '/', $filename); // md5 file contents $md5 = md5_file($filename); // remove "public/" $filename = substr($filename, 7, strlen($filename)); // Place md5 string inside filename $filename = preg_replace('/\\.' . $ext . '/', '.' . $md5 . '.' . $ext, $filename); // Does this file already exist? if (in_array($filename, $existing_files)) { // remove it $existing_files = array_diff($existing_files, array($filename)); // don't do anything else with this file continue; } // Upload this file $files_to_upload[] = array('name' => $filename, 'path' => $original_filename); // Only show this if we're publishing if ($publish) { $this->info('Uploading ' . $original_filename . ' as ' . $filename); } } } // Upload those files // Only if we're publishing if ($publish) { $container->uploadObjects($files_to_upload); } // Delete the necessary files // if they were safe to keep, they would have been removed from this array // Only delete if the command actions dictates if ($delete) { foreach ($existing_files as $ef) { $this->info('Deleting ' . $ef . ' from cdn'); $object = $container->getObject($ef); $object->delete(); } } $this->info('cdn ' . $action . ' finished'); $this->comment('You may want to clear cache! (php artisan cache:clear)'); }
/** * Execute the console command. * * @return mixed */ public function fire() { $action = $this->argument('action'); $publish = $action == 'publish'; $delete = !$publish; $this->info('Starting imgcdn ' . $action); // Get all files from the CDN, store their names. We'll be deleting those at the end. // Get our secret key // File kept out of repository. Contents: <?php return 'whatever_the_key_is'; $secret_key = (include 'secret_api_key.php'); $client = new \OpenCloud\Rackspace('https://identity.api.rackspacecloud.com/v2.0/', array('username' => 'nicholas.wright', 'apiKey' => $secret_key)); $objectStoreService = $client->objectStoreService(null, 'ORD'); $container = $objectStoreService->getContainer('CAAS_Assets'); $objects = $container->objectList(); $existing_files = []; foreach ($objects as $object) { $existing_files[] = $object->getName(); } // Only show this if we're publishing if ($publish) { $this->info(count($existing_files) . ' files found'); } // Recursively go through Images. // Get the md5 of the contents. // If it's already in the deleted block, remove it from that array, no action // Otherwise, prepare the new filename // and upload that file as the new filename into the cdn $files_to_upload = array(); $tally = 0; foreach (array('items/nq', 'items/hq') as $ext) { foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/var/www/ffxivcrafting-assets/assets/' . $ext)) as $filename) { // Ignore '..' or '.' directories. if (in_array(substr($filename, -2), array('..', '\\.', '/.'))) { // \. for windows, /. for linux continue; } $original_file = $filename->getPathname(); $save_as = $ext . '/' . $filename->getFilename(); // Upload this file if (!in_array($save_as, $existing_files)) { $files_to_upload[] = array('name' => $save_as, 'path' => $original_file); } // Upload in batches // Only show this if we're publishing // if ($publish) // $this->info('Uploading ' . $original_filename . ' as ' . $filename); } } if ($publish) { $this->info('Uploading ' . count($files_to_upload) . ' files in batches of 100.'); } // Upload those files // Only if we're publishing if ($publish) { foreach (array_chunk($files_to_upload, 100) as $files) { $this->info('Uploading ' . count($files) . ' files.'); $container->uploadObjects($files); } } // Delete the necessary files // if they were safe to keep, they would have been removed from this array // Only delete if the command actions dictates // if ($delete) // foreach ($existing_files as $ef) // { // $this->info('Deleting ' . $ef . ' from cdn'); // $object = $container->getObject($ef); // $object->delete(); // } $this->info('cdn ' . $action . ' finished'); // $this->comment('You may want to clear cache! (php artisan cache:clear)'); }
/** * Execute the console command. * * @return mixed */ public function fire() { $action = $this->argument('action'); if ($action == '') { $action = 'publish'; } $publish = $action == 'publish'; $preview = $action == 'preview'; $this->info('Starting Asset CDN ' . $action); $client = new \OpenCloud\Rackspace('https://identity.api.rackspacecloud.com/v2.0/', ['username' => env('RACKSPACE_USERNAME'), 'apiKey' => env('RACKSPACE_API_KEY')]); // Get all files from the CDN, store their names. We'll be deleting those at the end. $objectStoreService = $client->objectStoreService(null, 'ORD'); $container = $objectStoreService->getContainer('CAAS'); $objects = $container->objectList(); $existing_files = []; foreach ($objects as $object) { $existing_files[] = $object->getName(); } // Only show this if we're publishing if ($preview || $publish) { $this->info(count($existing_files) . ' files found'); } // Recursively go through JS and CSS. // Get the md5 of the contents. // If it's already in the deleted block, remove it from that array, no action // Otherwise, prepare the new filename // and upload that file as the new filename into the cdn $files_to_upload = []; foreach (['css', 'js'] as $ext) { foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator('public/' . $ext)) as $filename) { // Ignore '..' or '.' directories. if (in_array(substr($filename, -2), array('..', '\\.', '/.'))) { // \. for windows, /. for linux continue; } // Change \'s to /'s, save this for later too $original_filename = $filename = preg_replace('/\\\\/', '/', $filename); // md5 file contents, get first 8 characters $md5 = substr(md5_file($filename), 0, 8); // remove "public/" $filename = substr($filename, 7, strlen($filename)); // Place md5 string inside filename $filename = preg_replace('/\\.' . $ext . '/', '.' . $md5 . '.' . $ext, $filename); // Does this file already exist? if (in_array($filename, $existing_files)) { // remove it $existing_files = array_diff($existing_files, array($filename)); // don't do anything else with this file continue; } // Upload this file $files_to_upload[] = array('name' => $filename, 'path' => $original_filename); // Only show this if we're publishing if ($preview || $publish) { $this->info('Uploading ' . $original_filename . ' as ' . $filename); } } } // Upload those files // Only if we're publishing if ($preview) { var_dump($files_to_upload); } elseif ($publish) { $container->uploadObjects($files_to_upload); } // Delete the necessary files // if they were safe to keep, they would have been removed from this array // Only delete if the command actions dictates if ($action == 'delete') { foreach ($existing_files as $ef) { $this->info('Deleting ' . $ef . ' from cdn'); $object = $container->getObject($ef); $object->delete(); } } $this->info('cdn asset ' . $action . ' finished'); $this->comment('You may want to clear cache! (php artisan cache:clear)'); }
private function _rackspace_cf_upload($json, $q) { // Setup the Configs & Rackspace connections. $username = CMS\Libraries\Config::get('cp_media_rackspace_username'); $key = CMS\Libraries\Config::get('cp_media_rackspace_key'); $container = CMS\Libraries\Config::get('cp_media_rackspace_container'); $path = CMS\Libraries\Config::get('cp_media_rackspace_path'); $url = CMS\Libraries\Config::get('cp_media_rackspace_ssl_url'); $region = CMS\Libraries\Config::get('cp_media_rackspace_region'); $tmpdir = CMS\Libraries\Config::get('cp_tmp_dir'); $connection = new \OpenCloud\Rackspace(RACKSPACE_US, array('username' => $username, 'apiKey' => $key)); // Insert the file into the media database. $q['CMS_MediaStore'] = 'rackspace-cloud-files'; $q['CMS_MediaPath'] = CMS\Libraries\Config::get('cp_media_rackspace_path'); $json['data']['id'] = $id = $this->cms_media_model->insert($q); $d['CMS_MediaFile'] = $json['data']['raw_name'] . "_{$id}" . $json['data']['file_ext']; // If the file is an image build thumbnail & upload to rs. if ($json['data']['is_image']) { $d['CMS_MediaFileThumb'] = str_ireplace($json['data']['file_ext'], '_thumb' . $json['data']['file_ext'], $d['CMS_MediaFile']); $thumb = $this->_build_thumb_nail($json['data']['full_path'], $d['CMS_MediaFileThumb']); $d['CMS_MediaPathThumb'] = CMS\Libraries\Config::get('cp_media_rackspace_path'); // Upload to rackspace $ostore = $connection->objectStoreService('cloudFiles', $region); $cont = $ostore->getContainer($container); $data = fopen($tmpdir . '/' . $thumb, 'r+'); $cont->uploadObject($d['CMS_MediaPathThumb'] . $d['CMS_MediaFileThumb'], $data, array('Content-Type' => mime_content_type($tmpdir . '/' . $thumb))); unlink($tmpdir . '/' . $thumb); // Build FQDN $json['data']['thumburl'] = CMS\Libraries\Config::get('cp_media_rackspace_url') . $d['CMS_MediaPathThumb'] . $d['CMS_MediaFileThumb']; $json['data']['thumbsslurl'] = CMS\Libraries\Config::get('cp_media_rackspace_ssl_url') . $d['CMS_MediaPathThumb'] . $d['CMS_MediaFileThumb']; } // We have to insert and then update because of the id in the file name. $this->cms_media_model->update($d, $id); // Upload to rackspace $ostore = $connection->objectStoreService('cloudFiles', $region); $cont = $ostore->getContainer($container); $data = fopen($json['data']['full_path'], 'r+'); $cont->uploadObject($q['CMS_MediaPath'] . $d['CMS_MediaFile'], $data, array('Content-Type' => mime_content_type($json['data']['full_path']))); unlink($json['data']['full_path']); // Build FQDN $json['data']['url'] = CMS\Libraries\Config::get('cp_media_rackspace_url') . $q['CMS_MediaPath'] . $d['CMS_MediaFile']; $json['data']['sslurl'] = CMS\Libraries\Config::get('cp_media_rackspace_ssl_url') . $q['CMS_MediaPath'] . $d['CMS_MediaFile']; return $json; }