ExportZip.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. use Symfony\Component\Process\Process;
  9. use Symfony\Component\Process\Exception\ProcessFailedException;
  10. class ExportZip extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'export:zip {filename : filename} {title : title} {id : 标识符} {format? : zip file format 7z,lzma,gz }';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '压缩导出的文件';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. Log::debug('export offline: 开始压缩');
  41. $this->info('export offline: 开始压缩');
  42. $defaultExportPath = storage_path('app/public/export/offline');
  43. $exportFile = $this->argument('filename');
  44. $filename = basename($exportFile);
  45. if ($filename === $exportFile) {
  46. $exportFullFileName = $defaultExportPath . '/' . $filename;
  47. $exportPath = $defaultExportPath;
  48. } else {
  49. $exportFullFileName = $exportFile;
  50. $exportPath = dirname($exportFile);
  51. }
  52. Log::debug(
  53. 'export offline: zip file {filename} {format}',
  54. [
  55. 'filename' => $exportFile,
  56. 'format' => $this->argument('format'),
  57. 'exportFullFileName' => $exportFullFileName,
  58. 'exportPath' => $exportPath,
  59. ]
  60. );
  61. switch ($this->argument('format')) {
  62. case '7z':
  63. $zipFile = $filename . ".7z";
  64. break;
  65. case 'lzma':
  66. $zipFile = $filename . ".lzma";
  67. break;
  68. default:
  69. $zipFile = $filename . ".gz";
  70. break;
  71. }
  72. //
  73. if (!file_exists($exportFullFileName)) {
  74. Log::error('export offline: no file {filename}', ['filename' => $exportFullFileName]);
  75. $this->error('export offline: no file {filename}' . $exportFullFileName);
  76. return 1;
  77. }
  78. $zipFullFileName = $exportPath . '/' . $zipFile;
  79. if (file_exists($zipFullFileName)) {
  80. Log::debug('export offline: delete old zip file:' . $zipFullFileName);
  81. unlink($zipFullFileName);
  82. }
  83. shell_exec("cd " . $exportPath);
  84. switch ($this->argument('format')) {
  85. case '7z':
  86. $command = [
  87. '7z',
  88. 'a',
  89. '-t7z',
  90. '-m0=lzma',
  91. '-mx=9',
  92. '-mfb=64',
  93. '-md=32m',
  94. '-ms=on',
  95. $zipFullFileName,
  96. $exportFullFileName
  97. ];
  98. break;
  99. case 'lzma':
  100. $command = ['xz', '-k', '-9', '--format=lzma', $exportFullFileName];
  101. break;
  102. default:
  103. $command = ['gzip', $exportFullFileName];
  104. break;
  105. }
  106. $this->info(implode(' ', $command));
  107. Log::debug('export offline zip start', ['command' => $command, 'format' => $this->argument('format')]);
  108. $process = new Process($command);
  109. $process->run();
  110. $this->info($process->getOutput());
  111. $this->info('压缩完成');
  112. Log::debug(
  113. 'zip file {filename} in {format} saved.',
  114. [
  115. 'filename' => $exportFile,
  116. 'format' => $this->argument('format')
  117. ]
  118. );
  119. $url = array();
  120. foreach (config('mint.server.cdn_urls') as $key => $cdn) {
  121. $url[] = [
  122. 'link' => $cdn . '/' . $zipFile,
  123. 'hostname' => 'china cdn-' . $key,
  124. ];
  125. }
  126. $bucket = config('mint.attachments.bucket_name.temporary');
  127. $tmpFile = $bucket . '/' . $zipFile;
  128. $this->info('upload file=' . $tmpFile);
  129. Log::debug('export offline: upload file {filename}', ['filename' => $tmpFile]);
  130. Storage::put($tmpFile, file_get_contents($zipFullFileName));
  131. $this->info('upload done file=' . $tmpFile);
  132. Log::debug('export offline: upload done {filename}', ['filename' => $tmpFile]);
  133. if (App::environment('local')) {
  134. $link = Storage::url($tmpFile);
  135. } else {
  136. try {
  137. $link = Storage::temporaryUrl($tmpFile, now()->addDays(2));
  138. } catch (\Exception $e) {
  139. $this->error('generate temporaryUrl fail');
  140. Log::error(
  141. 'export offline: generate temporaryUrl fail {Exception}',
  142. [
  143. 'exception' => $e,
  144. 'file' => $tmpFile
  145. ]
  146. );
  147. return 1;
  148. }
  149. }
  150. $this->info('link = ' . $link);
  151. Log::info('export offline: link=' . $link);
  152. $url[] = [
  153. 'link' => $link,
  154. 'hostname' => 'Amazon cloud storage(Hongkong)',
  155. ];
  156. $info = RedisClusters::get('/offline/index');
  157. if (!is_array($info)) {
  158. $info = array();
  159. }
  160. $info[] = [
  161. 'id' => $this->argument('id'),
  162. 'title' => $this->argument('title'),
  163. 'filename' => $zipFile,
  164. 'url' => $url,
  165. 'create_at' => date("Y-m-d H:i:s"),
  166. 'chapter' => RedisClusters::get("/export/chapter/count"),
  167. 'filesize' => filesize($zipFullFileName),
  168. 'min_app_ver' => '1.3',
  169. ];
  170. RedisClusters::put('/offline/index', $info);
  171. sleep(5);
  172. try {
  173. unlink($exportFullFileName);
  174. } catch (\Throwable $th) {
  175. Log::error(
  176. 'export offline: delete file fail {Exception}',
  177. [
  178. 'exception' => $th,
  179. 'file' => $exportFullFileName
  180. ]
  181. );
  182. }
  183. return 0;
  184. }
  185. }