| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | <?phpnamespace addons\cos;use addons\cos\library\Auth;use app\common\library\Menu;use fast\Http;use Qcloud\Cos\Client;use think\Addons;use think\Loader;/** * COS插件 */class Cos extends Addons{    /**     * 插件安装方法     * @return bool     */    public function install()    {        return true;    }    /**     * 插件卸载方法     * @return bool     */    public function uninstall()    {        return true;    }    /**     * 插件启用方法     * @return bool     */    public function enable()    {        return true;    }    /**     * 插件禁用方法     * @return bool     */    public function disable()    {        return true;    }    /**     * 渲染命名空间配置信息     */    public function appInit()    {        if (!class_exists('\Qcloud\Cos\Client')) {            Loader::addNamespace('Qcloud\Cos', $this->addons_path . str_replace('/', DS, 'library/Qcloud/Cos/'));        }        if (!class_exists('\GuzzleHttp\Command\Command')) {            Loader::addNamespace('GuzzleHttp\Command', $this->addons_path . str_replace('/', DS, 'library/Guzzle/command/src/'));        }        if (!class_exists('\GuzzleHttp\Command\Guzzle\Description')) {            Loader::addNamespace('GuzzleHttp\Command\Guzzle', $this->addons_path . str_replace('/', DS, 'library/Guzzle/guzzle-services/src/'));        }    }    /**     *     */    public function uploadConfigInit(&$upload)    {        $config = $this->getConfig();        $data = ['deadline' => time() + $config['expire']];        $signature = hash_hmac('sha1', json_encode($data), $config['secretKey'], true);        $multipart = [            'costoken' => $config['appId'] . ':' . base64_encode($signature) . ':' . base64_encode(json_encode($data))        ];        $upload = array_merge($upload, [            'cdnurl'     => $config['cdnurl'],            'uploadurl'  => $config['uploadmode'] == 'client' ? $config['uploadurl'] : addon_url('cos/index/upload', [], false, true),            'uploadmode' => $config['uploadmode'],            'bucket'     => $config['bucket'],            'maxsize'    => $config['maxsize'],            'mimetype'   => $config['mimetype'],            'savekey'    => $config['savekey'],            'chunking'   => (bool)($config['chunking'] ?? $upload['chunking']),            'chunksize'  => (int)($config['chunksize'] ?? $upload['chunksize']),            'multipart'  => $multipart,            'storage'    => $this->getName(),            'multiple'   => $config['multiple'] ? true : false,        ]);    }    /**     * 附件删除后     */    public function uploadDelete($attachment)    {        $config = $this->getConfig();        if ($attachment['storage'] == 'cos' && isset($config['syncdelete']) && $config['syncdelete']) {            $cosConfig = array(                'region'      => $config['region'],                'schema'      => 'https', //协议头部,默认为http                'credentials' => array(                    'secretId'  => $config['secretId'],                    'secretKey' => $config['secretKey']                )            );            $oss = new Client($cosConfig);            $ret = $oss->deleteObject(array('Bucket' => $config['bucket'], 'Key' => ltrim($attachment->url, '/')));        }        return true;    }}
 |