<?php

namespace Extend\Alibaba;

use App\Exception\BusinessException;
use DateTime;
use DateTimeInterface;
use OSS\Core\OssException;
use OSS\OssClient;

/**
 * 阿里云oss
 */
class Oss
{
    protected array $config;// 系统配置

    public function __construct()
    {
        $this->config = config("alibaba.oss");
    }

    /**
     * 创建客户端
     * @return OssClient
     */
    public function createClient(): OssClient
    {
        try {
            return new OssClient($this->config['accessKey'], $this->config['accessKeySecret'], $this->config['endpoint']);
        } catch (OssException $e) {
            throw new BusinessException($e->getMessage());
        }
    }

    /**
     * 获取签名
     * @param string $dir 用户上传文件时指定的前缀。
     * @return array
     */
    public function signature(string $dir): array
    {
        try {
            $now = time();
            $expire = 30;  //设置该policy超时时间是10s. 即这个policy过了这个有效时间，将不能访问。
            $end = $now + $expire;
            $expiration = $this->gmt_iso8601($end);

            $start = array(0 => 'starts-with', 1 => '$key', 2 => $dir);
            $conditions[] = $start;

            $arr = array('expiration' => $expiration, 'conditions' => $conditions);
            $policy = json_encode($arr);
            $base64_policy = base64_encode($policy);
            $string_to_sign = $base64_policy;
            $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->config['accessKeySecret'], true));

            $response = array();
            $response['accessid'] = $this->config['accessKey'];
//            $response['host'] = "https://" . $this->config['bucket'] . '.' . $this->config['endpoint'];
            $response['host'] = config('alibaba.oss.custom_domain_name');
            $response['policy'] = $base64_policy;
            $response['signature'] = $signature;
            $response['expire'] = $end;
            $response['callback'] = "";
            $response['dir'] = $dir;  // 这个参数是设置用户上传文件时指定的前缀。
            return $response;

        } catch (\Exception $e) {
            throw new BusinessException($e->getMessage());
        }
    }

    /**
     * @param $time
     * @return string
     * @throws \Exception
     */
    protected function gmt_iso8601($time): string
    {
        return str_replace('+00:00', '.000Z', gmdate('c', $time));
    }

    /**
     * 上传
     * @param string $filename 图片名称
     * @param mixed $content 内容 字符串或文件流
     * @return string
     */
    public function putObject(string $filename, mixed $content): string
    {
        try {
            $ossClient = $this->createClient();

            $ossClient->putObject($this->config['bucket'], $filename, $content);

            return $filename;
        }catch(\Exception $e) {
            throw new BusinessException($e->getMessage());
        }
    }


    /**
     * 下载自定义风格文件到内存
     * @param string $filename
     * @param string $style
     * @return string
     */
    public function getCusTomObjectToRAM(string $filename,string $style = "image/resize"): string
    {
        try {
//            $download_file = "./runtime/aaa.jpg";


            $ossClient = $this->createClient();
            $options = array(
                OssClient::OSS_PROCESS => $style,
//                OssClient::OSS_FILE_DOWNLOAD => $download_file,
            );

            $object = $filename;
            return $ossClient->getObject($this->config['bucket'], $object,$options);
        }catch(\Exception $e) {
            throw new BusinessException($e->getMessage());
        }
    }

    /**
     * 下载文件到内存
     * @param string $filename
     * @param string $style
     * @return string
     */
    public function getObjectToRAM(string $filename): string
    {
        try {
            $ossClient = $this->createClient();
            return $ossClient->getObject($this->config['bucket'], $filename);
        }catch(\Exception $e) {
            throw new BusinessException($e->getMessage());
        }
    }

}