<commit_before><?php

require_once 'StorageBackend.php';

class Amazon_S3 extends StorageBackend
{
    private $zendServiceAmazonS3;
    
    public function Amazon_S3($securityCredentials)
    {
        $this->setBucket($securityCredentials['bucket']);
        $this->setAccessKey($securityCredentials['api_key']);
        $this->setSecretKey($securityCredentials['api_key_secret']);
        
        $this->zendServiceAmazonS3 = new Zend_Service_Amazon_S3(
            $this->getAccessKey(),
            $this->getSecretKey());
    }
    
    public function getAbsoluteFilePath($resourceId)
    {
        $endpoint = $this->zendServiceAmazonS3->getEndpoint();
        $scheme = $endpoint->getScheme();
        $host = $endpoint->getHost();
        $bucket = $this->getBucket();
        return "$scheme://$bucket.$host/".utf8_encode($resourceId);
    }

    public function getSignedURL($resourceId)
    {
        //URL will be active for 30 minutes
        $expires = time()+1800;
        
        $bucket = $this->getBucket();
        $secretKey = $this->getSecretKey();
        $accessKey = $this->getAccessKey();
        
        $string_to_sign = utf8_encode("GET\n\n\n$expires\n/$bucket/$resourceId");
        // We need to urlencode the entire signature in case the hashed signature
        // has spaces. (NOTE: utf8_encode() does not work here because it turns
        // spaces into non-breaking spaces)
        $signature = urlencode(base64_encode((hash_hmac("sha1", $string_to_sign, $secretKey, true))));
        
        $resourceURL = $this->getAbsoluteFilePath($resourceId);
        return $resourceURL."?AWSAccessKeyId=$accessKey&Expires=$expires&Signature=$signature";
    }
    
    public function getFileSize($resourceId)
    {
        $bucket = $this->getBucket();
        
        $amz_resource = utf8_encode("$bucket/$resourceId");
        $amz_resource_info = $this->zendServiceAmazonS3->getInfo($amz_resource);
        return $amz_resource_info["size"];
    }
    
    public function deletePhysicalFile($resourceId)
    {
        $bucket = $this->getBucket();
        $amz_resource = utf8_encode("$bucket/$resourceId");
        
        if ($this->zendServiceAmazonS3->isObjectAvailable($amz_resource)) {
           // removeObject() returns true even if the object was not deleted (bug?)
           // so that is not a good way to do error handling. isObjectAvailable()
           // does however return the correct value; We have to assume that if the
           // object is available the removeObject() function will work.
           $this->zendServiceAmazonS3->removeObject($amz_resource);
           } else {
               throw new Exception("ERROR: Could not locate object on Amazon S3");
           }
    }
}<commit_msg>SAAS-510: PYPO -> Files are downloaded even if they already exist in tmp cache dir<commit_after><?php

require_once 'StorageBackend.php';

class Amazon_S3 extends StorageBackend
{
    private $zendServiceAmazonS3;
    
    public function Amazon_S3($securityCredentials)
    {
        $this->setBucket($securityCredentials['bucket']);
        $this->setAccessKey($securityCredentials['api_key']);
        $this->setSecretKey($securityCredentials['api_key_secret']);
        
        $this->zendServiceAmazonS3 = new Zend_Service_Amazon_S3(
            $this->getAccessKey(),
            $this->getSecretKey());
    }
    
    public function getAbsoluteFilePath($resourceId)
    {
        $endpoint = $this->zendServiceAmazonS3->getEndpoint();
        $scheme = $endpoint->getScheme();
        $host = $endpoint->getHost();
        $bucket = $this->getBucket();
        return "$scheme://$bucket.$host/".utf8_encode($resourceId);
    }

    public function getSignedURL($resourceId)
    {
        //URL will be active for 30 minutes
        $expires = time()+1800;
        
        $bucket = $this->getBucket();
        $secretKey = $this->getSecretKey();
        $accessKey = $this->getAccessKey();
        
        $string_to_sign = utf8_encode("GET\n\n\n$expires\n/$bucket/$resourceId");
        // We need to urlencode the entire signature in case the hashed signature
        // has spaces. (NOTE: utf8_encode() does not work here because it turns
        // spaces into non-breaking spaces)
        $signature = urlencode(base64_encode((hash_hmac("sha1", $string_to_sign, $secretKey, true))));
        
        $resourceURL = $this->getAbsoluteFilePath($resourceId);
        return $resourceURL."?AWSAccessKeyId=$accessKey&Expires=$expires&Signature=$signature";
    }
    
    public function getFileSize($resourceId)
    {
        $bucket = $this->getBucket();
        
        $amz_resource = utf8_encode("$bucket/$resourceId");
        $amz_resource_info = $this->zendServiceAmazonS3->getInfo($amz_resource);
        return (int)$amz_resource_info["size"];
    }
    
    public function deletePhysicalFile($resourceId)
    {
        $bucket = $this->getBucket();
        $amz_resource = utf8_encode("$bucket/$resourceId");
        
        if ($this->zendServiceAmazonS3->isObjectAvailable($amz_resource)) {
           // removeObject() returns true even if the object was not deleted (bug?)
           // so that is not a good way to do error handling. isObjectAvailable()
           // does however return the correct value; We have to assume that if the
           // object is available the removeObject() function will work.
           $this->zendServiceAmazonS3->removeObject($amz_resource);
           } else {
               throw new Exception("ERROR: Could not locate object on Amazon S3");
           }
    }
}
{'fraction_non_alphanumeric': 0.12343470483005367, 'fraction_numerical': 0.009123434704830053, 'mean_word_length': 2.7198935462408516, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3949315', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1561, 'n_words': 343}