ExportOffline.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\Cache;
  6. class ExportOffline extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'export:offline {format?}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. //建表
  37. $this->info('create db');
  38. $this->call('export:create.db');
  39. //term
  40. $this->info('term');
  41. $this->call('export:term');
  42. //导出channel
  43. $this->info('channel');
  44. $this->call('export:channel');
  45. //tag
  46. $this->info('tag');
  47. $this->call('export:tag');
  48. $this->call('export:tag.map');
  49. //
  50. $this->info('pali text');
  51. $this->call('export:pali.text');
  52. //导出章节索引
  53. $this->info('chapter');
  54. $this->call('export:chapter.index');
  55. //导出译文
  56. $this->info('sentence');
  57. $this->call('export:sentence',['--type'=>'translation']);
  58. $this->call('export:sentence',['--type'=>'nissaya']);
  59. //导出原文
  60. $this->call('export:sentence',['--type'=>'original']);
  61. $this->info('zip');
  62. $exportPath = 'app/public/export/offline';
  63. $exportFile = 'sentence-'.date("Y-m-d").'.db3';
  64. $zipFile = "sentence-".date("Y-m-d").".db3.";
  65. if($this->argument('format')==='7z'){
  66. $zipFile .= "7z";
  67. }else if($this->argument('format')==='lzma'){
  68. $zipFile .= "lzma";
  69. }else{
  70. $zipFile .= "gz";
  71. }
  72. //
  73. $exportFullFileName = storage_path($exportPath.'/'.$exportFile);
  74. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  75. shell_exec("cd ".storage_path($exportPath));
  76. shell_exec("chmod 600 {$exportFullFileName}");
  77. if($this->argument('format')==='7z'){
  78. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  79. }else if($this->argument('format')==='lzma'){
  80. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  81. }else{
  82. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  83. }
  84. $this->info($command);
  85. shell_exec($command);
  86. $info = array();
  87. $info[] = ['filename'=>$zipFile,
  88. 'url'=>Storage::disk('local')->url($exportPath.'/'.$zipFile),
  89. 'create_at'=>date("Y-m-d H:i:s"),
  90. 'chapter'=>Cache::get("/export/chapter/count"),
  91. 'filesize'=>filesize($zipFullFileName),
  92. ];
  93. Storage::disk('local')->put("public/export/offline/index.json", json_encode($info));
  94. return 0;
  95. }
  96. }