最近发现用Thinkcmf开发的程序迁移服务器后,七牛云出了问题,cms后台上传的时候,看着七牛那边存储的文件不带后缀,切前台是带后缀的导致前台图片不显示问题。看了下七牛插件的源码,发现public/plugins/qiniu/Controller/AssetController里面的saveFile方法有些问题
1 2 3 4 | $signingStr = "/move/{$encodedEntryURISrc}/{$encodedEntryURIDest}" ; $authorization = $auth ->signRequest( $signingStr , '' ); $url = 'http://rs.qiniu.com/' . $signingStr ; $response = $client ->post( $url , null, [ 'Authorization' => 'QBox ' . $authorization ]); |
上面是改文件的一部分源码,执行上面程序七牛http返回码会直接301,原因是signingString多了第一个反斜线,去掉反斜线后,七牛返回的错误是bad_token,错误码是401,那就是token生成有误,token生成是
1 | $authorization = $auth ->signRequest( $signingStr , '' ); |
具体代码逻辑可以自己点进去看下,我没有找到官网源码token生成的bug,所以这里我修复的方法是不走原来代码的逻辑,直接用bucketmanager替换原来的方式,整个saveFile方法如下:
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 | public function saveFile() { $userId = cmf_get_current_admin_id(); $userId = $userId ? $userId : cmf_get_current_user_id(); if (empty( $userId )) { $this ->error( 'error' ); } $validate = new Validate([ 'filename' => 'require' , 'file_key' => 'require' , ]); $data = $this ->request->param(); $result = $validate ->check( $data ); if ( $result !== true) { $this ->error( $validate ); } $fileKey = $data [ 'file_key' ]; $suffix = cmf_get_file_extension( $data [ 'filename' ]); $config = $this ->getPlugin()->getConfig(); $accessKey = $config [ 'accessKey' ]; $secretKey = $config [ 'secretKey' ]; $auth = new Auth( $accessKey , $secretKey ); $qiniuconfig =new Config(); $bk =new BucketManager( $auth , $qiniuconfig ); $err = $bk -> rename ( $config [ 'bucket' ], $fileKey , $fileKey . ".{$suffix}" ); if ( $err ){ $this ->error( $err ); } list( $fileinfo , $error )= $bk -> stat ( $config [ 'bucket' ], $fileKey . ".{$suffix}" ); if ( $error ){ $this ->error( $error ); } $findAsset = Db::name( 'asset' )->where( 'file_key' , $fileKey )->find(); if (empty( $findAsset )) { Db::name( 'asset' )->insert([ 'user_id' => $userId , 'file_size' => $fileinfo [ 'fsize' ], 'filename' => $data [ 'filename' ], 'create_time' => time (), 'file_key' => $fileKey , 'file_path' => $fileKey . ".{$suffix}" , 'suffix' => $suffix ]); } $this ->success( 'success' ); } |
亲测没有问题
本文为Adamin90原创文章,转载无需和我联系,但请注明来自http://www.lixiaopeng.top
执着丶小郭:大哥 我的错。 是可以的,必须赞一个
2020-04-27 15:51:27 回复
执着丶小郭:嗯 不行
2020-04-27 10:45:24 回复