<?php
/**
 * JYmusic音乐管理系统
 *
 * @version     2.0
 * @author      战神~~巴蒂 <jyuucn@163.com>
 * @license     http://jyuu.cn [未经授权严禁私自出售，否者承担法律责任]
 * @copyright   2014 - 2017 JYmusic
 */

namespace addons\Upload\controller;

class AliossController extends BaseController
{
    
    protected  $uploadConf;
    
    public function _initialize()
    {
        $this->uploadConf = get_addon_config('Upload');
    }

    //用户上传需要验证token
    public function sign()
    {
        $id     = $this->uploadConf['alioss_id'];
        $key    = $this->uploadConf['alioss_key'];
        $host   = $this->uploadConf['alioss_host'];
        
        if (empty($id) || empty($key) || empty($host)) {
            return json([
                'code' => 40030,
                'error' => '阿里云OSS配置不正确，缺失参数'
            ]);
        }
        
        $maxSize  = $this->uploadConf['alioss_file_max_size'];
        $maxSize  = $maxSize? $maxSize * 1024*1024 : 1048576000;
    
        $filename = $this->request->param('filename');
        $uploadType = $this->request->param('upload_type');
        
        if ($uploadType == 'listen') {
            $uploadDir = $this->uploadConf['alioss_listen_dir'];
            $uploadDir = !empty($uploadDir)? $uploadDir : 'listen';
        } elseif ($uploadType == 'down') {
            $uploadDir = $this->uploadConf['alioss_down_dir'];
            $uploadDir = !empty($uploadDir)? $uploadDir : 'down';
        } else {
            $uploadDir = $this->uploadConf['alioss_image_dir'];
            $uploadDir = !empty($uploadDir)? $uploadDir : 'images';
        }
        $uploadDir = rtrim($uploadDir, '/') . '/';
        
        
        $now = time();
        $expire = 120; //设置该policy超时时间是10s. 即这个policy过了这个有效时间，将不能访问
        $end = $now + $expire;
        $expiration = $this->gmt_iso8601($end);
    
        $baseFile =  $uploadDir . $filename;
        //最大文件大小.用户可以自己设置
        $conditions[] = ['content-length-range', 0, $maxSize];
        //表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
        $conditions[] = [0=>'starts-with', 1=>'$key', 2=> $baseFile];
        
        $policy = ['expiration'=>$expiration,'conditions'=>$conditions];
        $base64Policy = base64_encode(json_encode($policy));
        $signature = base64_encode(hash_hmac('sha1', $base64Policy, $key, true));
    
        $result = [
            'accessid' => $id,
            'host' => $host,
            'policy' => $base64Policy,
            'signature' => $signature,
            'expire' => $end,
            'key' => $baseFile,
            'filepath' => ltrim($host, '/') .'/'. $baseFile
        ];
        
        $response = [
            'code' => 0,
            'result' => $result,
        ];
        
        return json($response);
    }
    
    protected function gmt_iso8601($time) {
        $dtStr = date("c", $time);
        $mydatetime = new \DateTime($dtStr);
        $expiration = $mydatetime->format(\DateTime::ISO8601);
        $pos = strpos($expiration, '+');
        $expiration = substr($expiration, 0, $pos);
        return $expiration."Z";
    }
}
