ExportOffline.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. //导出channel
  40. $this->info('channel');
  41. $this->call('export:channel');
  42. //tag
  43. $this->info('tag');
  44. $this->call('export:tag');
  45. $this->call('export:tag.map');
  46. //
  47. $this->info('pali text');
  48. $this->call('export:pali.text');
  49. //导出章节索引
  50. $this->info('chapter');
  51. $this->call('export:chapter.index');
  52. //导出译文
  53. $this->info('sentence');
  54. $this->call('export:sentence',['--type'=>'translation']);
  55. $this->call('export:sentence',['--type'=>'nissaya']);
  56. //导出原文
  57. $this->call('export:sentence',['--type'=>'original']);
  58. $this->info('zip');
  59. $exportPath = 'app/public/export/offline';
  60. $exportFile = 'sentence-'.date("Y-m-d").'.db3';
  61. $zipFile = "sentence-".date("Y-m-d").".db3.";
  62. if($this->argument('format')==='7z'){
  63. $zipFile .= "7z";
  64. }else if($this->argument('format')==='lzma'){
  65. $zipFile .= "lzma";
  66. }else{
  67. $zipFile .= "gz";
  68. }
  69. //
  70. $exportFullFileName = storage_path($exportPath.'/'.$exportFile);
  71. $zipFullFileName = storage_path($exportPath.'/'.$zipFile);
  72. shell_exec("cd ".storage_path($exportPath));
  73. shell_exec("chmod 600 {$exportFullFileName}");
  74. if($this->argument('format')==='7z'){
  75. $command = "7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on {$zipFullFileName} {$exportFullFileName}";
  76. }else if($this->argument('format')==='lzma'){
  77. $command = "xz -k -9 --format=lzma {$exportFullFileName}";
  78. }else{
  79. $command = "gzip -k -q --best -c {$exportFullFileName} > {$zipFullFileName}";
  80. }
  81. $this->info($command);
  82. shell_exec($command);
  83. $info = array();
  84. $info[] = ['filename'=>$zipFile,
  85. 'url'=>Storage::disk('local')->url($exportPath.'/'.$zipFile),
  86. 'create_at'=>date("Y-m-d H:i:s"),
  87. 'chapter'=>Cache::get("/export/chapter/count"),
  88. 'filesize'=>filesize($zipFullFileName),
  89. ];
  90. Storage::disk('local')->put("public/export/offline/index.json", json_encode($info));
  91. return 0;
  92. }
  93. }