ExportZip.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\Log;
  6. use App\Tools\RedisClusters;
  7. use Illuminate\Support\Facades\App;
  8. class ExportZip extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'export:zip {format? : zip file format 7z,lzma,gz }';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. Log::debug('export offline: 开始压缩');
  39. $this->info('export offline: 开始压缩');
  40. $exportPath = 'app/public/export/offline';
  41. $exportFile = 'wikipali-offline-'.date("Y-m-d").'.db3';
  42. Log::debug('export offline: zip file {filename} {format}',
  43. [
  44. 'filename'=>$exportFile,
  45. 'format'=>$this->argument('format')
  46. ]);
  47. switch ($this->argument('format')) {
  48. case '7z':
  49. $zipFile = $exportFile . ".7z";
  50. break;
  51. case 'lzma':
  52. $zipFile = $exportFile . ".lzma";
  53. break;
  54. default:
  55. $zipFile = $exportFile . ".gz";
  56. break;
  57. }
  58. //
  59. $exportFullFileName = storage_path($exportPath.'/'.$exportFile);
  60. if(!file_exists($exportFullFileName)){
  61. Log::error('export offline: no db file {filename}',['filename'=>$exportFullFileName]);
  62. $this->error('export offline: no db file {filename}'.$exportFullFileName);
  63. return 1;
  64. }
  65. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  66. shell_exec("cd ".storage_path($exportPath));
  67. if($this->argument('format')==='7z'){
  68. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  69. }else if($this->argument('format')==='lzma'){
  70. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  71. }else{
  72. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  73. }
  74. $this->info($command);
  75. Log::debug('export offline: zip command:'.$command);
  76. shell_exec($command);
  77. $this->info('压缩完成');
  78. Log::debug('zip file {filename} in {format} saved.',
  79. [
  80. 'filename'=>$exportFile,
  81. 'format'=>$this->argument('format')
  82. ]);
  83. $info = array();
  84. $url = array();
  85. foreach (config('mint.server.cdn_urls') as $key => $cdn) {
  86. $url[] = [
  87. 'link' => $cdn . '/' . $zipFile,
  88. 'hostname' =>'cdn-' . $key,
  89. ];
  90. }
  91. //s3
  92. Storage::put($zipFile, file_get_contents($zipFullFileName));
  93. if (App::environment('local')) {
  94. $s3Link = Storage::url($zipFile);
  95. }else{
  96. try{
  97. $s3Link = Storage::temporaryUrl($zipFile, now()->addDays(1));
  98. }catch(\Exception $e){
  99. Log::error('export offline generate temporaryUrl fail {Exception}',['exception'=>$e]);
  100. return 1;
  101. }
  102. }
  103. Log::info('export offline: link='.$s3Link);
  104. $url[] = [
  105. 'link'=>$s3Link,
  106. 'hostname'=>'Amazon cloud storage(Hongkong)',
  107. ];
  108. $info[] = ['filename'=>$zipFile,
  109. 'url' => $url,
  110. 'create_at'=>date("Y-m-d H:i:s"),
  111. 'chapter'=>RedisClusters::get("/export/chapter/count"),
  112. 'filesize'=>filesize($zipFullFileName),
  113. 'min_app_ver'=>'1.3',
  114. ];
  115. RedisClusters::put('/offline/index',$info);
  116. unlink($exportFullFileName);
  117. return 0;
  118. }
  119. }