<commit_before><?php

namespace ondrs\UploadManager\Storages;

use Aws\Result;
use Aws\S3\S3Client;
use GuzzleHttp\Promise;
use ondrs\UploadManager\Utils;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class S3Storage implements IStorage
{

    /** @var string */
    private $basePath;

    /** @var string */
    private $relativePath;

    /** @var S3Client */
    private $s3Client;


    /**
     * @param string $basePath
     * @param string $relativePath
     * @param S3Client $s3Client
     */
    public function __construct($basePath, $relativePath, S3Client $s3Client)
    {
        $this->basePath = rtrim($basePath, '/');
        $this->relativePath = rtrim(ltrim($relativePath, '/'), '/');
        $this->s3Client = $s3Client;
    }


    /**
     * @return string
     */
    public function getBasePath()
    {
        return $this->basePath;
    }


    /**
     * @return string
     */
    public function getRelativePath()
    {
        return $this->relativePath;
    }


    /**
     * @internal
     * @param string $source
     * @param string $destination
     * @return Promise\Promise
     */
    public function s3Upload($source, $destination)
    {
        return $this->s3Client->putObjectAsync([
            'Bucket' => $this->basePath,
            'Key' => Utils::normalizePath($this->relativePath . '/' . $destination),
            'SourceFile' => $source,
            'ACL' => 'public-read',
            'StorageClass' => 'REDUCED_REDUNDANCY',
        ]);
    }


    /**
     * @param string $source
     * @param string $destination
     * @return string
     */
    public function save($source, $destination)
    {
        /** @var Result $result */
        $result = $this->s3Upload($source, $destination)->wait();

        return $result->toArray()['ObjectURL'];
    }


    /**
     * @param array $files of [$source, $destination]
     * @return array
     */
    public function bulkSave(array $files)
    {
        $promises = [];

        foreach ($files as $file) {
            $promises[] = $this->s3Upload($file[0], $file[1]);
        }

        $results = Promise\all($promises)->wait();

        return array_map(function (Result $result) {<commit_msg>S3Storage: run GC manually after upload is done<commit_after><?php

namespace ondrs\UploadManager\Storages;

use Aws\Result;
use Aws\S3\S3Client;
use GuzzleHttp\Promise;
use ondrs\UploadManager\Utils;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class S3Storage implements IStorage
{

    /** @var string */
    private $basePath;

    /** @var string */
    private $relativePath;

    /** @var S3Client */
    private $s3Client;


    /**
     * @param string $basePath
     * @param string $relativePath
     * @param S3Client $s3Client
     */
    public function __construct($basePath, $relativePath, S3Client $s3Client)
    {
        $this->basePath = rtrim($basePath, '/');
        $this->relativePath = rtrim(ltrim($relativePath, '/'), '/');
        $this->s3Client = $s3Client;
    }


    /**
     * @return string
     */
    public function getBasePath()
    {
        return $this->basePath;
    }


    /**
     * @return string
     */
    public function getRelativePath()
    {
        return $this->relativePath;
    }


    /**
     * @internal
     * @param string $source
     * @param string $destination
     * @return Promise\Promise
     */
    public function s3Upload($source, $destination)
    {
        return $this->s3Client->putObjectAsync([
            'Bucket' => $this->basePath,
            'Key' => Utils::normalizePath($this->relativePath . '/' . $destination),
            'SourceFile' => $source,
            'ACL' => 'public-read',
            'StorageClass' => 'REDUCED_REDUNDANCY',
        ]);
    }


    /**
     * @param string $source
     * @param string $destination
     * @return string
     */
    public function save($source, $destination)
    {
        /** @var Result $result */
        $result = $this->s3Upload($source, $destination)->wait();

        gc_collect_cycles();

        return $result->toArray()['ObjectURL'];
    }


    /**
     * @param array $files of [$source, $destination]
     * @return array
     */
    public function bulkSave(array $files)
    {
        $promises = [];

        foreach ($files as $file) {
            $promises[] = $this->s3Upload($file[0], $file[1]);
        }

        $results = Promise\all($promises)->wait();

        gc_collect_cycles();

        return array_map(function (Result $result) {
{'fraction_non_alphanumeric': 0.14660493827160495, 'fraction_numerical': 0.007716049382716049, 'mean_word_length': 2.2360912981455066, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '2550792', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1356, 'n_words': 285}