<commit_before><?php

namespace App\Services;

use Aws;
use Exception;
use App\Traits\FileOperation;

/**
 * Class S3Service
 * This service allows to send and receive a file on Amazon S3
 */
class S3Service
{
    /**
     * File management tools
     */
    use FileOperation;

    /**
     * The Bucket
     *
     * @var string
     */
    public $bucket;

    /**
     * The file
     *
     * @var string
     */
    public $file;

    /**
     * Initializes connection to Amazon S3
     *
     * @var integer
     */
    public $sdk;

    /**
     * Initializes s3Client to Amazon S3
     *
     * @var integer
     */
    public $s3Client;


    /**
     * S3Service constructor
     * @param $region
     * @param $version
     * @param $access_key
     * @param $secret_key
     * @throws Exception
     */
    public function __construct($region, $version, $access_key, $secret_key)
    {
        /**
         * |-------------------------------------------
         * | These fields are required
         * |-------------------------------------------
         */
        if (empty($region)) {
            throw new Exception('S3Service: empty region');
        }
        if (empty($version)) {
            throw new Exception('S3Service: empty version');
        }
        if (empty($access_key)) {
            throw new Exception('S3Service: empty access_key');
        }
        if (empty($secret_key)) {
            throw new Exception('S3Service: empty secret_key');
        }

        /**
         * |-------------------------------------------
         * | Amazon instance
         * |-------------------------------------------
         */
        try {
            $this->sdk = new Aws\Sdk(
                [
                    'region'      => $region,
                    'version'     => $version,
                    'credentials' => [
                        'key'    => $access_key,
                        'secret' => $secret_key,
                    ],
                ]
            );
        } catch (\Exception $e) {
            Throw new Exception($e->getMessage());
        }

        /**
         * |-------------------------------------------
         * | S3 instance
         * |-------------------------------------------
         */
        try {
            $this->s3Client = $this->sdk->createS3();
        } catch (\Exception $e) {
            Throw new Exception($e->getMessage());
        }
    }

    /**
     * Adds an object to a bucket
     * @return bool
     * @throws Exception
     */
    public function put()
    {
        /**
         * |-------------------------------------------
         * | These fields are required
         * |-------------------------------------------
         */
        if (empty($this->bucket)) {
            throw new Exception();
        }
        if (empty($this->file)) {
            throw new Exception();
        }

        /**
         * |-------------------------------------------
         * | The file must exist
         * |-------------------------------------------
         */
        try {
            $this->isFile($this->file);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

        /**
         * |-------------------------------------------
         * | Recover mime type file
         * |-------------------------------------------
         */
        try {
            $contentType = $this->mime($this->file);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

        /**
         * |-------------------------------------------
         * | If the key is not set,
         * | the key will be the name of the file
         * |-------------------------------------------
         */
        if (empty($this->key)) {
            $this->key = $this->file;
        }

        /**
         * |-------------------------------------------
         * | Sending the file to Amazon S3
         * |-------------------------------------------
         */
        try {
            $this->s3Client->putObject(
                [
                    'Bucket'      => $this->bucket,
                    'Key'         => $this->key,
                    'SourceFile'  => $this->file,
                    'ContentType' => $contentType,
                    'ACL'         => AWS_ACL,
                ]
            );
        } catch (\Exception $e) {<commit_msg>feat(integrity): checking the integrity of files sent on S3<commit_after><?php

namespace App\Services;

use Aws;
use Exception;
use App\Traits\FileOperation;

/**
 * Class S3Service
 * This service allows to send and receive a file on Amazon S3
 */
class S3Service
{
    /**
     * File management tools
     */
    use FileOperation;

    /**
     * The Bucket
     *
     * @var string
     */
    public $bucket;

    /**
     * The file
     *
     * @var string
     */
    public $file;

    /**
     * Initializes connection to Amazon S3
     *
     * @var integer
     */
    public $sdk;

    /**
     * Initializes s3Client to Amazon S3
     *
     * @var integer
     */
    public $s3Client;


    /**
     * S3Service constructor
     * @param $region
     * @param $version
     * @param $access_key
     * @param $secret_key
     * @throws Exception
     */
    public function __construct($region, $version, $access_key, $secret_key)
    {
        /**
         * |-------------------------------------------
         * | These fields are required
         * |-------------------------------------------
         */
        if (empty($region)) {
            throw new Exception('S3Service: empty region');
        }
        if (empty($version)) {
            throw new Exception('S3Service: empty version');
        }
        if (empty($access_key)) {
            throw new Exception('S3Service: empty access_key');
        }
        if (empty($secret_key)) {
            throw new Exception('S3Service: empty secret_key');
        }

        /**
         * |-------------------------------------------
         * | Amazon instance
         * |-------------------------------------------
         */
        try {
            $this->sdk = new Aws\Sdk(
                [
                    'region'      => $region,
                    'version'     => $version,
                    'credentials' => [
                        'key'    => $access_key,
                        'secret' => $secret_key,
                    ],
                ]
            );
        } catch (\Exception $e) {
            Throw new Exception($e->getMessage());
        }

        /**
         * |-------------------------------------------
         * | S3 instance
         * |-------------------------------------------
         */
        try {
            $this->s3Client = $this->sdk->createS3();
        } catch (\Exception $e) {
            Throw new Exception($e->getMessage());
        }
    }

    /**
     * Adds an object to a bucket
     * @return bool
     * @throws Exception
     */
    public function put()
    {
        /**
         * |-------------------------------------------
         * | These fields are required
         * |-------------------------------------------
         */
        if (empty($this->bucket)) {
            throw new Exception();
        }
        if (empty($this->file)) {
            throw new Exception();
        }

        /**
         * |-------------------------------------------
         * | The file must exist
         * |-------------------------------------------
         */
        try {
            $this->isFile($this->file);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

        /**
         * |-------------------------------------------
         * | Recover mime type file
         * |-------------------------------------------
         */
        try {
            $contentType = $this->mime($this->file);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

        /**
         * |-------------------------------------------
         * | If the key is not set,
         * | the key will be the name of the file
         * |-------------------------------------------
         */
        if (empty($this->key)) {
            $this->key = $this->file;
        }

        /**
         * |-------------------------------------------
         * | Sending the file to Amazon S3
         * |-------------------------------------------
         */
        try {
            $this->s3Client->putObject(
                [
                    'Bucket'      => $this->bucket,
                    'Key'         => $this->key,
                    'SourceFile'  => $this->file,
                    'ContentType' => $contentType,
                    'ACL'         => AWS_ACL,
                    "Metadata"      => [
                        "Content-MD5" => base64_encode($this->file)
                    ],
                    "ContentSHA256" => hash_file("sha256", $this->file)
                ]
            );
        } catch (\Exception $e) {
{'fraction_non_alphanumeric': 0.26117440841367223, 'fraction_numerical': 0.004820333041191937, 'mean_word_length': 0.7802262090483619, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, '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': '3285681', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2144, 'n_words': 482}