Преглед на файлове

Merge pull request #1690 from visuddhinanda/laravel

导出章节用zip文件
visuddhinanda преди 2 години
родител
ревизия
491c3ae62f
променени са 4 файла, в които са добавени 130 реда и са изтрити 10 реда
  1. 43 9
      app/Console/Commands/ExportChapter.php
  2. 71 0
      app/Console/Commands/MqExportPaliChapter.php
  3. 3 1
      app/Http/Controllers/ExportController.php
  4. 13 0
      app/Tools/Tools.php

+ 43 - 9
app/Console/Commands/ExportChapter.php

@@ -13,12 +13,14 @@ use App\Http\Api\MdRender;
 use App\Tools\Export;
 use Illuminate\Support\Facades\Log;
 use App\Tools\RedisClusters;
+use Illuminate\Support\Str;
 
 class ExportChapter extends Command
 {
     protected $exportStatusKey = 'export/status';
     protected $exportStatusExpiry = 3600;
     protected $currExportStatusKey = '';
+    protected $realFilename = null;
     /**
      * The name and signature of the console command.
      * php artisan export:chapter 213 1913 a19eaf75-c63f-4b84-8125-1bce18311e23 213-1913 --format=html
@@ -50,11 +52,16 @@ class ExportChapter extends Command
      * message: string
      */
     protected function setStatus($progress,$message=''){
+        $data = [
+                    'progress'=>$progress,
+                    'message'=>$message,
+                    'content-type'=>'application/zip',
+                ];
+        if($this->realFilename){
+            $data['filename'] = $this->realFilename;
+        }
         RedisClusters::put($this->currExportStatusKey,
-                            [
-                                'progress'=>$progress,
-                                'message'=>$message,
-                            ],
+                            $data,
                             $this->exportStatusExpiry);
         $percent = (int)($progress * 100);
         $this->info("[{$percent}%]".$message);
@@ -84,6 +91,7 @@ class ExportChapter extends Command
         MdRender::init();
 
         $this->currExportStatusKey = $this->exportStatusKey . '/' . $this->argument('filename');
+        $this->realFilename = $this->argument('filename').'.zip';
 
         switch ($this->option('format')) {
             case 'md':
@@ -98,7 +106,8 @@ class ExportChapter extends Command
         }
         $book = $this->argument('book');
         $para = $this->argument('para');
-
+        //zip压缩包里面的文件名
+        $realFileName = "{$book}-{$para}.".$this->option('format');
         //获取原文channel
         $orgChannelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
 
@@ -348,15 +357,40 @@ class ExportChapter extends Command
                 $fileDate = implode('',$file);
                 break;
         }
-        $filename = $this->argument('filename');
+
+
+        $zipDir = storage_path('app/export/zip');
+        if(!is_dir($zipDir)){
+            $res = mkdir($zipDir,0755,true);
+            if(!$res){
+                Log::error('mkdir fail path='.$zipDir);
+                return 1;
+            }
+        }
+        $zipFile = $zipDir.'/'.Str::uuid().'.zip';
+
+        Log::debug('export chapter start zip  file='.$zipFile);
+
+        $zipOk = \App\Tools\Tools::zip($zipFile,[$realFileName=>$fileDate]);
+        if(!$zipOk){
+            Log::error('export chapter zip fail zip file='.$zipFile);
+            $this->setStatus(0.99,'export chapter zip fail');
+            $this->error('export chapter zip fail zip file='.$zipFile);
+            //TODO 给客户端返回错误状态
+            return 1;
+        }
+        $this->setStatus(0.96,'export chapter zip success');
+
         $bucket = config('mint.attachments.bucket_name.temporary');
-        $tmpFile =  $bucket.'/'. $filename ;
+        $tmpFile =  $bucket.'/'. $this->realFilename ;
         Log::debug('upload start filename='.$tmpFile);
         $this->setStatus(0.97,'upload start ');
-        Storage::put($tmpFile, $fileDate);
-
+        $zipData = file_get_contents($zipFile);
+        Storage::put($tmpFile, $zipData);
         $this->setStatus(1,'export chapter done');
         Log::debug('export chapter done, upload filename='.$tmpFile);
+
+        unlink($zipFile);
         return 0;
     }
 }

+ 71 - 0
app/Console/Commands/MqExportPaliChapter.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Http\Api\Mq;
+use Illuminate\Support\Facades\Log;
+
+class MqExportPaliChapter extends Command
+{
+    /**
+     * The name and signature of the console command.
+     * php artisan mq:export.pali.chapter
+     * @var string
+     */
+    protected $signature = 'mq:export.pali.chapter';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '导出巴利文章节';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $exchange = 'router';
+        $queue = 'export';
+        $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
+        Log::debug("mq:progress start.");
+        Mq::worker($exchange,$queue,function ($message){
+            $data = [
+                        'book'=>$message->book,
+                        'para'=>$message->para,
+                        'channel'=>$message->channel,
+                        '--format'=>$message->format,
+                        'filename'=>$message->filename,
+                    ];
+            if(isset($message->origin) && is_string($message->origin)){
+                $data['--origin'] = $message->origin;
+            }
+            if(isset($message->translation) && is_string($message->translation)){
+                $data['--translation'] = $message->translation;
+            }
+            $ok = $this->call('export:chapter',$data);
+            if($ok !== 0){
+                Log::error('mq:progress upgrade:progress fail',$data);
+            }else{
+                $this->info("Received book=".$message->book.' result='.$ok);
+                Log::debug("mq:export: done ",$data);
+                return $ok;
+            }
+        });
+        return 0;
+    }
+}

+ 3 - 1
app/Http/Controllers/ExportController.php

@@ -67,8 +67,10 @@ class ExportController extends Controller
         $output = array();
         $output['status'] = $exportStatus;
         if($exportStatus['progress']===1){
+            $realFilename = $exportStatus['filename'];
             $bucket = config('mint.attachments.bucket_name.temporary');
-            $tmpFile =  $bucket.'/'. $filename ;
+            $tmpFile =  $bucket.'/'. $realFilename ;
+            Log::debug('export download filename='.$tmpFile);
             if (App::environment('local')) {
                 $s3Link = Storage::url($tmpFile);
             }else{

+ 13 - 0
app/Tools/Tools.php

@@ -3,6 +3,19 @@ namespace App\Tools;
 use Illuminate\Support\Facades\Log;
 
 class Tools{
+    public static function zip($zipFile,$files){
+        $zip = new \ZipArchive;
+        $res = $zip->open($zipFile, \ZipArchive::CREATE);
+        if ($res === TRUE) {
+            foreach ($files as $key => $value) {
+                $zip->addFromString($key, $value);
+            }
+            $zip->close();
+            return true;
+        } else {
+            return false;
+        }
+    }
     public static function isStop(){
         if(file_exists(base_path('.stop'))){
             Log::debug('.stop exists');