ExportZip.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $this->info('upload file='.$zipFile);
  92. Log::debug('export offline: upload file {filename}',['filename'=>$zipFile]);
  93. $bucket = 'attachments-'.config('app.env');
  94. $tmpFile = $bucket.'/'. $zipFile ;
  95. Storage::put($tmpFile, file_get_contents($zipFullFileName));
  96. $this->info('upload done file='.$tmpFile);
  97. Log::debug('export offline: upload done {filename}',['filename'=>$tmpFile]);
  98. if (App::environment('local')) {
  99. $link = Storage::url($tmpFile);
  100. }else{
  101. try{
  102. $link = Storage::temporaryUrl($tmpFile, now()->addDays(2));
  103. }catch(\Exception $e){
  104. $this->error('generate temporaryUrl fail');
  105. Log::error('export offline: generate temporaryUrl fail {Exception}',
  106. [
  107. 'exception'=>$e,
  108. 'file'=>$tmpFile
  109. ]);
  110. return 1;
  111. }
  112. }
  113. $this->info('link = '.$link);
  114. Log::info('export offline: link='.$link);
  115. $url[] = [
  116. 'link'=>$link,
  117. 'hostname'=>'Amazon cloud storage(Hongkong)',
  118. ];
  119. $info[] = ['filename'=>$zipFile,
  120. 'url' => $url,
  121. 'create_at'=>date("Y-m-d H:i:s"),
  122. 'chapter'=>RedisClusters::get("/export/chapter/count"),
  123. 'filesize'=>filesize($zipFullFileName),
  124. 'min_app_ver'=>'1.3',
  125. ];
  126. RedisClusters::put('/offline/index',$info);
  127. unlink($exportFullFileName);
  128. return 0;
  129. }
  130. }