Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit 8493a29

Browse files
authored
Handle existing files with Filepond (#138)
1 parent cf70767 commit 8493a29

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2312
-652
lines changed

.github/workflows/run-table-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ jobs:
125125
- name: Migrate DB and Run Laravel Server (MySQL)
126126
run: |
127127
cd app
128+
php artisan storage:link
128129
php artisan migrate:fresh --seed
129130
php artisan serve &
130131
if: ${{ matrix.db == 'mysql' }}
@@ -134,6 +135,7 @@ jobs:
134135
- name: Migrate DB and Run Laravel Server (PostgreSQL)
135136
run: |
136137
cd app
138+
php artisan storage:link
137139
php artisan migrate:fresh --seed
138140
php artisan serve &
139141
if: ${{ matrix.db == 'postgres' }}
@@ -144,6 +146,7 @@ jobs:
144146
if: ${{ matrix.db == 'sqlite' }}
145147
run: |
146148
cd app
149+
php artisan storage:link
147150
php artisan migrate:fresh --seed
148151
php artisan serve &
149152

.github/workflows/run-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ jobs:
6060
cd app
6161
npm upgrade
6262
cp .env.example .env
63+
cp public/1.jpeg storage/app/public/1.jpeg
64+
cp public/2.jpeg storage/app/public/2.jpeg
6365
touch database/database.sqlite
6466
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
6567
npm run build
68+
php artisan storage:link
6669
php artisan migrate:fresh --seed
6770
php artisan dusk:chrome-driver `/opt/google/chrome/chrome --version | cut -d " " -f3 | cut -d "." -f1`
6871

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ vendor
1212
node_modules
1313
protonemedia-laravel-splade-*.tgz
1414
app/bootstrap/ssr*
15+
app/storage/splade-temporary-file-uploads/*

app/app/Http/Controllers/FilepondController.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
use App\Http\Requests\AvatarUploadFileRule;
77
use App\Http\Requests\TitleWithAvatarUploadFileRule;
88
use App\Models\Project;
9+
use App\Models\User;
910
use Illuminate\Http\Request;
1011
use Illuminate\Routing\Controller;
12+
use ProtoneMedia\Splade\Facades\Toast;
13+
use ProtoneMedia\Splade\FileUploads\ExistingFile;
1114
use ProtoneMedia\Splade\FileUploads\HandleSpladeFileUploads;
1215

1316
class FilepondController extends Controller
@@ -27,6 +30,34 @@ public function showValidation()
2730
return view('form.components.filepondValidation');
2831
}
2932

33+
public function showExisting()
34+
{
35+
$user = User::first();
36+
37+
return view('form.components.filepondExisting', [
38+
'avatar' => ExistingFile::fromMediaLibrary($user->getFirstMedia('avatar')),
39+
'photos' => ExistingFile::fromMediaLibrary($user->getMedia('photos'), 'thumb'),
40+
'documents' => ExistingFile::fromMediaLibrary($user->getMedia('documents')),
41+
]);
42+
}
43+
44+
public function storeExisting(Request $request)
45+
{
46+
$user = User::first();
47+
48+
if ($request->query('form') === 'avatar') {
49+
HandleSpladeFileUploads::syncMediaLibrary($request, $user, 'avatar', 'avatar');
50+
} elseif ($request->query('form') === 'photos') {
51+
HandleSpladeFileUploads::syncMediaLibrary($request, $user, 'photos', 'photos');
52+
} elseif ($request->query('form') === 'documents') {
53+
HandleSpladeFileUploads::syncMediaLibrary($request, $user, 'documents', 'documents');
54+
}
55+
56+
Toast::info('The photos have been saved.');
57+
58+
return redirect()->back();
59+
}
60+
3061
public function storeSingle(Request $request)
3162
{
3263
$request->validate([

app/app/Models/User.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
use Illuminate\Foundation\Auth\User as Authenticatable;
77
use Illuminate\Notifications\Notifiable;
88
use Laravel\Sanctum\HasApiTokens;
9+
use Spatie\MediaLibrary\HasMedia;
10+
use Spatie\MediaLibrary\InteractsWithMedia;
11+
use Spatie\MediaLibrary\MediaCollections\Models\Media;
912

10-
class User extends Authenticatable
13+
class User extends Authenticatable implements HasMedia
1114
{
12-
use HasApiTokens, HasFactory, Notifiable;
15+
use HasApiTokens, HasFactory, Notifiable, InteractsWithMedia;
1316

1417
/**
1518
* The attributes that are mass assignable.
@@ -51,4 +54,15 @@ public function keywords()
5154
{
5255
return $this->morphToMany(Keyword::class, 'keywordable');
5356
}
57+
58+
public function registerMediaCollections(): void
59+
{
60+
$this->addMediaCollection('avatar')->singleFile();
61+
$this->addMediaCollection('photos');
62+
}
63+
64+
public function registerMediaConversions(Media $media = null): void
65+
{
66+
$this->addMediaConversion('thumb')->fit('contain', 50, 50);
67+
}
5468
}

app/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"maatwebsite/excel": "^3.1",
1919
"psr/simple-cache": "^2.0",
2020
"pusher/pusher-php-server": "^7.0,<7.2.0",
21+
"spatie/laravel-medialibrary": "^10.7",
2122
"spatie/laravel-query-builder": "^5.0"
2223
},
2324
"require-dev": {
@@ -30,6 +31,7 @@
3031
"phpunit/phpunit": "^9.5.10",
3132
"protonemedia/laravel-splade": "*",
3233
"spatie/laravel-ignition": "^1.0",
34+
"spatie/laravel-ray": "^1.31",
3335
"spatie/phpunit-snapshot-assertions": "^4.2"
3436
},
3537
"autoload": {
@@ -77,4 +79,4 @@
7779
},
7880
"minimum-stability": "dev",
7981
"prefer-stable": true
80-
}
82+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::create('media', function (Blueprint $table) {
12+
$table->bigIncrements('id');
13+
14+
$table->morphs('model');
15+
$table->uuid('uuid')->nullable()->unique();
16+
$table->string('collection_name');
17+
$table->string('name');
18+
$table->string('file_name');
19+
$table->string('mime_type')->nullable();
20+
$table->string('disk');
21+
$table->string('conversions_disk')->nullable();
22+
$table->unsignedBigInteger('size');
23+
$table->json('manipulations');
24+
$table->json('custom_properties');
25+
$table->json('generated_conversions');
26+
$table->json('responsive_images');
27+
$table->unsignedInteger('order_column')->nullable()->index();
28+
29+
$table->nullableTimestamps();
30+
});
31+
}
32+
};

app/database/seeders/DatabaseSeeder.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Database\Seeders;
44

55
use App\Models\Dummy;
6+
use App\Models\User;
67
use Database\Factories\KeywordFactory;
78
use Database\Factories\ProjectFactory;
89
use Database\Factories\TagFactory;
@@ -22,11 +23,14 @@ public function run()
2223

2324
TagFactory::new()->count(10)->create();
2425

25-
UserFactory::new()->create([
26+
/** @var User $user */
27+
$firstUser = UserFactory::new()->create([
2628
'name' => 'Test User',
2729
'email' => 'test@example.com',
2830
]);
2931

32+
static::giveUserMedia($firstUser);
33+
3034
$users = UserFactory::new()->count(99)->create();
3135

3236
Dummy::create([
@@ -43,4 +47,18 @@ public function run()
4347
->count(30)
4448
->create();
4549
}
50+
51+
public static function giveUserMedia(User $user)
52+
{
53+
$user
54+
->clearMediaCollection('avatar')
55+
->clearMediaCollection('photos')
56+
->clearMediaCollection('documents');
57+
58+
$user->addMedia(public_path('1.jpeg'))->preservingOriginal()->toMediaCollection('avatar');
59+
$user->addMedia(public_path('1.jpeg'))->preservingOriginal()->toMediaCollection('photos');
60+
$user->addMedia(public_path('2.jpeg'))->preservingOriginal()->toMediaCollection('photos');
61+
$user->addMedia(public_path('dummy1.txt'))->preservingOriginal()->toMediaCollection('documents');
62+
$user->addMedia(public_path('dummy2.txt'))->preservingOriginal()->toMediaCollection('documents');
63+
}
4664
}

app/public/1.jpeg

3.16 KB
Loading

app/public/2.jpeg

3.94 KB
Loading

0 commit comments

Comments
 (0)