| <?php |
| |
| namespace App\Http\Controllers\Common; |
| |
| use Qiniu\Auth; |
| use zgldh\QiniuStorage\QiniuStorage; |
| use Qiniu\Processing\PersistentFop; |
| use Illuminate\Support\Str; |
| use function Qiniu\base64_urlSafeEncode; |
| use function Qiniu\entry; |
| |
| class QiniuCompressionController |
| { |
| private string $accessKey; |
| private string $secretKey; |
| private string $bucket; |
| private string $pipeline; |
| private string $domain; |
| |
| public function __construct() |
| { |
| $this->accessKey = env('accessKey'); |
| $this->secretKey = env('secretKey'); |
| $this->bucket = env('bucket'); |
| $this->pipeline = env('pipeline'); |
| $this->domain = env('domain'); |
| } |
| |
| public function multiFileCompression(array $source_array, string $package_name, string $callback_url = null) |
| { |
| |
| $auth = new Auth($this->accessKey, $this->secretKey); |
| |
| $disk = QiniuStorage::disk('qiniu'); |
| |
| $file_name = md5(time() . Str::random(6)).'.txt'; |
| |
| foreach ($source_array as $source) { |
| $str = ''; |
| $Base64EncodedURL = base64_urlSafeEncode($source['key']); |
| $Base64AliasEncodedURL = base64_urlSafeEncode($source['alias']); |
| $str .= '/url/'.$Base64EncodedURL; |
| if(!empty($source['alias'])){ |
| $str .= '/alias/'.$Base64AliasEncodedURL; |
| } |
| |
| file_put_contents($file_name, $str.PHP_EOL, FILE_APPEND); |
| } |
| |
| $file_content = file_get_contents($file_name); |
| |
| $dir = 'zips'; |
| $key = $dir . '/' . $file_name; |
| $bool = $disk->put($key,$file_content); |
| |
| |
| if ($bool) { |
| $fops = 'mkzip/4/|saveas/'.entry($this->bucket,$dir . '/' . $package_name); |
| |
| $pfop = new PersistentFop($auth, null); |
| |
| list($id, $err) = $pfop->execute($this->bucket,$key,$fops,$this->pipeline,$callback_url,false); |
| |
| if(is_null($err)){ |
| |
| while(true){ |
| $url = "https://api.qiniu.com/status/get/prefop?id=" . $id; |
| $json = file_get_contents($url); |
| $arr = json_decode($json, true); |
| if($arr['code'] == 0 ){ |
| break; |
| }else if($arr['code'] == 3 || $arr['code'] == 4){ |
| return ['code'=>500,'msg'=>'操作失败']; |
| } |
| } |
| |
| |
| unlink($file_name); |
| |
| $disk->delete($key); |
| |
| return [ |
| 'code'=>200, |
| 'download_url' => 'https://' . $this->domain . '/' . $dir . '/' . $package_name, |
| ]; |
| }else{ |
| return ['code'=>500,'msg'=>'操作失败']; |
| } |
| exit; |
| } else{ |
| return ['code'=>500,'msg'=>'操作失败']; |
| } |
| } |
| |
| public function do() |
| { |
| |
| $source_array = [ |
| [ |
| 'key' => 'http://xxxxxxxxxxxxxxx.jpg', |
| 'alias' => 'a.jpg', |
| ],[ |
| 'key' => 'http://yyyyyyyyyyyyyyy.jpg', |
| 'alias' => 'b.jpg', |
| ], |
| ]; |
| |
| |
| $package_name = date('YmdHis') . '-' .Str::random(6) . '.zip'; |
| |
| $data = $this->multiFileCompression($source_array,$package_name); |
| |
| |
| if($data['code'] == 200){ |
| $response = response()->streamDownload(function () use($data){ |
| echo file_get_contents($data['download_url']); |
| },$package_name,[ |
| 'Content-Type: application/octet-stream', |
| 'Content-Disposition: attachment; filename="'.$package_name.'"', |
| 'Content-Transfer-Encoding: binary' |
| ]); |
| |
| return $response; |
| } |
| } |
| } |