| Server IP : 54.94.228.101 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/app/Console/Commands/ |
Upload File : |
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Modules\PedidoVenda\Models\PedidoVenda;
use App\Modules\Tema\Models\Tema;
use App\Modules\Canal\Models\Canal;
use App\Modules\TipoDeProduto\Models\TipoDeProduto;
class AtualizaFilesS3 extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'atualiza_files_s3 {env=local}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Realiza o upload dos arquivos locais para o S3';
protected $env = 'local';
protected $urlAmbiente = [
'dev' => 'http://dev.vinumday.com.br',
'prod' => 'https://www.vinumday.com.br'
];
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @param $file Request $file
* @param $subFolder String 'pedido-venda/comprovante','produtos', 'etc'
* @return $url Object URL Armazenamento
* @return $filename Object Filename
*/
public function uploadFile($file, $subFolder, $options = []){
$tmpFilePath = ($this->env != 'local' ? $this->urlAmbiente[$this->env] : '') . '/uploads/' . $subFolder;
if($file){
$tmpFileName = time() . '-' . $file->getClientOriginalName();
$file = $file->move(public_path() . $tmpFilePath, $tmpFileName);
}else{
$tmpFileName = $options['filename'];
$options['delete'] = false;
}
$s3 = \Storage::disk('s3');
$filePath = '/images/' . $subFolder . '/' . $tmpFileName;
$content = @file_get_contents($tmpFilePath . '/' . $tmpFileName);
if($content === false){
return (object) ['url' => '', 'filename' => ''];
}
$s3->put($filePath, $content, 'public');
$url = config('filesystems.disks.s3.url').$filePath;
if(!isset($options['delete']) || (isset($options['delete']) && $options['delete'] == true)){
@unlink($file);
}
return (object) ['url' => $url, 'filename' => $tmpFileName];
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->env = $this->argument('env');
//Pedido-Venda/Comprovante
printf("Migrando Comprovantes... \n");
$PedidoVenda = new PedidoVenda();
if($this->env != 'local'){
$PedidoVenda->setConnection("mysql{$this->env}");
}
$comprovantes = $PedidoVenda->select('pedido_venda.comprovante')
->where('comprovante', '<>', '')
->where('comprovante', '<>', 'comprovante_vinumday.png')
->get();
foreach ($comprovantes as $item):
printf($item->comprovante . "\n");
$this->uploadFile(FALSE, 'pedido-venda/comprovante', ['filename' => $item->comprovante]);
endforeach;
printf("Comprovantes Migrados \n");
//Tema
printf("Migrando Temas... \n");
$Tema = new Tema();
if($this->env != 'local'){
$Tema->setConnection("mysql{$this->env}");
}
$temas = $Tema->select('tema.thumbnail_principal')->get();
foreach ($temas as $item):
printf($item->thumbnail_principal . "\n");
$this->uploadFile(FALSE, 'tema', ['filename' => $item->thumbnail_principal]);
endforeach;
printf("Temas Migrados \n");
//Canal
printf("Migrando Canais... \n");
$Canal = new Canal();
if($this->env != 'local'){
$Canal->setConnection("mysql{$this->env}");
}
$canais = $Canal->select('canal.thumbnail_principal')->get();
foreach ($canais as $item):
printf($item->thumbnail_principal . "\n");
$this->uploadFile(FALSE, 'canal', ['filename' => $item->thumbnail_principal]);
endforeach;
printf("Canais Migrados \n");
//Tipo de Produto
printf("Migrando Tipos de Produto... \n");
$TipoDeProduto = new TipoDeProduto();
if($this->env != 'local'){
$TipoDeProduto->setConnection("mysql{$this->env}");
}
$tipos_de_produto = $TipoDeProduto->select('tipo_de_produto.thumbnail_principal')->get();
foreach ($tipos_de_produto as $item):
printf($item->thumbnail_principal . "\n");
$this->uploadFile(FALSE, 'tipo-de-produto', ['filename' => $item->thumbnail_principal]);
endforeach;
printf("Tipos de Produto Migrados \n");
}
}