<commit_before><?php

namespace UAM\Bundle\AwsBundle\Propel;

class S3ObjectBehaviorObjectBuilderModifier
{
	protected $behavior, $builder;

	public function __construct($behavior)
	{
		$this->behavior = $behavior;
	}

	protected function setBuilder($builder)
	{
		$this->builder = $builder;
		$this->builder->declareClasses(
			'Aws\\S3\\S3Client',
			'Aws\\S3\\Enum\\CannedAcl',
			'Aws\\S3\\Exception\\S3Exception'
		);
	}

	public function objectMethods($builder)
	{

		$this->setBuilder($builder);
		$script = '';

		$this->addGetPresignedUrlMethod($script);
		$this->addUploadMethod($script);
		$this->addSanitizeFilenameMEthod($script);

		return $script;
	}

	protected function addGetPresignedUrlMethod(&$script)
	{
		$script .= "
/**
 * Returns a pre-signed url to the document on AWS S3.
 *
 * @param Aws\S3\S3Client a S3Client instance
 * @param int|string $expires The Unix timestamp to expire at or a string that can be evaluated by strtotime
 *
 * @return string
 * @throws InvalidArgumentException if the request is not associated with this client object  
 */
public function getPresignedUrl(S3Client \$s3, \$expires = \"+5 minutes\")
{
	if (\$region = \$this->getRegion()) {
		\$s3->setRegion($region);
	}

	\$url = sprintf(
		'%s/%s?response-content-disposition=attachment; filename=\"%s\"',
		\$this->getBucket(),
		\$this->getKey(),
		\$this->getOriginalFilename()
	);
	\$request = \$s3->get(\$url);
	\$signed = \$s3->createPresignedUrl(\$request, \$expires);

	return \$signed;
}
";
	}

	protected function addUploadMethod(&$script)
	{
		$script .= "
/**
 * Uploads a file to S3.
 *
 * @return 
 * @throws S3Exception if the request fails
 */
public function upload(S3Client \$s3, \$file)
{
	if (!file_exists(\$file)) {
		return; 
	}

	\$response = \$s3->putObject(array(
		'Bucket' => \$this->getBucket(),
		'Key'    => \$this->getKey(),
		'Body'   => fopen(\$file, 'r'),
		'ACL'    => CannedAcl::PRIVATE_ACCESS
	));

	return \$response;
}
";
	}

	protected function addSanitizeFilenameMethod(&$script)
	{
		$script .= "
/**
 * Convenience method to sanitize a file for use as a key in AWS S3.
 */
 public function sanitize(\$filename) {
	\$s = trim(\$filename);
	\$s = preg_replace('/^[.]*/', '', \$s); // remove leading periods
	\$s = preg_replace('/[.]*\$/', '', \$s); // remove trailing periods
	\$s = preg_replace('/\.[.]+/', '.', \$s); // remove any consecutive periods<commit_msg>updated s3object behavior<commit_after><?php

namespace UAM\Bundle\AwsBundle\Propel;

class S3ObjectBehaviorObjectBuilderModifier
{
	protected $behavior, $builder;

	public function __construct($behavior)
	{
		$this->behavior = $behavior;
	}

	protected function setBuilder($builder)
	{
		$this->builder = $builder;
		$this->builder->declareClasses(
			'Aws\\S3\\S3Client',
			'Aws\\S3\\Enum\\CannedAcl',
			'Aws\\S3\\Exception\\S3Exception'
		);
	}

	public function objectMethods($builder)
	{

		$this->setBuilder($builder);
		$script = '';

		$this->addGetPresignedUrlMethod($script);
		$this->addUploadMethod($script);
		$this->addSanitizeFilenameMEthod($script);

		return $script;
	}

	protected function addGetPresignedUrlMethod(&$script)
	{
		$script .= "
/**
 * Returns a pre-signed url to the document on AWS S3.
 *
 * @param Aws\S3\S3Client a S3Client instance
 * @param int|string $expires The Unix timestamp to expire at or a string that can be evaluated by strtotime
 *
 * @return string
 * @throws InvalidArgumentException if the request is not associated with this client object  
 */
public function getPresignedUrl(S3Client \$s3, \$expires = \"+5 minutes\")
{
	if (\$region = \$this->getRegion()) {
		\$s3->setRegion($region);
	}

	\$url = sprintf(
		'%s/%s?response-content-disposition=attachment; filename=\"%s\"',
		\$this->getBucket(),
		\$this->getKey(),
		\$this->getOriginalFilename()
	);
	\$request = \$s3->get(\$url);
	\$signed = \$s3->createPresignedUrl(\$request, \$expires);

	return \$signed;
}
";
	}

	protected function addUploadMethod(&$script)
	{
		$script .= "
/**
 * Uploads a file to S3.
 *
 * @param Aws\S3\S3Client an Aws S3Client
 * @param string|stream|Guzzle\Http\EntityBody the path to the file to upload; accepts any valid argument for the \'Body\' parameter passed to the S3Client::putObject method.
 * @param string the AWS S3 bucket to upload this file to. If unset, this instance's \'bucket\' property will be used.
 *
 * @return Guzzle\Service\Resource\Model reponse from S3Client request via Guzzle
 * @throws S3Exception if the request fails
 */
public function upload(S3Client \$s3, \$file, \$bucket = null)
{
	if (!\$file) {
		return; 
	}

	if (\$region = \$this->getRegion()) {
		\$s3->setRegion(\$region);
	}

	\$response = \$s3->putObject(array(
		'Bucket' => \$bucket ? \$bucket : \$this->getBucket(),
		'Key'    => \$this->getKey(),
		'Body'   => \$file,
		'ACL'    => CannedAcl::PRIVATE_ACCESS
	));

	return \$response;
}
";
	}

	protected function addSanitizeFilenameMethod(&$script)
	{
		$script .= "
/**
 * Convenience method to sanitize a file for use as a key in AWS S3.
 */
 public function sanitize(\$filename) {
	\$s = trim(\$filename);
	\$s = preg_replace('/^[.]*/', '', \$s); // remove leading periods
	\$s = preg_replace('/[.]*\$/', '', \$s); // remove trailing periods
	\$s = preg_replace('/\.[.]+/', '.', \$s); // remove any consecutive periods
{'fraction_non_alphanumeric': 0.1796508353669983, 'fraction_numerical': 0.009761591890369814, 'mean_word_length': 3.77847533632287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '1453239', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1786, 'n_words': 436}