updated
This commit is contained in:
67
app/Console/Commands/MediaMigrate.php
Normal file
67
app/Console/Commands/MediaMigrate.php
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Jobs\StoreUploadedFileJob;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Models\Media;
|
||||||
|
use File;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
|
class MediaMigrate extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'media:migrate';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Migrate the uploads folder to the CDN';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the command options.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->addOption(
|
||||||
|
'replace',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'Replace existing files'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$replace = $this->option('replace');
|
||||||
|
|
||||||
|
$files = File::allFiles(public_path('uploads'));
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$filename = pathinfo($file, PATHINFO_BASENAME);
|
||||||
|
|
||||||
|
$medium = Media::where('name', $filename)->first();
|
||||||
|
|
||||||
|
if ($medium !== null) {
|
||||||
|
$medium->update(['status' => 'Processing media']);
|
||||||
|
StoreUploadedFileJob::dispatch($medium, $file, $replace)->onQueue('media');
|
||||||
|
} else {
|
||||||
|
unlink($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
56
app/Console/Commands/MediaRebuild.php
Normal file
56
app/Console/Commands/MediaRebuild.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Jobs\StoreUploadedFileJob;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Models\Media;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
|
class MediaRebuild extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'media:rebuild';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Rebuild the media table';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the command options.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->addOption(
|
||||||
|
'replace',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'Replace existing files'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$replace = $this->option('replace');
|
||||||
|
|
||||||
|
$media = Media::where(['variants' => ''])->orWhere(['variants' => '[]'])->orWhere(['variants' => '{}'])->get();
|
||||||
|
foreach ($media as $medium) {
|
||||||
|
StoreUploadedFileJob::dispatch($medium, '', $replace)->onQueue('media');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -73,11 +73,31 @@ class StoreUploadedFileJob implements ShouldQueue
|
|||||||
$this->media->status = "Uploading to CDN";
|
$this->media->status = "Uploading to CDN";
|
||||||
$this->media->save();
|
$this->media->save();
|
||||||
|
|
||||||
if (Storage::disk($storageDisk)->exists($fileName) == false || $this->replaceExisting == true) {
|
if (strlen($this->uploadedFilePath) > 0) {
|
||||||
Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($this->uploadedFilePath), $fileName);
|
if (Storage::disk($storageDisk)->exists($fileName) == false || $this->replaceExisting == true) {
|
||||||
Log::info("uploading file {$storageDisk} / {$fileName} / {$this->uploadedFilePath}");
|
Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($this->uploadedFilePath), $fileName);
|
||||||
|
Log::info("uploading file {$storageDisk} / {$fileName} / {$this->uploadedFilePath}");
|
||||||
|
} else {
|
||||||
|
Log::info("file {$fileName} already exists in {$storageDisk} / {$this->uploadedFilePath}. Not replacing file and using local {$fileName} for variants.");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Log::info("file {$fileName} already exists in {$storageDisk} / {$this->uploadedFilePath}");
|
if (Storage::disk($storageDisk)->exists($fileName) == true) {
|
||||||
|
Log::info("file {$fileName} already exists in {$storageDisk} / {$this->uploadedFilePath}. No local {$fileName} for variants, downloading from CDN.");
|
||||||
|
$readStream = Storage::disk($storageDisk)->readStream($fileName);
|
||||||
|
$tempFilePath = tempnam(sys_get_temp_dir(), 'download-');
|
||||||
|
$writeStream = fopen($tempFilePath, 'w');
|
||||||
|
while (!feof($readStream)) {
|
||||||
|
fwrite($writeStream, fread($readStream, 8192));
|
||||||
|
}
|
||||||
|
fclose($readStream);
|
||||||
|
fclose($writeStream);
|
||||||
|
$this->uploadedFilePath = $tempFilePath;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$errorStr = "cannot upload file {$storageDisk} / {$fileName} / {$this->uploadedFilePath} as temp file is empty";
|
||||||
|
Log::info($errorStr);
|
||||||
|
throw new \Exception($errorStr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($this->media->mime_type, 'image/') === 0) {
|
if (strpos($this->media->mime_type, 'image/') === 0) {
|
||||||
@@ -153,7 +173,7 @@ class StoreUploadedFileJob implements ShouldQueue
|
|||||||
$this->media->variants = $variants;
|
$this->media->variants = $variants;
|
||||||
}//end if
|
}//end if
|
||||||
|
|
||||||
if ($this->uploadedFilePath !== '') {
|
if (strlen($this->uploadedFilePath) > 0) {
|
||||||
unlink($this->uploadedFilePath);
|
unlink($this->uploadedFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user