ExportZip.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. $this->error('export offline: no db file {filename}'.$exportFullFileName);
  62. return 1;
  63. }
  64. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  65. shell_exec("cd ".storage_path($exportPath));
  66. if($this->argument('format')==='7z'){
  67. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  68. }else if($this->argument('format')==='lzma'){
  69. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  70. }else{
  71. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  72. }
  73. $this->info($command);
  74. Log::debug('export offline: zip command:'.$command);
  75. shell_exec($command);
  76. $this->info('压缩完成');
  77. Log::debug('zip file {filename} in {format} saved.',
  78. [
  79. 'filename'=>$exportFile,
  80. 'format'=>$this->argument('format')
  81. ]);
  82. $info = array();
  83. $url = array();
  84. foreach (config('mint.server.cdn_urls') as $key => $cdn) {
  85. $url[] = [
  86. 'link' => $cdn . '/' . $zipFile,
  87. 'hostname' =>'cdn-' . $key,
  88. ];
  89. }
  90. //s3
  91. Storage::put($zipFile, file_get_contents($zipFullFileName));
  92. $s3Link = Storage::url($zipFile);
  93. Log::info('export offline: link='.$s3Link);
  94. $url[] = [
  95. 'link'=>$s3Link,
  96. 'hostname'=>'Amazon cloud storage(Hongkong)',
  97. ];
  98. $info[] = ['filename'=>$zipFile,
  99. 'url' => $url,
  100. 'create_at'=>date("Y-m-d H:i:s"),
  101. 'chapter'=>RedisClusters::get("/export/chapter/count"),
  102. 'filesize'=>filesize($zipFullFileName),
  103. 'min_app_ver'=>'1.3',
  104. ];
  105. RedisClusters::put('/offline/index',$info);
  106. unlink($exportFullFileName);
  107. return 0;
  108. }
  109. }