Skip to content

Commit

Permalink
🔧 Setup config for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonJnsson committed May 10, 2022
1 parent 303587b commit 14d1408
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
38 changes: 38 additions & 0 deletions app/Console/Commands/SyncFilesToOtherDisk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use MorningTrain\Laravel\Fields\Files\Models\File;

class SyncFilesToOtherDisk extends Command
{
protected $signature = 'files:sync-disk';

protected $description = 'Command for syncing all files to specific disk';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$newDisk = 's3';

File::all()
->each(function(File $file) use ($newDisk) {
if(!$file->file_exists) {
return;
}

$stored = Storage::disk($newDisk)->put($file->path, $file->content);

if($stored) {
$file->disk = $newDisk;
$file->save();
}
});
}
}
25 changes: 25 additions & 0 deletions app/Models/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\File as FileHTTP;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use MorningTrain\Laravel\Fields\Files\Filepond;

class File extends \MorningTrain\Laravel\Fields\Files\Models\File
{
public function getUrlAttribute()
{
if($this->disk === 's3') {
return $this->storage->temporaryUrl($this->path, now()->addMinute());
}

return $this->storage->url($this->path);
}
}

2 changes: 1 addition & 1 deletion config/filepond.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
| Here you may specify the filesystem disk that should be used.
|
*/
'disk' => 'uploads',
'disk' => env('FILEPOND_DISK', 'uploads'),

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_URL'),
],

'uploads' => [
Expand Down

0 comments on commit 14d1408

Please sign in to comment.