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. $defaultExportPath = storage_path('app/public/export/offline');
  42. $exportFile = $this->argument('filename');
  43. $filename = basename($exportFile);
  44. if ($filename === $exportFile) {
  45. $exportFullFileName = $defaultExportPath . '/' . $filename;
  46. $exportPath = $defaultExportPath;
  47. } else {
  48. $exportFullFileName = $exportFile;
  49. $exportPath = dirname($exportFile);
  50. }
  51. Log::debug(
  52. 'export offline: zip file {filename} {format}',
  53. [
  54. 'filename' => $exportFile,
  55. 'format' => $this->argument('format'),
  56. 'exportFullFileName' => $exportFullFileName,
  57. 'exportPath' => $exportPath,
  58. ]
  59. );
  60. switch ($this->argument('format')) {
  61. case '7z':
  62. $zipFile = $filename . ".7z";
  63. break;
  64. case 'lzma':
  65. $zipFile = $filename . ".lzma";
  66. break;
  67. default:
  68. $zipFile = $filename . ".gz";
  69. break;
  70. }
  71. //
  72. if (!file_exists($exportFullFileName)) {
  73. Log::error('export offline: no file {filename}', ['filename' => $exportFullFileName]);
  74. $this->error('export offline: no file {filename}' . $exportFullFileName);
  75. return 1;
  76. }
  77. $zipFullFileName = $exportPath . '/' . $zipFile;
  78. if (file_exists($zipFullFileName)) {
  79. Log::debug('export offline: delete old zip file:' . $zipFullFileName);
  80. unlink($zipFullFileName);
  81. }
  82. shell_exec("cd " . $exportPath);
  83. switch ($this->argument('format')) {
  84. case '7z':
  85. $command = [
  86. '7z',
  87. 'a',
  88. '-t7z',
  89. '-m0=lzma',
  90. '-mx=9',
  91. '-mfb=64',
  92. '-md=32m',
  93. '-ms=on',
  94. $zipFullFileName,
  95. $exportFullFileName
  96. ];
  97. break;
  98. case 'lzma':
  99. $command = ['xz', '-k', '-9', '--format=lzma', $exportFullFileName];
  100. break;
  101. default:
  102. $command = ['gzip', $exportFullFileName];
  103. break;
  104. }
  105. $this->info(implode(' ', $command));
  106. Log::debug('export offline zip start', ['command' => $command, 'format' => $this->argument('format')]);
  107. $process = new Process($command);
  108. $process->setTimeout(60 * 60 * 6);
  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. }