TemplateLibrarySyncPictures.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Site;
  4. use App\Http\Services\TemplateLibraryApiService;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class TemplateLibrarySyncPictures extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'sync_pictures:template_library';
  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. private $templateLibraryApiService;
  28. public function __construct(TemplateLibraryApiService $templateLibraryApiService)
  29. {
  30. parent::__construct();
  31. $this->templateLibraryApiService = $templateLibraryApiService;
  32. }
  33. const HOST = '121.199.40.85';
  34. const USER = 'root';
  35. const PASSWORD = 'JGJHD84@8&a';
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. /*try {
  44. $ssh2 = ssh2_connect(self::HOST, 22);
  45. ssh2_auth_password($ssh2, self::USER, self::PASSWORD);
  46. $siteId = 974;
  47. $domain = Site::query()->where('id', $siteId)->value('domain');
  48. //远程目录
  49. $targetDirectory = '/www/wwwroot/' . $domain . '/sepSsr/uploads';
  50. //递归创建目录
  51. $sftp = ssh2_sftp($ssh2);
  52. ssh2_sftp_mkdir($sftp, $targetDirectory, 0755, true);
  53. $connection = DB::connection($this->templateLibraryApiService->connection($siteId));
  54. $imagesList = $connection->table('image')->get();
  55. foreach ($imagesList as $item) {
  56. $imagePathArray = explode('storage', $item->src);
  57. $fileName = explode('/', $imagePathArray[1]);
  58. $file = array_pop($fileName);
  59. //本地目录
  60. $sourceDirectory = base_path() . '/storage/app/public' . $imagePathArray[1];
  61. $stream = ssh2_scp_send($ssh2, $sourceDirectory, $targetDirectory . '/' . $file, 0644);
  62. if ($stream) {
  63. $connection->table('image')->where('id', $item->id)->update(['is_synchronize' => 1]);
  64. }
  65. }
  66. } catch (\Exception $exception) {
  67. Log::info(date('Y-m-d H:i:s') . 'sync_pictures error!' . $exception->getMessage());
  68. }
  69. Log::info(date('Y-m-d H:i:s') . 'sync_pictures success!');*/
  70. }
  71. }