<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/AwsManagerHelper.php';


use Aws\S3\S3Client;
use Aws\Exception\AwsException;

// ini_set('display_errors', 1);
// error_reporting(E_ALL);


class S3Helper {

    private $s3;
    private $bucket;
    private $awsHelper;

     public function __construct() {
        $this->bucket = IS_PROD
            ? AWS_BUCKET_LIVE
            : AWS_BUCKET_DEV;  
        $this->awsHelper =   new  AwsManagerHelper();     
            
        $this->s3 = new S3Client([
            'version' => 'latest',
            'region'  => AWS_REGION,
            'use_path_style_endpoint' => false,
            'credentials' => [
                'key'    => $this->awsHelper->getValue('AWS_S3_ACCESS_KEY'),
                'secret'    => $this->awsHelper->getValue('AWS_S3_SECRET'),
                
            ],
            'suppress_php_deprecation_warning' => true,
        ]);
    }

public function upload($localPath, $folder, $fileName)
{
    try {

        if (!file_exists($localPath)) {
            echo "LOCAL FILE NOT FOUND: $localPath";
            exit;
        }
        $mime = mime_content_type($localPath);

       $result = $this->s3->putObject([
            'Bucket'     => $this->bucket,
            'Key'        => rtrim($folder, '/') . '/' . $fileName,
            'SourceFile' => $localPath,
            'ContentType' => $mime
        ]);
         return $result['ObjectURL'];
        // echo "UPLOAD SUCCESS<br>";
        // var_dump($result);
        // exit;

    } catch (\Aws\Exception\AwsException $e) {

        echo "<pre>";
        echo "AWS ERROR MESSAGE:\n";
        echo $e->getAwsErrorMessage() . "\n\n";

        echo "AWS ERROR CODE:\n";
        echo $e->getAwsErrorCode() . "\n\n";

        echo "HTTP STATUS:\n";
        echo $e->getStatusCode() . "\n\n";

        echo "FULL EXCEPTION:\n";
        print_r($e->toArray());
        echo "</pre>";
        exit;

    } catch (\Exception $e) {

        echo "GENERAL ERROR: " . $e->getMessage();
        exit;
    }
}
 public function uploadImageToS3($file, $img, $basePath, $sizes, $quality = 50)
    {
        if (empty($file['tmp_name'])) {
            return '';
        }


        $Image = new SimpleImage();
        $tmp   = $file['tmp_name'];
        foreach ($sizes as $folder => $dim) {

            $tempFile = sys_get_temp_dir() . '/' . $folder . '_' . $img;
            copy($tmp, $tempFile);

            if (is_array($dim)) {
                $Image->load($tempFile);
                $Image->resize($dim[0], $dim[1]);
                $Image->save($tempFile);
            }

            CompressImage($tempFile, $tempFile, $quality);
              $basePath = rtrim($basePath, '/') . '/';

            if ($folder === 'original') {
                $uploadPath = $basePath;
            } else {
                $uploadPath = $basePath . $folder . '/';
            }

            $this->upload($tempFile, $uploadPath, $img);

            unlink($tempFile);
        }

        return $img;
    }
}

