珂珂的个人博客 - 一个程序猿的个人网站

diccuz与百度云存储BCS集成

 在discuz后台搜索云存储,竟然只有2个,倒是有个百度云存储,还是收费的。好吧,自己来改造下吧。

我们先把百度云存储php的sdk放到discuz的根目录,在conf.inc.php文件下配置好自己的AK,SK,Bucket。

研究代码和调试发现要经过source/class/discuz/discuz_upload.php文件,我在这个文件上加了个方法


function save_to_bcs($source, $target) {
        $succeed = false;
        
        try {
            require_once( dirname(dirname(dirname(dirname(__FILE__)))). '/bcs/bcs.class.php' );
            //$root=str_ireplace(str_replace("/","\\",$_SERVER['PHP_SELF']),'',__FILE__)."\\";
            $tar=substr($target, strpos($target, "/data/attachment"));
            $tar=str_replace("/./", "/", $tar);
            $host = 'bcs.duapp.com'; //online
            $ak = '';
            $sk = '';
            $bucket = BCS_BUCKET;
            $object = $tar;
            $fileUpload = $source;
        
            $baidu_bcs = new BaiduBCS ( $ak, $sk, $host );
        
        
            $opt = array ();
            $opt ['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ;
            $opt [BaiduBCS::IMPORT_BCS_LOG_METHOD] = "bs_log";
            $opt ['curlopts'] = array (
                    CURLOPT_CONNECTTIMEOUT => 10,
                    CURLOPT_TIMEOUT => 1800 );
            $response = $baidu_bcs->create_object ( $bucket, $object, $fileUpload, $opt );
            //printResponse ( $response );
            $succeed = $response->isOK () ;
        }
        catch (Exception $e)
        {
                    
        }
        
        if($succeed)  {
            $this->errorcode = 0;
            @chmod($target, 0644); @unlink($source);
        } else {
            $this->errorcode = 0;
        }
        
        return $succeed;
    }

然后把这个文件的save_to_local替换为save_to_bcs,上传是上传成功了,可是上传界面上传完后还是显示不了图片。

然后再看这个图片的url,最后找到source/module/forum/forum_image.php 这个文件,加上上面的save_to_bcs方法,还可以判定文件存不存在,因为编辑帖子的时候上传界面之前的图片不需要再上传了,加上这个方法

function is_object_exist($object) {
    require_once( dirname(dirname(dirname(dirname(__FILE__)))). '/bcs/bcs.class.php' );
    $ak = '';
    $sk = '';
    $host = 'bcs.duapp.com'; //online
    $bucket = BCS_BUCKET;
    $baidu_bcs = new BaiduBCS ( $ak, $sk, $host );
    $bolRes = $baidu_bcs->is_object_exist ( $bucket, $object );
        
    return $bolRes;
}

在这个类中找到

 dheader('Content-Type: image');  @readfile($filename);

修改为

dheader('Content-Type: image');
        //@readfile($filename);
            
        $tar=substr($filename, strpos($filename, "/data/attachment"));
        $tar=str_replace("/./", "/", $tar);
        if(!is_object_exist($tar))
        {
            save_to_bcs($filename,$filename);
        }
        $tar="http://bcs.duapp.com/fyj-discuz".$tar;
        echo file_get_contents($tar);
            
        @unlink($filename);

最后一句是删除本地文件,tar是最后返回的url

先就这样吧。。。貌似不太好的样子

最后还说下discuz的附件表存放方式..以前找了很久都找不到

dz_forum_attachment,dz_forum_attachment_0,到dz_forum_attachment_9

第一个表是索引,后面十个表,按帖子末位数字记录附件信息….



上一篇:URL 签名验证

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧