2
0
visuddhinanda 1 жил өмнө
parent
commit
bb71671751

+ 53 - 23
api-v8/app/Console/Commands/ExportZip.php

@@ -8,6 +8,9 @@ use Illuminate\Support\Facades\Log;
 use App\Tools\RedisClusters;
 use Illuminate\Support\Facades\App;
 
+use Symfony\Component\Process\Process;
+use Symfony\Component\Process\Exception\ProcessFailedException;
+
 class ExportZip extends Command
 {
     /**
@@ -15,14 +18,14 @@ class ExportZip extends Command
      *
      * @var string
      */
-    protected $signature = 'export:zip {db : db filename} {format?  : zip file format 7z,lzma,gz }';
+    protected $signature = 'export:zip {filename : filename} {title : title} {format?  : zip file format 7z,lzma,gz }';
 
     /**
      * The console command description.
      *
      * @var string
      */
-    protected $description = 'Command description';
+    protected $description = '压缩导出的文件';
 
     /**
      * Create a new command instance.
@@ -44,7 +47,7 @@ class ExportZip extends Command
         Log::debug('export offline: 开始压缩');
         $this->info('export offline: 开始压缩');
         $exportPath = 'app/public/export/offline';
-        $exportFile = $this->argument('db').'-'.date("Y-m-d").'.db3';
+        $exportFile = $this->argument('filename');
 
         Log::debug('export offline: zip file {filename} {format}',
                     [
@@ -77,28 +80,39 @@ class ExportZip extends Command
         }
 
         shell_exec("cd ".storage_path($exportPath));
-        if($this->argument('format')==='7z'){
-            $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
-        }else if($this->argument('format')==='lzma'){
-            $command = "xz -k -9 --format=lzma {$exportFullFileName}";
-        }else{
-            $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
+        switch ($this->argument('format')) {
+            case '7z':
+                $command = [
+                    '7z', 'a', '-t7z', '-m0=lzma',
+                    '-mx=9', '-mfb=64', '-md=32m', '-ms=on',
+                    $zipFullFileName,$exportFullFileName
+                ];
+                break;
+            case 'lzma':
+                $command = ['xz', '-k', '-9', '--format=lzma',$exportFullFileName];
+                break;
+            default:
+                $command = ['gzip', $exportFullFileName];
+                break;
         }
-        $this->info($command);
-        Log::debug('export offline: zip command:'.$command);
-        shell_exec($command);
+
+        $this->info( implode(' ',$command));
+        Log::debug('export offline zip start',['command'=>$command,'format'=>$this->argument('format')]);
+        $process = new Process($command);
+        $process->run();
+        $this->info($process->getOutput());
         $this->info('压缩完成');
         Log::debug('zip file {filename} in {format} saved.',
                     [
                         'filename'=>$exportFile,
                         'format'=>$this->argument('format')
                     ]);
-        $info = array();
+
         $url = array();
         foreach (config('mint.server.cdn_urls') as $key => $cdn) {
             $url[] = [
                     'link' => $cdn . '/' . $zipFile,
-                    'hostname' =>'cdn-' . $key,
+                    'hostname' =>'china cdn-' . $key,
                 ];
         }
 
@@ -135,15 +149,31 @@ class ExportZip extends Command
             'link'=>$link,
             'hostname'=>'Amazon cloud storage(Hongkong)',
         ];
-        $info[] = ['filename'=>$zipFile,
-                    'url' => $url,
-                   'create_at'=>date("Y-m-d H:i:s"),
-                   'chapter'=>RedisClusters::get("/export/chapter/count"),
-                   'filesize'=>filesize($zipFullFileName),
-                   'min_app_ver'=>'1.3',
-                    ];
-        RedisClusters::put('/offline/index/'.$this->argument('db'),$info);
-        unlink($exportFullFileName);
+        $info = RedisClusters::get('/offline/index');
+        if(!is_array($info)){
+            $info = array();
+        }
+        $info[] = [
+            'title' => $this->argument('title'),
+            'filename'=>$zipFile,
+            'url' => $url,
+            'create_at'=>date("Y-m-d H:i:s"),
+            'chapter'=>RedisClusters::get("/export/chapter/count"),
+            'filesize'=>filesize($zipFullFileName),
+            'min_app_ver'=>'1.3',
+            ];
+        RedisClusters::put('/offline/index',$info);
+        sleep(5);
+        try {
+            unlink($exportFullFileName);
+        } catch (\Throwable $th) {
+            Log::error('export offline: delete  file fail {Exception}',
+                        [
+                            'exception'=>$th,
+                            'file'=>$exportFullFileName
+                        ]);
+        }
+
         return 0;
     }
 }