<?php
/****************************
 * File : downloadautoreport.php
 * Author : Cygnusa
 * @2019
****************************/

//SECURITY 
if ( ! defined('APPLICATION_PATH')) exit('No direct script access allowed');

//CLASS DEFINE
class downloadautoreport
{
	//FUNCTION LISTS
	function __construct($parent)
	{	
       
		$this->funcDownload($parent);		  
	}
	
	//FUNCTIONS
	private function funcDownload($parent)
	{
        
        globalfunc::checkMethodType($parent,array('GET'));		 
		globalfunc::checkParamCount($parent,1);         
        $decode_url= base64_decode(escape_str($parent->params[0])); 
		
        //CONNECT S3
    	$s3=awsfileserver::connectS3();         
        try{
            
            
            //$s3_sign_url=awsfileserver::getS3Url($s3,$obj);  
			$s3_sign_url=awsfileserver::getS3FileUrl($s3,S3_BUCKET,$decode_url);
            
            $obj=new stdClass();
            $obj->key_name=$decode_url;
            $info=awsfileserver::checkexist($s3,S3_BUCKET,$obj);
            
            if($info)
            {           
                $arr=@explode('?',$s3_sign_url);
                $filerr=@explode('/',$arr[0]);
                $download_file=end($filerr);

                header('Content-Type: application/octet-stream');
                header("Access-Control-Allow-Origin: *");
                header('Content-Disposition: filename='.$download_file);        
                @readfile($s3_sign_url);     
                
            }else
            {
                echo 'FILE NOT FOUND';exit;
            }
        }catch(Exception $e)
        {
            echo 'FILE NOT FOUND';exit;
        }
	}
	
}

?>