<commit_before><?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Name:			CDN
*
* Description:	A Library for dealing with content in the CDN
*
*/

class Cdn
{
	//	Class traits
	use NAILS_COMMON_TRAIT_ERROR_HANDLING;
	use NAILS_COMMON_TRAIT_CACHING;
	use NAILS_COMMON_TRAIT_GETCOUNT_COMMON;

	private $_ci;
	private $_cdn;
	private $db;

	// --------------------------------------------------------------------------


	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	none
	 * @return	void
	 **/
	public function __construct($options = null)
	{
		$this->_ci	=& get_instance();
		$this->db	=& get_instance()->db;

		// --------------------------------------------------------------------------

		//	Load langfile
		$this->_ci->lang->load('cdn/cdn');

		// --------------------------------------------------------------------------

		//	Load the helper
		$this->_ci->load->helper('cdn');

		// --------------------------------------------------------------------------

		//	Load the storage driver
		$_class = $this->_include_driver();
		$this->_cdn = new $_class($options);
	}


	// --------------------------------------------------------------------------


	/**
	 * Destruct the model
	 *
	 * @access	public
	 * @return	void
	 **/
	public function __destruct()
	{
		//	Clear cache's
		if (isset($this->_cache_keys) && $this->_cache_keys) :

			foreach ($this->_cache_keys AS $key) :

				$this->_unset_cache($key);

			endforeach;

		endif;
	}


	// --------------------------------------------------------------------------


	/**
	 * Loads the appropriate driver
	 *
	 * @access	protected
	 * @return	void
	 **/
	protected function _include_driver()
	{
		include_once NAILS_PATH . 'module-cdn/cdn/_resources/drivers/_driver.php';

		switch (strtoupper(APP_CDN_DRIVER)) :

			case 'AWS_LOCAL' :

				include_once NAILS_PATH . 'module-cdn/cdn/_resources/drivers/aws_local.php';
				return 'Aws_local_CDN';

			break;

			// --------------------------------------------------------------------------

			case 'LOCAL':
			default:

				include_once NAILS_PATH . 'module-cdn/cdn/_resources/drivers/local.php';
				return 'Local_CDN';

			break;

		endswitch;
	}


	// --------------------------------------------------------------------------


	/**
	 * Need a public route to the _set_error() method (for the driver)
	 * @param string $error the error message
	 */
	public function set_error($error)
	{
		return $this->_set_error($error);
	}


	// --------------------------------------------------------------------------


	/**
	 * Unset an object from the cache in one fell swoop
	 *
	 * @access	protected
	 * @param	object	$object	The object to remove from the cache
	 * @return	boolean
	 **/
	protected function _unset_cache_object($object, $clearCachedir = true)
	{
		$objectId		= isset($object->id) ? $object->id : '';
		$objectFilename	= isset($object->filename) ? $object->filename : '';
		$bucketId		= isset($object->bucket->id) ? $object->bucket->id : '';
		$bucketSlug		= isset($object->bucket->slug) ? $object->bucket->slug : '';

		// --------------------------------------------------------------------------

		$this->_unset_cache('object-' . $objectId);
		$this->_unset_cache('object-' . $objectFilename);
		$this->_unset_cache('object-' . $objectFilename . '-' . $bucketId);
		$this->_unset_cache('object-' . $objectFilename . '-' . $bucketSlug);

		// --------------------------------------------------------------------------

		//	Clear out any cache files
		if ($clearCachedir) {

			// Create a handler for the directory
			$pattern = '#^' . $bucketSlug . '-' . substr($objectFilename, 0, strrpos($objectFilename, '.')) . '#';
			$fh      = opendir(DEPLOY_CACHE_DIR);

			// Open directory and walk through the filenames
			while ($file = readdir($fh)) {

				// If file isn't this directory or its parent, add it to the results
				if ($file != '.' && $file != '..') {

					// Check with regex that the file format is what we're expecting and not something else
					if (preg_match($pattern, $file)) {

						// DESTROY!
						@unlink(DEPLOY_CACHE_DIR . $file);

					}
				}
			}
		}
	}

<commit_msg>Doing a directory check before attempting to map a directory (prevents logs filling up)<commit_after><?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Name:			CDN
*
* Description:	A Library for dealing with content in the CDN
*
*/

class Cdn
{
	//	Class traits
	use NAILS_COMMON_TRAIT_ERROR_HANDLING;
	use NAILS_COMMON_TRAIT_CACHING;
	use NAILS_COMMON_TRAIT_GETCOUNT_COMMON;

	private $_ci;
	private $_cdn;
	private $db;

	// --------------------------------------------------------------------------


	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	none
	 * @return	void
	 **/
	public function __construct($options = null)
	{
		$this->_ci	=& get_instance();
		$this->db	=& get_instance()->db;

		// --------------------------------------------------------------------------

		//	Load langfile
		$this->_ci->lang->load('cdn/cdn');

		// --------------------------------------------------------------------------

		//	Load the helper
		$this->_ci->load->helper('cdn');

		// --------------------------------------------------------------------------

		//	Load the storage driver
		$_class = $this->_include_driver();
		$this->_cdn = new $_class($options);
	}


	// --------------------------------------------------------------------------


	/**
	 * Destruct the model
	 *
	 * @access	public
	 * @return	void
	 **/
	public function __destruct()
	{
		//	Clear cache's
		if (isset($this->_cache_keys) && $this->_cache_keys) :

			foreach ($this->_cache_keys AS $key) :

				$this->_unset_cache($key);

			endforeach;

		endif;
	}


	// --------------------------------------------------------------------------


	/**
	 * Loads the appropriate driver
	 *
	 * @access	protected
	 * @return	void
	 **/
	protected function _include_driver()
	{
		include_once NAILS_PATH . 'module-cdn/cdn/_resources/drivers/_driver.php';

		switch (strtoupper(APP_CDN_DRIVER)) :

			case 'AWS_LOCAL' :

				include_once NAILS_PATH . 'module-cdn/cdn/_resources/drivers/aws_local.php';
				return 'Aws_local_CDN';

			break;

			// --------------------------------------------------------------------------

			case 'LOCAL':
			default:

				include_once NAILS_PATH . 'module-cdn/cdn/_resources/drivers/local.php';
				return 'Local_CDN';

			break;

		endswitch;
	}


	// --------------------------------------------------------------------------


	/**
	 * Need a public route to the _set_error() method (for the driver)
	 * @param string $error the error message
	 */
	public function set_error($error)
	{
		return $this->_set_error($error);
	}


	// --------------------------------------------------------------------------


	/**
	 * Unset an object from the cache in one fell swoop
	 *
	 * @access	protected
	 * @param	object	$object	The object to remove from the cache
	 * @return	boolean
	 **/
	protected function _unset_cache_object($object, $clearCachedir = true)
	{
		$objectId		= isset($object->id) ? $object->id : '';
		$objectFilename	= isset($object->filename) ? $object->filename : '';
		$bucketId		= isset($object->bucket->id) ? $object->bucket->id : '';
		$bucketSlug		= isset($object->bucket->slug) ? $object->bucket->slug : '';

		// --------------------------------------------------------------------------

		$this->_unset_cache('object-' . $objectId);
		$this->_unset_cache('object-' . $objectFilename);
		$this->_unset_cache('object-' . $objectFilename . '-' . $bucketId);
		$this->_unset_cache('object-' . $objectFilename . '-' . $bucketSlug);

		// --------------------------------------------------------------------------

		//	Clear out any cache files
		if ($clearCachedir) {

			// Create a handler for the directory
			$pattern = '#^' . $bucketSlug . '-' . substr($objectFilename, 0, strrpos($objectFilename, '.')) . '#';
			$fh      = @opendir(DEPLOY_CACHE_DIR);

			if ($fh !== false ) {

				// Open directory and walk through the filenames
				while ($file = readdir($fh)) {

					// If file isn't this directory or its parent, add it to the results
					if ($file != '.' && $file != '..') {

						// Check with regex that the file format is what we're expecting and not something else
						if (preg_match($pattern, $file)) {

							// DESTROY!
							@unlink(DEPLOY_CACHE_DIR . $file);

						}
					}
				}
			}
		}
	}


{'fraction_non_alphanumeric': 0.3205447010065127, 'fraction_numerical': 0.00023682652457075192, 'mean_word_length': 1.5054879857609018, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3059843', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2374, 'n_words': 542}