ExportZip.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 {db : db filename} {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 = $this->argument('db').'-'.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. if(file_exists($zipFullFileName)){
  67. Log::debug('export offline: delete old zip file:'.$zipFullFileName);
  68. unlink($zipFullFileName);
  69. }
  70. shell_exec("cd ".storage_path($exportPath));
  71. if($this->argument('format')==='7z'){
  72. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  73. }else if($this->argument('format')==='lzma'){
  74. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  75. }else{
  76. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  77. }
  78. $this->info($command);
  79. Log::debug('export offline: zip command:'.$command);
  80. shell_exec($command);
  81. $this->info('压缩完成');
  82. Log::debug('zip file {filename} in {format} saved.',
  83. [
  84. 'filename'=>$exportFile,
  85. 'format'=>$this->argument('format')
  86. ]);
  87. $info = array();
  88. $url = array();
  89. foreach (config('mint.server.cdn_urls') as $key => $cdn) {
  90. $url[] = [
  91. 'link' => $cdn . '/' . $zipFile,
  92. 'hostname' =>'cdn-' . $key,
  93. ];
  94. }
  95. $bucket = config('mint.attachments.bucket_name.temporary');
  96. $tmpFile = $bucket.'/'. $zipFile ;
  97. $this->info('upload file='.$tmpFile);
  98. Log::debug('export offline: upload file {filename}',['filename'=>$tmpFile]);
  99. Storage::put($tmpFile, file_get_contents($zipFullFileName));
  100. $this->info('upload done file='.$tmpFile);
  101. Log::debug('export offline: upload done {filename}',['filename'=>$tmpFile]);
  102. if (App::environment('local')) {
  103. $link = Storage::url($tmpFile);
  104. }else{
  105. try{
  106. $link = Storage::temporaryUrl($tmpFile, now()->addDays(2));
  107. }catch(\Exception $e){
  108. $this->error('generate temporaryUrl fail');
  109. Log::error('export offline: generate temporaryUrl fail {Exception}',
  110. [
  111. 'exception'=>$e,
  112. 'file'=>$tmpFile
  113. ]);
  114. return 1;
  115. }
  116. }
  117. $this->info('link = '.$link);
  118. Log::info('export offline: link='.$link);
  119. $url[] = [
  120. 'link'=>$link,
  121. 'hostname'=>'Amazon cloud storage(Hongkong)',
  122. ];
  123. $info[] = ['filename'=>$zipFile,
  124. 'url' => $url,
  125. 'create_at'=>date("Y-m-d H:i:s"),
  126. 'chapter'=>RedisClusters::get("/export/chapter/count"),
  127. 'filesize'=>filesize($zipFullFileName),
  128. 'min_app_ver'=>'1.3',
  129. ];
  130. RedisClusters::put('/offline/index/'.$this->argument('db'),$info);
  131. unlink($exportFullFileName);
  132. return 0;
  133. }
  134. }