<?php
/****************************
 * File : awsfileserver.php
 * Author : Cygnusa
 * @2018
 ****************************/

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

//AMAZON S3
require dirname(__FILE__) . '/_api/awsapi/aws-autoloader.php';
use Aws\Exception\AwsException;
use Aws\S3\S3Client;

//CLASS DEFINE
class awsfileserver
{
    //FUNCTION LISTS

    public function __construct()
    {
        //return connectS3();
    }

    /* ////////////////////////////////////// S3 CONNECT ///////////////////////////////// */
    public static function connectS3()
    {
        //S3 CONNECTION
        $s3 = S3Client::factory(array(
            'version' => 'latest',
            'region' => S3_REGION,
            'credentials' => array(
                'key' => S3_ACCESSKEY,
                'secret' => S3_SECRET_ACCESS,
            ),
        ));
        return $s3;
    }
    /* ////////////////////////////////////// S3 URL ///////////////////////////////// */
    public static function getS3Url($s3, $bucket, $obj)
    {
        $presignedUrl = '';
        if (is_object($obj)) {
            $cmd = $s3->getCommand('GetObject', [
                'Bucket' => $bucket,
                'Key' => $obj->key_name,
            ]);
            $request = $s3->createPresignedRequest($cmd, '+7200 minutes');
            $presignedUrl = (string) $request->getUri();
        }
        return $presignedUrl;
    }

    /* /////////////////////////////////   EXIST  ///////////////////////////////////// */

    public static function checkexist($s3, $bucket, $obj)
    {
        $info = $s3->doesObjectExist($bucket, $obj->key_name);
        return $info;
    }
    /* /////////////////////////////////  UPLOAD   /////////////////////////////////// */

    public static function checkupload($s3, $bucket, $upload_key, $s3upload_key)
    {

        try {

            $params = array(
                'Bucket' => $bucket,
                'Key' => $upload_key,
                'SourceFile' => $s3upload_key,
            );

            // Using commands explicitly.
            $command = $s3->getCommand('PutObject', $params);
            $result = $s3->execute($command);

        } catch (AwsException $e) {
            self::errormsg('S3 SERVER ERROR' . $e);
        }
    }

    /* ////////////////////////////////////////////////////////////////////////////// */

    public static function deleteobject($s3, $bucket, $s3upload_key)
    {

        //$keyfile_name="uploads/noimage.jpg";
        try {

            $result = $s3->deleteObject(array(
                'Bucket' => $bucket,
                'Key' => $s3upload_key,
            ));

            // print_r($result);
            return true;

        } catch (AwsException $e) {
            self::errormsg('S3 SERVER ERROR' . $e);
        }
    }

    public static function errormsg($msg)
    {
        $obj = new stdClass();
        $obj->mess = $msg;
        $obj->cont = false;
        echo json_encode($obj);exit;
    }
}
