<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'libraries/vendor/autoload.php';

//define('ACCESS_KEY', "dfs;dfsldfsdfls;,dfs");
//define('SECRET_KEY', "w8bZCeW3ab/jjn6cpIC5QEYasdasdasdamvoMohTmIUUfZ3bU7");
use Aws\Credentials\Credentials;
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

class Upload {
    
    private $CI;
    static private $CIs;
    
    function __construct() 
    {
        //$this->CI =& get_instance();
        //self::$CIs =& get_instance();
        //self::$CIs->load->model('newsletter/newsletter_model');
    }
    
    public static function xxxini($planilha, $modelo)
    {
        self::$planilha = $planilha;
        self::$modelo   = $modelo;
        return new static();
    }
    
    public static function putObject($putImg)
    {
        //$credentials = new Credentials(getenv('SPACES_KEY'), getenv('SPACES_SECRET'));
        $s3 = new S3Client([
            'version' => 'latest',
            'region' => 'us-west-1',
            'endpoint' => getenv('SPACES_ENDPOINT'),
            'credentials' => [
                'key' => getenv('SPACES_KEY'),
                'secret' => getenv('SPACES_SECRET'),
                ]
            ]);
        $resImg = $s3->putObject($putImg);
        
        $urlReturn = $resImg['ObjectURL'];
        $ENDPOINT  = str_replace('https://', '', getenv('SPACES_ENDPOINT'));
        $MEDIA_URL = str_replace('https://', '', getenv('MEDIA_URL'));
        $BUCKET    = getenv('SPACES_BUCKET');
        
        $urlMedia = str_replace($BUCKET.'.'.$ENDPOINT, $MEDIA_URL, $urlReturn);
        
        return $urlMedia;
    }
    
    
    public static function putObjectResize($tmp_name, $name, $width, $upload_path)
    {
        $key = $upload_path.'/'.$name;
        $sourceFile = $tmp_name;
        
        $path_parts = pathinfo($name);
        $extension = $path_parts['extension'];
        
        if(in_array($extension,['gif','jpeg','jpg','png']) && $width != null)
        {
            // versão atualizada do resize das imagens com PNG transparente!
    		$sourceFile = FCPATH.'assets/uploads/'.rand().'-'.$name;
    		
            $count = strlen($extension)+1;
    		
    		$CI = &get_instance();
            $CI->load->library('image_lib');
    		
    		$config['image_library'] = 'gd2';
            $config['source_image'] = $tmp_name;
            $config['new_image'] = $sourceFile;
            
            $config['create_thumb']  = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width']          = $width;
            
            $CI->image_lib->clear();
            $CI->image_lib->initialize($config);
            
            if (!$CI->image_lib->resize())
            {
                echo $CI->image_lib->display_errors();
            }
            
            $sourceFile = substr($sourceFile,0,-$count).'_thumb.'.$extension;
            
            //$contentType = 'image/jpeg'; 
        }
        
        $contentType = $extension == 'pdf'?'application/pdf':'image/jpeg'; 
        
        return self::putObject([
            'ACL' => 'public-read',
            'Bucket' => getenv('SPACES_BUCKET'),
            'Key'    => $key,
            'SourceFile' => $sourceFile,
            'ContentDisposition' => 'inline; filename='.$name,
            'ContentType' => $contentType
            ]);
        
         
    }
    
    
    
}