ExportZip.php 3.6 KB

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