审查视图

addons/qiniu/Qiniu.php 3.1 KB
wangzhi authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<?php

namespace addons\qiniu;

use addons\qiniu\library\Auth;
use fast\Http;
use think\Addons;

/**
 * 七牛上传插件
 */
class Qiniu extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        return true;
    }

    /**
     * 上传初始化时
     */
    public function uploadConfigInit(&$upload)
    {
        $config = $this->getConfig();

        $policy = array(
            'saveKey' => ltrim($config['savekey'], '/'),
        );
        //如果启用服务端回调
        if ($config['notifyenabled']) {
            $policy = array_merge($policy, [
                'callbackUrl'  => $config['notifyurl'],
                'callbackBody' => 'filename=$(fname)&key=$(key)&imageInfo=$(imageInfo)&filesize=$(fsize)&admin=$(x:admin)&user=$(x:user)'
            ]);
        }

        if ($config['uploadmode'] == 'client') {
            $auth = new Auth($config['app_key'], $config['secret_key']);
            $multipart['token'] = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
            $multipart['x:admin'] = (int)session('admin.id');
            $multipart['x:user'] = (int)cookie('uid');
            $upload = [
                'cdnurl'    => $config['cdnurl'],
                'uploadurl' => $config['uploadurl'],
                'bucket'    => $config['bucket'],
                'maxsize'   => $config['maxsize'],
                'mimetype'  => $config['mimetype'],
                'multipart' => $multipart,
                'multiple'  => $config['multiple'] ? true : false,
            ];
        } else {
            $upload = array_merge($upload, [
                'cdnurl'    => $config['cdnurl'],
                'uploadurl' => addon_url('qiniu/index/upload'),
                'maxsize'   => $config['maxsize'],
                'mimetype'  => $config['mimetype'],
                'multiple'  => $config['multiple'] ? true : false,
            ]);
        }
    }

    /**
     * 附件删除后
     */
    public function uploadDelete($attachment)
    {
        $config = $this->getConfig();
        if ($attachment['storage'] == 'qiniu' && isset($config['syncdelete']) && $config['syncdelete']) {
            $auth = new Auth($config['app_key'], $config['secret_key']);
            $entry = $config['bucket'] . ':' . ltrim($attachment->url, '/');
            $encodedEntryURI = $auth->base64_urlSafeEncode($entry);
            $url = 'http://rs.qiniu.com/delete/' . $encodedEntryURI;
            $headers = $auth->authorization($url);
            //删除云储存文件
            $ret = Http::sendRequest($url, [], 'POST', [CURLOPT_HTTPHEADER => ['Authorization: ' . $headers['Authorization']]]);
            //如果是服务端中转,还需要删除本地文件
            if ($config['uploadmode'] == 'server') {
                $filePath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url);
                if ($filePath) {
                    @unlink($filePath);
                }
            }
        }
        return true;
    }

}